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) {
307307const Toc = struct {
308308 nodes: []Node,
309309 toc: []u8,
310 urls: std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8),
310 urls: std.StringHashMap(Token),
311311};
312312
313313const Action = enum {
......@@ -316,7 +316,7 @@ const Action = enum {
316316};
317317
318318fn 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);
320320 errdefer urls.deinit();
321321
322322 var header_stack_size: usize = 0;
src-self-hosted/arg.zig+2-2
......@@ -5,7 +5,7 @@ const mem = std.mem;
55
66const Allocator = mem.Allocator;
77const ArrayList = std.ArrayList;
8const HashMap = std.HashMap;
8const StringHashMap = std.StringHashMap;
99
1010fn trimStart(slice: []const u8, ch: u8) []const u8 {
1111 var i: usize = 0;
......@@ -73,7 +73,7 @@ fn readFlagArguments(allocator: *Allocator, args: []const []const u8, required:
7373 }
7474}
7575
76const HashMapFlags = HashMap([]const u8, FlagArg, std.hash.Fnv1a_32.hash, mem.eql_slice_u8);
76const HashMapFlags = StringHashMap(FlagArg);
7777
7878// A store for querying found flags and positional arguments.
7979pub const Args = struct {
src-self-hosted/compilation.zig+1-1
......@@ -249,7 +249,7 @@ pub const Compilation = struct {
249249 const ArrayTypeTable = std.HashMap(*const Type.Array.Key, *Type.Array, Type.Array.Key.hash, Type.Array.Key.eql);
250250 const PtrTypeTable = std.HashMap(*const Type.Pointer.Key, *Type.Pointer, Type.Pointer.Key.hash, Type.Pointer.Key.eql);
251251 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
254254 const CompileErrList = std.ArrayList(*Msg);
255255
src-self-hosted/decl.zig+1-1
......@@ -20,7 +20,7 @@ pub const Decl = struct {
2020 // TODO when we destroy the decl, deref the tree scope
2121 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
2525 pub fn cast(base: *Decl, comptime T: type) ?*T {
2626 if (base.id != @field(Id, @typeName(T))) return null;
src-self-hosted/main.zig+1-1
......@@ -541,7 +541,7 @@ const Fmt = struct {
541541 color: errmsg.Color,
542542 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);
545545};
546546
547547fn 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 {
1010 /// relative to root_src_dir
1111 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
1515 /// makes internal copies of root_src_dir and root_src_path
1616 /// 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 {
343343 color: errmsg.Color,
344344 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);
347347};
348348
349349fn printErrMsgToFile(
......@@ -376,7 +376,7 @@ fn printErrMsgToFile(
376376 const text = text_buf.toOwnedSlice();
377377
378378 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
381381 if (!color_on) return;
382382
std/buf_map.zig+2-2
......@@ -1,5 +1,5 @@
11const std = @import("std.zig");
2const HashMap = std.HashMap;
2const StringHashMap = std.StringHashMap;
33const mem = std.mem;
44const Allocator = mem.Allocator;
55const testing = std.testing;
......@@ -9,7 +9,7 @@ const testing = std.testing;
99pub const BufMap = struct {
1010 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
1414 pub fn init(allocator: *Allocator) BufMap {
1515 var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) };
std/buf_set.zig+2-2
......@@ -1,5 +1,5 @@
11const std = @import("std.zig");
2const HashMap = @import("hash_map.zig").HashMap;
2const StringHashMap = std.StringHashMap;
33const mem = @import("mem.zig");
44const Allocator = mem.Allocator;
55const testing = std.testing;
......@@ -7,7 +7,7 @@ const testing = std.testing;
77pub const BufSet = struct {
88 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
1212 pub fn init(a: *Allocator) BufSet {
1313 var self = BufSet{ .hash_map = BufSetHashMap.init(a) };
std/build.zig+3-3
......@@ -8,7 +8,7 @@ const panic = std.debug.panic;
88const assert = debug.assert;
99const warn = std.debug.warn;
1010const ArrayList = std.ArrayList;
11const HashMap = std.HashMap;
11const StringHashMap = std.StringHashMap;
1212const Allocator = mem.Allocator;
1313const process = std.process;
1414const BufSet = std.BufSet;
......@@ -61,8 +61,8 @@ pub const Builder = struct {
6161 C11,
6262 };
6363
64 const UserInputOptionsMap = HashMap([]const u8, UserInputOption, mem.hash_slice_u8, mem.eql_slice_u8);
65 const AvailableOptionsMap = HashMap([]const u8, AvailableOption, mem.hash_slice_u8, mem.eql_slice_u8);
64 const UserInputOptionsMap = StringHashMap(UserInputOption);
65 const AvailableOptionsMap = StringHashMap(AvailableOption);
6666
6767 const AvailableOption = struct {
6868 name: []const u8,
std/hash_map.zig+1-3
......@@ -23,9 +23,7 @@ pub fn StringHashMap(comptime V: type) type {
2323}
2424
2525pub 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;
26 return mem.eql(u8, a, b);
2927}
3028
3129pub fn hashString(s: []const u8) u32 {
std/json.zig+2-2
......@@ -989,7 +989,7 @@ test "json.validate" {
989989const Allocator = std.mem.Allocator;
990990const ArenaAllocator = std.heap.ArenaAllocator;
991991const ArrayList = std.ArrayList;
992const HashMap = std.HashMap;
992const StringHashMap = std.StringHashMap;
993993
994994pub const ValueTree = struct {
995995 arena: ArenaAllocator,
......@@ -1000,7 +1000,7 @@ pub const ValueTree = struct {
10001000 }
10011001};
10021002
1003pub const ObjectMap = HashMap([]const u8, Value, mem.hash_slice_u8, mem.eql_slice_u8);
1003pub const ObjectMap = StringHashMap(Value);
10041004
10051005pub const Value = union(enum) {
10061006 Null,
std/mem.zig+13-25
......@@ -339,6 +339,7 @@ test "mem.lessThan" {
339339/// Compares two slices and returns whether they are equal.
340340pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
341341 if (a.len != b.len) return false;
342 if (a.ptr == b.ptr) return true;
342343 for (a) |item, index| {
343344 if (b[index] != item) return false;
344345 }
......@@ -738,47 +739,34 @@ test "writeIntBig and writeIntLittle" {
738739 var buf9: [9]u8 = undefined;
739740
740741 writeIntBig(u0, &buf0, 0x0);
741 testing.expect(eql_slice_u8(buf0[0..], [_]u8{}));
742 testing.expect(eql(u8, buf0[0..], [_]u8{}));
742743 writeIntLittle(u0, &buf0, 0x0);
743 testing.expect(eql_slice_u8(buf0[0..], [_]u8{}));
744 testing.expect(eql(u8, buf0[0..], [_]u8{}));
744745
745746 writeIntBig(u8, &buf1, 0x12);
746 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0x12}));
747 testing.expect(eql(u8, buf1[0..], [_]u8{0x12}));
747748 writeIntLittle(u8, &buf1, 0x34);
748 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0x34}));
749 testing.expect(eql(u8, buf1[0..], [_]u8{0x34}));
749750
750751 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 }));
752753 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
755756 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 }));
757758 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
760761 writeIntBig(i8, &buf1, -1);
761 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0xff}));
762 testing.expect(eql(u8, buf1[0..], [_]u8{0xff}));
762763 writeIntLittle(i8, &buf1, -2);
763 testing.expect(eql_slice_u8(buf1[0..], [_]u8{0xfe}));
764 testing.expect(eql(u8, buf1[0..], [_]u8{0xfe}));
764765
765766 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 }));
767768 writeIntLittle(i16, &buf2, -4);
768 testing.expect(eql_slice_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);
769 testing.expect(eql(u8, buf2[0..], [_]u8{ 0xfc, 0xff }));
782770}
783771
784772/// Returns an iterator that iterates over the slices of `buffer` that are not