@@ -77,14 +77,28 @@ pub fn BTreeMap(
7777 return null ;
7878 }
7979
80- /// Inserts a key-value pair. If the key exists, the value is updated.
81- pub fn put (self : * Self , key : K , value : V ) ! void {
82- // Check if key exists and just update the value in place
83- if (self .get (key ) != null ) {
84- _ = self .remove (key );
85- // Don't increment len here, it will be incremented below
80+ /// Retrieves a mutable pointer to the value associated with `key`.
81+ pub fn getPtr (self : * Self , key : K ) ? * V {
82+ var current = self .root ;
83+ while (current ) | node | {
84+ const res = std .sort .binarySearch (K , node .keys [0.. node .len ], key , compareFn );
85+ if (res ) | index | return & node .values [index ];
86+
87+ if (node .is_leaf ) return null ;
88+
89+ const insertion_point = std .sort .lowerBound (K , node .keys [0.. node .len ], key , compareFn );
90+ current = node .children [insertion_point ];
8691 }
92+ return null ;
93+ }
94+
95+ /// Returns true if the map contains the given key.
96+ pub fn contains (self : * const Self , key : K ) bool {
97+ return self .get (key ) != null ;
98+ }
8799
100+ /// Inserts a key-value pair. If the key exists, the value is updated.
101+ pub fn put (self : * Self , key : K , value : V ) ! void {
88102 var root_node = if (self .root ) | r | r else {
89103 const new_node = try self .createNode ();
90104 new_node .keys [0 ] = key ;
@@ -104,8 +118,10 @@ pub fn BTreeMap(
104118 root_node = new_root ;
105119 }
106120
107- self .insertNonFull (root_node , key , value );
108- self .len += 1 ;
121+ const is_new = self .insertNonFull (root_node , key , value );
122+ if (is_new ) {
123+ self .len += 1 ;
124+ }
109125 }
110126
111127 fn splitChild (self : * Self , parent : * Node , index : u16 ) void {
@@ -153,25 +169,47 @@ pub fn BTreeMap(
153169 parent .len += 1 ;
154170 }
155171
156- fn insertNonFull (self : * Self , node : * Node , key : K , value : V ) void {
172+ fn insertNonFull (self : * Self , node : * Node , key : K , value : V ) bool {
157173 var i = node .len ;
158174 if (node .is_leaf ) {
175+ // Check if key already exists
176+ var j : u16 = 0 ;
177+ while (j < node .len ) : (j += 1 ) {
178+ if (compare (key , node .keys [j ]) == .eq ) {
179+ // Update existing value
180+ node .values [j ] = value ;
181+ return false ; // Not a new insertion
182+ }
183+ }
184+
185+ // Insert new key
159186 while (i > 0 and compare (key , node .keys [i - 1 ]) == .lt ) : (i -= 1 ) {
160187 node .keys [i ] = node .keys [i - 1 ];
161188 node .values [i ] = node .values [i - 1 ];
162189 }
163190 node .keys [i ] = key ;
164191 node .values [i ] = value ;
165192 node .len += 1 ;
193+ return true ; // New insertion
166194 } else {
195+ // Check if key exists in current node
196+ var j : u16 = 0 ;
197+ while (j < node .len ) : (j += 1 ) {
198+ if (compare (key , node .keys [j ]) == .eq ) {
199+ // Update existing value
200+ node .values [j ] = value ;
201+ return false ; // Not a new insertion
202+ }
203+ }
204+
167205 while (i > 0 and compare (key , node .keys [i - 1 ]) == .lt ) : (i -= 1 ) {}
168206 if (node .children [i ].? .len == BRANCHING_FACTOR - 1 ) {
169207 self .splitChild (node , i );
170208 if (compare (node .keys [i ], key ) == .lt ) {
171209 i += 1 ;
172210 }
173211 }
174- self .insertNonFull (node .children [i ].? , key , value );
212+ return self .insertNonFull (node .children [i ].? , key , value );
175213 }
176214 }
177215
@@ -227,18 +265,23 @@ pub fn BTreeMap(
227265 const pred = self .getPredecessor (node , index );
228266 node .keys [index ] = pred .key ;
229267 node .values [index ] = pred .value ;
268+ // deleteFromNode already decremented self.len, so increment it back
269+ // because we're replacing, not actually removing
270+ const old_len = self .len ;
230271 _ = self .deleteFromNode (node .children [index ].? , pred .key );
231- self .len += 1 ;
272+ self .len = old_len ;
232273 } else if (node .children [index + 1 ].? .len > MIN_KEYS ) {
233274 const succ = self .getSuccessor (node , index );
234275 node .keys [index ] = succ .key ;
235276 node .values [index ] = succ .value ;
277+ const old_len = self .len ;
236278 _ = self .deleteFromNode (node .children [index + 1 ].? , succ .key );
237- self .len += 1 ;
279+ self .len = old_len ;
238280 } else {
239281 self .merge (node , index );
282+ const old_len = self .len ;
240283 _ = self .deleteFromNode (node .children [index ].? , key );
241- self .len += 1 ;
284+ self .len = old_len ;
242285 }
243286 }
244287
0 commit comments