diff options
Diffstat (limited to 'hashmap.ha')
| -rw-r--r-- | hashmap.ha | 39 |
1 files changed, 20 insertions, 19 deletions
@@ -1,28 +1,29 @@ -def BUCKETS: size = 1 << 17; +def HASHMAP_SIZE: size = 1 << 14; -type hashmap = [BUCKETS][](size, data); +type hashmap = [HASHMAP_SIZE](size, stat); -fn getitem(map: *hashmap, hash: size) data = { - let bucket = &map[hash & (BUCKETS - 1)]; - for (let i = 0z; i < len(bucket); i += 1) { - if (bucket[i].0 == hash) - return bucket[i].1; +fn getitem(map: *hashmap, hash: size) stat = { + let i = hash & (HASHMAP_SIZE - 1); + for (true) { + if (map[i].0 == 0 && map[i].1.count == 0) + break; + if (map[i].0 == hash) + return map[i].1; + i = (i + 1) & (HASHMAP_SIZE - 1); }; - return data { ... }; + return stat { ... }; }; -fn setitem(map: *hashmap, hash: size, value: data) void = { - let bucket = &map[hash & (BUCKETS - 1)]; - for (let i = 0z; i < len(bucket); i += 1) { - if (bucket[i].0 == hash) { - bucket[i].1 = value; +fn setitem(map: *hashmap, hash: size, value: stat) void = { + let i = hash & (HASHMAP_SIZE - 1); + for (true) { + if (map[i].0 == 0 && map[i].1.count == 0) + break; + if (map[i].0 == hash) { + map[i].1 = value; return; }; + i = (i + 1) & (HASHMAP_SIZE - 1); }; - append(bucket, (hash, value))!; -}; - -fn finish(map: *hashmap) void = { - for (let i = 0z; i < len(map); i += 1) - free(map[i]); + map[i] = (hash, value); }; |
