@@ -38,6 +38,7 @@ pub fn[A : Eq + Hash] contains(self : T[A], key : A) -> Bool {
38
38
guard self ._ is Some (node ) else { return false }
39
39
loop (node , @path .of (key )) {
40
40
(Leaf (key1 , bucket ), _ ) => key == key1 || bucket .contains (key )
41
+ (Flat (key1 , path1 ), path ) => path == path1 && key == key1
41
42
(Branch (children ), path ) => {
42
43
let idx = path .idx ()
43
44
if children .has (idx ) {
@@ -50,13 +51,24 @@ pub fn[A : Eq + Hash] contains(self : T[A], key : A) -> Bool {
50
51
}
51
52
52
53
///|
53
- fn [A ] make_leaf (key : A , path : Path ) -> Node [A ] {
54
- // make sure leaf nodes always appear at the bottom of the tree
55
- if path .is_end () {
56
- Leaf (key , @list .empty ())
54
+ /// require: key1 != key2, path1 and path2 has the same length
55
+ fn [A ] join_2 (key1 : A , path1 : Path , key2 : A , path2 : Path ) -> Node [A ] {
56
+ let idx1 = path1 .idx ()
57
+ let idx2 = path2 .idx ()
58
+ if idx1 == idx2 {
59
+ let node = if path1 .is_last () {
60
+ Leaf (key2 , @list .singleton (key1 ))
61
+ } else {
62
+ join_2 (key1 , path1 .next (), key2 , path2 .next ())
63
+ }
64
+ Branch (@sparse_array .singleton (idx1 , node ))
57
65
} else {
58
- let child = make_leaf (key , path .next ())
59
- Branch (@sparse_array .singleton (path .idx (), child ))
66
+ let (node1 , node2 ) = if path1 .is_last () {
67
+ (Leaf (key1 , @list .empty ()), Leaf (key2 , @list .empty ()))
68
+ } else {
69
+ (Flat (key1 , path1 .next ()), Flat (key2 , path2 .next ()))
70
+ }
71
+ Branch (@sparse_array .doubleton (idx1 , node1 , idx2 , node2 ))
60
72
}
61
73
}
62
74
@@ -69,14 +81,20 @@ fn[A : Eq] add_with_path(self : Node[A], key : A, path : Path) -> Node[A] {
69
81
} else {
70
82
Leaf (key , bucket .add (key1 ))
71
83
}
84
+ Flat (key1 , path1 ) =>
85
+ if path == path1 && key == key1 {
86
+ self
87
+ } else {
88
+ join_2 (key1 , path1 , key , path )
89
+ }
72
90
Branch (children ) => {
73
91
let idx = path .idx ()
74
92
if children .has (idx ) {
75
93
let child = children [idx ]
76
94
let child = child .add_with_path (key , path .next ())
77
95
Branch (children .replace (idx , child ))
78
96
} else {
79
- let child = make_leaf (key , path .next ())
97
+ let child = Flat (key , path .next ())
80
98
Branch (children .add (idx , child ))
81
99
}
82
100
}
@@ -87,7 +105,7 @@ fn[A : Eq] add_with_path(self : Node[A], key : A, path : Path) -> Node[A] {
87
105
/// Add a key to the hashset.
88
106
pub fn [A : Eq + Hash ] add (self : T [A ], key : A ) -> T [A ] {
89
107
match self ._ {
90
- None => Some (make_leaf (key , @path .of (key )))
108
+ None => Some (Flat (key , @path .of (key )))
91
109
Some (node ) => Some (node .add_with_path (key , @path .of (key )))
92
110
}
93
111
}
@@ -115,15 +133,26 @@ fn[A : Eq] remove_with_path(self : Node[A], key : A, path : Path) -> Node[A]? {
115
133
} else {
116
134
Some (self )
117
135
}
136
+ Flat (key1 , path1 ) =>
137
+ if path == path1 && key == key1 {
138
+ None
139
+ } else {
140
+ Some (self )
141
+ }
118
142
Branch (children ) => {
119
143
let idx = path .idx ()
120
144
if children .has (idx ) {
121
145
let child = children [idx ]
122
146
let new_child = child .remove_with_path (key , path .next ())
123
- match (children .size (), new_child ) {
124
- (1 , None ) => None
125
- (_ , None ) => Some (Branch (children .remove (idx )))
126
- (_ , Some (new_child )) => Some (Branch (children .replace (idx , new_child )))
147
+ let new_children = match (children .size (), new_child ) {
148
+ (1 , None ) => return None
149
+ (_ , None ) => children .remove (idx )
150
+ (_ , Some (new_child )) => children .replace (idx , new_child )
151
+ }
152
+ match new_children .data {
153
+ [Flat (key1 , path1 )] =>
154
+ Some (Flat (key1 , path1 .push (new_children .elem_info.fitst_idx ())))
155
+ _ => Some (Branch (new_children ))
127
156
}
128
157
} else {
129
158
Some (self )
@@ -140,6 +169,7 @@ pub fn[A] size(self : T[A]) -> Int {
140
169
fn node_size (node ) {
141
170
match node {
142
171
Leaf (_ , bucket ) => 1 + bucket .length ()
172
+ Flat (_ ) => 1
143
173
Branch (children ) =>
144
174
for i = 0 , total_size = 0 {
145
175
if i < children .data.length () {
@@ -194,6 +224,7 @@ pub fn[A] each(self : T[A], f : (A) -> Unit) -> Unit {
194
224
f (k )
195
225
bucket .each (f )
196
226
}
227
+ Flat (k , _ ) => f (k )
197
228
Branch (children ) => children .each (go )
198
229
}
199
230
}
@@ -210,6 +241,7 @@ pub fn[A] iter(self : T[A]) -> Iter[A] {
210
241
fn go (node ) -> Iter [A ] {
211
242
match node {
212
243
Leaf (k , bucket ) => Iter ::singleton (k ) + bucket .iter ()
244
+ Flat (k , _ ) => Iter ::singleton (k )
213
245
Branch (children ) => children .data.iter ().flat_map (go )
214
246
}
215
247
}
@@ -262,6 +294,7 @@ impl[A : Eq] Eq for Node[A] with op_equal(self, other) {
262
294
match (self , other ) {
263
295
(Leaf (x , xs ), Leaf (y , ys )) =>
264
296
xs .length () == ys .length () && xs .add (x ).iter ().all (ys .add (y ).contains (_ ))
297
+ (Flat (x , pathx ), Flat (y , pathy )) => pathx == pathy && x == y
265
298
(Branch (xs ), Branch (ys )) => xs == ys
266
299
_ => false
267
300
}
0 commit comments