nctref/examples/ListDLU.nct

44 lines
883 B
Plaintext

/*
* ListDLU: Dynamic, Linear growth, unmanaged
*/
use Alloc;
record ListDLU[T, S; growth] {
S capacity;
S size;
T[?]* data;
}
ListDLU_remove: [T, S; growth]u0(ListDLU[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;
};
ListDLU_add: [T, S; growth]u0(ListDLU[T, S; growth]* this, Alloc *alloc, T value) -> {
if((*this).size == (*this).capacity) {
u32 newcap = (*this).capacity + growth;
(*this).capacity = newcap;
(*this).data = ((*alloc).realloc)((*alloc).userdata, (*this).data, newcap * @sizeof T);
}
(*((*this).data))[(*this).size] = value;
(*this).size = (*this).size + 1;
return;
};