35 lines
751 B
Plaintext
35 lines
751 B
Plaintext
#import "modules/std.sx";
|
|
|
|
List :: struct ($T: Type) {
|
|
items: [*]T = null;
|
|
len: s64 = 0;
|
|
cap: s64 = 0;
|
|
}
|
|
|
|
append :: (list: *List($T), item: T) {
|
|
if list.len >= list.cap {
|
|
new_cap := if list.cap == 0 then 4 else list.cap * 2;
|
|
new_items : [*]T = xx malloc(new_cap * size_of(T));
|
|
if list.len > 0 {
|
|
memcpy(new_items, list.items, list.len * size_of(T));
|
|
free(list.items);
|
|
}
|
|
list.items = new_items;
|
|
list.cap = new_cap;
|
|
}
|
|
list.items[list.len] = item;
|
|
list.len += 1;
|
|
}
|
|
|
|
main :: () {
|
|
list := List(s32).{};
|
|
append(list, 3);
|
|
append(list, 1);
|
|
|
|
list.append(3);
|
|
list.append(4);
|
|
list.append(1);
|
|
list.append(5);
|
|
|
|
print("{}\n", list);
|
|
} |