const std = @import("std"); const Alloc = std.mem.Allocator; const value = @import("../value.zig"); const IstrPtr = value.istr.IstrPtr; const IstrHead = value.istr.IstrHead; const Set = @This(); const max_fill_percent = 80; const Bucket = packed struct(u64) { ptr: u48 = 0, fp: u16 = 0, pub fn empty(self: Bucket) bool { return self.ptr == 0; } pub fn istrPtr(self: *const Bucket) IstrPtr { return @ptrFromInt(self.ptr); } pub fn putIstrPtr(self: *Bucket, istr: IstrPtr) void { self.ptr = @intCast(@intFromPtr(istr)); } }; alloc: Alloc, buckets: []Bucket = undefined, used_buckets: usize = 0, used_threshold: usize = undefined, const test_stdlib_impl = false; const default_bcount = 512; pub fn init(alloc: Alloc) !Set { return initCustom(alloc, default_bcount); } pub fn initCustom(alloc: Alloc, bcount: usize) !Set { if (test_stdlib_impl) { try map.ensureTotalCapacity(alloc, 512); return Set{ .alloc = alloc }; } std.debug.assert(@popCount(bcount) == 1); // Must be power of 2. var self = Set{ .alloc = alloc }; try self.allocBuckets(bcount); return self; } fn allocBuckets(self: *Set, bcount: usize) !void { self.buckets = try self.alloc.alloc(Bucket, bcount); @memset(self.buckets, Bucket{}); self.used_threshold = bcount * max_fill_percent / 100; } pub fn deinit(self: *Set) void { self.alloc.free(self.buckets); } /// Get the istr with the given string contents, or alloc and store a new one. pub fn getOrNew(self: *Set, s: []const u8) !IstrPtr { if (test_stdlib_impl) { return self.addStdlib(s); } std.debug.assert(s.len < 256); return self.getOrPutOrNew(s, null); } /// Get the canonical istr with the same contents, or store this one as it. pub fn getOrPut(self: *Set, istr: IstrPtr) !IstrPtr { return self.getOrPutOrNew(istr.str(), istr); } fn getOrPutOrNew(self: *Set, s: []const u8, ptr: ?IstrPtr) !IstrPtr { if (self.used_buckets > self.used_threshold) { try self.resize(); } const hash = strHash(s); const fp = hashFp(hash); const idx_mask = self.buckets.len - 1; const idx_start = hash & idx_mask; var idx = idx_start; while (true) : (idx = (idx + 1) & idx_mask) { const bucket = &self.buckets[idx]; if (bucket.empty()) { self.used_buckets += 1; const istr = ptr orelse try value.istr.new(self.alloc, s); bucket.putIstrPtr(istr); bucket.fp = fp; return istr; } if (bucket.fp == fp) { const istr = bucket.istrPtr(); if (strEq(s, istr.str())) return istr; } } } fn resize(self: *Set) !void { const old_buckets = self.buckets; try self.allocBuckets(old_buckets.len << 1); defer self.alloc.free(old_buckets); const new_buckets = self.buckets; const idx_mask = new_buckets.len - 1; for (old_buckets) |old| { if (old.empty()) continue; const str = old.istrPtr().str(); var idx = strHash(str) & idx_mask; while (!new_buckets[idx].empty()) idx = (idx + 1) & idx_mask; new_buckets[idx] = old; } } fn strHash(s: []const u8) u64 { return std.hash_map.hashString(s); } fn strEq(s: []const u8, s2: []const u8) bool { return std.hash_map.eqlString(s, s2); } fn hashFp(hash: u64) u16 { return @intCast(hash >> 48); } // Using stdlib, to compare const str_ctx = std.hash_map.StringContext{}; const Map = std.hash_map.StringHashMapUnmanaged(IstrPtr); var map: Map = .empty; pub fn addStdlib(self: *Set, s: []const u8) !IstrPtr { const gop = map.getOrPutAdapted(self.alloc, s, str_ctx) catch @panic("OOM"); if (gop.found_existing) { return gop.value_ptr.*; } const istr = try self.newIstr(s); gop.key_ptr.* = istr.str(); gop.value_ptr.* = istr; return istr; }