-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAVLTreeMap.drv
2676 lines (2178 loc) · 80.4 KB
/
AVLTreeMap.drv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2002-2024 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package PACKAGE;
#if ! KEYS_REFERENCE
import it.unimi.dsi.fastutil.objects.AbstractObjectSortedSet;
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator;
import it.unimi.dsi.fastutil.objects.ObjectListIterator;
import it.unimi.dsi.fastutil.objects.ObjectSortedSet;
#endif
#if KEY_INDEX != VALUE_INDEX && !(KEYS_REFERENCE && VALUES_REFERENCE)
import VALUE_PACKAGE.VALUE_COLLECTION;
import VALUE_PACKAGE.VALUE_ABSTRACT_COLLECTION;
import VALUE_PACKAGE.VALUE_ITERATOR;
#if VALUES_PRIMITIVE
import VALUE_PACKAGE.VALUE_LIST_ITERATOR;
#endif
#endif
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.NoSuchElementException;
/** A type-specific AVL tree map with a fast, small-footprint implementation.
*
* <p>The iterators provided by the views of this class are type-specific {@linkplain
* it.unimi.dsi.fastutil.BidirectionalIterator bidirectional iterators}.
* Moreover, the iterator returned by {@code iterator()} can be safely cast
* to a type-specific {@linkplain java.util.ListIterator list iterator}.
*/
public class AVL_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VALUE_GENERIC implements java.io.Serializable, Cloneable {
/** A reference to the root entry. */
protected transient Entry KEY_VALUE_GENERIC tree;
/** Number of entries in this map. */
protected int count;
/** The first key in this map. */
protected transient Entry KEY_VALUE_GENERIC firstEntry;
/** The last key in this map. */
protected transient Entry KEY_VALUE_GENERIC lastEntry;
/** Cached set of entries. */
protected transient ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC> entries;
/** Cached set of keys. */
protected transient SORTED_SET KEY_GENERIC keys;
/** Cached collection of values. */
protected transient VALUE_COLLECTION VALUE_GENERIC values;
/** The value of this variable remembers, after a {@code put()}
* or a {@code remove()}, whether the <em>domain</em> of the map
* has been modified. */
protected transient boolean modified;
/** This map's comparator, as provided in the constructor. */
protected Comparator<? super KEY_GENERIC_CLASS> storedComparator;
/** This map's actual comparator; it may differ from {@link #storedComparator} because it is
always a type-specific comparator, so it could be derived from the former by wrapping. */
protected transient KEY_COMPARATOR KEY_SUPER_GENERIC actualComparator;
private static final long serialVersionUID = -7046029254386353129L;
{
allocatePaths();
}
/** Creates a new empty tree map.
*/
public AVL_TREE_MAP() {
tree = null;
count = 0;
}
/** Generates the comparator that will be actually used.
*
* <p>When a given {@link Comparator} is specified and stored in {@link
* #storedComparator}, we must check whether it is type-specific. If it is
* so, we can used directly, and we store it in {@link #actualComparator}. Otherwise,
* we adapt it using a helper static method.
*/
private void setActualComparator() {
#if KEY_CLASS_Object
actualComparator = storedComparator;
#else
actualComparator = COMPARATORS.AS_KEY_COMPARATOR(storedComparator);
#endif
}
/** Creates a new empty tree map with the given comparator.
*
* @param c a (possibly type-specific) comparator.
*/
public AVL_TREE_MAP(final Comparator<? super KEY_GENERIC_CLASS> c) {
this();
storedComparator = c;
setActualComparator();
}
/** Creates a new tree map copying a given map.
*
* @param m a {@link Map} to be copied into the new tree map.
*/
public AVL_TREE_MAP(final Map<? extends KEY_GENERIC_CLASS, ? extends VALUE_GENERIC_CLASS> m) {
this();
putAll(m);
}
/** Creates a new tree map copying a given sorted map (and its {@link Comparator}).
*
* @param m a {@link SortedMap} to be copied into the new tree map.
*/
public AVL_TREE_MAP(final SortedMap<KEY_GENERIC_CLASS,VALUE_GENERIC_CLASS> m) {
this(m.comparator());
putAll(m);
}
/** Creates a new tree map copying a given map.
*
* @param m a type-specific map to be copied into the new tree map.
*/
public AVL_TREE_MAP(final MAP KEY_VALUE_EXTENDS_GENERIC m) {
this();
putAll(m);
}
/** Creates a new tree map copying a given sorted map (and its {@link Comparator}).
*
* @param m a type-specific sorted map to be copied into the new tree map.
*/
public AVL_TREE_MAP(final SORTED_MAP KEY_VALUE_GENERIC m) {
this(m.comparator());
putAll(m);
}
/** Creates a new tree map using the elements of two parallel arrays and the given comparator.
*
* @param k the array of keys of the new tree map.
* @param v the array of corresponding values in the new tree map.
* @param c a (possibly type-specific) comparator.
* @throws IllegalArgumentException if {@code k} and {@code v} have different lengths.
*/
public AVL_TREE_MAP(final KEY_GENERIC_TYPE[] k, final VALUE_GENERIC_TYPE v[], final Comparator<? super KEY_GENERIC_CLASS> c) {
this(c);
if (k.length != v.length) throw new IllegalArgumentException("The key array and the value array have different lengths (" + k.length + " and " + v.length + ")");
for(int i = 0; i < k.length; i++) this.put(k[i], v[i]);
}
/** Creates a new tree map using the elements of two parallel arrays.
*
* @param k the array of keys of the new tree map.
* @param v the array of corresponding values in the new tree map.
* @throws IllegalArgumentException if {@code k} and {@code v} have different lengths.
*/
public AVL_TREE_MAP(final KEY_GENERIC_TYPE[] k, final VALUE_GENERIC_TYPE v[]) {
this(k, v, null);
}
/*
* The following methods implements some basic building blocks used by
* all accessors. They are (and should be maintained) identical to those used in AVLTreeSet.drv.
*
* The put()/remove() code is derived from Ben Pfaff's GNU libavl
* (https://adtinfo.org/). If you want to understand what's
* going on, you should have a look at the literate code contained therein
* first.
*/
/** Compares two keys in the right way.
*
* <p>This method uses the {@link #actualComparator} if it is non-{@code null}.
* Otherwise, it resorts to primitive type comparisons or to {@link Comparable#compareTo(Object) compareTo()}.
*
* @param k1 the first key.
* @param k2 the second key.
* @return a number smaller than, equal to or greater than 0, as usual
* (i.e., when k1 < k2, k1 = k2 or k1 > k2, respectively).
*/
SUPPRESS_WARNINGS_KEY_UNCHECKED
final int compare(final KEY_GENERIC_TYPE k1, final KEY_GENERIC_TYPE k2) {
return actualComparator == null ? KEY_CMP(k1, k2) : actualComparator.compare(k1, k2);
}
/** Returns the entry corresponding to the given key, if it is in the tree; {@code null}, otherwise.
*
* @param k the key to search for.
* @return the corresponding entry, or {@code null} if no entry with the given key exists.
*/
final Entry KEY_VALUE_GENERIC findKey(final KEY_GENERIC_TYPE k) {
Entry KEY_VALUE_GENERIC e = tree;
int cmp;
while (e != null && (cmp = compare(k, e.key)) != 0) e = cmp < 0 ? e.left() : e.right();
return e;
}
/** Locates a key.
*
* @param k a key.
* @return the last entry on a search for the given key; this will be
* the given key, if it present; otherwise, it will be either the smallest greater key or the greatest smaller key.
*/
final Entry KEY_VALUE_GENERIC locateKey(final KEY_GENERIC_TYPE k) {
Entry KEY_VALUE_GENERIC e = tree, last = tree;
int cmp = 0;
while (e != null && (cmp = compare(k, e.key)) != 0) {
last = e;
e = cmp < 0 ? e.left() : e.right();
}
return cmp == 0 ? e : last;
}
/** This vector remembers the directions followed during
* the current insertion. It suffices for about 2<sup>32</sup> entries. */
private transient boolean dirPath[];
private void allocatePaths() {
dirPath = new boolean[48];
}
#if VALUES_PRIMITIVE && !VALUE_CLASS_Boolean
/** Adds an increment to value currently associated with a key.
*
* <p>Note that this method respects the {@linkplain #defaultReturnValue() default return value} semantics: when
* called with a key that does not currently appears in the map, the key
* will be associated with the default return value plus
* the given increment.
*
* @param k the key.
* @param incr the increment.
* @return the old value, or the {@linkplain #defaultReturnValue() default return value} if no value was present for the given key.
*/
public VALUE_GENERIC_TYPE addTo(final KEY_GENERIC_TYPE k, final VALUE_GENERIC_TYPE incr) {
Entry KEY_VALUE_GENERIC e = add(k);
final VALUE_GENERIC_TYPE oldValue = e.value;
e.value += incr;
return oldValue;
}
#endif
@Override
public VALUE_GENERIC_TYPE put(final KEY_GENERIC_TYPE k, final VALUE_GENERIC_TYPE v) {
Entry KEY_VALUE_GENERIC e = add(k);
final VALUE_GENERIC_TYPE oldValue = e.value;
e.value = v;
return oldValue;
}
/** Returns a node with key k in the balanced tree, creating one with defRetValue if necessary.
*
* @param k the key
* @return a node with key k. If a node with key k already exists, then that node is returned,
* otherwise a new node with defRetValue is created ensuring that the tree is balanced
* after creation of the node.
*/
private Entry KEY_VALUE_GENERIC add(final KEY_GENERIC_TYPE k) {
REQUIRE_KEY_NON_NULL(k)
/* After execution of this method, modified is true iff a new entry has been inserted. */
modified = false;
Entry KEY_VALUE_GENERIC e = null;
if (tree == null) { // The case of the empty tree is treated separately.
count++;
e = tree = lastEntry = firstEntry = new Entry KEY_VALUE_GENERIC_DIAMOND(k, defRetValue);
modified = true;
}
else {
Entry KEY_VALUE_GENERIC p = tree, q = null, y = tree, z = null, w = null;
int cmp, i = 0;
while(true) {
if ((cmp = compare(k, p.key)) == 0) {
return p;
}
if (p.balance() != 0) {
i = 0;
z = q;
y = p;
}
if (dirPath[i++] = cmp > 0) {
if (p.succ()) {
count++;
e = new Entry KEY_VALUE_GENERIC_DIAMOND(k, defRetValue);
modified = true;
if (p.right == null) lastEntry = e;
e.left = p;
e.right = p.right;
p.right(e);
break;
}
q = p;
p = p.right;
}
else {
if (p.pred()) {
count++;
e = new Entry KEY_VALUE_GENERIC_DIAMOND(k, defRetValue);
modified = true;
if (p.left == null) firstEntry = e;
e.right = p;
e.left = p.left;
p.left(e);
break;
}
q = p;
p = p.left;
}
}
p = y;
i = 0;
while(p != e) {
if (dirPath[i]) p.incBalance();
else p.decBalance();
p = dirPath[i++] ? p.right : p.left;
}
if (y.balance() == -2) {
Entry KEY_VALUE_GENERIC x = y.left;
if (x.balance() == -1) {
w = x;
if (x.succ()) {
x.succ(false);
y.pred(x);
}
else y.left = x.right;
x.right = y;
x.balance(0);
y.balance(0);
}
else {
assert x.balance() == 1;
w = x.right;
x.right = w.left;
w.left = x;
y.left = w.right;
w.right = y;
if (w.balance() == -1) {
x.balance(0);
y.balance(1);
}
else if (w.balance() == 0) {
x.balance(0);
y.balance(0);
}
else {
x.balance(-1);
y.balance(0);
}
w.balance(0);
if (w.pred()) {
x.succ(w);
w.pred(false);
}
if (w.succ()) {
y.pred(w);
w.succ(false);
}
}
}
else if (y.balance() == +2) {
Entry KEY_VALUE_GENERIC x = y.right;
if (x.balance() == 1) {
w = x;
if (x.pred()) {
x.pred(false);
y.succ(x);
}
else y.right = x.left;
x.left = y;
x.balance(0);
y.balance(0);
}
else {
assert x.balance() == -1;
w = x.left;
x.left = w.right;
w.right = x;
y.right = w.left;
w.left = y;
if (w.balance() == 1) {
x.balance(0);
y.balance(-1);
}
else if (w.balance() == 0) {
x.balance(0);
y.balance(0);
}
else {
x.balance(1);
y.balance(0);
}
w.balance(0);
if (w.pred()) {
y.succ(w);
w.pred(false);
}
if (w.succ()) {
x.pred(w);
w.succ(false);
}
}
}
else return e;
if (z == null) tree = w;
else {
if (z.left == y) z.left = w;
else z.right = w;
}
}
return e;
}
/** Finds the parent of an entry.
*
* @param e a node of the tree.
* @return the parent of the given node, or {@code null} for the root.
*/
private Entry KEY_VALUE_GENERIC parent(final Entry KEY_VALUE_GENERIC e) {
if (e == tree) return null;
Entry KEY_VALUE_GENERIC x, y, p;
x = y = e;
while(true) {
if (y.succ()) {
p = y.right;
if (p == null || p.left != e) {
while(! x.pred()) x = x.left;
p = x.left;
}
return p;
}
else if (x.pred()) {
p = x.left;
if (p == null || p.right != e) {
while(! y.succ()) y = y.right;
p = y.right;
}
return p;
}
x = x.left;
y = y.right;
}
}
/* After execution of this method, {@link #modified} is true iff an entry
has been deleted. */
SUPPRESS_WARNINGS_KEY_UNCHECKED
@Override
public VALUE_GENERIC_TYPE REMOVE_VALUE(final KEY_TYPE k) {
modified = false;
if (tree == null) return defRetValue;
int cmp;
Entry KEY_VALUE_GENERIC p = tree, q = null;
boolean dir = false;
final KEY_GENERIC_TYPE kk = KEY_GENERIC_CAST k;
while(true) {
if ((cmp = compare(kk, p.key)) == 0) break;
else if (dir = cmp > 0) {
q = p;
if ((p = p.right()) == null) return defRetValue;
}
else {
q = p;
if ((p = p.left()) == null) return defRetValue;
}
}
if (p.left == null) firstEntry = p.next();
if (p.right == null) lastEntry = p.prev();
if (p.succ()) {
if (p.pred()) {
if (q != null) {
if (dir) q.succ(p.right);
else q.pred(p.left);
}
else tree = dir ? p.right : p.left;
}
else {
p.prev().right = p.right;
if (q != null) {
if (dir) q.right = p.left;
else q.left = p.left;
}
else tree = p.left;
}
}
else {
Entry KEY_VALUE_GENERIC r = p.right;
if (r.pred()) {
r.left = p.left;
r.pred(p.pred());
if (! r.pred()) r.prev().right = r;
if (q != null) {
if (dir) q.right = r;
else q.left = r;
}
else tree = r;
r.balance(p.balance());
q = r;
dir = true;
}
else {
Entry KEY_VALUE_GENERIC s;
while(true) {
s = r.left;
if (s.pred()) break;
r = s;
}
if (s.succ()) r.pred(s);
else r.left = s.right;
s.left = p.left;
if (! p.pred()) {
p.prev().right = s;
s.pred(false);
}
s.right = p.right;
s.succ(false);
if (q != null) {
if (dir) q.right = s;
else q.left = s;
}
else tree = s;
s.balance(p.balance());
q = r;
dir = false;
}
}
Entry KEY_VALUE_GENERIC y;
while(q != null) {
y = q;
q = parent(y);
if (! dir) {
dir = q != null && q.left != y;
y.incBalance();
if (y.balance() == 1) break;
else if (y.balance() == 2) {
Entry KEY_VALUE_GENERIC x = y.right;
assert x != null;
if (x.balance() == -1) {
Entry KEY_VALUE_GENERIC w;
assert x.balance() == -1;
w = x.left;
x.left = w.right;
w.right = x;
y.right = w.left;
w.left = y;
if (w.balance() == 1) {
x.balance(0);
y.balance(-1);
}
else if (w.balance() == 0) {
x.balance(0);
y.balance(0);
}
else {
assert w.balance() == -1;
x.balance(1);
y.balance(0);
}
w.balance(0);
if (w.pred()) {
y.succ(w);
w.pred(false);
}
if (w.succ()) {
x.pred(w);
w.succ(false);
}
if (q != null) {
if (dir) q.right = w;
else q.left = w;
}
else tree = w;
}
else {
if (q != null) {
if (dir) q.right = x;
else q.left = x;
}
else tree = x;
if (x.balance() == 0) {
y.right = x.left;
x.left = y;
x.balance(-1);
y.balance(+1);
break;
}
assert x.balance() == 1;
if (x.pred()) {
y.succ(true);
x.pred(false);
}
else y.right = x.left;
x.left = y;
y.balance(0);
x.balance(0);
}
}
}
else {
dir = q != null && q.left != y;
y.decBalance();
if (y.balance() == -1) break;
else if (y.balance() == -2) {
Entry KEY_VALUE_GENERIC x = y.left;
assert x != null;
if (x.balance() == 1) {
Entry KEY_VALUE_GENERIC w;
assert x.balance() == 1;
w = x.right;
x.right = w.left;
w.left = x;
y.left = w.right;
w.right = y;
if (w.balance() == -1) {
x.balance(0);
y.balance(1);
}
else if (w.balance() == 0) {
x.balance(0);
y.balance(0);
}
else {
assert w.balance() == 1;
x.balance(-1);
y.balance(0);
}
w.balance(0);
if (w.pred()) {
x.succ(w);
w.pred(false);
}
if (w.succ()) {
y.pred(w);
w.succ(false);
}
if (q != null) {
if (dir) q.right = w;
else q.left = w;
}
else tree = w;
}
else {
if (q != null) {
if (dir) q.right = x;
else q.left = x;
}
else tree = x;
if (x.balance() == 0) {
y.left = x.right;
x.right = y;
x.balance(+1);
y.balance(-1);
break;
}
assert x.balance() == -1;
if (x.succ()) {
y.pred(true);
x.succ(false);
}
else y.left = x.right;
x.right = y;
y.balance(0);
x.balance(0);
}
}
}
}
modified = true;
count--;
return p.value;
}
@Override
public boolean containsValue(final VALUE_TYPE v) {
final ValueIterator i = new ValueIterator();
VALUE_GENERIC_TYPE ev;
int j = count;
while(j-- != 0) {
ev = i.NEXT_VALUE();
if (VALUE_EQUALS(ev, v)) return true;
}
return false;
}
@Override
public void clear() {
count = 0;
tree = null;
entries = null;
values = null;
keys = null;
firstEntry = lastEntry = null;
}
/** This class represent an entry in a tree map.
*
* <p>We use the only "metadata", i.e., {@link Entry#info}, to store
* information about balance, predecessor status and successor status.
*
* <p>Note that since the class is recursive, it can be
* considered equivalently a tree.
*/
private static final class Entry KEY_VALUE_GENERIC extends ABSTRACT_MAP.BasicEntry KEY_VALUE_GENERIC implements Cloneable {
/** If the bit in this mask is true, {@link #right} points to a successor. */
private static final int SUCC_MASK = 1 << 31;
/** If the bit in this mask is true, {@link #left} points to a predecessor. */
private static final int PRED_MASK = 1 << 30;
/** The bits in this mask hold the node balance info. You can get it just by casting to byte. */
private static final int BALANCE_MASK = 0xFF;
/** The pointers to the left and right subtrees. */
Entry KEY_VALUE_GENERIC left, right;
/** This integers holds different information in different bits (see {@link #SUCC_MASK}, {@link #PRED_MASK} and {@link #BALANCE_MASK}). */
int info;
Entry() {
super(KEY_NULL, VALUE_NULL);
}
/** Creates a new entry with the given key and value.
*
* @param k a key.
* @param v a value.
*/
Entry(final KEY_GENERIC_TYPE k, final VALUE_GENERIC_TYPE v) {
super(k, v);
info = SUCC_MASK | PRED_MASK;
}
/** Returns the left subtree.
*
* @return the left subtree ({@code null} if the left
* subtree is empty).
*/
Entry KEY_VALUE_GENERIC left() {
return (info & PRED_MASK) != 0 ? null : left;
}
/** Returns the right subtree.
*
* @return the right subtree ({@code null} if the right
* subtree is empty).
*/
Entry KEY_VALUE_GENERIC right() {
return (info & SUCC_MASK) != 0 ? null : right;
}
/** Checks whether the left pointer is really a predecessor.
* @return true if the left pointer is a predecessor.
*/
boolean pred() {
return (info & PRED_MASK) != 0;
}
/** Checks whether the right pointer is really a successor.
* @return true if the right pointer is a successor.
*/
boolean succ() {
return (info & SUCC_MASK) != 0;
}
/** Sets whether the left pointer is really a predecessor.
* @param pred if true then the left pointer will be considered a predecessor.
*/
void pred(final boolean pred) {
if (pred) info |= PRED_MASK;
else info &= ~PRED_MASK;
}
/** Sets whether the right pointer is really a successor.
* @param succ if true then the right pointer will be considered a successor.
*/
void succ(final boolean succ) {
if (succ) info |= SUCC_MASK;
else info &= ~SUCC_MASK;
}
/** Sets the left pointer to a predecessor.
* @param pred the predecessr.
*/
void pred(final Entry KEY_VALUE_GENERIC pred) {
info |= PRED_MASK;
left = pred;
}
/** Sets the right pointer to a successor.
* @param succ the successor.
*/
void succ(final Entry KEY_VALUE_GENERIC succ) {
info |= SUCC_MASK;
right = succ;
}
/** Sets the left pointer to the given subtree.
* @param left the new left subtree.
*/
void left(final Entry KEY_VALUE_GENERIC left) {
info &= ~PRED_MASK;
this.left = left;
}
/** Sets the right pointer to the given subtree.
* @param right the new right subtree.
*/
void right(final Entry KEY_VALUE_GENERIC right) {
info &= ~SUCC_MASK;
this.right = right;
}
/** Returns the current level of the node.
* @return the current level of this node.
*/
int balance() {
return (byte)info;
}
/** Sets the level of this node.
* @param level the new level of this node.
*/
void balance(int level) {
info &= ~BALANCE_MASK;
info |= (level & BALANCE_MASK);
}
/** Increments the level of this node. */
void incBalance() {
info = info & ~BALANCE_MASK | ((byte)info + 1) & 0xFF;
}
/** Decrements the level of this node. */
protected void decBalance() {
info = info & ~BALANCE_MASK | ((byte)info - 1) & 0xFF;
}
/** Computes the next entry in the set order.
*
* @return the next entry ({@code null}) if this is the last entry).
*/
Entry KEY_VALUE_GENERIC next() {
Entry KEY_VALUE_GENERIC next = this.right;
if ((info & SUCC_MASK) == 0) while ((next.info & PRED_MASK) == 0) next = next.left;
return next;
}
/** Computes the previous entry in the set order.
*
* @return the previous entry ({@code null}) if this is the first entry).
*/
Entry KEY_VALUE_GENERIC prev() {
Entry KEY_VALUE_GENERIC prev = this.left;
if ((info & PRED_MASK) == 0) while ((prev.info & SUCC_MASK) == 0) prev = prev.right;
return prev;
}
@Override
public VALUE_GENERIC_TYPE setValue(final VALUE_GENERIC_TYPE value) {
final VALUE_GENERIC_TYPE oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
SUPPRESS_WARNINGS_KEY_VALUE_UNCHECKED
public Entry KEY_VALUE_GENERIC clone() {
Entry KEY_VALUE_GENERIC c;
try {
c = (Entry KEY_VALUE_GENERIC)super.clone();
}
catch(CloneNotSupportedException cantHappen) {
throw new InternalError();
}