1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
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);
}
|