| ... | ... | @@ -0,0 +1,134 @@ |
| 1 | const std = @import("std.zig"); |
| 2 | const mem = std.mem; |
| 3 | |
| 4 | /// Like ComptimeStringHashMap but optimized for small sets of disparate string keys. |
| 5 | /// Works by separating the keys by length at comptime and only checking strings of |
| 6 | /// equal length at runtime. |
| 7 | /// |
| 8 | /// `kvs` expects a list literal containing list literals or an array/slice of structs |
| 9 | /// where `.@"0"` is the `[]const u8` key and `.@"1"` is the associated value of type `V`. |
| 10 | /// TODO: https://github.com/ziglang/zig/issues/4335 |
| 11 | pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type { |
| 12 | const precomputed = comptime blk: { |
| 13 | @setEvalBranchQuota(2000); |
| 14 | const KV = struct { |
| 15 | key: []const u8, |
| 16 | value: V, |
| 17 | }; |
| 18 | var sorted_kvs: [kvs.len]KV = undefined; |
| 19 | const lenAsc = (struct { |
| 20 | fn lenAsc(a: KV, b: KV) bool { |
| 21 | return a.key.len < b.key.len; |
| 22 | } |
| 23 | }).lenAsc; |
| 24 | for (kvs) |kv, i| { |
| 25 | sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"}; |
| 26 | } |
| 27 | std.sort.sort(KV, &sorted_kvs, lenAsc); |
| 28 | const min_len = sorted_kvs[0].key.len; |
| 29 | const max_len = sorted_kvs[sorted_kvs.len - 1].key.len; |
| 30 | var len_indexes: [max_len + 1]usize = undefined; |
| 31 | var len: usize = 0; |
| 32 | var i: usize = 0; |
| 33 | while (len <= max_len) : (len += 1) { |
| 34 | // find the first keyword len == len |
| 35 | while (len > sorted_kvs[i].key.len) { |
| 36 | i += 1; |
| 37 | } |
| 38 | len_indexes[len] = i; |
| 39 | } |
| 40 | break :blk .{ |
| 41 | .min_len = min_len, |
| 42 | .max_len = max_len, |
| 43 | .sorted_kvs = sorted_kvs, |
| 44 | .len_indexes = len_indexes, |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | return struct { |
| 49 | pub fn has(str: []const u8) bool { |
| 50 | return get(str) != null; |
| 51 | } |
| 52 | |
| 53 | pub fn get(str: []const u8) ?V { |
| 54 | if (str.len < precomputed.min_len or str.len > precomputed.max_len) |
| 55 | return null; |
| 56 | |
| 57 | var i = precomputed.len_indexes[str.len]; |
| 58 | while (true) { |
| 59 | const kv = precomputed.sorted_kvs[i]; |
| 60 | if (kv.key.len != str.len) |
| 61 | return null; |
| 62 | if (mem.eql(u8, kv.key, str)) |
| 63 | return kv.value; |
| 64 | i += 1; |
| 65 | if (i >= precomputed.sorted_kvs.len) |
| 66 | return null; |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | const TestEnum = enum { |
| 73 | A, |
| 74 | B, |
| 75 | C, |
| 76 | D, |
| 77 | E, |
| 78 | }; |
| 79 | |
| 80 | test "ComptimeStringMap list literal of list literals" { |
| 81 | const map = ComptimeStringMap(TestEnum, .{ |
| 82 | .{"these", .D}, |
| 83 | .{"have", .A}, |
| 84 | .{"nothing", .B}, |
| 85 | .{"incommon", .C}, |
| 86 | .{"samelen", .E}, |
| 87 | }); |
| 88 | |
| 89 | testMap(map); |
| 90 | } |
| 91 | |
| 92 | test "ComptimeStringMap array of structs" { |
| 93 | const KV = struct { |
| 94 | @"0": []const u8, |
| 95 | @"1": TestEnum, |
| 96 | }; |
| 97 | const map = ComptimeStringMap(TestEnum, [_]KV{ |
| 98 | .{.@"0" = "these", .@"1" = .D}, |
| 99 | .{.@"0" = "have", .@"1" = .A}, |
| 100 | .{.@"0" = "nothing", .@"1" = .B}, |
| 101 | .{.@"0" = "incommon", .@"1" = .C}, |
| 102 | .{.@"0" = "samelen", .@"1" = .E}, |
| 103 | }); |
| 104 | |
| 105 | testMap(map); |
| 106 | } |
| 107 | |
| 108 | test "ComptimeStringMap slice of structs" { |
| 109 | const KV = struct { |
| 110 | @"0": []const u8, |
| 111 | @"1": TestEnum, |
| 112 | }; |
| 113 | const slice: []const KV = &[_]KV{ |
| 114 | .{.@"0" = "these", .@"1" = .D}, |
| 115 | .{.@"0" = "have", .@"1" = .A}, |
| 116 | .{.@"0" = "nothing", .@"1" = .B}, |
| 117 | .{.@"0" = "incommon", .@"1" = .C}, |
| 118 | .{.@"0" = "samelen", .@"1" = .E}, |
| 119 | }; |
| 120 | const map = ComptimeStringMap(TestEnum, slice); |
| 121 | |
| 122 | testMap(map); |
| 123 | } |
| 124 | |
| 125 | fn testMap(comptime map: var) void { |
| 126 | std.testing.expectEqual(TestEnum.A, map.get("have").?); |
| 127 | std.testing.expectEqual(TestEnum.B, map.get("nothing").?); |
| 128 | std.testing.expect(null == map.get("missing")); |
| 129 | std.testing.expectEqual(TestEnum.D, map.get("these").?); |
| 130 | std.testing.expectEqual(TestEnum.E, map.get("samelen").?); |
| 131 | |
| 132 | std.testing.expect(!map.has("missing")); |
| 133 | std.testing.expect(map.has("these")); |
| 134 | } |