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 {...@@ -719,6 +719,16 @@ pub const WatchEventId = enum {
719 Delete,719 Delete,
720};720};
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
722//pub const WatchEventError = error{732//pub const WatchEventError = error{
723// UserResourceLimitReached,733// UserResourceLimitReached,
724// SystemResources,734// SystemResources,
...@@ -736,7 +746,7 @@ pub const WatchEventId = enum {...@@ -736,7 +746,7 @@ pub const WatchEventId = enum {
736// file_table: FileTable,746// file_table: FileTable,
737// table_lock: event.Lock,747// table_lock: event.Lock,
738//748//
739// const FileTable = std.AutoHashMap([]const u8, *Put);749// const FileTable = std.StringHashmap(*Put);
740// const Put = struct {750// const Put = struct {
741// putter: anyframe,751// putter: anyframe,
742// value_ptr: *V,752// value_ptr: *V,
...@@ -755,8 +765,8 @@ pub const WatchEventId = enum {...@@ -755,8 +765,8 @@ pub const WatchEventId = enum {
755// all_putters: std.atomic.Queue(anyframe),765// all_putters: std.atomic.Queue(anyframe),
756// ref_count: std.atomic.Int(usize),766// ref_count: std.atomic.Int(usize),
757//767//
758// const DirTable = std.AutoHashMap([]const u8, *Dir);768// const DirTable = std.StringHashMap(*Dir);
759// const FileTable = std.AutoHashMap([]const u16, V);769// const FileTable = std.HashMap([]const u16, V, hashString, eqlString);
760//770//
761// const Dir = struct {771// const Dir = struct {
762// putter: anyframe,772// putter: anyframe,
...@@ -772,7 +782,7 @@ pub const WatchEventId = enum {...@@ -772,7 +782,7 @@ pub const WatchEventId = enum {
772// table_lock: event.Lock,782// table_lock: event.Lock,
773//783//
774// const WdTable = std.AutoHashMap(i32, Dir);784// const WdTable = std.AutoHashMap(i32, Dir);
775// const FileTable = std.AutoHashMap([]const u8, V);785// const FileTable = std.StringHashMap(V);
776//786//
777// const Dir = struct {787// const Dir = struct {
778// dirname: []const u8,788// dirname: []const u8,
...@@ -780,7 +790,7 @@ pub const WatchEventId = enum {...@@ -780,7 +790,7 @@ pub const WatchEventId = enum {
780// };790// };
781// };791// };
782//792//
783// const FileToHandle = std.AutoHashMap([]const u8, anyframe);793// const FileToHandle = std.StringHashMap(anyframe);
784//794//
785// const Self = @This();795// const Self = @This();
786//796//
std/hash_map.zig+15
...@@ -17,6 +17,21 @@ pub fn AutoHashMap(comptime K: type, comptime V: type) type {...@@ -17,6 +17,21 @@ pub fn AutoHashMap(comptime K: type, comptime V: type) type {
17 return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K));17 return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K));
18}18}
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
20pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type {35pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type {
21 return struct {36 return struct {
22 entries: []Entry,37 entries: []Entry,
std/http/headers.zig+1-11
...@@ -102,19 +102,9 @@ test "HeaderEntry" {...@@ -102,19 +102,9 @@ test "HeaderEntry" {
102 testing.expectEqualSlices(u8, "x", e.value);102 testing.expectEqualSlices(u8, "x", e.value);
103}103}
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
115const HeaderList = std.ArrayList(HeaderEntry);105const HeaderList = std.ArrayList(HeaderEntry);
116const HeaderIndexList = std.ArrayList(usize);106const HeaderIndexList = std.ArrayList(usize);
117const HeaderIndex = std.HashMap([]const u8, HeaderIndexList, stringHash, stringEql);107const HeaderIndex = std.StringHashMap(HeaderIndexList);
118108
119pub const Headers = struct {109pub const Headers = struct {
120 // the owned header field name is stored in the index as part of the key110 // 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;...@@ -17,6 +17,7 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;
17pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex;17pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex;
18pub const SegmentedList = @import("segmented_list.zig").SegmentedList;18pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
19pub const SpinLock = @import("spinlock.zig").SpinLock;19pub const SpinLock = @import("spinlock.zig").SpinLock;
20pub const StringHashMap = @import("hash_map.zig").StringHashMap;
20pub const ChildProcess = @import("child_process.zig").ChildProcess;21pub const ChildProcess = @import("child_process.zig").ChildProcess;
21pub const TailQueue = @import("linked_list.zig").TailQueue;22pub const TailQueue = @import("linked_list.zig").TailQueue;
22pub const Thread = @import("thread.zig").Thread;23pub const Thread = @import("thread.zig").Thread;
tools/process_headers.zig+2-5
...@@ -504,12 +504,9 @@ const Contents = struct {...@@ -504,12 +504,9 @@ const Contents = struct {
504 }504 }
505};505};
506506
507comptime {507const HashToContents = std.StringHashMap(Contents);
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);
511const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql);508const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql);
512const PathTable = std.AutoHashMap([]const u8, *TargetToHash);509const PathTable = std.StringHashMap(*TargetToHash);
513510
514const LibCVendor = enum {511const LibCVendor = enum {
515 musl,512 musl,
tools/update_glibc.zig+3-3
...@@ -118,7 +118,7 @@ const FunctionSet = struct {...@@ -118,7 +118,7 @@ const FunctionSet = struct {
118 list: std.ArrayList(VersionedFn),118 list: std.ArrayList(VersionedFn),
119 fn_vers_list: FnVersionList,119 fn_vers_list: FnVersionList,
120};120};
121const FnVersionList = std.AutoHashMap([]const u8, std.ArrayList(usize));121const FnVersionList = std.StringHashMap(std.ArrayList(usize));
122122
123const VersionedFn = struct {123const VersionedFn = struct {
124 ver: []const u8, // example: "GLIBC_2.15"124 ver: []const u8, // example: "GLIBC_2.15"
...@@ -140,8 +140,8 @@ pub fn main() !void {...@@ -140,8 +140,8 @@ pub fn main() !void {
140 const prefix = try fs.path.join(allocator, [_][]const u8{ in_glibc_dir, "sysdeps", "unix", "sysv", "linux" });140 const prefix = try fs.path.join(allocator, [_][]const u8{ in_glibc_dir, "sysdeps", "unix", "sysv", "linux" });
141 const glibc_out_dir = try fs.path.join(allocator, [_][]const u8{ zig_src_dir, "libc", "glibc" });141 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);143 var global_fn_set = std.StringHashMap(Function).init(allocator);
144 var global_ver_set = std.AutoHashMap([]const u8, usize).init(allocator);144 var global_ver_set = std.StringHashMap(usize).init(allocator);
145 var target_functions = std.AutoHashMap(usize, FunctionSet).init(allocator);145 var target_functions = std.AutoHashMap(usize, FunctionSet).init(allocator);
146146
147 for (abi_lists) |*abi_list| {147 for (abi_lists) |*abi_list| {