Commit 4c882e731f

Sahnvour <sahnvour@pm.me>
2019-08-22 22:46:37
hash_map: adding a StringHashMap for convenience
1 parent 1498cca
std/event/fs.zig
@@ -719,6 +719,16 @@ pub const WatchEventId = enum {
     Delete,
 };
 
+fn eqlString(a: []const u16, b: []const u16) bool {
+    if (a.len != b.len) return false;
+    if (a.ptr == b.ptr) return true;
+    return mem.compare(u16, a, b) == .Equal;
+}
+
+fn hashString(s: []const u16) u32 {
+    return @truncate(u32, std.hash.Wyhash.hash(0, @sliceToBytes(s)));
+}
+
 //pub const WatchEventError = error{
 //    UserResourceLimitReached,
 //    SystemResources,
@@ -736,7 +746,7 @@ pub const WatchEventId = enum {
 //                file_table: FileTable,
 //                table_lock: event.Lock,
 //
-//                const FileTable = std.AutoHashMap([]const u8, *Put);
+//                const FileTable = std.StringHashmap(*Put);
 //                const Put = struct {
 //                    putter: anyframe,
 //                    value_ptr: *V,
@@ -755,8 +765,8 @@ pub const WatchEventId = enum {
 //            all_putters: std.atomic.Queue(anyframe),
 //            ref_count: std.atomic.Int(usize),
 //
-//            const DirTable = std.AutoHashMap([]const u8, *Dir);
-//            const FileTable = std.AutoHashMap([]const u16, V);
+//            const DirTable = std.StringHashMap(*Dir);
+//            const FileTable = std.HashMap([]const u16, V, hashString, eqlString);
 //
 //            const Dir = struct {
 //                putter: anyframe,
@@ -772,7 +782,7 @@ pub const WatchEventId = enum {
 //            table_lock: event.Lock,
 //
 //            const WdTable = std.AutoHashMap(i32, Dir);
-//            const FileTable = std.AutoHashMap([]const u8, V);
+//            const FileTable = std.StringHashMap(V);
 //
 //            const Dir = struct {
 //                dirname: []const u8,
@@ -780,7 +790,7 @@ pub const WatchEventId = enum {
 //            };
 //        };
 //
-//        const FileToHandle = std.AutoHashMap([]const u8, anyframe);
+//        const FileToHandle = std.StringHashMap(anyframe);
 //
 //        const Self = @This();
 //
std/http/headers.zig
@@ -102,19 +102,9 @@ test "HeaderEntry" {
     testing.expectEqualSlices(u8, "x", e.value);
 }
 
-fn stringEql(a: []const u8, b: []const u8) bool {
-    if (a.len != b.len) return false;
-    if (a.ptr == b.ptr) return true;
-    return mem.compare(u8, a, b) == .Equal;
-}
-
-fn stringHash(s: []const u8) u32 {
-    return @truncate(u32, std.hash.Wyhash.hash(0, s));
-}
-
 const HeaderList = std.ArrayList(HeaderEntry);
 const HeaderIndexList = std.ArrayList(usize);
-const HeaderIndex = std.HashMap([]const u8, HeaderIndexList, stringHash, stringEql);
+const HeaderIndex = std.StringHashMap(HeaderIndexList);
 
 pub const Headers = struct {
     // the owned header field name is stored in the index as part of the key
std/hash_map.zig
@@ -17,6 +17,21 @@ pub fn AutoHashMap(comptime K: type, comptime V: type) type {
     return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K));
 }
 
+/// Builtin hashmap for strings as keys.
+pub fn StringHashMap(comptime V: type) type {
+    return HashMap([]const u8, V, hashString, eqlString);
+}
+
+pub fn eqlString(a: []const u8, b: []const u8) bool {
+    if (a.len != b.len) return false;
+    if (a.ptr == b.ptr) return true;
+    return mem.compare(u8, a, b) == .Equal;
+}
+
+pub fn hashString(s: []const u8) u32 {
+    return @truncate(u32, std.hash.Wyhash.hash(0, s));
+}
+
 pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type {
     return struct {
         entries: []Entry,
std/std.zig
@@ -17,6 +17,7 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;
 pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex;
 pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
 pub const SpinLock = @import("spinlock.zig").SpinLock;
+pub const StringHashMap = @import("hash_map.zig").StringHashMap;
 pub const ChildProcess = @import("child_process.zig").ChildProcess;
 pub const TailQueue = @import("linked_list.zig").TailQueue;
 pub const Thread = @import("thread.zig").Thread;
tools/process_headers.zig
@@ -504,12 +504,9 @@ const Contents = struct {
     }
 };
 
-comptime {
-    @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.");
-}
-const HashToContents = std.AutoHashMap([]const u8, Contents);
+const HashToContents = std.StringHashMap(Contents);
 const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql);
-const PathTable = std.AutoHashMap([]const u8, *TargetToHash);
+const PathTable = std.StringHashMap(*TargetToHash);
 
 const LibCVendor = enum {
     musl,
tools/update_glibc.zig
@@ -118,7 +118,7 @@ const FunctionSet = struct {
     list: std.ArrayList(VersionedFn),
     fn_vers_list: FnVersionList,
 };
-const FnVersionList = std.AutoHashMap([]const u8, std.ArrayList(usize));
+const FnVersionList = std.StringHashMap(std.ArrayList(usize));
 
 const VersionedFn = struct {
     ver: []const u8, // example: "GLIBC_2.15"
@@ -140,8 +140,8 @@ pub fn main() !void {
     const prefix = try fs.path.join(allocator, [_][]const u8{ in_glibc_dir, "sysdeps", "unix", "sysv", "linux" });
     const glibc_out_dir = try fs.path.join(allocator, [_][]const u8{ zig_src_dir, "libc", "glibc" });
 
-    var global_fn_set = std.AutoHashMap([]const u8, Function).init(allocator);
-    var global_ver_set = std.AutoHashMap([]const u8, usize).init(allocator);
+    var global_fn_set = std.StringHashMap(Function).init(allocator);
+    var global_ver_set = std.StringHashMap(usize).init(allocator);
     var target_functions = std.AutoHashMap(usize, FunctionSet).init(allocator);
 
     for (abi_lists) |*abi_list| {