• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 265

29 Sep 2025 09:00AM UTC coverage: 62.244% (+0.02%) from 62.222%
265

Pull #1096

circleci

Frotty
restore determinism, fix tests
Pull Request #1096: Perf improvements

17480 of 28083 relevant lines covered (62.24%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.57
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/intermediatelang/ILconstArray.java
1
package de.peeeq.wurstscript.intermediatelang;
2

3
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
4
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
5
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
6

7
import java.util.Arrays;
8
import java.util.Map;
9
import java.util.function.Supplier;
10

11
import static com.ibm.icu.text.PluralRules.Operand.e;
12

13
public class ILconstArray extends ILconstAbstract {
14

15
    // Sparse storage for explicit / first-touched entries.
16
    // Much lighter than TreeMap both in time and memory.
17
    private final Int2ObjectOpenHashMap<ILconst> values;
18

19
    /** Logical length / bound (can be Integer.MAX_VALUE for “unbounded”). */
20
    private final int size;
21

22
    /** Supplier for default values (e.g., nested arrays or primitive defaults). */
23
    private final Supplier<ILconst> defaultValue;
24

25
    public ILconstArray(int size, Supplier<ILconst> defaultValue) {
1✔
26
        this.size = size;
1✔
27
        this.defaultValue = defaultValue;
1✔
28
        // start tiny; will grow automatically
29
        this.values = new Int2ObjectOpenHashMap<>(0);
1✔
30
    }
1✔
31

32
    @Override
33
    public String print() {
34
        // We only need a sorted view when PRINTING for determinism.
35
        // Sorting keys here keeps runtime fast elsewhere.
36
        if (values.isEmpty()) return "[]";
×
37

38
        int[] keys = new int[values.size()];
×
39
        int k = 0;
×
40
        for (Int2ObjectMap.Entry<ILconst> e: values.int2ObjectEntrySet()) {
×
41
            keys[k++] = e.getIntKey();
×
42
        }
×
43
        Arrays.sort(keys);
×
44

45
        StringBuilder sb = new StringBuilder(8 + values.size() * 16);
×
46
        sb.append('[');
×
47
        for (int i = 0; i < keys.length; i++) {
×
48
            if (i > 0) sb.append(", ");
×
49
            int idx = keys[i];
×
50
            sb.append(idx).append(": ").append(values.get(idx));
×
51
        }
52
        sb.append(']');
×
53
        return sb.toString();
×
54
    }
55

56
    @Override
57
    public boolean isEqualTo(ILconst other) {
58
        // Preserve previous semantics: identity equality only.
59
        return other == this;
×
60
    }
61

62
    public void set(int index, ILconst value) {
63
        checkIndex(index);
1✔
64
        values.put(index, value);
1✔
65
    }
1✔
66

67
    public ILconst get(int index) {
68
        checkIndex(index);
1✔
69
        ILconst v = values.get(index);
1✔
70
        if (v != null) return v;
1✔
71

72
        // First touch for this index: create and store default so that
73
        // nested writes (e.g., multi-d arrays) have a place to land.
74
        v = defaultValue.get();
1✔
75
        values.put(index, v);
1✔
76
        return v;
1✔
77
    }
78

79
    private void checkIndex(int index) {
80
        if (index < 0) {
1✔
81
            throw new InterpreterException("Array index " + index + " was negative.");
×
82
        }
83
        if (index >= size) {
1✔
84
            throw new InterpreterException("Array index " + index + " must be smaller than array size " + size);
×
85
        }
86
    }
1✔
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc