|
1 | 1 | const std = @import("std.zig");
|
2 | 2 | const assert = std.debug.assert;
|
3 | 3 |
|
4 |
| -/// 'comptime' optimized mapping between string keys and associated values. |
| 4 | +/// 'comptime' optimized mapping between string keys and associated `V` values. |
5 | 5 | pub fn StaticStringMap(comptime V: type) type {
|
6 |
| - return StaticStringMapAdvanced(V, u8, defaultEql); |
| 6 | + return StaticArrayMapWithEql(V, u8, defaultEql); |
7 | 7 | }
|
8 | 8 |
|
9 | 9 | /// Same as StaticStringMap, except keys are compared case-insensitively.
|
10 | 10 | pub fn StaticStringMapIgnoreCase(comptime V: type) type {
|
11 |
| - return StaticStringMapAdvanced(V, u8, ignoreCaseEql); |
| 11 | + return StaticArrayMapWithEql(V, u8, ignoreCaseEql); |
| 12 | +} |
| 13 | + |
| 14 | +/// Same as StaticStringMap, but allows you to provide the `eql` function yourself. |
| 15 | +pub fn StaticStringMapWithEql( |
| 16 | + comptime V: type, |
| 17 | + comptime eql: fn (comptime usize, comptime anytype, anytype) bool, |
| 18 | +) type { |
| 19 | + return StaticArrayMapWithEql(V, u8, eql); |
| 20 | +} |
| 21 | + |
| 22 | +/// 'comptime' optimized mapping between `[]const T` keys and associated `V` values. |
| 23 | +pub fn StaticArrayMap(comptime T: type, comptime V: type) type { |
| 24 | + return StaticArrayMapWithEql(V, T, defaultEql); |
12 | 25 | }
|
13 | 26 |
|
14 | 27 | pub fn defaultEql(comptime len: usize, comptime expected: anytype, actual: anytype) bool {
|
@@ -59,7 +72,7 @@ fn toLowerSimd(comptime len: usize, input: [len]u8) [len]u8 {
|
59 | 72 |
|
60 | 73 | /// Static string map constructed at compile time for additional optimizations.
|
61 | 74 | /// First branches on the key length, then compares the possible matching keys.
|
62 |
| -pub fn StaticStringMapAdvanced( |
| 75 | +pub fn StaticArrayMapWithEql( |
63 | 76 | /// The type of the value
|
64 | 77 | comptime V: type,
|
65 | 78 | /// The type of the element in the array, eg. []const T - would be u8 for a string
|
@@ -414,3 +427,26 @@ test "sorting kvs doesn't exceed eval branch quota" {
|
414 | 427 | });
|
415 | 428 | try testing.expectEqual(1, TypeToByteSizeLUT.get("t1"));
|
416 | 429 | }
|
| 430 | + |
| 431 | +test "static array map" { |
| 432 | + const map = StaticArrayMap(u16, u4).initComptime(.{ |
| 433 | + .{ &[_]u16{ 0, 1, 2, 3 }, 0 }, |
| 434 | + .{ &[_]u16{ 4, 5, 6, 7, 8 }, 1 }, |
| 435 | + .{ &[_]u16{ 9, 10, 1 << 13, 4 }, 2 }, |
| 436 | + .{ &[_]u16{0}, 3 }, |
| 437 | + .{ &[_]u16{}, 4 }, |
| 438 | + }); |
| 439 | + |
| 440 | + try testing.expectEqual(0, map.get(&[_]u16{ 0, 1, 2, 3 }).?); |
| 441 | + try testing.expectEqual(2, map.get(&[_]u16{ 9, 10, 1 << 13, 4 }).?); |
| 442 | + try testing.expectEqual(4, map.get(&[_]u16{}).?); |
| 443 | + |
| 444 | + try testing.expectEqual(null, map.get(&[_]u16{ 7, 7, 7 })); |
| 445 | + try testing.expectEqual(null, map.get(&[_]u16{ 0, 1 })); |
| 446 | + |
| 447 | + try testing.expectEqual(true, map.has(&[_]u16{ 4, 5, 6, 7, 8 })); |
| 448 | + try testing.expectEqual(true, map.has(&[_]u16{0})); |
| 449 | + |
| 450 | + try testing.expectEqual(false, map.has(&[_]u16{5})); |
| 451 | + try testing.expectEqual(false, map.has(&[_]u16{ 0, 0 })); |
| 452 | +} |
0 commit comments