nctref/tests/ListDLC.nct
2025-06-10 22:07:38 +03:00

44 lines
858 B
Plaintext

/*
* ListDLC: Dynamic, Linear growth, C-managed
*/
extern u8*(u8*, u32) realloc;
record ListDLC[T, S; growth] {
S capacity;
S size;
T[?]* data;
}
ListDLC_remove: [T, S; growth]u0(ListDLC[T, S; growth]* this, S index) -> {
T* data0 = &((*((*this).data))[index]);
T* data1 = data0 + @sizeof T;
S sz = (*this).size;
(*this).size = (*this).size - 1;
loop {
if(index == sz) {
break;
}
*data0 = *data1;
data0 = data0 + @sizeof T;
data1 = data1 + @sizeof T;
index = index + 1;
}
return;
};
ListDLC_add: [T, S; growth]u0(ListDLC[T, S; growth]* this, T value) -> {
if((*this).size == (*this).capacity) {
u32 newcap = (*this).capacity + growth;
(*this).capacity = newcap;
(*this).data = realloc((*this).data, newcap * @sizeof T);
}
(*((*this).data))[(*this).size] = value;
(*this).size = (*this).size + 1;
return;
};