summaryrefslogtreecommitdiff
path: root/src/comptime_map.zig
diff options
context:
space:
mode:
authorGravatar Sam Atman2026-02-04 21:21:14 -0500
committerGravatar Sam Atman2026-02-04 21:21:14 -0500
commit904fa4d94f30825bec490133ff402c6350f45e26 (patch)
treecbfc44b59ca1e539d6d2a4ff8f083fb4e25d41ef /src/comptime_map.zig
parentRest of the 'easy' stuff (diff)
downloadzg-904fa4d94f30825bec490133ff402c6350f45e26.tar.gz
zg-904fa4d94f30825bec490133ff402c6350f45e26.tar.xz
zg-904fa4d94f30825bec490133ff402c6350f45e26.zip
Teasing out canonicalization
After coping with a spuriously broken autohash for awhile, I got the one remaining hash table moved into memory, so there's no further reason to put up with allocation of basic structures. So that's nice.
Diffstat (limited to 'src/comptime_map.zig')
-rw-r--r--src/comptime_map.zig12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/comptime_map.zig b/src/comptime_map.zig
index b2c4d11..d41f6f8 100644
--- a/src/comptime_map.zig
+++ b/src/comptime_map.zig
@@ -28,7 +28,7 @@ pub fn ComptimeHashMap(comptime K: type, comptime V: type, comptime ctx: type, c
28 }; 28 };
29 29
30 // ensure that the hash map will be at most 60% full 30 // ensure that the hash map will be at most 60% full
31 const size = if (@import("builtin").is_test) values.len else math.ceilPowerOfTwo(usize, values.len * 5 / 3) catch unreachable; 31 const size = math.ceilPowerOfTwo(usize, values.len * 5 / 3) catch unreachable;
32 comptime var slots = [1]Entry{.{}} ** size; 32 comptime var slots = [1]Entry{.{}} ** size;
33 comptime var distance: [size]usize = .{0} ** size; 33 comptime var distance: [size]usize = .{0} ** size;
34 34
@@ -148,6 +148,15 @@ test "auto comptime hash map" {
148 try testing.expect(map.get(4_000_000) == null); 148 try testing.expect(map.get(4_000_000) == null);
149} 149}
150 150
151test "array pair comptime hash map" {
152 const map = AutoComptimeHashMap([2]u32, u21, .{
153 .{ .{ 2, 3 }, 5 },
154 .{ .{ 42, 56 }, 12 },
155 .{ .{ 2, 4 }, 6 },
156 });
157 try testing.expect(map.has(.{ 2, 4 }));
158}
159
151const std = @import("std"); 160const std = @import("std");
152const hash_map = std.hash_map; 161const hash_map = std.hash_map;
153const testing = std.testing; 162const testing = std.testing;
@@ -174,3 +183,4 @@ const math = std.math;
174// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 183// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
175// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 184// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
176// SOFTWARE. 185// SOFTWARE.
186