diff options
Diffstat (limited to 'src/hash_map.zig')
-rw-r--r-- | src/hash_map.zig | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/src/hash_map.zig b/src/hash_map.zig index 3c09e64f4..4f5c72e2f 100644 --- a/src/hash_map.zig +++ b/src/hash_map.zig @@ -112,7 +112,7 @@ pub fn HashMap( ) type { return struct { unmanaged: Unmanaged, - allocator: *Allocator, + allocator: Allocator, pub const Unmanaged = HashMapUnmanaged(K, V, hashFn, eqlFn, max_load_percentage); pub const Entry = Unmanaged.Entry; @@ -123,7 +123,7 @@ pub fn HashMap( const Self = @This(); - pub fn init(allocator: *Allocator) Self { + pub fn init(allocator: Allocator) Self { return .{ .unmanaged = .{}, .allocator = allocator, @@ -411,7 +411,7 @@ pub fn HashMapUnmanaged( index: Size = 0, pub fn next(it: *Iterator) ?*Entry { - if (std.builtin.mode != .ReleaseFast) assert(it.index <= it.hm.capacity()); + if (@import("builtin").mode != .ReleaseFast) assert(it.index <= it.hm.capacity()); if (it.hm.size == 0) return null; const cap = it.hm.capacity(); @@ -440,7 +440,7 @@ pub fn HashMapUnmanaged( pub const Managed = HashMap(K, V, hashFn, eqlFn, max_load_percentage); - pub fn promote(self: Self, allocator: *Allocator) Managed { + pub fn promote(self: Self, allocator: Allocator) Managed { return .{ .unmanaged = self, .allocator = allocator, @@ -451,12 +451,12 @@ pub fn HashMapUnmanaged( return size * 100 < max_load_percentage * cap; } - pub fn deinit(self: *Self, allocator: *Allocator) void { + pub fn deinit(self: *Self, allocator: Allocator) void { self.deallocate(allocator); self.* = undefined; } - fn deallocate(self: *Self, allocator: *Allocator) void { + fn deallocate(self: *Self, allocator: Allocator) void { if (self.metadata == null) return; const cap = self.capacity(); @@ -482,7 +482,7 @@ pub fn HashMapUnmanaged( return new_cap; } - pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void { + pub fn ensureCapacity(self: *Self, allocator: Allocator, new_size: Size) !void { if (new_size > self.size) try self.growIfNeeded(allocator, new_size - self.size); } @@ -495,7 +495,7 @@ pub fn HashMapUnmanaged( } } - pub fn clearAndFree(self: *Self, allocator: *Allocator) void { + pub fn clearAndFree(self: *Self, allocator: Allocator) void { self.deallocate(allocator); self.size = 0; self.available = 0; @@ -524,8 +524,8 @@ pub fn HashMapUnmanaged( } /// Insert an entry in the map. Assumes it is not already present. - pub fn putNoClobber(self: *Self, allocator: *Allocator, key: K, value: V) !void { - if (std.builtin.mode != .ReleaseFast) assert(!self.contains(key)); + pub fn putNoClobber(self: *Self, allocator: Allocator, key: K, value: V) !void { + if (@import("builtin").mode != .ReleaseFast) assert(!self.contains(key)); try self.growIfNeeded(allocator, 1); self.putAssumeCapacityNoClobber(key, value); @@ -542,7 +542,7 @@ pub fn HashMapUnmanaged( /// Insert an entry in the map. Assumes it is not already present, /// and that no allocation is needed. pub fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void { - if (std.builtin.mode != .ReleaseFast) assert(!self.contains(key)); + if (@import("builtin").mode != .ReleaseFast) assert(!self.contains(key)); const hash = hashFn(key); putAssumeCapacityNoClobberWithHash(self, key, hash, value); @@ -561,7 +561,7 @@ pub fn HashMapUnmanaged( } if (!metadata[0].isTombstone()) { - if (std.builtin.mode != .ReleaseFast) assert(self.available > 0); + if (@import("builtin").mode != .ReleaseFast) assert(self.available > 0); self.available -= 1; } @@ -573,7 +573,7 @@ pub fn HashMapUnmanaged( } /// Inserts a new `Entry` into the hash map, returning the previous one, if any. - pub fn fetchPut(self: *Self, allocator: *Allocator, key: K, value: V) !?Entry { + pub fn fetchPut(self: *Self, allocator: Allocator, key: K, value: V) !?Entry { const gop = try self.getOrPut(allocator, key); var result: ?Entry = null; if (gop.found_existing) { @@ -632,19 +632,19 @@ pub fn HashMapUnmanaged( } /// Insert an entry if the associated key is not already present, otherwise update preexisting value. - pub fn put(self: *Self, allocator: *Allocator, key: K, value: V) !void { + pub fn put(self: *Self, allocator: Allocator, key: K, value: V) !void { const result = try self.getOrPut(allocator, key); result.entry.value = value; } /// Insert an entry if the associated key is not already present, otherwise update preexisting value. - pub fn putWithHash(self: *Self, allocator: *Allocator, key: K, hash: u64, value: V) !void { + pub fn putWithHash(self: *Self, allocator: Allocator, key: K, hash: u64, value: V) !void { const result = try self.getOrPutWithHash(allocator, key, hash); result.entry.value = value; } /// Get an optional pointer to the value associated with key, if present. - pub fn getHash(self: Self, key: K) u64 { + pub fn getHash(key: K) u64 { return hashFn(key); } @@ -699,13 +699,13 @@ pub fn HashMapUnmanaged( return null; } - pub fn getOrPut(self: *Self, allocator: *Allocator, key: K) !GetOrPutResult { + pub fn getOrPut(self: *Self, allocator: Allocator, key: K) !GetOrPutResult { try self.growIfNeeded(allocator, 1); return self.getOrPutAssumeCapacity(key); } - pub fn getOrPutWithHash(self: *Self, allocator: *Allocator, key: K, hash: u64) !GetOrPutResult { + pub fn getOrPutWithHash(self: *Self, allocator: Allocator, key: K, hash: u64) !GetOrPutResult { try self.growIfNeeded(allocator, 1); return self.getOrPutAssumeCapacityWithHash(key, hash); @@ -753,7 +753,7 @@ pub fn HashMapUnmanaged( return GetOrPutResult{ .entry = entry, .found_existing = false }; } - pub fn getOrPutValue(self: *Self, allocator: *Allocator, key: K, value: V) !*Entry { + pub fn getOrPutValue(self: *Self, allocator: Allocator, key: K, value: V) !*Entry { const res = try self.getOrPut(allocator, key); if (!res.found_existing) res.entry.value = value; return res.entry; @@ -796,7 +796,7 @@ pub fn HashMapUnmanaged( /// Asserts there is an `Entry` with matching key, deletes it from the hash map, /// and discards it. pub fn removeAssertDiscard(self: *Self, key: K) void { - if (std.builtin.mode != .ReleaseFast) assert(self.contains(key)); + if (@import("builtin").mode != .ReleaseFast) assert(self.contains(key)); const hash = hashFn(key); const mask = self.capacity() - 1; @@ -829,17 +829,17 @@ pub fn HashMapUnmanaged( // what has to stay under the max_load_percentage of capacity. fn load(self: *const Self) Size { const max_load = (self.capacity() * max_load_percentage) / 100; - if (std.builtin.mode != .ReleaseFast) assert(max_load >= self.available); + if (@import("builtin").mode != .ReleaseFast) assert(max_load >= self.available); return @truncate(Size, max_load - self.available); } - fn growIfNeeded(self: *Self, allocator: *Allocator, new_count: Size) !void { + fn growIfNeeded(self: *Self, allocator: Allocator, new_count: Size) !void { if (new_count > self.available) { try self.grow(allocator, capacityForSize(self.load() + new_count)); } } - pub fn clone(self: Self, allocator: *Allocator) !Self { + pub fn clone(self: Self, allocator: Allocator) !Self { var other = Self{}; if (self.size == 0) return other; @@ -864,10 +864,10 @@ pub fn HashMapUnmanaged( return other; } - fn grow(self: *Self, allocator: *Allocator, new_capacity: Size) !void { + fn grow(self: *Self, allocator: Allocator, new_capacity: Size) !void { const new_cap = std.math.max(new_capacity, minimal_capacity); - if (std.builtin.mode != .ReleaseFast) assert(new_cap > self.capacity()); - if (std.builtin.mode != .ReleaseFast) assert(std.math.isPowerOfTwo(new_cap)); + if (@import("builtin").mode != .ReleaseFast) assert(new_cap > self.capacity()); + if (@import("builtin").mode != .ReleaseFast) assert(std.math.isPowerOfTwo(new_cap)); var map = Self{}; defer map.deinit(allocator); @@ -894,7 +894,7 @@ pub fn HashMapUnmanaged( std.mem.swap(Self, self, &map); } - fn allocate(self: *Self, allocator: *Allocator, new_capacity: Size) !void { + fn allocate(self: *Self, allocator: Allocator, new_capacity: Size) !void { const meta_size = @sizeOf(Header) + new_capacity * @sizeOf(Metadata); const alignment = @alignOf(Entry) - 1; @@ -908,7 +908,7 @@ pub fn HashMapUnmanaged( const metadata = ptr + @sizeOf(Header); var entry_ptr = ptr + meta_size; entry_ptr = (entry_ptr + alignment) & ~@as(usize, alignment); - if (std.builtin.mode != .ReleaseFast) assert(entry_ptr + @as(usize, new_capacity) * @sizeOf(Entry) <= ptr + total_size); + if (@import("builtin").mode != .ReleaseFast) assert(entry_ptr + @as(usize, new_capacity) * @sizeOf(Entry) <= ptr + total_size); const hdr = @intToPtr(*Header, ptr); hdr.entries = @intToPtr([*]Entry, entry_ptr); @@ -1289,7 +1289,7 @@ test "std.hash_map getOrPut" { i = 0; while (i < 20) : (i += 1) { - var n = try map.getOrPutValue(i, 1); + _ = try map.getOrPutValue(i, 1); } i = 0; |