2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("std.zig");
|
2017-12-24 03:08:53 +00:00
|
|
|
const debug = std.debug;
|
2016-08-12 06:25:13 +01:00
|
|
|
const assert = debug.assert;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2017-12-24 03:08:53 +00:00
|
|
|
const math = std.math;
|
|
|
|
const mem = std.mem;
|
2019-06-30 10:35:57 +01:00
|
|
|
const meta = std.meta;
|
2019-07-02 18:46:51 +01:00
|
|
|
const autoHash = std.hash.autoHash;
|
2019-07-01 12:23:26 +01:00
|
|
|
const Wyhash = std.hash.Wyhash;
|
2016-05-09 00:05:41 +01:00
|
|
|
const Allocator = mem.Allocator;
|
2017-05-01 18:12:38 +01:00
|
|
|
const builtin = @import("builtin");
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2017-05-02 22:34:21 +01:00
|
|
|
const want_modification_safety = builtin.mode != builtin.Mode.ReleaseFast;
|
2016-05-09 23:07:38 +01:00
|
|
|
const debug_u32 = if (want_modification_safety) u32 else void;
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
pub fn AutoHashMap(comptime K: type, comptime V: type) type {
|
|
|
|
return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K));
|
|
|
|
}
|
|
|
|
|
2019-08-22 21:46:37 +01:00
|
|
|
/// Builtin hashmap for strings as keys.
|
|
|
|
pub fn StringHashMap(comptime V: type) type {
|
|
|
|
return HashMap([]const u8, V, hashString, eqlString);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eqlString(a: []const u8, b: []const u8) bool {
|
2019-09-03 22:56:04 +01:00
|
|
|
return mem.eql(u8, a, b);
|
2019-08-22 21:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hashString(s: []const u8) u32 {
|
|
|
|
return @truncate(u32, std.hash.Wyhash.hash(0, s));
|
|
|
|
}
|
|
|
|
|
2018-05-30 21:09:11 +01:00
|
|
|
pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type {
|
2018-11-13 13:08:37 +00:00
|
|
|
return struct {
|
2016-12-19 00:40:26 +00:00
|
|
|
entries: []Entry,
|
|
|
|
size: usize,
|
|
|
|
max_distance_from_start_index: usize,
|
2018-05-31 15:56:59 +01:00
|
|
|
allocator: *Allocator,
|
2019-10-16 06:29:16 +01:00
|
|
|
|
|
|
|
/// This is used to detect bugs where a hashtable is edited while an iterator is running.
|
2016-12-19 00:40:26 +00:00
|
|
|
modification_count: debug_u32,
|
|
|
|
|
2018-09-13 21:34:33 +01:00
|
|
|
const Self = @This();
|
2016-12-19 00:40:26 +00:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const KV = struct {
|
2016-12-19 00:40:26 +00:00
|
|
|
key: K,
|
|
|
|
value: V,
|
|
|
|
};
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Entry = struct {
|
2018-08-03 22:22:17 +01:00
|
|
|
used: bool,
|
|
|
|
distance_from_start_index: usize,
|
|
|
|
kv: KV,
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const GetOrPutResult = struct {
|
2018-08-03 22:22:17 +01:00
|
|
|
kv: *KV,
|
|
|
|
found_existing: bool,
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const Iterator = struct {
|
2018-05-31 15:56:59 +01:00
|
|
|
hm: *const Self,
|
2016-12-19 00:40:26 +00:00
|
|
|
// how many items have we returned
|
|
|
|
count: usize,
|
|
|
|
// iterator through the entry array
|
|
|
|
index: usize,
|
|
|
|
// used to detect concurrent modification
|
|
|
|
initial_modification_count: debug_u32,
|
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
pub fn next(it: *Iterator) ?*KV {
|
2016-12-19 00:40:26 +00:00
|
|
|
if (want_modification_safety) {
|
|
|
|
assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
2016-12-19 00:40:26 +00:00
|
|
|
if (it.count >= it.hm.size) return null;
|
2017-05-03 23:12:07 +01:00
|
|
|
while (it.index < it.hm.entries.len) : (it.index += 1) {
|
2016-12-19 00:40:26 +00:00
|
|
|
const entry = &it.hm.entries[it.index];
|
|
|
|
if (entry.used) {
|
|
|
|
it.index += 1;
|
|
|
|
it.count += 1;
|
2018-08-03 22:22:17 +01:00
|
|
|
return &entry.kv;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-22 05:50:30 +00:00
|
|
|
unreachable; // no next item
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
2018-05-03 14:54:33 +01:00
|
|
|
|
|
|
|
// Reset the iterator to the initial index
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn reset(it: *Iterator) void {
|
2018-05-03 14:54:33 +01:00
|
|
|
it.count = 0;
|
|
|
|
it.index = 0;
|
|
|
|
// Resetting the modification count too
|
|
|
|
it.initial_modification_count = it.hm.modification_count;
|
|
|
|
}
|
2016-12-19 00:40:26 +00:00
|
|
|
};
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn init(allocator: *Allocator) Self {
|
2018-11-13 13:08:37 +00:00
|
|
|
return Self{
|
2019-06-10 00:24:24 +01:00
|
|
|
.entries = [_]Entry{},
|
2017-04-06 10:34:04 +01:00
|
|
|
.allocator = allocator,
|
|
|
|
.size = 0,
|
|
|
|
.max_distance_from_start_index = 0,
|
2018-01-17 04:19:05 +00:00
|
|
|
.modification_count = if (want_modification_safety) 0 else {},
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2016-05-09 23:07:38 +01:00
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
pub fn deinit(hm: Self) void {
|
2017-01-23 05:11:21 +00:00
|
|
|
hm.allocator.free(hm.entries);
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn clear(hm: *Self) void {
|
2016-12-19 00:40:26 +00:00
|
|
|
for (hm.entries) |*entry| {
|
|
|
|
entry.used = false;
|
2016-05-09 23:07:38 +01:00
|
|
|
}
|
2016-12-19 00:40:26 +00:00
|
|
|
hm.size = 0;
|
|
|
|
hm.max_distance_from_start_index = 0;
|
|
|
|
hm.incrementModificationCount();
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
2016-05-09 23:07:38 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
pub fn count(self: Self) usize {
|
|
|
|
return self.size;
|
2018-05-03 14:54:33 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
/// If key exists this function cannot fail.
|
|
|
|
/// If there is an existing item with `key`, then the result
|
|
|
|
/// kv pointer points to it, and found_existing is true.
|
|
|
|
/// Otherwise, puts a new item with undefined value, and
|
|
|
|
/// the kv pointer points to it. Caller should then initialize
|
|
|
|
/// the data.
|
|
|
|
pub fn getOrPut(self: *Self, key: K) !GetOrPutResult {
|
|
|
|
// TODO this implementation can be improved - we should only
|
|
|
|
// have to hash once and find the entry once.
|
|
|
|
if (self.get(key)) |kv| {
|
2018-11-13 13:08:37 +00:00
|
|
|
return GetOrPutResult{
|
2018-08-03 22:22:17 +01:00
|
|
|
.kv = kv,
|
|
|
|
.found_existing = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self.incrementModificationCount();
|
2019-05-02 07:38:52 +01:00
|
|
|
try self.autoCapacity();
|
2018-08-03 22:22:17 +01:00
|
|
|
const put_result = self.internalPut(key);
|
|
|
|
assert(put_result.old_kv == null);
|
2018-11-13 13:08:37 +00:00
|
|
|
return GetOrPutResult{
|
2018-08-03 22:22:17 +01:00
|
|
|
.kv = &put_result.new_entry.kv,
|
|
|
|
.found_existing = false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:10:15 +00:00
|
|
|
pub fn getOrPutValue(self: *Self, key: K, value: V) !*KV {
|
|
|
|
const res = try self.getOrPut(key);
|
|
|
|
if (!res.found_existing)
|
|
|
|
res.kv.value = value;
|
|
|
|
|
|
|
|
return res.kv;
|
|
|
|
}
|
|
|
|
|
2019-05-03 01:01:30 +01:00
|
|
|
fn optimizedCapacity(expected_count: usize) usize {
|
|
|
|
// ensure that the hash map will be at most 60% full if
|
2019-05-04 05:15:00 +01:00
|
|
|
// expected_count items are put into it
|
2019-05-03 01:01:30 +01:00
|
|
|
var optimized_capacity = expected_count * 5 / 3;
|
2019-06-06 07:36:51 +01:00
|
|
|
// an overflow here would mean the amount of memory required would not
|
|
|
|
// be representable in the address space
|
|
|
|
return math.ceilPowerOfTwo(usize, optimized_capacity) catch unreachable;
|
2019-05-03 01:01:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-04 05:15:00 +01:00
|
|
|
/// Increases capacity so that the hash map will be at most
|
2019-05-03 01:01:30 +01:00
|
|
|
/// 60% full when expected_count items are put into it
|
|
|
|
pub fn ensureCapacity(self: *Self, expected_count: usize) !void {
|
|
|
|
const optimized_capacity = optimizedCapacity(expected_count);
|
|
|
|
return self.ensureCapacityExact(optimized_capacity);
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:38:52 +01:00
|
|
|
/// Sets the capacity to the new capacity if the new
|
|
|
|
/// capacity is greater than the current capacity.
|
2019-05-03 01:01:30 +01:00
|
|
|
/// New capacity must be a power of two.
|
2019-05-03 01:43:51 +01:00
|
|
|
fn ensureCapacityExact(self: *Self, new_capacity: usize) !void {
|
2019-06-06 07:26:48 +01:00
|
|
|
// capacity must always be a power of two to allow for modulo
|
|
|
|
// optimization in the constrainIndex fn
|
2019-06-10 06:20:17 +01:00
|
|
|
assert(math.isPowerOfTwo(new_capacity));
|
2019-05-03 01:01:30 +01:00
|
|
|
|
2019-05-02 07:38:52 +01:00
|
|
|
if (new_capacity <= self.entries.len) {
|
|
|
|
return;
|
2017-01-05 08:40:04 +00:00
|
|
|
}
|
2019-05-03 01:01:30 +01:00
|
|
|
|
2019-05-02 07:38:52 +01:00
|
|
|
const old_entries = self.entries;
|
2019-05-03 01:01:30 +01:00
|
|
|
try self.initCapacity(new_capacity);
|
2019-05-04 05:15:00 +01:00
|
|
|
self.incrementModificationCount();
|
2019-05-02 07:38:52 +01:00
|
|
|
if (old_entries.len > 0) {
|
2016-12-19 00:40:26 +00:00
|
|
|
// dump all of the old elements into the new table
|
|
|
|
for (old_entries) |*old_entry| {
|
|
|
|
if (old_entry.used) {
|
2018-08-03 22:22:17 +01:00
|
|
|
self.internalPut(old_entry.kv.key).new_entry.kv.value = old_entry.kv.value;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 22:22:17 +01:00
|
|
|
self.allocator.free(old_entries);
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2018-08-03 22:22:17 +01:00
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
/// Returns the kv pair that was already there.
|
|
|
|
pub fn put(self: *Self, key: K, value: V) !?KV {
|
2019-05-02 07:38:52 +01:00
|
|
|
try self.autoCapacity();
|
2019-05-03 01:42:38 +01:00
|
|
|
return putAssumeCapacity(self, key, value);
|
|
|
|
}
|
|
|
|
|
2019-06-13 04:39:53 +01:00
|
|
|
/// Calls put() and asserts that no kv pair is clobbered.
|
|
|
|
pub fn putNoClobber(self: *Self, key: K, value: V) !void {
|
|
|
|
assert((try self.put(key, value)) == null);
|
|
|
|
}
|
|
|
|
|
2019-05-03 01:42:38 +01:00
|
|
|
pub fn putAssumeCapacity(self: *Self, key: K, value: V) ?KV {
|
|
|
|
assert(self.count() < self.entries.len);
|
|
|
|
self.incrementModificationCount();
|
2018-08-03 22:22:17 +01:00
|
|
|
|
|
|
|
const put_result = self.internalPut(key);
|
|
|
|
put_result.new_entry.kv.value = value;
|
|
|
|
return put_result.old_kv;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
pub fn get(hm: *const Self, key: K) ?*KV {
|
2017-04-03 23:11:57 +01:00
|
|
|
if (hm.entries.len == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-12-19 00:40:26 +00:00
|
|
|
return hm.internalGet(key);
|
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2019-06-13 12:26:40 +01:00
|
|
|
pub fn getValue(hm: *const Self, key: K) ?V {
|
|
|
|
return if (hm.get(key)) |kv| kv.value else null;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn contains(hm: *const Self, key: K) bool {
|
2018-01-17 05:22:33 +00:00
|
|
|
return hm.get(key) != null;
|
|
|
|
}
|
|
|
|
|
2019-06-13 04:39:53 +01:00
|
|
|
/// Returns any kv pair that was removed.
|
2019-04-06 22:15:12 +01:00
|
|
|
pub fn remove(hm: *Self, key: K) ?KV {
|
2018-04-11 05:32:42 +01:00
|
|
|
if (hm.entries.len == 0) return null;
|
2016-12-19 00:40:26 +00:00
|
|
|
hm.incrementModificationCount();
|
|
|
|
const start_index = hm.keyToIndex(key);
|
2018-05-10 05:29:49 +01:00
|
|
|
{
|
|
|
|
var roll_over: usize = 0;
|
|
|
|
while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
|
2019-06-06 07:26:48 +01:00
|
|
|
const index = hm.constrainIndex(start_index + roll_over);
|
2018-05-10 05:29:49 +01:00
|
|
|
var entry = &hm.entries[index];
|
|
|
|
|
|
|
|
if (!entry.used) return null;
|
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
if (!eql(entry.kv.key, key)) continue;
|
2018-05-10 05:29:49 +01:00
|
|
|
|
2019-04-06 22:15:12 +01:00
|
|
|
const removed_kv = entry.kv;
|
2018-05-10 05:29:49 +01:00
|
|
|
while (roll_over < hm.entries.len) : (roll_over += 1) {
|
2019-06-06 07:26:48 +01:00
|
|
|
const next_index = hm.constrainIndex(start_index + roll_over + 1);
|
2018-05-10 05:29:49 +01:00
|
|
|
const next_entry = &hm.entries[next_index];
|
|
|
|
if (!next_entry.used or next_entry.distance_from_start_index == 0) {
|
|
|
|
entry.used = false;
|
|
|
|
hm.size -= 1;
|
2019-04-06 22:15:12 +01:00
|
|
|
return removed_kv;
|
2018-05-10 05:29:49 +01:00
|
|
|
}
|
|
|
|
entry.* = next_entry.*;
|
|
|
|
entry.distance_from_start_index -= 1;
|
|
|
|
entry = next_entry;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2018-05-10 05:29:49 +01:00
|
|
|
unreachable; // shifting everything in the table
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
2018-05-10 05:29:49 +01:00
|
|
|
}
|
2017-04-03 23:11:57 +01:00
|
|
|
return null;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2019-06-13 04:39:53 +01:00
|
|
|
/// Calls remove(), asserts that a kv pair is removed, and discards it.
|
|
|
|
pub fn removeAssertDiscard(hm: *Self, key: K) void {
|
|
|
|
assert(hm.remove(key) != null);
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn iterator(hm: *const Self) Iterator {
|
2018-11-13 13:08:37 +00:00
|
|
|
return Iterator{
|
2016-12-19 00:40:26 +00:00
|
|
|
.hm = hm,
|
|
|
|
.count = 0,
|
|
|
|
.index = 0,
|
|
|
|
.initial_modification_count = hm.modification_count,
|
|
|
|
};
|
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-01 21:26:37 +01:00
|
|
|
pub fn clone(self: Self) !Self {
|
|
|
|
var other = Self.init(self.allocator);
|
|
|
|
try other.initCapacity(self.entries.len);
|
|
|
|
var it = self.iterator();
|
|
|
|
while (it.next()) |entry| {
|
2019-06-13 04:39:53 +01:00
|
|
|
try other.putNoClobber(entry.key, entry.value);
|
2018-08-01 21:26:37 +01:00
|
|
|
}
|
|
|
|
return other;
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:38:52 +01:00
|
|
|
fn autoCapacity(self: *Self) !void {
|
|
|
|
if (self.entries.len == 0) {
|
2019-05-03 01:01:30 +01:00
|
|
|
return self.ensureCapacityExact(16);
|
2019-05-02 07:38:52 +01:00
|
|
|
}
|
|
|
|
// if we get too full (60%), double the capacity
|
|
|
|
if (self.size * 5 >= self.entries.len * 3) {
|
2019-05-03 01:01:30 +01:00
|
|
|
return self.ensureCapacityExact(self.entries.len * 2);
|
2019-05-02 07:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
fn initCapacity(hm: *Self, capacity: usize) !void {
|
2018-01-07 21:51:46 +00:00
|
|
|
hm.entries = try hm.allocator.alloc(Entry, capacity);
|
2016-12-19 00:40:26 +00:00
|
|
|
hm.size = 0;
|
|
|
|
hm.max_distance_from_start_index = 0;
|
|
|
|
for (hm.entries) |*entry| {
|
|
|
|
entry.used = false;
|
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
fn incrementModificationCount(hm: *Self) void {
|
2016-12-19 00:40:26 +00:00
|
|
|
if (want_modification_safety) {
|
|
|
|
hm.modification_count +%= 1;
|
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const InternalPutResult = struct {
|
2018-08-03 22:22:17 +01:00
|
|
|
new_entry: *Entry,
|
|
|
|
old_kv: ?KV,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns a pointer to the new entry.
|
|
|
|
/// Asserts that there is enough space for the new item.
|
|
|
|
fn internalPut(self: *Self, orig_key: K) InternalPutResult {
|
2016-12-19 00:40:26 +00:00
|
|
|
var key = orig_key;
|
2018-08-03 22:22:17 +01:00
|
|
|
var value: V = undefined;
|
|
|
|
const start_index = self.keyToIndex(key);
|
2016-12-19 00:40:26 +00:00
|
|
|
var roll_over: usize = 0;
|
|
|
|
var distance_from_start_index: usize = 0;
|
2018-08-03 22:22:17 +01:00
|
|
|
var got_result_entry = false;
|
2018-11-13 13:08:37 +00:00
|
|
|
var result = InternalPutResult{
|
2018-08-03 22:22:17 +01:00
|
|
|
.new_entry = undefined,
|
|
|
|
.old_kv = null,
|
|
|
|
};
|
|
|
|
while (roll_over < self.entries.len) : ({
|
2018-05-10 05:29:49 +01:00
|
|
|
roll_over += 1;
|
|
|
|
distance_from_start_index += 1;
|
|
|
|
}) {
|
2019-06-06 07:26:48 +01:00
|
|
|
const index = self.constrainIndex(start_index + roll_over);
|
2018-08-03 22:22:17 +01:00
|
|
|
const entry = &self.entries[index];
|
2016-12-19 00:40:26 +00:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
if (entry.used and !eql(entry.kv.key, key)) {
|
2016-12-19 00:40:26 +00:00
|
|
|
if (entry.distance_from_start_index < distance_from_start_index) {
|
|
|
|
// robin hood to the rescue
|
2018-05-10 05:29:49 +01:00
|
|
|
const tmp = entry.*;
|
2018-08-03 22:22:17 +01:00
|
|
|
self.max_distance_from_start_index = math.max(self.max_distance_from_start_index, distance_from_start_index);
|
|
|
|
if (!got_result_entry) {
|
|
|
|
got_result_entry = true;
|
|
|
|
result.new_entry = entry;
|
|
|
|
}
|
2018-11-13 13:08:37 +00:00
|
|
|
entry.* = Entry{
|
2016-12-19 00:40:26 +00:00
|
|
|
.used = true,
|
|
|
|
.distance_from_start_index = distance_from_start_index,
|
2018-11-13 13:08:37 +00:00
|
|
|
.kv = KV{
|
2018-08-03 22:22:17 +01:00
|
|
|
.key = key,
|
|
|
|
.value = value,
|
|
|
|
},
|
2016-12-19 00:40:26 +00:00
|
|
|
};
|
2018-08-03 22:22:17 +01:00
|
|
|
key = tmp.kv.key;
|
|
|
|
value = tmp.kv.value;
|
2016-12-19 00:40:26 +00:00
|
|
|
distance_from_start_index = tmp.distance_from_start_index;
|
|
|
|
}
|
|
|
|
continue;
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 10:34:04 +01:00
|
|
|
if (entry.used) {
|
2018-08-03 22:22:17 +01:00
|
|
|
result.old_kv = entry.kv;
|
2017-04-06 10:34:04 +01:00
|
|
|
} else {
|
2016-12-19 00:40:26 +00:00
|
|
|
// adding an entry. otherwise overwriting old value with
|
|
|
|
// same key
|
2018-08-03 22:22:17 +01:00
|
|
|
self.size += 1;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
self.max_distance_from_start_index = math.max(distance_from_start_index, self.max_distance_from_start_index);
|
|
|
|
if (!got_result_entry) {
|
|
|
|
result.new_entry = entry;
|
|
|
|
}
|
2018-11-13 13:08:37 +00:00
|
|
|
entry.* = Entry{
|
2016-12-19 00:40:26 +00:00
|
|
|
.used = true,
|
|
|
|
.distance_from_start_index = distance_from_start_index,
|
2018-11-13 13:08:37 +00:00
|
|
|
.kv = KV{
|
2018-08-03 22:22:17 +01:00
|
|
|
.key = key,
|
|
|
|
.value = value,
|
|
|
|
},
|
2016-12-19 00:40:26 +00:00
|
|
|
};
|
2017-04-06 10:34:04 +01:00
|
|
|
return result;
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2017-12-22 05:50:30 +00:00
|
|
|
unreachable; // put into a full map
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
fn internalGet(hm: Self, key: K) ?*KV {
|
2016-12-19 00:40:26 +00:00
|
|
|
const start_index = hm.keyToIndex(key);
|
2018-05-10 05:29:49 +01:00
|
|
|
{
|
|
|
|
var roll_over: usize = 0;
|
|
|
|
while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
|
2019-06-06 07:26:48 +01:00
|
|
|
const index = hm.constrainIndex(start_index + roll_over);
|
2018-05-10 05:29:49 +01:00
|
|
|
const entry = &hm.entries[index];
|
|
|
|
|
|
|
|
if (!entry.used) return null;
|
2018-08-03 22:22:17 +01:00
|
|
|
if (eql(entry.kv.key, key)) return &entry.kv;
|
2018-05-10 05:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 00:40:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-05-09 00:05:41 +01:00
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
fn keyToIndex(hm: Self, key: K) usize {
|
2019-06-06 07:26:48 +01:00
|
|
|
return hm.constrainIndex(usize(hash(key)));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn constrainIndex(hm: Self, i: usize) usize {
|
|
|
|
// this is an optimization for modulo of power of two integers;
|
|
|
|
// it requires hm.entries.len to always be a power of two
|
|
|
|
return i & (hm.entries.len - 1);
|
2016-12-19 00:40:26 +00:00
|
|
|
}
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 17:49:21 +00:00
|
|
|
test "basic hash map usage" {
|
2019-06-22 15:33:00 +01:00
|
|
|
var map = AutoHashMap(i32, i32).init(std.heap.direct_allocator);
|
2016-05-09 00:05:41 +01:00
|
|
|
defer map.deinit();
|
2016-05-09 23:07:38 +01:00
|
|
|
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect((try map.put(1, 11)) == null);
|
|
|
|
testing.expect((try map.put(2, 22)) == null);
|
|
|
|
testing.expect((try map.put(3, 33)) == null);
|
|
|
|
testing.expect((try map.put(4, 44)) == null);
|
2018-01-09 05:07:01 +00:00
|
|
|
|
2019-06-13 06:48:46 +01:00
|
|
|
try map.putNoClobber(5, 55);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect((try map.put(5, 66)).?.value == 55);
|
|
|
|
testing.expect((try map.put(5, 55)).?.value == 66);
|
2018-08-03 22:22:17 +01:00
|
|
|
|
|
|
|
const gop1 = try map.getOrPut(5);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(gop1.found_existing == true);
|
|
|
|
testing.expect(gop1.kv.value == 55);
|
2018-08-03 22:22:17 +01:00
|
|
|
gop1.kv.value = 77;
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(map.get(5).?.value == 77);
|
2018-08-03 22:22:17 +01:00
|
|
|
|
|
|
|
const gop2 = try map.getOrPut(99);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(gop2.found_existing == false);
|
2018-08-03 22:22:17 +01:00
|
|
|
gop2.kv.value = 42;
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(map.get(99).?.value == 42);
|
2016-05-09 23:07:38 +01:00
|
|
|
|
2018-11-29 08:10:15 +00:00
|
|
|
const gop3 = try map.getOrPutValue(5, 5);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(gop3.value == 77);
|
2018-11-29 08:10:15 +00:00
|
|
|
|
|
|
|
const gop4 = try map.getOrPutValue(100, 41);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(gop4.value == 41);
|
2018-11-29 08:10:15 +00:00
|
|
|
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(map.contains(2));
|
|
|
|
testing.expect(map.get(2).?.value == 22);
|
2019-06-13 12:26:40 +01:00
|
|
|
testing.expect(map.getValue(2).? == 22);
|
2019-04-06 22:15:12 +01:00
|
|
|
|
|
|
|
const rmv1 = map.remove(2);
|
|
|
|
testing.expect(rmv1.?.key == 2);
|
|
|
|
testing.expect(rmv1.?.value == 22);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(map.remove(2) == null);
|
|
|
|
testing.expect(map.get(2) == null);
|
2019-06-13 12:26:40 +01:00
|
|
|
testing.expect(map.getValue(2) == null);
|
2019-06-13 04:39:53 +01:00
|
|
|
|
|
|
|
map.removeAssertDiscard(3);
|
2016-05-09 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:54:33 +01:00
|
|
|
test "iterator hash map" {
|
2019-06-22 15:33:00 +01:00
|
|
|
var reset_map = AutoHashMap(i32, i32).init(std.heap.direct_allocator);
|
2018-05-03 14:54:33 +01:00
|
|
|
defer reset_map.deinit();
|
|
|
|
|
2019-06-13 06:48:46 +01:00
|
|
|
try reset_map.putNoClobber(1, 11);
|
|
|
|
try reset_map.putNoClobber(2, 22);
|
|
|
|
try reset_map.putNoClobber(3, 33);
|
2018-05-03 14:54:33 +01:00
|
|
|
|
2019-06-30 10:35:57 +01:00
|
|
|
// TODO this test depends on the hashing algorithm, because it assumes the
|
|
|
|
// order of the elements in the hashmap. This should not be the case.
|
2019-06-10 00:24:24 +01:00
|
|
|
var keys = [_]i32{
|
2019-06-30 10:35:57 +01:00
|
|
|
1,
|
2018-05-10 05:29:49 +01:00
|
|
|
3,
|
2018-08-07 05:54:19 +01:00
|
|
|
2,
|
2018-05-10 05:29:49 +01:00
|
|
|
};
|
2019-06-10 00:24:24 +01:00
|
|
|
var values = [_]i32{
|
2019-06-30 10:35:57 +01:00
|
|
|
11,
|
2018-05-10 05:29:49 +01:00
|
|
|
33,
|
2018-08-07 05:54:19 +01:00
|
|
|
22,
|
2018-05-10 05:29:49 +01:00
|
|
|
};
|
2018-05-03 14:54:33 +01:00
|
|
|
|
|
|
|
var it = reset_map.iterator();
|
2018-05-10 05:29:49 +01:00
|
|
|
var count: usize = 0;
|
2018-05-03 14:54:33 +01:00
|
|
|
while (it.next()) |next| {
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(next.key == keys[count]);
|
|
|
|
testing.expect(next.value == values[count]);
|
2018-05-03 14:54:33 +01:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(count == 3);
|
|
|
|
testing.expect(it.next() == null);
|
2018-05-03 14:54:33 +01:00
|
|
|
it.reset();
|
|
|
|
count = 0;
|
|
|
|
while (it.next()) |next| {
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(next.key == keys[count]);
|
|
|
|
testing.expect(next.value == values[count]);
|
2018-05-03 14:54:33 +01:00
|
|
|
count += 1;
|
|
|
|
if (count == 2) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
it.reset();
|
2018-06-10 04:42:14 +01:00
|
|
|
var entry = it.next().?;
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(entry.key == keys[0]);
|
|
|
|
testing.expect(entry.value == values[0]);
|
2018-05-03 14:54:33 +01:00
|
|
|
}
|
|
|
|
|
2019-05-04 05:15:00 +01:00
|
|
|
test "ensure capacity" {
|
2019-06-22 15:33:00 +01:00
|
|
|
var map = AutoHashMap(i32, i32).init(std.heap.direct_allocator);
|
2019-05-04 05:15:00 +01:00
|
|
|
defer map.deinit();
|
|
|
|
|
|
|
|
try map.ensureCapacity(20);
|
|
|
|
const initialCapacity = map.entries.len;
|
|
|
|
testing.expect(initialCapacity >= 20);
|
2019-05-12 17:56:01 +01:00
|
|
|
var i: i32 = 0;
|
2019-05-04 05:15:00 +01:00
|
|
|
while (i < 20) : (i += 1) {
|
2019-05-12 17:56:01 +01:00
|
|
|
testing.expect(map.putAssumeCapacity(i, i + 10) == null);
|
2019-05-04 05:15:00 +01:00
|
|
|
}
|
|
|
|
// shouldn't resize from putAssumeCapacity
|
|
|
|
testing.expect(initialCapacity == map.entries.len);
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:08:34 +01:00
|
|
|
pub fn getHashPtrAddrFn(comptime K: type) (fn (K) u32) {
|
2018-11-13 13:08:37 +00:00
|
|
|
return struct {
|
2018-08-24 04:08:34 +01:00
|
|
|
fn hash(key: K) u32 {
|
|
|
|
return getAutoHashFn(usize)(@ptrToInt(key));
|
|
|
|
}
|
|
|
|
}.hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getTrivialEqlFn(comptime K: type) (fn (K, K) bool) {
|
2018-11-13 13:08:37 +00:00
|
|
|
return struct {
|
2018-08-24 04:08:34 +01:00
|
|
|
fn eql(a: K, b: K) bool {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
}.eql;
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:22:17 +01:00
|
|
|
pub fn getAutoHashFn(comptime K: type) (fn (K) u32) {
|
2018-11-13 13:08:37 +00:00
|
|
|
return struct {
|
2018-08-03 22:22:17 +01:00
|
|
|
fn hash(key: K) u32 {
|
2019-07-02 17:40:01 +01:00
|
|
|
var hasher = Wyhash.init(0);
|
|
|
|
autoHash(&hasher, key);
|
|
|
|
return @truncate(u32, hasher.final());
|
2018-08-03 22:22:17 +01:00
|
|
|
}
|
|
|
|
}.hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {
|
2018-11-13 13:08:37 +00:00
|
|
|
return struct {
|
2018-08-03 22:22:17 +01:00
|
|
|
fn eql(a: K, b: K) bool {
|
2019-06-30 10:35:57 +01:00
|
|
|
return meta.eql(a, b);
|
2018-08-03 22:22:17 +01:00
|
|
|
}
|
|
|
|
}.eql;
|
|
|
|
}
|
2019-10-11 22:08:08 +01:00
|
|
|
|
|
|
|
pub fn getAutoHashStratFn(comptime K: type, comptime strategy: std.hash.Strategy) (fn (K) u32) {
|
|
|
|
return struct {
|
|
|
|
fn hash(key: K) u32 {
|
|
|
|
var hasher = Wyhash.init(0);
|
|
|
|
std.hash.autoHashStrat(&hasher, key, strategy);
|
|
|
|
return @truncate(u32, hasher.final());
|
|
|
|
}
|
|
|
|
}.hash;
|
|
|
|
}
|