authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-05-26 21:22:20-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-05-26 21:34:55-07:00
log0865e5d360500e101fbf5e23b842bb6e26435a38
treebaf11f7789cb7b5c4e8daceed2f1812c3b3bf8f9
parentba41a9d5d7b761ad8e3ad0f1b863c7f6457598a6

Add std.ComptimeStringMap


2 files changed, 135 insertions(+), 0 deletions(-)

lib/std/comptime_string_map.zig created+134
...@@ -0,0 +1,134 @@
1const std = @import("std.zig");
2const 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
11pub 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
72const TestEnum = enum {
73 A,
74 B,
75 C,
76 D,
77 E,
78};
79
80test "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
92test "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
108test "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
125fn 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}
lib/std/std.zig+1
...@@ -8,6 +8,7 @@ pub const BloomFilter = @import("bloom_filter.zig").BloomFilter;...@@ -8,6 +8,7 @@ pub const BloomFilter = @import("bloom_filter.zig").BloomFilter;
8pub const BufMap = @import("buf_map.zig").BufMap;8pub const BufMap = @import("buf_map.zig").BufMap;
9pub const BufSet = @import("buf_set.zig").BufSet;9pub const BufSet = @import("buf_set.zig").BufSet;
10pub const ChildProcess = @import("child_process.zig").ChildProcess;10pub const ChildProcess = @import("child_process.zig").ChildProcess;
11pub const ComptimeStringMap = @import("comptime_string_map.zig").ComptimeStringMap;
11pub const DynLib = @import("dynamic_library.zig").DynLib;12pub const DynLib = @import("dynamic_library.zig").DynLib;
12pub const HashMap = @import("hash_map.zig").HashMap;13pub const HashMap = @import("hash_map.zig").HashMap;
13pub const Mutex = @import("mutex.zig").Mutex;14pub const Mutex = @import("mutex.zig").Mutex;