diff --git a/lib2/core/hashtable.lsts b/lib2/core/hashtable.lsts index b54f34a40..5c3f25fc2 100644 --- a/lib2/core/hashtable.lsts +++ b/lib2/core/hashtable.lsts @@ -83,15 +83,15 @@ let .length(h: Hashtable): USize = ( # Reallocates the hashtable with a new capacity, rehashing all existing entries. # Used internally when the hashtable needs to grow. let .realloc(h: Hashtable, target-capacity: USize): Hashtable = ( - let new-data = mk-sparse-owned-data(type((HashtableRowExists,k,v)), target-capacity); + let new-data = Hashtable(mk-sparse-owned-data(type((HashtableRowExists,k,v)), target-capacity)); let old-i = 0_sz; while old-i < h.data.capacity { let old-kv = h.data[old-i]; if is(old-kv.first,HashtableRowFilled) - then new-data[old-i] = h.data[old-i]; + then new-data.bind-direct(old-kv.second, old-kv.third); old-i = old-i + 1; }; - Hashtable(new-data) + new-data ); # new allocations = 1 if realloc is necessary diff --git a/tests/promises/hashtable/comparison.lsts b/tests/promises/hashtable/comparison.lsts index d3dcbe40e..7ed21c9ef 100644 --- a/tests/promises/hashtable/comparison.lsts +++ b/tests/promises/hashtable/comparison.lsts @@ -25,3 +25,9 @@ assert( safe-alloc-block-count == 0 ); assert( {"1":"2"}.lookup("1","3") == "2" ); assert( {"1":"2"}.lookup("2","3") == "3" ); assert( safe-alloc-block-count == 0 ); + +assert( {"1":"2"}["1"] == {"1":"2","2":"3","3":"4","4":"5","5":"6","6":"7","7":"8","8":"9","9":"10","10":"11","11":"12","12":"13","13":"14","14":"15","15":"16","16":"17","17":"18", + "18":"19","19":"20","20":"21","21":"22","22":"23"}["1"] ); +assert( {"1":"2","2":"3","3":"4","4":"5","5":"6","6":"7","7":"8","8":"9","9":"10","10":"11","11":"12","12":"13","13":"14","14":"15","15":"16","16":"17","17":"18", + "18":"19","19":"20","20":"21","21":"22","22":"23","23":"24","24":"25","25":"26","26":"27","27":"28","28":"29","29":"30","30":"31","31":"32","32":"33"}["11"] == Some("12") ); +assert( safe-alloc-block-count == 0 ); diff --git a/tests/promises/hashtable/realloc.lsts b/tests/promises/hashtable/realloc.lsts new file mode 100644 index 000000000..f722e8808 --- /dev/null +++ b/tests/promises/hashtable/realloc.lsts @@ -0,0 +1,16 @@ + +import lib/std/minimal.lsts; + +let table = mk-hashtable(type(String), type(U64)); + +let ti = 0_u64; +while ti < 5000 { + table = table.bind("\{ti}", ti); + ti = ti + 1; +}; + +ti = 0_u64; +while ti < 5000 { + assert(table.lookup("\{ti}", 0_u64) == ti); + ti = ti + 1; +};