diff options
author | 2021-06-06 21:16:43 -0700 | |
---|---|---|
committer | 2021-06-06 21:16:43 -0700 | |
commit | 079fe523d4c397c5e4d877cc2faf8d29139e0a07 (patch) | |
tree | 58bea423b5ccde9bb4e6e7aa703e583f2191bd99 /src/ast | |
parent | 13653a93a2a4181fcea35576338fe24f465fc748 (diff) | |
download | bun-079fe523d4c397c5e4d877cc2faf8d29139e0a07.tar.gz bun-079fe523d4c397c5e4d877cc2faf8d29139e0a07.tar.zst bun-079fe523d4c397c5e4d877cc2faf8d29139e0a07.zip |
Upgrade hash table
Former-commit-id: 5d208f9ea0be4e5f2a682f25b0a20a623ce61091
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/base.zig | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/ast/base.zig b/src/ast/base.zig index 59d7c6c12..f1877018c 100644 --- a/src/ast/base.zig +++ b/src/ast/base.zig @@ -22,15 +22,22 @@ pub const NodeIndexNone = 4294967293; // be an array of arrays indexed first by source index, then by inner index. // The maps can be merged quickly by creating a single outer array containing // all inner arrays from all parsed files. + +pub const RefHashCtx = struct { + pub fn hash(ctx: @This(), key: Ref) u32 { + return @truncate(u32, std.hash.Wyhash.hash(0, std.mem.asBytes(&key))); + } + + pub fn eql(ctx: @This(), ref: Ref, b: Ref) bool { + return std.mem.readIntNative(u64, std.mem.asBytes(&ref)) == std.mem.readIntNative(u64, std.mem.asBytes(&b)); + } +}; + pub const Ref = packed struct { source_index: Int = std.math.maxInt(Ref.Int), inner_index: Int = 0, is_source_contents_slice: bool = false, - pub fn hash(key: Ref) u32 { - return @truncate(u32, std.hash.Wyhash.hash(0, std.mem.asBytes(&key))); - } - // 2 bits of padding for whatever is the parent pub const Int = u30; pub const None = Ref{ @@ -44,6 +51,14 @@ pub const Ref = packed struct { pub fn toInt(int: anytype) Int { return @intCast(Int, int); } + + pub fn hash(key: Ref) u32 { + return @truncate(u32, std.hash.Wyhash.hash(0, std.mem.asBytes(&key))); + } + + pub fn eql(ref: Ref, b: Ref) bool { + return std.mem.readIntNative(u64, std.mem.asBytes(&ref)) == std.mem.readIntNative(u64, std.mem.asBytes(&b)); + } pub fn isNull(self: *const Ref) bool { return self.source_index == std.math.maxInt(Ref.Int) and self.inner_index == std.math.maxInt(Ref.Int); } @@ -56,10 +71,6 @@ pub const Ref = packed struct { return int == std.math.maxInt(Ref.Int); } - pub fn eql(ref: Ref, b: Ref) bool { - return std.mem.readIntNative(u64, std.mem.asBytes(&ref)) == std.mem.readIntNative(u64, std.mem.asBytes(&b)); - } - pub fn jsonStringify(self: *const Ref, options: anytype, writer: anytype) !void { return try std.json.stringify([2]u32{ self.source_index, self.inner_index }, options, writer); } |