Skip to content

Commit

Permalink
Handle zero capacity grow
Browse files Browse the repository at this point in the history
  • Loading branch information
yj-qin committed Mar 25, 2024
1 parent 4a7be0c commit 0101b9f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions hashmap/hashmap.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ fn shift_back[K : Hash, V](self : HashMap[K, V], start_index : Int) -> Unit {
}

fn grow[K : Hash + Eq, V](self : HashMap[K, V]) -> Unit {
// handle zero capacity
if self.capacity == 0 {
self.capacity = default_init_capacity
self.growAt = calc_grow_threshold(self.capacity)
self.size = 0
self.entries = @array.new(self.capacity, fn() { Empty })
return
}
let old_entries = self.entries
self.entries = @array.new(self.capacity * 2, fn() { Empty })
self.capacity = self.capacity * 2
Expand Down Expand Up @@ -455,3 +463,11 @@ test "grow" {
"_,(0,C,1),(0,Go,2),(0,C++,3),(0,Java,4),(0,Scala,5),(1,Julia,5),(2,Cobol,5),(2,Python,6),(2,Haskell,7),(2,Rescript,8),_,_,_,_,_",
)?
}

test "zero_capacity_grow" {
let m : HashMap[String, Int] = HashMap::new(~capacity=0)
@assertion.assert_eq(m.capacity(), 0)?
m.set("a", 1)
@assertion.assert_eq(m.size(), 1)?
@assertion.assert_eq(m.capacity(), 8)?
}

0 comments on commit 0101b9f

Please sign in to comment.