-
Notifications
You must be signed in to change notification settings - Fork 5
/
list.capy
136 lines (106 loc) · 2.98 KB
/
list.capy
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
core :: mod "core";
ptr :: core.ptr;
libc :: core.libc;
math :: core.math;
// A dynamically allocated List type that is generic at runtime.
//
// This, and Runtime Generics in general, has been largely inspired by the blog post
// [A Simple Yet Useful Version of Generics by Luna Razzaghipour](https://blog.xoria.org/simple-generics/)
List :: struct {
ty: type,
buf: ^mut any,
cap: usize,
len: usize,
};
make :: (ty: type) -> List {
List.{
ty = ty,
buf = ptr.mut_null(),
cap = 0,
len = 0,
}
}
make_with_capacity :: (ty: type, cap: usize) -> List {
if cap == 0 { return make(ty); }
raw_cap :: cap * core.meta.stride_of(ty);
if raw_cap < cap || raw_cap < core.meta.stride_of(ty) {
core.panic_with("overflow when trying to allocate the backing array of a list");
}
buf := libc.malloc(raw_cap) as ^mut char;
if cap > 0 && ptr.is_null(buf) {
core.panic_with("Error allocating the List");
}
List.{
ty = ty,
buf = buf,
len = 0,
cap = cap,
}
}
free :: (self: ^mut List) {
libc.free(self.buf);
// to make sure the list isn't accidentally used after free
self.cap = 0;
self.len = 0;
}
push :: (list: ^mut List, value: core.Any) {
_grow_by(list, 1);
offset := ptr.mut_offset(list.buf, list.len * core.meta.stride_of(list.ty));
core.cast_into(value, list.ty, offset);
list.len = list.len + 1;
}
get :: (list: ^List, index: usize) -> core.Any {
if index >= list.len {
core.panic_with("called `get` with an index outside the list");
}
core.Any.{
data = ptr.const_offset(list.buf, index * core.meta.stride_of(list.ty)),
ty = list.ty,
}
}
// todo: return the item
pop :: (list: ^mut List) {
if list.len == 0 {
core.panic_with("cannot pop from an empty list");
}
list.len = list.len - 1;
}
print :: (list: ^List) {
ty_stride := core.meta.stride_of(list.ty);
core.print("[ ");
idx := 0;
while idx < list.len {
core.print(core.Any.{
ty = list.ty,
data = ptr.const_offset(list.buf, idx * ty_stride)
});
idx = idx + 1;
if idx < list.len {
core.print(", ");
}
}
core.print(" ]");
}
println :: (list: ^List) {
print(list);
core.print('\n');
}
to_slice :: (list: ^List) -> [] any {
ptr.slice_from_raw_parts(list.buf, list.len)
}
_grow_by :: (list: ^mut List, len: usize) {
if list.len + len > list.cap {
new_cap := math.max_usize(list.cap * 2, 1);
new_cap = math.max_usize(
math.next_pow_of_two(list.len + len),
new_cap
);
ty_stride := core.meta.stride_of(list.ty);
new_cap_raw :: new_cap * ty_stride;
if new_cap_raw < new_cap || new_cap_raw < ty_stride {
core.panic_with("overflow when trying to grow list");
}
list.buf = libc.realloc(list.buf, new_cap_raw) as ^mut char;
list.cap = new_cap;
}
}