2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("index.zig");
|
|
|
|
const debug = std.debug;
|
2016-08-11 22:25:13 -07:00
|
|
|
const assert = debug.assert;
|
2017-12-23 19:08:53 -08:00
|
|
|
const math = std.math;
|
|
|
|
const mem = std.mem;
|
2016-05-08 16:05:41 -07:00
|
|
|
const Allocator = mem.Allocator;
|
2017-05-01 10:12:38 -07:00
|
|
|
const builtin = @import("builtin");
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2017-05-02 14:34:21 -07:00
|
|
|
const want_modification_safety = builtin.mode != builtin.Mode.ReleaseFast;
|
2016-05-09 15:07:38 -07:00
|
|
|
const debug_u32 = if (want_modification_safety) u32 else void;
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-30 13:09:11 -07: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 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return struct {
|
2016-12-18 16:40:26 -08:00
|
|
|
entries: []Entry,
|
|
|
|
size: usize,
|
|
|
|
max_distance_from_start_index: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
allocator: *Allocator,
|
2016-12-18 16:40:26 -08:00
|
|
|
// this is used to detect bugs where a hashtable is edited while an iterator is running.
|
|
|
|
modification_count: debug_u32,
|
|
|
|
|
|
|
|
const Self = this;
|
|
|
|
|
|
|
|
pub const Entry = struct {
|
|
|
|
used: bool,
|
|
|
|
distance_from_start_index: usize,
|
|
|
|
key: K,
|
|
|
|
value: V,
|
|
|
|
};
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2016-12-18 16:40:26 -08:00
|
|
|
pub const Iterator = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
hm: *const Self,
|
2016-12-18 16:40:26 -08: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-05-31 07:56:59 -07:00
|
|
|
pub fn next(it: *Iterator) ?*Entry {
|
2016-12-18 16:40:26 -08:00
|
|
|
if (want_modification_safety) {
|
|
|
|
assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
if (it.count >= it.hm.size) return null;
|
2017-05-03 15:12:07 -07:00
|
|
|
while (it.index < it.hm.entries.len) : (it.index += 1) {
|
2016-12-18 16:40:26 -08:00
|
|
|
const entry = &it.hm.entries[it.index];
|
|
|
|
if (entry.used) {
|
|
|
|
it.index += 1;
|
|
|
|
it.count += 1;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 21:50:30 -08:00
|
|
|
unreachable; // no next item
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
2018-05-03 06:54:33 -07:00
|
|
|
|
|
|
|
// Reset the iterator to the initial index
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn reset(it: *Iterator) void {
|
2018-05-03 06:54:33 -07:00
|
|
|
it.count = 0;
|
|
|
|
it.index = 0;
|
|
|
|
// Resetting the modification count too
|
|
|
|
it.initial_modification_count = it.hm.modification_count;
|
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
};
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn init(allocator: *Allocator) Self {
|
2018-05-09 21:29:49 -07:00
|
|
|
return Self{
|
2017-04-06 02:34:04 -07:00
|
|
|
.entries = []Entry{},
|
|
|
|
.allocator = allocator,
|
|
|
|
.size = 0,
|
|
|
|
.max_distance_from_start_index = 0,
|
2018-01-16 20:19:05 -08:00
|
|
|
.modification_count = if (want_modification_safety) 0 else {},
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2016-05-09 15:07:38 -07:00
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn deinit(hm: *const Self) void {
|
2017-01-22 21:11:21 -08:00
|
|
|
hm.allocator.free(hm.entries);
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn clear(hm: *Self) void {
|
2016-12-18 16:40:26 -08:00
|
|
|
for (hm.entries) |*entry| {
|
|
|
|
entry.used = false;
|
2016-05-09 15:07:38 -07:00
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
hm.size = 0;
|
|
|
|
hm.max_distance_from_start_index = 0;
|
|
|
|
hm.incrementModificationCount();
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
2016-05-09 15:07:38 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn count(hm: *const Self) usize {
|
2018-05-03 06:54:33 -07:00
|
|
|
return hm.size;
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:34:04 -07:00
|
|
|
/// Returns the value that was already there.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn put(hm: *Self, key: K, value: *const V) !?V {
|
2017-01-05 00:40:04 -08:00
|
|
|
if (hm.entries.len == 0) {
|
2018-01-07 13:51:46 -08:00
|
|
|
try hm.initCapacity(16);
|
2017-01-05 00:40:04 -08:00
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
hm.incrementModificationCount();
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2016-12-30 22:31:23 -08:00
|
|
|
// if we get too full (60%), double the capacity
|
|
|
|
if (hm.size * 5 >= hm.entries.len * 3) {
|
2016-12-18 16:40:26 -08:00
|
|
|
const old_entries = hm.entries;
|
2018-01-07 13:51:46 -08:00
|
|
|
try hm.initCapacity(hm.entries.len * 2);
|
2016-12-18 16:40:26 -08:00
|
|
|
// dump all of the old elements into the new table
|
|
|
|
for (old_entries) |*old_entry| {
|
|
|
|
if (old_entry.used) {
|
2017-04-06 02:34:04 -07:00
|
|
|
_ = hm.internalPut(old_entry.key, old_entry.value);
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-22 21:11:21 -08:00
|
|
|
hm.allocator.free(old_entries);
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2017-04-06 02:34:04 -07:00
|
|
|
return hm.internalPut(key, value);
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn get(hm: *const Self, key: K) ?*Entry {
|
2017-04-03 15:11:57 -07:00
|
|
|
if (hm.entries.len == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
return hm.internalGet(key);
|
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn contains(hm: *const Self, key: K) bool {
|
2018-01-16 21:22:33 -08:00
|
|
|
return hm.get(key) != null;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn remove(hm: *Self, key: K) ?*Entry {
|
2018-04-10 21:32:42 -07:00
|
|
|
if (hm.entries.len == 0) return null;
|
2016-12-18 16:40:26 -08:00
|
|
|
hm.incrementModificationCount();
|
|
|
|
const start_index = hm.keyToIndex(key);
|
2018-05-09 21:29:49 -07:00
|
|
|
{
|
|
|
|
var roll_over: usize = 0;
|
|
|
|
while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
|
|
|
|
const index = (start_index + roll_over) % hm.entries.len;
|
|
|
|
var entry = &hm.entries[index];
|
|
|
|
|
|
|
|
if (!entry.used) return null;
|
|
|
|
|
|
|
|
if (!eql(entry.key, key)) continue;
|
|
|
|
|
|
|
|
while (roll_over < hm.entries.len) : (roll_over += 1) {
|
|
|
|
const next_index = (start_index + roll_over + 1) % hm.entries.len;
|
|
|
|
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;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
entry.* = next_entry.*;
|
|
|
|
entry.distance_from_start_index -= 1;
|
|
|
|
entry = next_entry;
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2018-05-09 21:29:49 -07:00
|
|
|
unreachable; // shifting everything in the table
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
2018-05-09 21:29:49 -07:00
|
|
|
}
|
2017-04-03 15:11:57 -07:00
|
|
|
return null;
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn iterator(hm: *const Self) Iterator {
|
2018-05-09 21:29:49 -07:00
|
|
|
return Iterator{
|
2016-12-18 16:40:26 -08:00
|
|
|
.hm = hm,
|
|
|
|
.count = 0,
|
|
|
|
.index = 0,
|
|
|
|
.initial_modification_count = hm.modification_count,
|
|
|
|
};
|
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn initCapacity(hm: *Self, capacity: usize) !void {
|
2018-01-07 13:51:46 -08:00
|
|
|
hm.entries = try hm.allocator.alloc(Entry, capacity);
|
2016-12-18 16:40:26 -08:00
|
|
|
hm.size = 0;
|
|
|
|
hm.max_distance_from_start_index = 0;
|
|
|
|
for (hm.entries) |*entry| {
|
|
|
|
entry.used = false;
|
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn incrementModificationCount(hm: *Self) void {
|
2016-12-18 16:40:26 -08:00
|
|
|
if (want_modification_safety) {
|
|
|
|
hm.modification_count +%= 1;
|
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2017-04-06 02:34:04 -07:00
|
|
|
/// Returns the value that was already there.
|
2018-05-31 07:56:59 -07:00
|
|
|
fn internalPut(hm: *Self, orig_key: K, orig_value: *const V) ?V {
|
2016-12-18 16:40:26 -08:00
|
|
|
var key = orig_key;
|
2018-05-09 21:29:49 -07:00
|
|
|
var value = orig_value.*;
|
2016-12-18 16:40:26 -08:00
|
|
|
const start_index = hm.keyToIndex(key);
|
|
|
|
var roll_over: usize = 0;
|
|
|
|
var distance_from_start_index: usize = 0;
|
2018-05-09 21:29:49 -07:00
|
|
|
while (roll_over < hm.entries.len) : ({
|
|
|
|
roll_over += 1;
|
|
|
|
distance_from_start_index += 1;
|
|
|
|
}) {
|
2016-12-18 16:40:26 -08:00
|
|
|
const index = (start_index + roll_over) % hm.entries.len;
|
|
|
|
const entry = &hm.entries[index];
|
|
|
|
|
2017-03-26 02:21:28 -07:00
|
|
|
if (entry.used and !eql(entry.key, key)) {
|
2016-12-18 16:40:26 -08:00
|
|
|
if (entry.distance_from_start_index < distance_from_start_index) {
|
|
|
|
// robin hood to the rescue
|
2018-05-09 21:29:49 -07:00
|
|
|
const tmp = entry.*;
|
|
|
|
hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index, distance_from_start_index);
|
|
|
|
entry.* = Entry{
|
2016-12-18 16:40:26 -08:00
|
|
|
.used = true,
|
|
|
|
.distance_from_start_index = distance_from_start_index,
|
|
|
|
.key = key,
|
|
|
|
.value = value,
|
|
|
|
};
|
|
|
|
key = tmp.key;
|
|
|
|
value = tmp.value;
|
|
|
|
distance_from_start_index = tmp.distance_from_start_index;
|
|
|
|
}
|
|
|
|
continue;
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2017-04-06 02:34:04 -07:00
|
|
|
var result: ?V = null;
|
|
|
|
if (entry.used) {
|
|
|
|
result = entry.value;
|
|
|
|
} else {
|
2016-12-18 16:40:26 -08:00
|
|
|
// adding an entry. otherwise overwriting old value with
|
|
|
|
// same key
|
|
|
|
hm.size += 1;
|
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2016-12-18 16:40:26 -08:00
|
|
|
hm.max_distance_from_start_index = math.max(distance_from_start_index, hm.max_distance_from_start_index);
|
2018-05-09 21:29:49 -07:00
|
|
|
entry.* = Entry{
|
2016-12-18 16:40:26 -08:00
|
|
|
.used = true,
|
|
|
|
.distance_from_start_index = distance_from_start_index,
|
|
|
|
.key = key,
|
|
|
|
.value = value,
|
|
|
|
};
|
2017-04-06 02:34:04 -07:00
|
|
|
return result;
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2017-12-21 21:50:30 -08:00
|
|
|
unreachable; // put into a full map
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn internalGet(hm: *const Self, key: K) ?*Entry {
|
2016-12-18 16:40:26 -08:00
|
|
|
const start_index = hm.keyToIndex(key);
|
2018-05-09 21:29:49 -07:00
|
|
|
{
|
|
|
|
var roll_over: usize = 0;
|
|
|
|
while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
|
|
|
|
const index = (start_index + roll_over) % hm.entries.len;
|
|
|
|
const entry = &hm.entries[index];
|
|
|
|
|
|
|
|
if (!entry.used) return null;
|
|
|
|
if (eql(entry.key, key)) return entry;
|
|
|
|
}
|
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
return null;
|
|
|
|
}
|
2016-05-08 16:05:41 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn keyToIndex(hm: *const Self, key: K) usize {
|
2016-12-18 16:40:26 -08:00
|
|
|
return usize(hash(key)) % hm.entries.len;
|
|
|
|
}
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2018-02-23 09:49:21 -08:00
|
|
|
test "basic hash map usage" {
|
2018-04-10 21:32:42 -07:00
|
|
|
var direct_allocator = std.heap.DirectAllocator.init();
|
|
|
|
defer direct_allocator.deinit();
|
|
|
|
|
|
|
|
var map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
|
2016-05-08 16:05:41 -07:00
|
|
|
defer map.deinit();
|
2016-05-09 15:07:38 -07:00
|
|
|
|
2018-07-09 07:33:12 -07:00
|
|
|
assert((try map.put(1, 11)) == null);
|
|
|
|
assert((try map.put(2, 22)) == null);
|
|
|
|
assert((try map.put(3, 33)) == null);
|
|
|
|
assert((try map.put(4, 44)) == null);
|
|
|
|
assert((try map.put(5, 55)) == null);
|
2018-01-08 21:07:01 -08:00
|
|
|
|
2018-07-09 07:33:12 -07:00
|
|
|
assert((try map.put(5, 66)).? == 55);
|
|
|
|
assert((try map.put(5, 55)).? == 66);
|
2016-05-09 15:07:38 -07:00
|
|
|
|
2018-05-04 14:48:14 -07:00
|
|
|
assert(map.contains(2));
|
2018-06-09 20:42:14 -07:00
|
|
|
assert(map.get(2).?.value == 22);
|
2017-04-03 15:11:57 -07:00
|
|
|
_ = map.remove(2);
|
|
|
|
assert(map.remove(2) == null);
|
2017-05-03 14:23:11 -07:00
|
|
|
assert(map.get(2) == null);
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
|
|
|
|
2018-05-03 06:54:33 -07:00
|
|
|
test "iterator hash map" {
|
|
|
|
var direct_allocator = std.heap.DirectAllocator.init();
|
|
|
|
defer direct_allocator.deinit();
|
2018-05-04 14:48:14 -07:00
|
|
|
|
2018-05-03 06:54:33 -07:00
|
|
|
var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
|
|
|
|
defer reset_map.deinit();
|
|
|
|
|
2018-07-09 07:33:12 -07:00
|
|
|
assert((try reset_map.put(1, 11)) == null);
|
|
|
|
assert((try reset_map.put(2, 22)) == null);
|
|
|
|
assert((try reset_map.put(3, 33)) == null);
|
2018-05-03 06:54:33 -07:00
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
var keys = []i32{
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
};
|
|
|
|
var values = []i32{
|
|
|
|
11,
|
|
|
|
22,
|
|
|
|
33,
|
|
|
|
};
|
2018-05-03 06:54:33 -07:00
|
|
|
|
|
|
|
var it = reset_map.iterator();
|
2018-05-09 21:29:49 -07:00
|
|
|
var count: usize = 0;
|
2018-05-03 06:54:33 -07:00
|
|
|
while (it.next()) |next| {
|
|
|
|
assert(next.key == keys[count]);
|
|
|
|
assert(next.value == values[count]);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(count == 3);
|
|
|
|
assert(it.next() == null);
|
|
|
|
it.reset();
|
|
|
|
count = 0;
|
|
|
|
while (it.next()) |next| {
|
|
|
|
assert(next.key == keys[count]);
|
|
|
|
assert(next.value == values[count]);
|
|
|
|
count += 1;
|
|
|
|
if (count == 2) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
it.reset();
|
2018-06-09 20:42:14 -07:00
|
|
|
var entry = it.next().?;
|
2018-05-03 06:54:33 -07:00
|
|
|
assert(entry.key == keys[0]);
|
|
|
|
assert(entry.value == values[0]);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn hash_i32(x: i32) u32 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(u32, x);
|
2016-05-08 16:05:41 -07:00
|
|
|
}
|
2017-04-06 02:34:04 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn eql_i32(a: i32, b: i32) bool {
|
2017-12-21 21:50:30 -08:00
|
|
|
return a == b;
|
2018-05-04 14:48:14 -07:00
|
|
|
}
|