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, fp: u16, }; 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) { istr_hash_map = .init(alloc); 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, std.mem.zeroes(Bucket)); self.used_threshold = bcount * max_fill_percent / 100; } pub fn deinit(self: *Set) void { self.alloc.free(self.buckets); } /// Add, or get existing, string. pub fn add(self: *Set, s: []const u8) !IstrPtr { std.debug.assert(s.len < 256); if (test_stdlib_impl) { return addStdlib(self.alloc, s); } if (self.used_buckets > self.used_threshold) { try self.resize(); } const idx_mask = self.buckets.len - 1; const hash = std.hash_map.hashString(s); const fp: u16 = @truncate(hash); const idx_init = @as(usize, @truncate(hash & idx_mask)); var idx = idx_init; while (true) : (idx = (idx +% 1) & idx_mask) { const bucket = self.buckets[idx]; if (bucket.ptr == 0) break; if (bucket.fp == fp) { const istr: IstrPtr = @ptrFromInt(bucket.ptr); if (std.hash_map.eqlString(s, istr.str())) return istr; } } self.used_buckets += 1; const algn = std.mem.Alignment.of(IstrPtr); const size = @sizeOf(IstrHead) + s.len; const ptr = try self.alloc.alignedAlloc(u8, algn, size); const istr: IstrPtr = @ptrCast(ptr); istr.* = .{ .len = @intCast(s.len) }; istr.putStr(s); self.buckets[idx] = .{ .fp = fp, .ptr = @intCast(@intFromPtr(istr)), }; return istr; } fn resize(self: *Set) !void { const old_bcount = self.buckets.len; const new_bcount = self.buckets.len << 1; const old_mask = old_bcount - 1; const new_mask = new_bcount - 1; const old_buckets = self.buckets; try self.allocBuckets(new_bcount); defer self.alloc.free(old_buckets); const new_buckets = self.buckets; // Make sure we're not in the middle of a cluster; find an empty bucket. const idx_start = b: for (old_buckets, 0..) |_, idx| { if (old_buckets[idx].ptr == 0) break :b idx; } else unreachable; // Resize strategy guarantees empty slots var idx = idx_start; while (true) { // +1 at start of first iteration is OK since idx_start is empty. idx = (idx +% 1) & old_mask; if (idx == idx_start) break; if (old_buckets[idx].ptr == 0) continue; var dest_lo = idx; var dest_hi = idx + old_bcount; while (true) : (idx = (idx +% 1) & old_mask) { const bucket = old_buckets[idx]; if (bucket.ptr == 0) break; const istr: IstrPtr = @ptrFromInt(bucket.ptr); const hash = std.hash_map.hashString(istr.str()); const new_idx = @as(usize, @truncate(hash & new_mask)); if (new_idx < old_bcount) { new_buckets[dest_lo] = bucket; dest_lo += 1; } else { new_buckets[dest_hi] = bucket; dest_hi = (dest_hi +% 1) & new_mask; } } if (idx == idx_start) break; } } // Using stdlib, to compare const Map = std.hash_map.StringHashMap(IstrPtr); const str_context = std.hash_map.StringContext{}; var istr_hash_map: Map = undefined; pub fn addStdlib(alloc: Alloc, str: []const u8) !IstrPtr { const gop = istr_hash_map.getOrPutAdapted( str, str_context, ) catch @panic("OOM"); if (gop.found_existing) { return gop.value_ptr.*; } std.debug.assert(str.len <= std.math.maxInt(u48)); const header: IstrHead = .{ .len = @intCast(str.len) }; const h_align: std.mem.Alignment = @enumFromInt(@alignOf(IstrPtr)); const h_size = @sizeOf(IstrHead); const size = str.len + h_size; const mem = alloc.alignedAlloc(u8, h_align, size) catch @panic("OOM"); const h_bytes: [h_size]u8 = @bitCast(header); @memcpy(mem[0..h_size], &h_bytes); @memcpy(mem[h_size..size], str); gop.key_ptr.* = alloc.dupe(u8, str) catch @panic("OOM"); gop.value_ptr.* = @ptrCast(mem.ptr); return @ptrCast(mem.ptr); }