@@ -9,86 +9,6 @@ const NeverFailingAllocator = main.heap.NeverFailingAllocator;
99pub const file_monitor = @import ("utils/file_monitor.zig" );
1010pub const VirtualList = @import ("utils/virtual_mem.zig" ).VirtualList ;
1111
12- pub const Hash = struct {
13- const Biome = main .server .terrain .biomes ;
14- const Structures = main .server .terrain .structures ;
15-
16- pub fn hashGeneric (input : anytype ) u64 {
17- const T = @TypeOf (input );
18- return switch (@typeInfo (T )) {
19- .bool = > hashCombine (hashInt (@intFromBool (input )), 0xbf58476d1ce4e5b9 ),
20- .@"enum" = > hashCombine (hashInt (@as (u64 , @intFromEnum (input ))), 0x94d049bb133111eb ),
21- .int , .float = > blk : {
22- const value = @as (std .meta .Int (.unsigned , @bitSizeOf (T )), @bitCast (input ));
23- break :blk hashInt (@as (u64 , value ));
24- },
25- .@"struct" = > blk : {
26- if (@hasDecl (T , "getHash" )) {
27- break :blk input .getHash ();
28- }
29- var result : u64 = hashGeneric (@typeName (T ));
30- inline for (@typeInfo (T ).@"struct" .fields ) | field | {
31- const keyHash = hashGeneric (@as ([]const u8 , field .name ));
32- const valueHash = hashGeneric (@field (input , field .name ));
33- const keyValueHash = hashCombine (keyHash , valueHash );
34- result = hashCombine (result , keyValueHash );
35- }
36- break :blk result ;
37- },
38- .optional = > if (input ) | _input | hashGeneric (_input ) else 0 ,
39- .pointer = > switch (@typeInfo (T ).pointer .size ) {
40- .one = > blk : {
41- if (@typeInfo (@typeInfo (T ).pointer .child ) == .@"fn" ) break :blk 0 ;
42- if (@typeInfo (T ).pointer .child == Biome ) return hashGeneric (input .id );
43- if (@typeInfo (T ).pointer .child == anyopaque ) break :blk 0 ;
44- if (@typeInfo (T ).pointer .child == Structures ) return hashGeneric (input .id );
45- break :blk hashGeneric (input .* );
46- },
47- .slice = > blk : {
48- var result : u64 = hashInt (input .len );
49- for (input ) | val | {
50- const valueHash = hashGeneric (val );
51- result = hashCombine (result , valueHash );
52- }
53- break :blk result ;
54- },
55- else = > @compileError ("Unsupported type " ++ @typeName (T )),
56- },
57- .array = > blk : {
58- var result : u64 = 0xbf58476d1ce4e5b9 ;
59- for (input ) | val | {
60- const valueHash = hashGeneric (val );
61- result = hashCombine (result , valueHash );
62- }
63- break :blk result ;
64- },
65- .vector = > blk : {
66- var result : u64 = 0x94d049bb133111eb ;
67- inline for (0.. @typeInfo (T ).vector .len ) | i | {
68- const valueHash = hashGeneric (input [i ]);
69- result = hashCombine (result , valueHash );
70- }
71- break :blk result ;
72- },
73- else = > @compileError ("Unsupported type " ++ @typeName (T )),
74- };
75- }
76-
77- // https://stackoverflow.com/questions/5889238/why-is-xor-the-default-way-to-combine-hashes
78- pub fn hashCombine (left : u64 , right : u64 ) u64 {
79- return left ^ (right +% 0x517cc1b727220a95 +% (left << 6 ) +% (left >> 2 ));
80- }
81-
82- // https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key
83- pub fn hashInt (input : u64 ) u64 {
84- var x = input ;
85- x = (x ^ (x >> 30 ))*% 0xbf58476d1ce4e5b9 ;
86- x = (x ^ (x >> 27 ))*% 0x94d049bb133111eb ;
87- x = x ^ (x >> 31 );
88- return x ;
89- }
90- };
91-
9212pub const Compression = struct { // MARK: Compression
9313 pub fn deflate (allocator : NeverFailingAllocator , data : []const u8 , level : std.compress.flate.deflate.Level ) []u8 {
9414 var result = main .List (u8 ).init (allocator );
0 commit comments