authorgravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-08-22 22:46:37+02:00
committergravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-08-24 15:30:23+02:00
log4c882e731f26aa595a2b6c17725f01bbc1974e37
tree23f5652288c4a7fc38a43de3042f217b5ab1f3fa
parent1498ccac2a7c4370ae09cd69a0f2ade7758fcd64

hash_map: adding a StringHashMap for convenience


6 files changed, 37 insertions(+), 24 deletions(-)

std/event/fs.zig+15-5
......@@ -719,6 +719,16 @@ pub const WatchEventId = enum {
719719 Delete,
720720};
721721
722fn eqlString(a: []const u16, b: []const u16) bool {
723 if (a.len != b.len) return false;
724 if (a.ptr == b.ptr) return true;
725 return mem.compare(u16, a, b) == .Equal;
726}
727
728fn hashString(s: []const u16) u32 {
729 return @truncate(u32, std.hash.Wyhash.hash(0, @sliceToBytes(s)));
730}
731
722732//pub const WatchEventError = error{
723733// UserResourceLimitReached,
724734// SystemResources,
......@@ -736,7 +746,7 @@ pub const WatchEventId = enum {
736746// file_table: FileTable,
737747// table_lock: event.Lock,
738748//
739// const FileTable = std.AutoHashMap([]const u8, *Put);
749// const FileTable = std.StringHashmap(*Put);
740750// const Put = struct {
741751// putter: anyframe,
742752// value_ptr: *V,
......@@ -755,8 +765,8 @@ pub const WatchEventId = enum {
755765// all_putters: std.atomic.Queue(anyframe),
756766// ref_count: std.atomic.Int(usize),
757767//
758// const DirTable = std.AutoHashMap([]const u8, *Dir);
759// const FileTable = std.AutoHashMap([]const u16, V);
768// const DirTable = std.StringHashMap(*Dir);
769// const FileTable = std.HashMap([]const u16, V, hashString, eqlString);
760770//
761771// const Dir = struct {
762772// putter: anyframe,
......@@ -772,7 +782,7 @@ pub const WatchEventId = enum {
772782// table_lock: event.Lock,
773783//
774784// const WdTable = std.AutoHashMap(i32, Dir);
775// const FileTable = std.AutoHashMap([]const u8, V);
785// const FileTable = std.StringHashMap(V);
776786//
777787// const Dir = struct {
778788// dirname: []const u8,
......@@ -780,7 +790,7 @@ pub const WatchEventId = enum {
780790// };
781791// };
782792//
783// const FileToHandle = std.AutoHashMap([]const u8, anyframe);
793// const FileToHandle = std.StringHashMap(anyframe);
784794//
785795// const Self = @This();
786796//
std/hash_map.zig+15
......@@ -17,6 +17,21 @@ pub fn AutoHashMap(comptime K: type, comptime V: type) type {
1717 return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K));
1818}
1919
20/// Builtin hashmap for strings as keys.
21pub fn StringHashMap(comptime V: type) type {
22 return HashMap([]const u8, V, hashString, eqlString);
23}
24
25pub fn eqlString(a: []const u8, b: []const u8) bool {
26 if (a.len != b.len) return false;
27 if (a.ptr == b.ptr) return true;
28 return mem.compare(u8, a, b) == .Equal;
29}
30
31pub fn hashString(s: []const u8) u32 {
32 return @truncate(u32, std.hash.Wyhash.hash(0, s));
33}
34
2035pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type {
2136 return struct {
2237 entries: []Entry,
std/http/headers.zig+1-11
......@@ -102,19 +102,9 @@ test "HeaderEntry" {
102102 testing.expectEqualSlices(u8, "x", e.value);
103103}
104104
105fn stringEql(a: []const u8, b: []const u8) bool {
106 if (a.len != b.len) return false;
107 if (a.ptr == b.ptr) return true;
108 return mem.compare(u8, a, b) == .Equal;
109}
110
111fn stringHash(s: []const u8) u32 {
112 return @truncate(u32, std.hash.Wyhash.hash(0, s));
113}
114
115105const HeaderList = std.ArrayList(HeaderEntry);
116106const HeaderIndexList = std.ArrayList(usize);
117const HeaderIndex = std.HashMap([]const u8, HeaderIndexList, stringHash, stringEql);
107const HeaderIndex = std.StringHashMap(HeaderIndexList);
118108
119109pub const Headers = struct {
120110 // the owned header field name is stored in the index as part of the key
std/std.zig+1
......@@ -17,6 +17,7 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;
1717pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex;
1818pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
1919pub const SpinLock = @import("spinlock.zig").SpinLock;
20pub const StringHashMap = @import("hash_map.zig").StringHashMap;
2021pub const ChildProcess = @import("child_process.zig").ChildProcess;
2122pub const TailQueue = @import("linked_list.zig").TailQueue;
2223pub const Thread = @import("thread.zig").Thread;
tools/process_headers.zig+2-5
......@@ -504,12 +504,9 @@ const Contents = struct {
504504 }
505505};
506506
507comptime {
508 @compileError("the behavior of std.AutoHashMap changed and []const u8 will be treated as a pointer. will need to update the hash maps to actually do some kind of hashing on the slices.");
509}
510const HashToContents = std.AutoHashMap([]const u8, Contents);
507const HashToContents = std.StringHashMap(Contents);
511508const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql);
512const PathTable = std.AutoHashMap([]const u8, *TargetToHash);
509const PathTable = std.StringHashMap(*TargetToHash);
513510
514511const LibCVendor = enum {
515512 musl,
tools/update_glibc.zig+3-3
......@@ -118,7 +118,7 @@ const FunctionSet = struct {
118118 list: std.ArrayList(VersionedFn),
119119 fn_vers_list: FnVersionList,
120120};
121const FnVersionList = std.AutoHashMap([]const u8, std.ArrayList(usize));
121const FnVersionList = std.StringHashMap(std.ArrayList(usize));
122122
123123const VersionedFn = struct {
124124 ver: []const u8, // example: "GLIBC_2.15"
......@@ -140,8 +140,8 @@ pub fn main() !void {
140140 const prefix = try fs.path.join(allocator, [_][]const u8{ in_glibc_dir, "sysdeps", "unix", "sysv", "linux" });
141141 const glibc_out_dir = try fs.path.join(allocator, [_][]const u8{ zig_src_dir, "libc", "glibc" });
142142
143 var global_fn_set = std.AutoHashMap([]const u8, Function).init(allocator);
144 var global_ver_set = std.AutoHashMap([]const u8, usize).init(allocator);
143 var global_fn_set = std.StringHashMap(Function).init(allocator);
144 var global_ver_set = std.StringHashMap(usize).init(allocator);
145145 var target_functions = std.AutoHashMap(usize, FunctionSet).init(allocator);
146146
147147 for (abi_lists) |*abi_list| {