Skip to content

Commit 71de481

Browse files
committed
Update sparse array
1 parent 3725408 commit 71de481

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

snap-common/src/main/java/org/snapscript/common/SparseArray.java

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,85 @@
22

33
public class SparseArray<T> {
44

5-
private final Object[][] segments;
6-
private final int block;
5+
private volatile Object[][] segments;
6+
private volatile int capacity;
7+
private volatile int block;
78

89
public SparseArray(int length) {
910
this(length, 32); // compute better default distribution
1011
}
1112

1213
public SparseArray(int length, int block) {
1314
this.segments = new Object[length / block + 1][];
15+
this.capacity = length;
1416
this.block = block;
1517
}
1618

17-
public void set(int index, T value) {
19+
public T set(int index, T value) {
20+
int offset = index % block;
21+
Object[] segment = segment(index);
22+
Object result = segment[offset];
23+
24+
segment[offset] = value;
25+
return (T)result;
26+
}
27+
28+
public T get(int index) {
1829
int section = index /block;
19-
Object[] segment = segments[section];
2030

21-
if(segment == null) {
22-
segment = segments[section] = new Object[block];
31+
if(section < segments.length) {
32+
Object[] segment = segments[section];
33+
34+
if(segment != null) {
35+
return (T)segment[index % block];
36+
}
2337
}
24-
segment[index % block] = value;
38+
return null;
2539
}
2640

27-
public T get(int index) {
41+
public T remove(int index) {
2842
int section = index /block;
29-
Object[] segment = segments[section];
3043

31-
if(segment != null) {
32-
return (T)segment[index % block];
44+
if(section < segments.length) {
45+
Object[] segment = segments[section];
46+
47+
if(segment != null) {
48+
int offset = index % block;
49+
Object result = segment[offset];
50+
51+
segment[offset] = null;
52+
return (T)result;
53+
}
3354
}
3455
return null;
3556
}
3657

37-
public void remove(int index) {
58+
protected Object[] segment(int index) {
3859
int section = index /block;
60+
61+
if(index >= capacity) {
62+
expand(capacity * 2 < index ? index : capacity *2);
63+
}
3964
Object[] segment = segments[section];
4065

41-
if(segment != null) {
42-
segment[index % block] = null;
66+
if(segment == null) {
67+
segments[section] = new Object[block];
4368
}
69+
return segments[section];
70+
}
71+
72+
protected void expand(int length) {
73+
Object[][] copy = new Object[length / block + 1][];
74+
75+
for(int i = 0; i < segments.length; i++) {
76+
copy[i] = segments[i];
77+
}
78+
capacity = length;
79+
segments = copy;
80+
}
81+
82+
public int length() {
83+
return capacity;
4484
}
4585
}
86+

snap-tree/src/main/java/org/snapscript/tree/define/TraitConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public Initializer compile(Initializer initializer, Type type) throws Exception
5555
String name = value.getString();
5656

5757
if(!checker.isConstant()) {
58+
throw new InternalStateException("Variable '" + name + "' for '" + type + "' must be constant");
5859
}
5960
Accessor accessor = new StaticAccessor(initializer, scope, type, name);
6061
Property property = new AccessorProperty(name, type, constraint, accessor, ModifierType.STATIC.mask | ModifierType.CONSTANT.mask);

0 commit comments

Comments
 (0)