authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-03 20:30:26-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-09-03 20:30:26-04:00
loga4ce10df8087e7051340e14e4acd018092f935f0
tree67a494c9ab0e80105aca3e3a222ff3adc0d48a91
parent42cc4a406bd4d037f4203fa2ebca2853db33c780
parent9d6f236728cf433b44048e21f81efdd167fe0098
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3169 from Sahnvour/string_hash_map

Using StringHashMap everywhere it's needed

13 files changed, 33 insertions(+), 47 deletions(-)

doc/docgen.zig+2-2
...@@ -307,7 +307,7 @@ const Node = union(enum) {...@@ -307,7 +307,7 @@ const Node = union(enum) {
307const Toc = struct {307const Toc = struct {
308 nodes: []Node,308 nodes: []Node,
309 toc: []u8,309 toc: []u8,
310 urls: std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8),310 urls: std.StringHashMap(Token),
311};311};
312312
313const Action = enum {313const Action = enum {
...@@ -316,7 +316,7 @@ const Action = enum {...@@ -316,7 +316,7 @@ const Action = enum {
316};316};
317317
318fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {318fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
319 var urls = std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator);319 var urls = std.StringHashMap(Token).init(allocator);
320 errdefer urls.deinit();320 errdefer urls.deinit();
321321
322 var header_stack_size: usize = 0;322 var header_stack_size: usize = 0;
src-self-hosted/arg.zig+2-2
...@@ -5,7 +5,7 @@ const mem = std.mem;...@@ -5,7 +5,7 @@ const mem = std.mem;
55
6const Allocator = mem.Allocator;6const Allocator = mem.Allocator;
7const ArrayList = std.ArrayList;7const ArrayList = std.ArrayList;
8const HashMap = std.HashMap;8const StringHashMap = std.StringHashMap;
99
10fn trimStart(slice: []const u8, ch: u8) []const u8 {10fn trimStart(slice: []const u8, ch: u8) []const u8 {
11 var i: usize = 0;11 var i: usize = 0;
...@@ -73,7 +73,7 @@ fn readFlagArguments(allocator: *Allocator, args: []const []const u8, required:...@@ -73,7 +73,7 @@ fn readFlagArguments(allocator: *Allocator, args: []const []const u8, required:
73 }73 }
74}74}
7575
76const HashMapFlags = HashMap([]const u8, FlagArg, std.hash.Fnv1a_32.hash, mem.eql_slice_u8);76const HashMapFlags = StringHashMap(FlagArg);
7777
78// A store for querying found flags and positional arguments.78// A store for querying found flags and positional arguments.
79pub const Args = struct {79pub const Args = struct {
src-self-hosted/compilation.zig+1-1
...@@ -249,7 +249,7 @@ pub const Compilation = struct {...@@ -249,7 +249,7 @@ pub const Compilation = struct {
249 const ArrayTypeTable = std.HashMap(*const Type.Array.Key, *Type.Array, Type.Array.Key.hash, Type.Array.Key.eql);249 const ArrayTypeTable = std.HashMap(*const Type.Array.Key, *Type.Array, Type.Array.Key.hash, Type.Array.Key.eql);
250 const PtrTypeTable = std.HashMap(*const Type.Pointer.Key, *Type.Pointer, Type.Pointer.Key.hash, Type.Pointer.Key.eql);250 const PtrTypeTable = std.HashMap(*const Type.Pointer.Key, *Type.Pointer, Type.Pointer.Key.hash, Type.Pointer.Key.eql);
251 const FnTypeTable = std.HashMap(*const Type.Fn.Key, *Type.Fn, Type.Fn.Key.hash, Type.Fn.Key.eql);251 const FnTypeTable = std.HashMap(*const Type.Fn.Key, *Type.Fn, Type.Fn.Key.hash, Type.Fn.Key.eql);
252 const TypeTable = std.HashMap([]const u8, *Type, mem.hash_slice_u8, mem.eql_slice_u8);252 const TypeTable = std.StringHashMap(*Type);
253253
254 const CompileErrList = std.ArrayList(*Msg);254 const CompileErrList = std.ArrayList(*Msg);
255255
src-self-hosted/decl.zig+1-1
...@@ -20,7 +20,7 @@ pub const Decl = struct {...@@ -20,7 +20,7 @@ pub const Decl = struct {
20 // TODO when we destroy the decl, deref the tree scope20 // TODO when we destroy the decl, deref the tree scope
21 tree_scope: *Scope.AstTree,21 tree_scope: *Scope.AstTree,
2222
23 pub const Table = std.HashMap([]const u8, *Decl, mem.hash_slice_u8, mem.eql_slice_u8);23 pub const Table = std.StringHashMap(*Decl);
2424
25 pub fn cast(base: *Decl, comptime T: type) ?*T {25 pub fn cast(base: *Decl, comptime T: type) ?*T {
26 if (base.id != @field(Id, @typeName(T))) return null;26 if (base.id != @field(Id, @typeName(T))) return null;
src-self-hosted/main.zig+1-1
...@@ -541,7 +541,7 @@ const Fmt = struct {...@@ -541,7 +541,7 @@ const Fmt = struct {
541 color: errmsg.Color,541 color: errmsg.Color,
542 loop: *event.Loop,542 loop: *event.Loop,
543543
544 const SeenMap = std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);544 const SeenMap = std.StringHashMap(void);
545};545};
546546
547fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_file: []const u8) void {547fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_file: []const u8) void {
src-self-hosted/package.zig+1-1
...@@ -10,7 +10,7 @@ pub const Package = struct {...@@ -10,7 +10,7 @@ pub const Package = struct {
10 /// relative to root_src_dir10 /// relative to root_src_dir
11 table: Table,11 table: Table,
1212
13 pub const Table = std.HashMap([]const u8, *Package, mem.hash_slice_u8, mem.eql_slice_u8);13 pub const Table = std.StringHashMap(*Package);
1414
15 /// makes internal copies of root_src_dir and root_src_path15 /// makes internal copies of root_src_dir and root_src_path
16 /// allocator should be an arena allocator because Package never frees anything16 /// allocator should be an arena allocator because Package never frees anything
src-self-hosted/stage1.zig+2-2
...@@ -343,7 +343,7 @@ const Fmt = struct {...@@ -343,7 +343,7 @@ const Fmt = struct {
343 color: errmsg.Color,343 color: errmsg.Color,
344 allocator: *mem.Allocator,344 allocator: *mem.Allocator,
345345
346 const SeenMap = std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);346 const SeenMap = std.StringHashMap(void);
347};347};
348348
349fn printErrMsgToFile(349fn printErrMsgToFile(
...@@ -376,7 +376,7 @@ fn printErrMsgToFile(...@@ -376,7 +376,7 @@ fn printErrMsgToFile(
376 const text = text_buf.toOwnedSlice();376 const text = text_buf.toOwnedSlice();
377377
378 const stream = &file.outStream().stream;378 const stream = &file.outStream().stream;
379 try stream.print( "{}:{}:{}: error: {}\n", path, start_loc.line + 1, start_loc.column + 1, text);379 try stream.print("{}:{}:{}: error: {}\n", path, start_loc.line + 1, start_loc.column + 1, text);
380380
381 if (!color_on) return;381 if (!color_on) return;
382382
std/buf_map.zig+2-2
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const HashMap = std.HashMap;2const StringHashMap = std.StringHashMap;
3const mem = std.mem;3const mem = std.mem;
4const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
5const testing = std.testing;5const testing = std.testing;
...@@ -9,7 +9,7 @@ const testing = std.testing;...@@ -9,7 +9,7 @@ const testing = std.testing;
9pub const BufMap = struct {9pub const BufMap = struct {
10 hash_map: BufMapHashMap,10 hash_map: BufMapHashMap,
1111
12 const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);12 const BufMapHashMap = StringHashMap([]const u8);
1313
14 pub fn init(allocator: *Allocator) BufMap {14 pub fn init(allocator: *Allocator) BufMap {
15 var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) };15 var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) };
std/buf_set.zig+2-2
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const HashMap = @import("hash_map.zig").HashMap;2const StringHashMap = std.StringHashMap;
3const mem = @import("mem.zig");3const mem = @import("mem.zig");
4const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
5const testing = std.testing;5const testing = std.testing;
...@@ -7,7 +7,7 @@ const testing = std.testing;...@@ -7,7 +7,7 @@ const testing = std.testing;
7pub const BufSet = struct {7pub const BufSet = struct {
8 hash_map: BufSetHashMap,8 hash_map: BufSetHashMap,
99
10 const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);10 const BufSetHashMap = StringHashMap(void);
1111
12 pub fn init(a: *Allocator) BufSet {12 pub fn init(a: *Allocator) BufSet {
13 var self = BufSet{ .hash_map = BufSetHashMap.init(a) };13 var self = BufSet{ .hash_map = BufSetHashMap.init(a) };
std/build.zig+3-3
...@@ -8,7 +8,7 @@ const panic = std.debug.panic;...@@ -8,7 +8,7 @@ const panic = std.debug.panic;
8const assert = debug.assert;8const assert = debug.assert;
9const warn = std.debug.warn;9const warn = std.debug.warn;
10const ArrayList = std.ArrayList;10const ArrayList = std.ArrayList;
11const HashMap = std.HashMap;11const StringHashMap = std.StringHashMap;
12const Allocator = mem.Allocator;12const Allocator = mem.Allocator;
13const process = std.process;13const process = std.process;
14const BufSet = std.BufSet;14const BufSet = std.BufSet;
...@@ -61,8 +61,8 @@ pub const Builder = struct {...@@ -61,8 +61,8 @@ pub const Builder = struct {
61 C11,61 C11,
62 };62 };
6363
64 const UserInputOptionsMap = HashMap([]const u8, UserInputOption, mem.hash_slice_u8, mem.eql_slice_u8);64 const UserInputOptionsMap = StringHashMap(UserInputOption);
65 const AvailableOptionsMap = HashMap([]const u8, AvailableOption, mem.hash_slice_u8, mem.eql_slice_u8);65 const AvailableOptionsMap = StringHashMap(AvailableOption);
6666
67 const AvailableOption = struct {67 const AvailableOption = struct {
68 name: []const u8,68 name: []const u8,
std/hash_map.zig+1-3
...@@ -23,9 +23,7 @@ pub fn StringHashMap(comptime V: type) type {...@@ -23,9 +23,7 @@ pub fn StringHashMap(comptime V: type) type {
23}23}
2424
25pub fn eqlString(a: []const u8, b: []const u8) bool {25pub fn eqlString(a: []const u8, b: []const u8) bool {
26 if (a.len != b.len) return false;26 return mem.eql(u8, a, b);
27 if (a.ptr == b.ptr) return true;
28 return mem.compare(u8, a, b) == .Equal;
29}27}
3028
31pub fn hashString(s: []const u8) u32 {29pub fn hashString(s: []const u8) u32 {
std/json.zig+2-2
...@@ -989,7 +989,7 @@ test "json.validate" {...@@ -989,7 +989,7 @@ test "json.validate" {
989const Allocator = std.mem.Allocator;989const Allocator = std.mem.Allocator;
990const ArenaAllocator = std.heap.ArenaAllocator;990const ArenaAllocator = std.heap.ArenaAllocator;
991const ArrayList = std.ArrayList;991const ArrayList = std.ArrayList;
992const HashMap = std.HashMap;992const StringHashMap = std.StringHashMap;
993993
994pub const ValueTree = struct {994pub const ValueTree = struct {
995 arena: ArenaAllocator,995 arena: ArenaAllocator,
...@@ -1000,7 +1000,7 @@ pub const ValueTree = struct {...@@ -1000,7 +1000,7 @@ pub const ValueTree = struct {
1000 }1000 }
1001};1001};
10021002
1003pub const ObjectMap = HashMap([]const u8, Value, mem.hash_slice_u8, mem.eql_slice_u8);1003pub const ObjectMap = StringHashMap(Value);
10041004
1005pub const Value = union(enum) {1005pub const Value = union(enum) {
1006 Null,1006 Null,
std/mem.zig+13-25
...@@ -339,6 +339,7 @@ test "mem.lessThan" {...@@ -339,6 +339,7 @@ test "mem.lessThan" {
339/// Compares two slices and returns whether they are equal.339/// Compares two slices and returns whether they are equal.
340pub fn eql(comptime T: type, a: []const T, b: []const T) bool {340pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
341 if (a.len != b.len) return false;341 if (a.len != b.len) return false;
342 if (a.ptr == b.ptr) return true;
342 for (a) |item, index| {343 for (a) |item, index| {
343 if (b[index] != item) return false;344 if (b[index] != item) return false;
344 }345 }
...@@ -738,47 +739,34 @@ test "writeIntBig and writeIntLittle" {...@@ -738,47 +739,34 @@ test "writeIntBig and writeIntLittle" {
738 var buf9: [9]u8 = undefined;739 var buf9: [9]u8 = undefined;
739740
740 writeIntBig(u0, &buf0, 0x0);741 writeIntBig(u0, &buf0, 0x0);
741 testing.expect(eql_slice_u8(buf0[0..], [_]u8{}));742 testing.expect(eql(u8, buf0[0..], [_]u8{}));
742 writeIntLittle(u0, &buf0, 0x0);743 writeIntLittle(u0, &buf0, 0x0);
743 testing.expect(eql_slice_u8(buf0[0..], [_]u8{}));744 testing.expect(eql(u8, buf0[0..], [_]u8{}));
744745
745 writeIntBig(u8, &buf1, 0x12);746 writeIntBig(u8, &buf1, 0x12);
746 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0x12}));747 testing.expect(eql(u8, buf1[0..], [_]u8{0x12}));
747 writeIntLittle(u8, &buf1, 0x34);748 writeIntLittle(u8, &buf1, 0x34);
748 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0x34}));749 testing.expect(eql(u8, buf1[0..], [_]u8{0x34}));
749750
750 writeIntBig(u16, &buf2, 0x1234);751 writeIntBig(u16, &buf2, 0x1234);
751 testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0x12, 0x34 }));752 testing.expect(eql(u8, buf2[0..], [_]u8{ 0x12, 0x34 }));
752 writeIntLittle(u16, &buf2, 0x5678);753 writeIntLittle(u16, &buf2, 0x5678);
753 testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0x78, 0x56 }));754 testing.expect(eql(u8, buf2[0..], [_]u8{ 0x78, 0x56 }));
754755
755 writeIntBig(u72, &buf9, 0x123456789abcdef024);756 writeIntBig(u72, &buf9, 0x123456789abcdef024);
756 testing.expect(eql_slice_u8(buf9[0..], [_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }));757 testing.expect(eql(u8, buf9[0..], [_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }));
757 writeIntLittle(u72, &buf9, 0xfedcba9876543210ec);758 writeIntLittle(u72, &buf9, 0xfedcba9876543210ec);
758 testing.expect(eql_slice_u8(buf9[0..], [_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }));759 testing.expect(eql(u8, buf9[0..], [_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }));
759760
760 writeIntBig(i8, &buf1, -1);761 writeIntBig(i8, &buf1, -1);
761 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0xff}));762 testing.expect(eql(u8, buf1[0..], [_]u8{0xff}));
762 writeIntLittle(i8, &buf1, -2);763 writeIntLittle(i8, &buf1, -2);
763 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0xfe}));764 testing.expect(eql(u8, buf1[0..], [_]u8{0xfe}));
764765
765 writeIntBig(i16, &buf2, -3);766 writeIntBig(i16, &buf2, -3);
766 testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0xff, 0xfd }));767 testing.expect(eql(u8, buf2[0..], [_]u8{ 0xff, 0xfd }));
767 writeIntLittle(i16, &buf2, -4);768 writeIntLittle(i16, &buf2, -4);
768 testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0xfc, 0xff }));769 testing.expect(eql(u8, buf2[0..], [_]u8{ 0xfc, 0xff }));
769}
770
771pub fn hash_slice_u8(k: []const u8) u32 {
772 // FNV 32-bit hash
773 var h: u32 = 2166136261;
774 for (k) |b| {
775 h = (h ^ b) *% 16777619;
776 }
777 return h;
778}
779
780pub fn eql_slice_u8(a: []const u8, b: []const u8) bool {
781 return eql(u8, a, b);
782}770}
783771
784/// Returns an iterator that iterates over the slices of `buffer` that are not772/// Returns an iterator that iterates over the slices of `buffer` that are not