-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_array.jai
More file actions
75 lines (59 loc) · 2.69 KB
/
static_array.jai
File metadata and controls
75 lines (59 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Static_Array :: struct(capacity: int, T: Type) {
count: int;
data: [capacity] T;
}
to_view :: (array: *Static_Array) -> [] array.T {
return .{ array.count, array.data.data };
}
copy_from_view :: (array: *Static_Array, view: [] array.T) -> bool {
if view.count > array.capacity return false;
array.count = view.count;
memcpy(*array.data, view.data, view.count);
}
operator *[] :: inline (array: *Static_Array, index: int) -> *array.T { return *array.data[index]; }
operator [] :: inline (array: Static_Array, index: int) -> array.T { return array.data[index]; }
try_append :: inline (array: *Static_Array, elem: array.T) -> bool {
if array.count >= array.capacity return false;
array.data[array.count] = elem;
array.count += 1;
return true;
}
for_expansion :: (array: *Static_Array, body: Code, flags: For_Flags) #expand {
REVERSE :: cast(bool) flags & .REVERSE;
DO_POINTER :: cast(bool) flags & .POINTER;
for <=REVERSE *=DO_POINTER `it, `it_index: array.data {
if it_index == array.count break;
#insert body;
}
}
push :: inline (array: *Static_Array, elem: array.T) -> bool {
assert(array.count < array.capacity);
array.data[array.count] = elem;
array.count += 1;
}
pop :: inline (array: *Static_Array) -> array.T {
assert(array.count > 0);
defer array.count -= 1;
return array.data[array.count];
}
reflect_static_array :: (a: Any) -> count_ptr: *int, data: *u8, capacity: int, element_type: *Type_Info {
assert(is_static_array(a.type));
struct_info := a.type.(*Type_Info_Struct);
assert(struct_info.specified_parameters[0].name == "capacity");
assert(struct_info.specified_parameters[0].type == xx int);
capacity_cs_offset := struct_info.specified_parameters[0].offset_into_constant_storage;
assert(capacity_cs_offset != -1);
assert(struct_info.specified_parameters[1].name == "T");
assert(struct_info.specified_parameters[1].type == xx Type);
type_cs_offset := struct_info.specified_parameters[1].offset_into_constant_storage;
assert(type_cs_offset != -1);
count_ptr := a.value_pointer.(*int);
data := (a.value_pointer + size_of(int)).(*u8);
capacity := (struct_info.constant_storage.data + capacity_cs_offset).(*int).*;
element_type := (struct_info.constant_storage.data + type_cs_offset).(*Type).*.(*Type_Info);
return count_ptr, data, capacity, element_type;
}
is_static_array :: (a: Any) -> bool { return is_static_array(a.type); }
is_static_array :: (ti: *Type_Info) -> bool {
return ti.type == .STRUCT && ti.(*Type_Info_Struct).polymorph_source_struct == type_info(Static_Array(0, void)).polymorph_source_struct;
}