Skip to content

Commit d93419d

Browse files
committed
Tweak the interface to better reflect the underlying types it provides, and provide a test for StaticArrayMap.
1 parent b31c185 commit d93419d

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ set(ZIG_STAGE2_SOURCES
484484
lib/std/process/Child.zig
485485
lib/std/sort.zig
486486
lib/std/start.zig
487-
lib/std/static_string_map.zig
487+
lib/std/static_array_map.zig
488488
lib/std/std.zig
489489
lib/std/time.zig
490490
lib/std/treap.zig

lib/std/static_string_map.zig renamed to lib/std/static_array_map.zig

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
const std = @import("std.zig");
22
const assert = std.debug.assert;
33

4-
/// 'comptime' optimized mapping between string keys and associated values.
4+
/// 'comptime' optimized mapping between string keys and associated `V` values.
55
pub fn StaticStringMap(comptime V: type) type {
6-
return StaticStringMapAdvanced(V, u8, defaultEql);
6+
return StaticArrayMapWithEql(V, u8, defaultEql);
77
}
88

99
/// Same as StaticStringMap, except keys are compared case-insensitively.
1010
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);
1225
}
1326

1427
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 {
5972

6073
/// Static string map constructed at compile time for additional optimizations.
6174
/// First branches on the key length, then compares the possible matching keys.
62-
pub fn StaticStringMapAdvanced(
75+
pub fn StaticArrayMapWithEql(
6376
/// The type of the value
6477
comptime V: type,
6578
/// 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" {
414427
});
415428
try testing.expectEqual(1, TypeToByteSizeLUT.get("t1"));
416429
}
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+
}

lib/std/std.zig

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ pub const BoundedArrayAligned = @import("bounded_array.zig").BoundedArrayAligned
1414
pub const Build = @import("Build.zig");
1515
pub const BufMap = @import("buf_map.zig").BufMap;
1616
pub const BufSet = @import("buf_set.zig").BufSet;
17-
pub const StaticStringMap = static_string_map.StaticStringMap;
18-
pub const StaticStringMapIgnoreCase = static_string_map.StaticStringMapIgnoreCase;
19-
pub const StaticStringMapAdvanced = static_string_map.StaticStringMapAdvanced;
17+
pub const StaticArrayMap = static_array_map.StaticArrayMap;
18+
pub const StaticArrayMapWithEql = static_array_map.StaticArrayMapWithEql;
19+
pub const StaticStringMap = static_array_map.StaticStringMap;
20+
pub const StaticStringMapIgnoreCase = static_array_map.StaticStringMapIgnoreCase;
21+
pub const StaticStringMapWithEql = static_array_map.StaticStringMapWithEql;
2022
pub const DoublyLinkedList = @import("linked_list.zig").DoublyLinkedList;
2123
pub const DynLib = @import("dynamic_library.zig").DynLib;
2224
pub const DynamicBitSet = bit_set.DynamicBitSet;
@@ -58,7 +60,7 @@ pub const builtin = @import("builtin.zig");
5860
pub const c = @import("c.zig");
5961
pub const coff = @import("coff.zig");
6062
pub const compress = @import("compress.zig");
61-
pub const static_string_map = @import("static_string_map.zig");
63+
pub const static_array_map = @import("static_array_map.zig");
6264
pub const crypto = @import("crypto.zig");
6365
pub const debug = @import("debug.zig");
6466
pub const dwarf = @import("dwarf.zig");

0 commit comments

Comments
 (0)