Allow 128 bit map keys

This commit is contained in:
Ginger Bill
2017-06-05 18:01:41 +01:00
parent ebe5beaafd
commit eba2c74bff
8 changed files with 108 additions and 84 deletions
+9 -9
View File
@@ -26,7 +26,7 @@
// The compiler relies upon this _exact_ order
TypeInfoEnumValue :: raw_union {
f: f64,
i: i64,
i: i128,
}
// NOTE(bill): This must match the compiler's
CallingConvention :: enum {
@@ -537,24 +537,24 @@ __slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
// Map stuff
__default_hash :: proc(data: []byte) -> u64 {
fnv64a :: proc(data: []byte) -> u64 {
h: u64 = 0xcbf29ce484222325;
__default_hash :: proc(data: []byte) -> u128 {
fnv128a :: proc(data: []byte) -> u128 {
h: u128 = 0x6c62272e07bb014262b821756295c58d;
for b in data {
h = (h ~ u64(b)) * 0x100000001b3;
h = (h ~ u128(b)) * 0x1000000000000000000013b;
}
return h;
}
return fnv64a(data);
return fnv128a(data);
}
__default_hash_string :: proc(s: string) -> u64 {
__default_hash_string :: proc(s: string) -> u128 {
return __default_hash([]byte(s));
}
__INITIAL_MAP_CAP :: 16;
__MapKey :: struct #ordered {
hash: u64,
hash: u128,
str: string,
}
@@ -699,7 +699,7 @@ __dynamic_map_hash_equal :: proc(h: __MapHeader, a, b: __MapKey) -> bool {
__dynamic_map_find :: proc(using h: __MapHeader, key: __MapKey) -> __MapFindResult {
fr := __MapFindResult{-1, -1, -1};
if len(m.hashes) > 0 {
fr.hash_index = int(key.hash % u64(len(m.hashes)));
fr.hash_index = int(key.hash % u128(len(m.hashes)));
fr.entry_index = m.hashes[fr.hash_index];
for fr.entry_index >= 0 {
entry := __dynamic_map_get_entry(h, fr.entry_index);
+60 -54
View File
@@ -13,6 +13,26 @@ StringBuffer :: union {
Dynamic{buf: [dynamic]byte},
}
FmtInfo :: struct {
minus: bool,
plus: bool,
space: bool,
zero: bool,
hash: bool,
width_set: bool,
prec_set: bool,
width: int,
prec: int,
reordered: bool,
good_arg_index: bool,
buf: ^StringBuffer,
arg: any, // Temporary
}
make_string_buffer_from_slice :: proc(b: []byte) -> StringBuffer {
return StringBuffer.Static{b};
}
@@ -21,7 +41,13 @@ make_string_dynamic_buffer :: proc() -> StringBuffer {
return StringBuffer.Dynamic{make([dynamic]byte)};
}
string_buffer_data :: proc(buf: ^StringBuffer) -> []byte {
return string_buffer_data(buf^);
match b in buf {
case StringBuffer.Static:
return b.buf[..];
case StringBuffer.Dynamic:
return b.buf[..];
}
return nil;
}
string_buffer_data :: proc(buf: StringBuffer) -> []byte {
match b in buf {
@@ -66,23 +92,15 @@ write_rune :: proc(buf: ^StringBuffer, r: rune) {
write_bytes(buf, b[0..<n]);
}
FmtInfo :: struct {
minus: bool,
plus: bool,
space: bool,
zero: bool,
hash: bool,
width_set: bool,
prec_set: bool,
width: int,
prec: int,
reordered: bool,
good_arg_index: bool,
buf: ^StringBuffer,
arg: any, // Temporary
write_int :: proc(buf: ^StringBuffer, i: i128, base: int) {
b: [129]byte;
s := strconv.append_bits(b[0..<0], u128(i), base, true, 128, strconv.digits, 0);
write_string(buf, s);
}
write_int :: proc(buf: ^StringBuffer, i: i64, base: int) {
b: [129]byte;
s := strconv.append_bits(b[0..<0], u128(i), base, true, 64, strconv.digits, 0);
write_string(buf, s);
}
@@ -114,15 +132,13 @@ fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
}
print :: proc(args: ..any) -> int {
return fprint(os.stdout, ..args);
}
println :: proc(args: ..any) -> int {
return fprintln(os.stdout, ..args);
}
printf :: proc(fmt: string, args: ..any) -> int {
return fprintf(os.stdout, fmt, ..args);
}
// print* procedures return the number of bytes written
print :: proc(args: ..any) -> int { return fprint(os.stdout, ..args); }
print_err :: proc(args: ..any) -> int { return fprint(os.stderr, ..args); }
println :: proc(args: ..any) -> int { return fprintln(os.stdout, ..args); }
println_err :: proc(args: ..any) -> int { return fprintln(os.stderr, ..args); }
printf :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
printf_err :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
// aprint* procedures return a string that was allocated with the current context
@@ -144,10 +160,7 @@ aprintf :: proc(fmt: string, args: ..any) -> string {
}
// bprint* procedures
// aprint* procedure return a string that was allocated with the current context
// bprint* procedures return a string that was allocated with the current context
// They must be freed accordingly
bprint :: proc(buf: []byte, args: ..any) -> string {
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
@@ -376,16 +389,6 @@ write_type :: proc(buf: ^StringBuffer, ti: ^TypeInfo) {
}
}
write_int :: proc(buf: ^StringBuffer, i: i128, base: int) {
b: [129]byte;
s := strconv.append_bits(b[0..<0], u128(i), base, true, 128, strconv.digits, 0);
write_string(buf, s);
}
write_int :: proc(buf: ^StringBuffer, i: i64, base: int) {
b: [129]byte;
s := strconv.append_bits(b[0..<0], u128(i), base, true, 64, strconv.digits, 0);
write_string(buf, s);
}
_parse_int :: proc(s: string, offset: int) -> (result: int, offset: int, ok: bool) {
is_digit :: proc(r: rune) -> bool #inline {
@@ -703,23 +706,26 @@ fmt_enum :: proc(fi: ^FmtInfo, v: any, verb: rune) {
case 'd', 'f':
fmt_arg(fi, any{v.data, type_info_base(e.base)}, verb);
case 's', 'v':
i: i64;
i: i128;
f: f64;
ok := false;
a := any{v.data, type_info_base(e.base)};
match v in a {
case i8: i = i64(v);
case i16: i = i64(v);
case i32: i = i64(v);
case i64: i = i64(v);
case int: i = i64(v);
case u8: i = i64(v);
case u16: i = i64(v);
case u32: i = i64(v);
case u64: i = i64(v);
case uint: i = i64(v);
case f32: f = f64(v); i = transmute(i64, f);
case f64: f = f64(v); i = transmute(i64, f);
case i8: i = i128(v);
case i16: i = i128(v);
case i32: i = i128(v);
case i64: i = i128(v);
case i128: i = i128(v);
case int: i = i128(v);
case u8: i = i128(v);
case u16: i = i128(v);
case u32: i = i128(v);
case u64: i = i128(v);
case u128: i = i128(v);
case uint: i = i128(v);
case f32: f = f64(v); i = i128(transmute(i64, f));
case f64: f = f64(v); i = i128(transmute(i64, f));
}
if types.is_string(e.base) {