list
This commit is contained in:
@@ -1,12 +1,32 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
List :: struct ($T: Type) {
|
||||
items: [*]T = .[];
|
||||
len: s32 = 0;
|
||||
cap: s32 = 0;
|
||||
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);
|
||||
append(list, 4);
|
||||
append(list, 1);
|
||||
append(list, 5);
|
||||
print("{}\n", list);
|
||||
}
|
||||
@@ -194,7 +194,9 @@ slice_to_string :: (items: []$T) -> string {
|
||||
|
||||
pointer_to_string :: (p: $T) -> string {
|
||||
addr : s64 = xx p;
|
||||
concat(type_name(T), concat("@", int_to_hex_string(addr)));
|
||||
if addr == 0 { "null"; } else {
|
||||
concat(type_name(T), concat("@0x", int_to_hex_string(addr)));
|
||||
}
|
||||
}
|
||||
|
||||
union_to_string :: (u: $T) -> string {
|
||||
|
||||
Reference in New Issue
Block a user