@@ -74,7 +74,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
7474 };
7575 }
7676
77- pub fn deinit (hm : & Self ) void {
77+ pub fn deinit (hm : & const Self ) void {
7878 hm .allocator .free (hm .entries );
7979 }
8080
@@ -114,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type,
114114 return hm .internalPut (key , value );
115115 }
116116
117- pub fn get (hm : & Self , key : K ) ? & Entry {
117+ pub fn get (hm : & const Self , key : K ) ? & Entry {
118118 if (hm .entries .len == 0 ) {
119119 return null ;
120120 }
121121 return hm .internalGet (key );
122122 }
123123
124- pub fn contains (hm : & Self , key : K ) bool {
124+ pub fn contains (hm : & const Self , key : K ) bool {
125125 return hm .get (key ) != null ;
126126 }
127127
@@ -230,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
230230 unreachable ; // put into a full map
231231 }
232232
233- fn internalGet (hm : & Self , key : K ) ? & Entry {
233+ fn internalGet (hm : & const Self , key : K ) ? & Entry {
234234 const start_index = hm .keyToIndex (key );
235235 {var roll_over : usize = 0 ; while (roll_over <= hm .max_distance_from_start_index ) : (roll_over += 1 ) {
236236 const index = (start_index + roll_over ) % hm .entries .len ;
@@ -242,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
242242 return null ;
243243 }
244244
245- fn keyToIndex (hm : & Self , key : K ) usize {
245+ fn keyToIndex (hm : & const Self , key : K ) usize {
246246 return usize (hash (key )) % hm .entries .len ;
247247 }
248248 };
@@ -264,6 +264,7 @@ test "basic hash map usage" {
264264 assert (?? (map .put (5 , 66 ) catch unreachable ) == 55 );
265265 assert (?? (map .put (5 , 55 ) catch unreachable ) == 66 );
266266
267+ assert (map .contains (2 ));
267268 assert ((?? map .get (2 )).value == 22 );
268269 _ = map .remove (2 );
269270 assert (map .remove (2 ) == null );
@@ -273,7 +274,7 @@ test "basic hash map usage" {
273274test "iterator hash map" {
274275 var direct_allocator = std .heap .DirectAllocator .init ();
275276 defer direct_allocator .deinit ();
276-
277+
277278 var reset_map = HashMap (i32 , i32 , hash_i32 , eql_i32 ).init (& direct_allocator .allocator );
278279 defer reset_map .deinit ();
279280
@@ -315,4 +316,4 @@ fn hash_i32(x: i32) u32 {
315316
316317fn eql_i32 (a : i32 , b : i32 ) bool {
317318 return a == b ;
318- }
319+ }
0 commit comments