mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-01 04:10:07 +00:00
Remove := with var and :: with const
This commit is contained in:
+146
-146
@@ -24,19 +24,19 @@
|
||||
|
||||
// IMPORTANT NOTE(bill): Do not change the order of any of this data
|
||||
// The compiler relies upon this _exact_ order
|
||||
TypeInfoEnumValue :: raw_union {
|
||||
const TypeInfoEnumValue = raw_union {
|
||||
f: f64,
|
||||
i: i128,
|
||||
}
|
||||
// NOTE(bill): This must match the compiler's
|
||||
CallingConvention :: enum {
|
||||
const CallingConvention = enum {
|
||||
Odin = 0,
|
||||
C = 1,
|
||||
Std = 2,
|
||||
Fast = 3,
|
||||
}
|
||||
|
||||
TypeInfoRecord :: struct #ordered {
|
||||
const TypeInfoRecord = struct #ordered {
|
||||
types: []^TypeInfo,
|
||||
names: []string,
|
||||
offsets: []int, // offsets may not be used in tuples
|
||||
@@ -46,7 +46,7 @@ TypeInfoRecord :: struct #ordered {
|
||||
custom_align: bool,
|
||||
}
|
||||
|
||||
TypeInfo :: union {
|
||||
const TypeInfo = union {
|
||||
size: int,
|
||||
align: int,
|
||||
|
||||
@@ -109,16 +109,16 @@ TypeInfo :: union {
|
||||
|
||||
// NOTE(bill): only the ones that are needed (not all types)
|
||||
// This will be set by the compiler
|
||||
__type_table: []TypeInfo;
|
||||
var __type_table: []TypeInfo;
|
||||
|
||||
__argv__: ^^u8;
|
||||
__argc__: i32;
|
||||
var __argv__: ^^u8;
|
||||
var __argc__: i32;
|
||||
|
||||
type_info_base :: proc(info: ^TypeInfo) -> ^TypeInfo {
|
||||
const type_info_base = proc(info: ^TypeInfo) -> ^TypeInfo {
|
||||
if info == nil {
|
||||
return nil;
|
||||
}
|
||||
base := info;
|
||||
var base = info;
|
||||
match i in base {
|
||||
case TypeInfo.Named:
|
||||
base = i.base;
|
||||
@@ -127,11 +127,11 @@ type_info_base :: proc(info: ^TypeInfo) -> ^TypeInfo {
|
||||
}
|
||||
|
||||
|
||||
type_info_base_without_enum :: proc(info: ^TypeInfo) -> ^TypeInfo {
|
||||
const type_info_base_without_enum = proc(info: ^TypeInfo) -> ^TypeInfo {
|
||||
if info == nil {
|
||||
return nil;
|
||||
}
|
||||
base := info;
|
||||
var base = info;
|
||||
match i in base {
|
||||
case TypeInfo.Named:
|
||||
base = i.base;
|
||||
@@ -143,30 +143,30 @@ type_info_base_without_enum :: proc(info: ^TypeInfo) -> ^TypeInfo {
|
||||
|
||||
|
||||
|
||||
assume :: proc(cond: bool) #foreign __llvm_core "llvm.assume";
|
||||
const assume = proc(cond: bool) #foreign __llvm_core "llvm.assume";
|
||||
|
||||
__debug_trap :: proc() #foreign __llvm_core "llvm.debugtrap";
|
||||
__trap :: proc() #foreign __llvm_core "llvm.trap";
|
||||
read_cycle_counter :: proc() -> u64 #foreign __llvm_core "llvm.readcyclecounter";
|
||||
const __debug_trap = proc() #foreign __llvm_core "llvm.debugtrap";
|
||||
const __trap = proc() #foreign __llvm_core "llvm.trap";
|
||||
const read_cycle_counter = proc() -> u64 #foreign __llvm_core "llvm.readcyclecounter";
|
||||
|
||||
|
||||
// IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
|
||||
AllocatorMode :: enum u8 {
|
||||
const AllocatorMode = enum u8 {
|
||||
Alloc,
|
||||
Free,
|
||||
FreeAll,
|
||||
Resize,
|
||||
}
|
||||
AllocatorProc :: #type proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
|
||||
Allocator :: struct #ordered {
|
||||
const AllocatorProc = type proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
|
||||
const Allocator = struct #ordered {
|
||||
procedure: AllocatorProc,
|
||||
data: rawptr,
|
||||
}
|
||||
|
||||
|
||||
Context :: struct #ordered {
|
||||
const Context = struct #ordered {
|
||||
thread_id: int,
|
||||
|
||||
allocator: Allocator,
|
||||
@@ -175,14 +175,14 @@ Context :: struct #ordered {
|
||||
user_index: int,
|
||||
}
|
||||
|
||||
#thread_local __context: Context;
|
||||
#thread_local var __context: Context;
|
||||
|
||||
|
||||
DEFAULT_ALIGNMENT :: align_of([vector 4]f32);
|
||||
const DEFAULT_ALIGNMENT = align_of([vector 4]f32);
|
||||
|
||||
|
||||
__check_context :: proc() {
|
||||
c := &__context;
|
||||
const __check_context = proc() {
|
||||
var c = &__context;
|
||||
|
||||
if c.allocator.procedure == nil {
|
||||
c.allocator = default_allocator();
|
||||
@@ -192,15 +192,15 @@ __check_context :: proc() {
|
||||
}
|
||||
}
|
||||
|
||||
alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNMENT); }
|
||||
const alloc = proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNMENT); }
|
||||
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
const alloc_align = proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
var a = context.allocator;
|
||||
return a.procedure(a.data, AllocatorMode.Alloc, size, alignment, nil, 0, 0);
|
||||
}
|
||||
|
||||
free_ptr_with_allocator :: proc(a: Allocator, ptr: rawptr) #inline {
|
||||
const free_ptr_with_allocator = proc(a: Allocator, ptr: rawptr) #inline {
|
||||
if ptr == nil {
|
||||
return;
|
||||
}
|
||||
@@ -210,28 +210,28 @@ free_ptr_with_allocator :: proc(a: Allocator, ptr: rawptr) #inline {
|
||||
a.procedure(a.data, AllocatorMode.Free, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
|
||||
free_ptr :: proc(ptr: rawptr) #inline {
|
||||
const free_ptr = proc(ptr: rawptr) #inline {
|
||||
__check_context();
|
||||
free_ptr_with_allocator(context.allocator, ptr);
|
||||
}
|
||||
|
||||
free_all :: proc() #inline {
|
||||
const free_all = proc() #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
var a = context.allocator;
|
||||
a.procedure(a.data, AllocatorMode.FreeAll, 0, 0, nil, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT); }
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
const resize = proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT); }
|
||||
const resize_align = proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
var a = context.allocator;
|
||||
return a.procedure(a.data, AllocatorMode.Resize, new_size, alignment, ptr, old_size, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
|
||||
const default_resize_align = proc(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
|
||||
if old_memory == nil {
|
||||
return alloc_align(new_size, alignment);
|
||||
}
|
||||
@@ -245,7 +245,7 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
return old_memory;
|
||||
}
|
||||
|
||||
new_memory := alloc_align(new_size, alignment);
|
||||
var new_memory = alloc_align(new_size, alignment);
|
||||
if new_memory == nil {
|
||||
return nil;
|
||||
}
|
||||
@@ -256,7 +256,7 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
}
|
||||
|
||||
|
||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
const default_allocator_proc = proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using AllocatorMode;
|
||||
@@ -273,7 +273,7 @@ default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
// NOTE(bill): Does nothing
|
||||
|
||||
case Resize:
|
||||
ptr := os.heap_resize(old_memory, size);
|
||||
var ptr = os.heap_resize(old_memory, size);
|
||||
assert(ptr != nil);
|
||||
return ptr;
|
||||
}
|
||||
@@ -281,7 +281,7 @@ default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
return nil;
|
||||
}
|
||||
|
||||
default_allocator :: proc() -> Allocator {
|
||||
const default_allocator = proc() -> Allocator {
|
||||
return Allocator{
|
||||
procedure = default_allocator_proc,
|
||||
data = nil,
|
||||
@@ -296,7 +296,7 @@ default_allocator :: proc() -> Allocator {
|
||||
|
||||
|
||||
|
||||
__string_eq :: proc(a, b: string) -> bool {
|
||||
const __string_eq = proc(a, b: string) -> bool {
|
||||
if len(a) != len(b) {
|
||||
return false;
|
||||
}
|
||||
@@ -309,34 +309,34 @@ __string_eq :: proc(a, b: string) -> bool {
|
||||
return __string_cmp(a, b) == 0;
|
||||
}
|
||||
|
||||
__string_cmp :: proc(a, b: string) -> int {
|
||||
const __string_cmp = proc(a, b: string) -> int {
|
||||
return __mem_compare(&a[0], &b[0], min(len(a), len(b)));
|
||||
}
|
||||
|
||||
__string_ne :: proc(a, b: string) -> bool #inline { return !__string_eq(a, b); }
|
||||
__string_lt :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) < 0; }
|
||||
__string_gt :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) > 0; }
|
||||
__string_le :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) <= 0; }
|
||||
__string_ge :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) >= 0; }
|
||||
const __string_ne = proc(a, b: string) -> bool #inline { return !__string_eq(a, b); }
|
||||
const __string_lt = proc(a, b: string) -> bool #inline { return __string_cmp(a, b) < 0; }
|
||||
const __string_gt = proc(a, b: string) -> bool #inline { return __string_cmp(a, b) > 0; }
|
||||
const __string_le = proc(a, b: string) -> bool #inline { return __string_cmp(a, b) <= 0; }
|
||||
const __string_ge = proc(a, b: string) -> bool #inline { return __string_cmp(a, b) >= 0; }
|
||||
|
||||
|
||||
__complex64_eq :: proc(a, b: complex64) -> bool #inline { return real(a) == real(b) && imag(a) == imag(b); }
|
||||
__complex64_ne :: proc(a, b: complex64) -> bool #inline { return real(a) != real(b) || imag(a) != imag(b); }
|
||||
const __complex64_eq = proc(a, b: complex64) -> bool #inline { return real(a) == real(b) && imag(a) == imag(b); }
|
||||
const __complex64_ne = proc(a, b: complex64) -> bool #inline { return real(a) != real(b) || imag(a) != imag(b); }
|
||||
|
||||
__complex128_eq :: proc(a, b: complex128) -> bool #inline { return real(a) == real(b) && imag(a) == imag(b); }
|
||||
__complex128_ne :: proc(a, b: complex128) -> bool #inline { return real(a) != real(b) || imag(a) != imag(b); }
|
||||
const __complex128_eq = proc(a, b: complex128) -> bool #inline { return real(a) == real(b) && imag(a) == imag(b); }
|
||||
const __complex128_ne = proc(a, b: complex128) -> bool #inline { return real(a) != real(b) || imag(a) != imag(b); }
|
||||
|
||||
__assert :: proc(file: string, line, column: int, msg: string) #inline {
|
||||
const __assert = proc(file: string, line, column: int, msg: string) #inline {
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Runtime assertion: %s\n",
|
||||
file, line, column, msg);
|
||||
__debug_trap();
|
||||
}
|
||||
__panic :: proc(file: string, line, column: int, msg: string) #inline {
|
||||
const __panic = proc(file: string, line, column: int, msg: string) #inline {
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Panic: %s\n",
|
||||
file, line, column, msg);
|
||||
__debug_trap();
|
||||
}
|
||||
__bounds_check_error :: proc(file: string, line, column: int, index, count: int) {
|
||||
const __bounds_check_error = proc(file: string, line, column: int, index, count: int) {
|
||||
if 0 <= index && index < count {
|
||||
return;
|
||||
}
|
||||
@@ -345,7 +345,7 @@ __bounds_check_error :: proc(file: string, line, column: int, index, count: int)
|
||||
__debug_trap();
|
||||
}
|
||||
|
||||
__slice_expr_error :: proc(file: string, line, column: int, low, high, max: int) {
|
||||
const __slice_expr_error = proc(file: string, line, column: int, low, high, max: int) {
|
||||
if 0 <= low && low <= high && high <= max {
|
||||
return;
|
||||
}
|
||||
@@ -354,7 +354,7 @@ __slice_expr_error :: proc(file: string, line, column: int, low, high, max: int)
|
||||
__debug_trap();
|
||||
}
|
||||
|
||||
__substring_expr_error :: proc(file: string, line, column: int, low, high: int) {
|
||||
const __substring_expr_error = proc(file: string, line, column: int, low, high: int) {
|
||||
if 0 <= low && low <= high {
|
||||
return;
|
||||
}
|
||||
@@ -362,7 +362,7 @@ __substring_expr_error :: proc(file: string, line, column: int, low, high: int)
|
||||
file, line, column, low, high);
|
||||
__debug_trap();
|
||||
}
|
||||
__type_assertion_check :: proc(ok: bool, file: string, line, column: int, from, to: ^TypeInfo) {
|
||||
const __type_assertion_check = proc(ok: bool, file: string, line, column: int, from, to: ^TypeInfo) {
|
||||
if !ok {
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Invalid type_assertion from %T to %T\n",
|
||||
file, line, column, from, to);
|
||||
@@ -370,33 +370,33 @@ __type_assertion_check :: proc(ok: bool, file: string, line, column: int, from,
|
||||
}
|
||||
}
|
||||
|
||||
__string_decode_rune :: proc(s: string) -> (rune, int) #inline {
|
||||
const __string_decode_rune = proc(s: string) -> (rune, int) #inline {
|
||||
return utf8.decode_rune(s);
|
||||
}
|
||||
|
||||
|
||||
__mem_set :: proc(data: rawptr, value: i32, len: int) -> rawptr {
|
||||
llvm_memset_64bit :: proc(dst: rawptr, val: u8, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memset.p0i8.i64";
|
||||
const __mem_set = proc(data: rawptr, value: i32, len: int) -> rawptr {
|
||||
const llvm_memset_64bit = proc(dst: rawptr, val: u8, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memset.p0i8.i64";
|
||||
llvm_memset_64bit(data, u8(value), len, 1, false);
|
||||
return data;
|
||||
}
|
||||
__mem_zero :: proc(data: rawptr, len: int) -> rawptr {
|
||||
const __mem_zero = proc(data: rawptr, len: int) -> rawptr {
|
||||
return __mem_set(data, 0, len);
|
||||
}
|
||||
__mem_copy :: proc(dst, src: rawptr, len: int) -> rawptr {
|
||||
const __mem_copy = proc(dst, src: rawptr, len: int) -> rawptr {
|
||||
// NOTE(bill): This _must_ be implemented like C's memmove
|
||||
llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memmove.p0i8.p0i8.i64";
|
||||
const llvm_memmove_64bit = proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memmove.p0i8.p0i8.i64";
|
||||
llvm_memmove_64bit(dst, src, len, 1, false);
|
||||
return dst;
|
||||
}
|
||||
__mem_copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr {
|
||||
const __mem_copy_non_overlapping = proc(dst, src: rawptr, len: int) -> rawptr {
|
||||
// NOTE(bill): This _must_ be implemented like C's memcpy
|
||||
llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memcpy.p0i8.p0i8.i64";
|
||||
const llvm_memcpy_64bit = proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memcpy.p0i8.p0i8.i64";
|
||||
llvm_memcpy_64bit(dst, src, len, 1, false);
|
||||
return dst;
|
||||
}
|
||||
|
||||
__mem_compare :: proc(a, b: ^u8, n: int) -> int {
|
||||
const __mem_compare = proc(a, b: ^u8, n: int) -> int {
|
||||
for i in 0..<n {
|
||||
match {
|
||||
case (a+i)^ < (b+i)^:
|
||||
@@ -408,22 +408,22 @@ __mem_compare :: proc(a, b: ^u8, n: int) -> int {
|
||||
return 0;
|
||||
}
|
||||
|
||||
__sqrt_f32 :: proc(x: f32) -> f32 #foreign __llvm_core "llvm.sqrt.f32";
|
||||
__sqrt_f64 :: proc(x: f64) -> f64 #foreign __llvm_core "llvm.sqrt.f64";
|
||||
__abs_complex64 :: proc(x: complex64) -> f32 #inline {
|
||||
r, i := real(x), imag(x);
|
||||
const __sqrt_f32 = proc(x: f32) -> f32 #foreign __llvm_core "llvm.sqrt.f32";
|
||||
const __sqrt_f64 = proc(x: f64) -> f64 #foreign __llvm_core "llvm.sqrt.f64";
|
||||
const __abs_complex64 = proc(x: complex64) -> f32 #inline {
|
||||
var r, i = real(x), imag(x);
|
||||
return __sqrt_f32(r*r + i*i);
|
||||
}
|
||||
__abs_complex128 :: proc(x: complex128) -> f64 #inline {
|
||||
r, i := real(x), imag(x);
|
||||
const __abs_complex128 = proc(x: complex128) -> f64 #inline {
|
||||
var r, i = real(x), imag(x);
|
||||
return __sqrt_f64(r*r + i*i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
__dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int) {
|
||||
array := ^raw.DynamicArray(array_);
|
||||
const __dynamic_array_make = proc(array_: rawptr, elem_size, elem_align: int, len, cap: int) {
|
||||
var array = ^raw.DynamicArray(array_);
|
||||
__check_context();
|
||||
array.allocator = context.allocator;
|
||||
assert(array.allocator.procedure != nil);
|
||||
@@ -434,8 +434,8 @@ __dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, ca
|
||||
}
|
||||
}
|
||||
|
||||
__dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: int) -> bool {
|
||||
array := ^raw.DynamicArray(array_);
|
||||
const __dynamic_array_reserve = proc(array_: rawptr, elem_size, elem_align: int, cap: int) -> bool {
|
||||
var array = ^raw.DynamicArray(array_);
|
||||
|
||||
if cap <= array.cap {
|
||||
return true;
|
||||
@@ -447,11 +447,11 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap:
|
||||
}
|
||||
assert(array.allocator.procedure != nil);
|
||||
|
||||
old_size := array.cap * elem_size;
|
||||
new_size := cap * elem_size;
|
||||
allocator := array.allocator;
|
||||
var old_size = array.cap * elem_size;
|
||||
var new_size = cap * elem_size;
|
||||
var allocator = array.allocator;
|
||||
|
||||
new_data := allocator.procedure(allocator.data, AllocatorMode.Resize, new_size, elem_align, array.data, old_size, 0);
|
||||
var new_data = allocator.procedure(allocator.data, AllocatorMode.Resize, new_size, elem_align, array.data, old_size, 0);
|
||||
if new_data == nil {
|
||||
return false;
|
||||
}
|
||||
@@ -461,10 +461,10 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap:
|
||||
return true;
|
||||
}
|
||||
|
||||
__dynamic_array_resize :: proc(array_: rawptr, elem_size, elem_align: int, len: int) -> bool {
|
||||
array := ^raw.DynamicArray(array_);
|
||||
const __dynamic_array_resize = proc(array_: rawptr, elem_size, elem_align: int, len: int) -> bool {
|
||||
var array = ^raw.DynamicArray(array_);
|
||||
|
||||
ok := __dynamic_array_reserve(array_, elem_size, elem_align, len);
|
||||
var ok = __dynamic_array_reserve(array_, elem_size, elem_align, len);
|
||||
if ok {
|
||||
array.len = len;
|
||||
}
|
||||
@@ -472,53 +472,53 @@ __dynamic_array_resize :: proc(array_: rawptr, elem_size, elem_align: int, len:
|
||||
}
|
||||
|
||||
|
||||
__dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int,
|
||||
const __dynamic_array_append = proc(array_: rawptr, elem_size, elem_align: int,
|
||||
items: rawptr, item_count: int) -> int {
|
||||
array := ^raw.DynamicArray(array_);
|
||||
var array = ^raw.DynamicArray(array_);
|
||||
|
||||
if item_count <= 0 || items == nil {
|
||||
return array.len;
|
||||
}
|
||||
|
||||
|
||||
ok := true;
|
||||
var ok = true;
|
||||
if array.cap <= array.len+item_count {
|
||||
cap := 2 * array.cap + max(8, item_count);
|
||||
var cap = 2 * array.cap + max(8, item_count);
|
||||
ok = __dynamic_array_reserve(array, elem_size, elem_align, cap);
|
||||
}
|
||||
if !ok {
|
||||
// TODO(bill): Better error handling for failed reservation
|
||||
return array.len;
|
||||
}
|
||||
data := ^u8(array.data);
|
||||
var data = ^u8(array.data);
|
||||
assert(data != nil);
|
||||
__mem_copy(data + (elem_size*array.len), items, elem_size * item_count);
|
||||
array.len += item_count;
|
||||
return array.len;
|
||||
}
|
||||
|
||||
__dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: int) -> int {
|
||||
array := ^raw.DynamicArray(array_);
|
||||
const __dynamic_array_append_nothing = proc(array_: rawptr, elem_size, elem_align: int) -> int {
|
||||
var array = ^raw.DynamicArray(array_);
|
||||
|
||||
ok := true;
|
||||
var ok = true;
|
||||
if array.cap <= array.len+1 {
|
||||
cap := 2 * array.cap + max(8, 1);
|
||||
var cap = 2 * array.cap + max(8, 1);
|
||||
ok = __dynamic_array_reserve(array, elem_size, elem_align, cap);
|
||||
}
|
||||
if !ok {
|
||||
// TODO(bill): Better error handling for failed reservation
|
||||
return array.len;
|
||||
}
|
||||
data := ^u8(array.data);
|
||||
var data = ^u8(array.data);
|
||||
assert(data != nil);
|
||||
__mem_zero(data + (elem_size*array.len), elem_size);
|
||||
array.len++;
|
||||
return array.len;
|
||||
}
|
||||
|
||||
__slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
|
||||
const __slice_append = proc(slice_: rawptr, elem_size, elem_align: int,
|
||||
items: rawptr, item_count: int) -> int {
|
||||
slice := ^raw.Slice(slice_);
|
||||
var slice = ^raw.Slice(slice_);
|
||||
|
||||
if item_count <= 0 || items == nil {
|
||||
return slice.len;
|
||||
@@ -526,7 +526,7 @@ __slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
|
||||
|
||||
item_count = min(slice.cap-slice.len, item_count);
|
||||
if item_count > 0 {
|
||||
data := ^u8(slice.data);
|
||||
var data = ^u8(slice.data);
|
||||
assert(data != nil);
|
||||
__mem_copy(data + (elem_size*slice.len), items, elem_size * item_count);
|
||||
slice.len += item_count;
|
||||
@@ -537,9 +537,9 @@ __slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
|
||||
|
||||
// Map stuff
|
||||
|
||||
__default_hash :: proc(data: []u8) -> u128 {
|
||||
fnv128a :: proc(data: []u8) -> u128 {
|
||||
h: u128 = 0x6c62272e07bb014262b821756295c58d;
|
||||
const __default_hash = proc(data: []u8) -> u128 {
|
||||
const fnv128a = proc(data: []u8) -> u128 {
|
||||
var h: u128 = 0x6c62272e07bb014262b821756295c58d;
|
||||
for b in data {
|
||||
h = (h ~ u128(b)) * 0x1000000000000000000013b;
|
||||
}
|
||||
@@ -547,24 +547,24 @@ __default_hash :: proc(data: []u8) -> u128 {
|
||||
}
|
||||
return fnv128a(data);
|
||||
}
|
||||
__default_hash_string :: proc(s: string) -> u128 {
|
||||
const __default_hash_string = proc(s: string) -> u128 {
|
||||
return __default_hash([]u8(s));
|
||||
}
|
||||
|
||||
__INITIAL_MAP_CAP :: 16;
|
||||
const __INITIAL_MAP_CAP = 16;
|
||||
|
||||
__MapKey :: struct #ordered {
|
||||
const __MapKey = struct #ordered {
|
||||
hash: u128,
|
||||
str: string,
|
||||
}
|
||||
|
||||
__MapFindResult :: struct #ordered {
|
||||
const __MapFindResult = struct #ordered {
|
||||
hash_index: int,
|
||||
entry_prev: int,
|
||||
entry_index: int,
|
||||
}
|
||||
|
||||
__MapEntryHeader :: struct #ordered {
|
||||
const __MapEntryHeader = struct #ordered {
|
||||
key: __MapKey,
|
||||
next: int,
|
||||
/*
|
||||
@@ -572,7 +572,7 @@ __MapEntryHeader :: struct #ordered {
|
||||
*/
|
||||
}
|
||||
|
||||
__MapHeader :: struct #ordered {
|
||||
const __MapHeader = struct #ordered {
|
||||
m: ^raw.DynamicMap,
|
||||
is_key_string: bool,
|
||||
entry_size: int,
|
||||
@@ -581,18 +581,18 @@ __MapHeader :: struct #ordered {
|
||||
value_size: int,
|
||||
}
|
||||
|
||||
__dynamic_map_reserve :: proc(using header: __MapHeader, cap: int) {
|
||||
const __dynamic_map_reserve = proc(using header: __MapHeader, cap: int) {
|
||||
__dynamic_array_reserve(&m.hashes, size_of(int), align_of(int), cap);
|
||||
__dynamic_array_reserve(&m.entries, entry_size, entry_align, cap);
|
||||
}
|
||||
|
||||
__dynamic_map_rehash :: proc(using header: __MapHeader, new_count: int) {
|
||||
new_header: __MapHeader = header;
|
||||
nm: raw.DynamicMap;
|
||||
const __dynamic_map_rehash = proc(using header: __MapHeader, new_count: int) {
|
||||
var new_header: __MapHeader = header;
|
||||
var nm: raw.DynamicMap;
|
||||
new_header.m = &nm;
|
||||
|
||||
header_hashes := ^raw.DynamicArray(&header.m.hashes);
|
||||
nm_hashes := ^raw.DynamicArray(&nm.hashes);
|
||||
var header_hashes = ^raw.DynamicArray(&header.m.hashes);
|
||||
var nm_hashes = ^raw.DynamicArray(&nm.hashes);
|
||||
|
||||
__dynamic_array_resize(nm_hashes, size_of(int), align_of(int), new_count);
|
||||
__dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len);
|
||||
@@ -600,26 +600,26 @@ __dynamic_map_rehash :: proc(using header: __MapHeader, new_count: int) {
|
||||
nm.hashes[i] = -1;
|
||||
}
|
||||
|
||||
for i := 0; i < m.entries.len; i++ {
|
||||
for var i = 0; i < m.entries.len; i++ {
|
||||
if len(nm.hashes) == 0 {
|
||||
__dynamic_map_grow(new_header);
|
||||
}
|
||||
|
||||
entry_header := __dynamic_map_get_entry(header, i);
|
||||
data := ^u8(entry_header);
|
||||
var entry_header = __dynamic_map_get_entry(header, i);
|
||||
var data = ^u8(entry_header);
|
||||
|
||||
fr := __dynamic_map_find(new_header, entry_header.key);
|
||||
j := __dynamic_map_add_entry(new_header, entry_header.key);
|
||||
var fr = __dynamic_map_find(new_header, entry_header.key);
|
||||
var j = __dynamic_map_add_entry(new_header, entry_header.key);
|
||||
if fr.entry_prev < 0 {
|
||||
nm.hashes[fr.hash_index] = j;
|
||||
} else {
|
||||
e := __dynamic_map_get_entry(new_header, fr.entry_prev);
|
||||
var e = __dynamic_map_get_entry(new_header, fr.entry_prev);
|
||||
e.next = j;
|
||||
}
|
||||
|
||||
e := __dynamic_map_get_entry(new_header, j);
|
||||
var e = __dynamic_map_get_entry(new_header, j);
|
||||
e.next = fr.entry_index;
|
||||
ndata := ^u8(e);
|
||||
var ndata = ^u8(e);
|
||||
__mem_copy(ndata+value_offset, data+value_offset, value_size);
|
||||
|
||||
if __dynamic_map_full(new_header) {
|
||||
@@ -631,18 +631,18 @@ __dynamic_map_rehash :: proc(using header: __MapHeader, new_count: int) {
|
||||
header.m^ = nm;
|
||||
}
|
||||
|
||||
__dynamic_map_get :: proc(h: __MapHeader, key: __MapKey) -> rawptr {
|
||||
index := __dynamic_map_find(h, key).entry_index;
|
||||
const __dynamic_map_get = proc(h: __MapHeader, key: __MapKey) -> rawptr {
|
||||
var index = __dynamic_map_find(h, key).entry_index;
|
||||
if index >= 0 {
|
||||
data := ^u8(__dynamic_map_get_entry(h, index));
|
||||
val := data + h.value_offset;
|
||||
var data = ^u8(__dynamic_map_get_entry(h, index));
|
||||
var val = data + h.value_offset;
|
||||
return val;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
__dynamic_map_set :: proc(using h: __MapHeader, key: __MapKey, value: rawptr) {
|
||||
index: int;
|
||||
const __dynamic_map_set = proc(using h: __MapHeader, key: __MapKey, value: rawptr) {
|
||||
var index: int;
|
||||
assert(value != nil);
|
||||
|
||||
|
||||
@@ -651,22 +651,22 @@ __dynamic_map_set :: proc(using h: __MapHeader, key: __MapKey, value: rawptr) {
|
||||
__dynamic_map_grow(h);
|
||||
}
|
||||
|
||||
fr := __dynamic_map_find(h, key);
|
||||
var fr = __dynamic_map_find(h, key);
|
||||
if fr.entry_index >= 0 {
|
||||
index = fr.entry_index;
|
||||
} else {
|
||||
index = __dynamic_map_add_entry(h, key);
|
||||
if fr.entry_prev >= 0 {
|
||||
entry := __dynamic_map_get_entry(h, fr.entry_prev);
|
||||
var entry = __dynamic_map_get_entry(h, fr.entry_prev);
|
||||
entry.next = index;
|
||||
} else {
|
||||
m.hashes[fr.hash_index] = index;
|
||||
}
|
||||
}
|
||||
{
|
||||
e := __dynamic_map_get_entry(h, index);
|
||||
var e = __dynamic_map_get_entry(h, index);
|
||||
e.key = key;
|
||||
val := ^u8(e) + value_offset;
|
||||
var val = ^u8(e) + value_offset;
|
||||
__mem_copy(val, value, value_size);
|
||||
}
|
||||
|
||||
@@ -676,17 +676,17 @@ __dynamic_map_set :: proc(using h: __MapHeader, key: __MapKey, value: rawptr) {
|
||||
}
|
||||
|
||||
|
||||
__dynamic_map_grow :: proc(using h: __MapHeader) {
|
||||
new_count := max(2*m.entries.cap + 8, __INITIAL_MAP_CAP);
|
||||
const __dynamic_map_grow = proc(using h: __MapHeader) {
|
||||
var new_count = max(2*m.entries.cap + 8, __INITIAL_MAP_CAP);
|
||||
__dynamic_map_rehash(h, new_count);
|
||||
}
|
||||
|
||||
__dynamic_map_full :: proc(using h: __MapHeader) -> bool {
|
||||
const __dynamic_map_full = proc(using h: __MapHeader) -> bool {
|
||||
return int(0.75 * f64(len(m.hashes))) <= m.entries.cap;
|
||||
}
|
||||
|
||||
|
||||
__dynamic_map_hash_equal :: proc(h: __MapHeader, a, b: __MapKey) -> bool {
|
||||
const __dynamic_map_hash_equal = proc(h: __MapHeader, a, b: __MapKey) -> bool {
|
||||
if a.hash == b.hash {
|
||||
if h.is_key_string {
|
||||
return a.str == b.str;
|
||||
@@ -696,13 +696,13 @@ __dynamic_map_hash_equal :: proc(h: __MapHeader, a, b: __MapKey) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
__dynamic_map_find :: proc(using h: __MapHeader, key: __MapKey) -> __MapFindResult {
|
||||
fr := __MapFindResult{-1, -1, -1};
|
||||
const __dynamic_map_find = proc(using h: __MapHeader, key: __MapKey) -> __MapFindResult {
|
||||
var fr = __MapFindResult{-1, -1, -1};
|
||||
if len(m.hashes) > 0 {
|
||||
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);
|
||||
var entry = __dynamic_map_get_entry(h, fr.entry_index);
|
||||
if __dynamic_map_hash_equal(h, entry.key, key) {
|
||||
return fr;
|
||||
}
|
||||
@@ -713,11 +713,11 @@ __dynamic_map_find :: proc(using h: __MapHeader, key: __MapKey) -> __MapFindResu
|
||||
return fr;
|
||||
}
|
||||
|
||||
__dynamic_map_add_entry :: proc(using h: __MapHeader, key: __MapKey) -> int {
|
||||
prev := m.entries.len;
|
||||
c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align);
|
||||
const __dynamic_map_add_entry = proc(using h: __MapHeader, key: __MapKey) -> int {
|
||||
var prev = m.entries.len;
|
||||
var c = __dynamic_array_append_nothing(&m.entries, entry_size, entry_align);
|
||||
if c != prev {
|
||||
end := __dynamic_map_get_entry(h, c-1);
|
||||
var end = __dynamic_map_get_entry(h, c-1);
|
||||
end.key = key;
|
||||
end.next = -1;
|
||||
}
|
||||
@@ -725,19 +725,19 @@ __dynamic_map_add_entry :: proc(using h: __MapHeader, key: __MapKey) -> int {
|
||||
}
|
||||
|
||||
|
||||
__dynamic_map_delete :: proc(using h: __MapHeader, key: __MapKey) {
|
||||
fr := __dynamic_map_find(h, key);
|
||||
const __dynamic_map_delete = proc(using h: __MapHeader, key: __MapKey) {
|
||||
var fr = __dynamic_map_find(h, key);
|
||||
if fr.entry_index >= 0 {
|
||||
__dynamic_map_erase(h, fr);
|
||||
}
|
||||
}
|
||||
|
||||
__dynamic_map_get_entry :: proc(using h: __MapHeader, index: int) -> ^__MapEntryHeader {
|
||||
data := ^u8(m.entries.data) + index*entry_size;
|
||||
const __dynamic_map_get_entry = proc(using h: __MapHeader, index: int) -> ^__MapEntryHeader {
|
||||
var data = ^u8(m.entries.data) + index*entry_size;
|
||||
return ^__MapEntryHeader(data);
|
||||
}
|
||||
|
||||
__dynamic_map_erase :: proc(using h: __MapHeader, fr: __MapFindResult) {
|
||||
const __dynamic_map_erase = proc(using h: __MapHeader, fr: __MapFindResult) {
|
||||
if fr.entry_prev < 0 {
|
||||
m.hashes[fr.hash_index] = __dynamic_map_get_entry(h, fr.entry_index).next;
|
||||
} else {
|
||||
@@ -748,7 +748,7 @@ __dynamic_map_erase :: proc(using h: __MapHeader, fr: __MapFindResult) {
|
||||
m.entries.len--;
|
||||
}
|
||||
__mem_copy(__dynamic_map_get_entry(h, fr.entry_index), __dynamic_map_get_entry(h, m.entries.len-1), entry_size);
|
||||
last := __dynamic_map_find(h, __dynamic_map_get_entry(h, fr.entry_index).key);
|
||||
var last = __dynamic_map_find(h, __dynamic_map_get_entry(h, fr.entry_index).key);
|
||||
if last.entry_prev >= 0 {
|
||||
__dynamic_map_get_entry(h, last.entry_prev).next = fr.entry_index;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user