Local Variables 22

This commit is contained in:
nazrin 2025-06-07 23:52:16 +00:00
parent 4f2211eb72
commit 72a41e81e6
18 changed files with 1335 additions and 115 deletions

View file

@ -0,0 +1,45 @@
module clox.container.dynarray;
import clox.memory;
struct DynArray(T){
size_t count;
size_t capacity;
T* ptr;
void initialise(){
count = 0;
capacity = 0;
ptr = null;
}
void opOpAssign(string op: "~")(in T value){
if(capacity < count + 1){
size_t oldCapacity = capacity;
capacity = GROW_CAPACITY(oldCapacity);
ptr = GROW_ARRAY!T(ptr, oldCapacity, capacity);
}
ptr[count] = value;
count++;
}
auto opSlice(){
assert(ptr || !count);
return ptr[0 .. count];
}
ref auto opIndex(size_t i){
assert(ptr && count);
return ptr[i];
}
size_t opDollar(size_t pos: 0)(){
return count;
}
void free(){
if(ptr)
FREE_ARRAY!T(ptr, capacity);
initialise();
}
auto pop(){
assert(count);
count--;
return this[count-1];
}
}