@@ -21,6 +21,7 @@ public static void setMaxLen(int maxLen) {
21
21
Leaf _root ;
22
22
int _count ;
23
23
final Edit _edit ;
24
+ int _version = 0 ;
24
25
25
26
PersistentSortedSet () { this (null , RT .DEFAULT_COMPARATOR ); }
26
27
public PersistentSortedSet (Comparator cmp ) { this (null , cmp ); }
@@ -31,11 +32,12 @@ public PersistentSortedSet(IPersistentMap meta, Comparator cmp) {
31
32
_count = 0 ;
32
33
}
33
34
34
- public PersistentSortedSet (IPersistentMap meta , Comparator cmp , Leaf root , int count , Edit edit ) {
35
+ public PersistentSortedSet (IPersistentMap meta , Comparator cmp , Leaf root , int count , Edit edit , int version ) {
35
36
super (meta , cmp );
36
37
_root = root ;
37
38
_count = count ;
38
39
_edit = edit ;
40
+ _version = version ;
39
41
}
40
42
41
43
void ensureEditable (boolean value ) {
@@ -55,10 +57,10 @@ public Seq slice(Object from, Object to, Comparator cmp) {
55
57
if (from == null ) {
56
58
while (true ) {
57
59
if (node instanceof Node ) {
58
- seq = new Seq (null , this ,seq , node , 0 , null , null , true );
60
+ seq = new Seq (null , this ,seq , node , 0 , null , null , true , _version );
59
61
node = seq .child ();
60
62
} else {
61
- seq = new Seq (null , this ,seq , node , 0 , to , cmp , true );
63
+ seq = new Seq (null , this ,seq , node , 0 , to , cmp , true , _version );
62
64
return seq .over () ? null : seq ;
63
65
}
64
66
}
@@ -69,10 +71,10 @@ public Seq slice(Object from, Object to, Comparator cmp) {
69
71
if (idx < 0 ) idx = -idx -1 ;
70
72
if (idx == node ._len ) return null ;
71
73
if (node instanceof Node ) {
72
- seq = new Seq (null , this ,seq , node , idx , null , null , true );
74
+ seq = new Seq (null , this ,seq , node , idx , null , null , true , _version );
73
75
node = seq .child ();
74
76
} else { // Leaf
75
- seq = new Seq (null , this ,seq , node , idx , to , cmp , true );
77
+ seq = new Seq (null , this ,seq , node , idx , to , cmp , true , _version );
76
78
return seq .over () ? null : seq ;
77
79
}
78
80
}
@@ -90,10 +92,10 @@ public Seq rslice(Object from, Object to, Comparator cmp) {
90
92
while (true ) {
91
93
int idx = node ._len -1 ;
92
94
if (node instanceof Node ) {
93
- seq = new Seq (null , this ,seq , node , idx , null , null , false );
95
+ seq = new Seq (null , this ,seq , node , idx , null , null , false , _version );
94
96
node = seq .child ();
95
97
} else {
96
- seq = new Seq (null , this ,seq , node , idx , to , cmp , false );
98
+ seq = new Seq (null , this ,seq , node , idx , to , cmp , false , _version );
97
99
return seq .over () ? null : seq ;
98
100
}
99
101
}
@@ -103,15 +105,15 @@ public Seq rslice(Object from, Object to, Comparator cmp) {
103
105
if (node instanceof Node ) {
104
106
int idx = node .searchLast (from , cmp ) + 1 ;
105
107
if (idx == node ._len ) --idx ; // last or beyond, clamp to last
106
- seq = new Seq (null , this ,seq , node , idx , null , null , false );
108
+ seq = new Seq (null , this ,seq , node , idx , null , null , false , _version );
107
109
node = seq .child ();
108
110
} else { // Leaf
109
111
int idx = node .searchLast (from , cmp );
110
112
if (idx == -1 ) { // not in this, so definitely in prev
111
- seq = new Seq (null , this ,seq , node , 0 , to , cmp , false );
113
+ seq = new Seq (null , this ,seq , node , 0 , to , cmp , false , _version );
112
114
return seq .advance () ? seq : null ;
113
115
} else { // exact match
114
- seq = new Seq (null , this ,seq , node , idx , to , cmp , false );
116
+ seq = new Seq (null , this ,seq , node , idx , to , cmp , false , _version );
115
117
return seq .over () ? null : seq ;
116
118
}
117
119
}
@@ -134,7 +136,7 @@ public String toString() {
134
136
// IObj
135
137
public PersistentSortedSet withMeta (IPersistentMap meta ) {
136
138
if (_meta == meta ) return this ;
137
- return new PersistentSortedSet (meta , _cmp , _root , _count , _edit );
139
+ return new PersistentSortedSet (meta , _cmp , _root , _count , _edit , _version );
138
140
}
139
141
140
142
// Counted
@@ -178,15 +180,16 @@ public PersistentSortedSet cons(Object key, Comparator cmp) {
178
180
_root = new Node (keys , nodes , 2 , _edit );
179
181
}
180
182
_count ++;
183
+ _version ++;
181
184
return this ;
182
185
}
183
186
184
187
if (1 == nodes .length )
185
- return new PersistentSortedSet (_meta , _cmp , nodes [0 ], _count +1 , _edit );
188
+ return new PersistentSortedSet (_meta , _cmp , nodes [0 ], _count +1 , _edit , _version + 1 );
186
189
187
190
Object keys [] = new Object [] { nodes [0 ].maxKey (), nodes [1 ].maxKey () };
188
191
Leaf newRoot = new Node (keys , nodes , 2 , _edit );
189
- return new PersistentSortedSet (_meta , _cmp , newRoot , _count +1 , _edit );
192
+ return new PersistentSortedSet (_meta , _cmp , newRoot , _count +1 , _edit , _version + 1 );
190
193
}
191
194
192
195
// IPersistentSet
@@ -200,20 +203,21 @@ public PersistentSortedSet disjoin(Object key, Comparator cmp) {
200
203
// not in set
201
204
if (UNCHANGED == nodes ) return this ;
202
205
// in place update
203
- if (nodes == EARLY_EXIT ) { _count --; return this ; }
206
+ if (nodes == EARLY_EXIT ) { _count --; _version ++; return this ; }
204
207
Leaf newRoot = nodes [1 ];
205
208
if (_edit .editable ()) {
206
209
if (newRoot instanceof Node && newRoot ._len == 1 )
207
210
newRoot = ((Node ) newRoot )._children [0 ];
208
211
_root = newRoot ;
209
212
_count --;
213
+ _version ++;
210
214
return this ;
211
215
}
212
216
if (newRoot instanceof Node && newRoot ._len == 1 ) {
213
217
newRoot = ((Node ) newRoot )._children [0 ];
214
- return new PersistentSortedSet (_meta , _cmp , newRoot , _count -1 , _edit );
218
+ return new PersistentSortedSet (_meta , _cmp , newRoot , _count -1 , _edit , _version + 1 );
215
219
}
216
- return new PersistentSortedSet (_meta , _cmp , newRoot , _count -1 , _edit );
220
+ return new PersistentSortedSet (_meta , _cmp , newRoot , _count -1 , _edit , _version + 1 );
217
221
}
218
222
219
223
public boolean contains (Object key ) {
@@ -223,7 +227,7 @@ public boolean contains(Object key) {
223
227
// IEditableCollection
224
228
public PersistentSortedSet asTransient () {
225
229
ensureEditable (false );
226
- return new PersistentSortedSet (_meta , _cmp , _root , _count , new Edit (true ));
230
+ return new PersistentSortedSet (_meta , _cmp , _root , _count , new Edit (true ), _version );
227
231
}
228
232
229
233
// ITransientCollection
0 commit comments