diff --git a/hashmap/README.md b/hashmap/README.md index 3cf22c15a..307fb139f 100644 --- a/hashmap/README.md +++ b/hashmap/README.md @@ -15,11 +15,11 @@ let map1 : HashMap[String, Int] = HashMap::new() let map2 = HashMap::[1, 2, 3, 4, 5] ``` -When creating via `new()`, you can set initial capacity and custom hasher by providing labeled argument `~capacity` and `~hasher`. Note that the capacity must be a zero or a power of 2. +When creating via `new()`, you can set a custom hasher by providing a labeled argument `~hasher`. ```moonbit -// Create with custom hasher and capacity. -let map = HashMap::new(~capacity=32, ~hasher=Some(fn(k) { k.length() })) +// Create with custom hasher. +let map = HashMap::new(~hasher=Some(fn(k) { k.length() })) ``` ### Set & Get diff --git a/hashmap/hashmap.mbt b/hashmap/hashmap.mbt index 4db44ab75..6e4a45079 100644 --- a/hashmap/hashmap.mbt +++ b/hashmap/hashmap.mbt @@ -48,25 +48,17 @@ struct HashMap[K, V] { /// Create new hash map. pub fn HashMap::new[K, V]( - ~capacity : Int = default_init_capacity, ~hasher : Option[(K) -> Int] = None ) -> HashMap[K, V] { - if capacity != 0 && not(is_power_of_two(capacity)) { - abort("The capacity is not a zero or a power of 2.") - } { size: 0, - capacity, - growAt: calc_grow_threshold(capacity), + capacity: default_init_capacity, + growAt: calc_grow_threshold(default_init_capacity), hasher, - entries: @array.new(capacity, fn() { Empty }), + entries: @array.new(default_init_capacity, fn() { Empty }), } } -fn is_power_of_two(n : Int) -> Bool { - n > 0 && n.land(n - 1) == 0 -} - /// Create new hash map from array. pub fn HashMap::from_array[K : Hash + Eq, V]( arr : Array[(K, V)] @@ -297,8 +289,7 @@ test "new" { test "set" { let m : HashMap[String, Int] = HashMap::new( - ~hasher=Some(fn(k) { k.length() }), - ~capacity=16, + ~hasher=Some(fn(k) { k.length() }) ) m.set("a", 1) m.set("b", 1) @@ -367,8 +358,7 @@ test "from_array" { test "remove" { let m : HashMap[String, Int] = HashMap::new( - ~hasher=Some(fn(k) { k.length() }), - ~capacity=16, + ~hasher=Some(fn(k) { k.length() }) ) m.set("a", 1) m.set("ab", 2) @@ -380,7 +370,7 @@ test "remove" { @assertion.assert_eq(m.size(), 5)? @assertion.assert_eq( m.debug_entries(), - "_,(0,a,1),(0,bc,2),(1,cd,2),(1,abc,3),_,(0,abcdef,6),_,_,_,_,_,_,_,_,_", + "_,(0,a,1),(0,bc,2),(1,cd,2),(1,abc,3),_,(0,abcdef,6),_", )? } @@ -406,11 +396,6 @@ test "size" { @assertion.assert_eq(m.size(), 1)? } -test "capacity" { - let m : HashMap[String, Int] = HashMap::new(~capacity=128) - @assertion.assert_eq(m.capacity(), 128)? -} - test "is_empty" { let m : HashMap[String, Int] = HashMap::new() @assertion.assert_eq(m.is_empty(), true)? @@ -475,12 +460,4 @@ test "grow" { m.debug_entries(), "_,(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)? -} +} \ No newline at end of file