Calls and Functions 24

This commit is contained in:
nazrin 2025-06-09 03:54:01 +00:00
parent 2cd7a44bc7
commit a45f6a9e17
18 changed files with 550 additions and 285 deletions

View file

@ -3,28 +3,31 @@ module clox.container.dynarray;
import clox.memory;
struct DynArray(T){
size_t count;
size_t capacity;
private size_t _count;
private size_t _capacity;
ref size_t count() => _count;
size_t capacity() => _capacity;
// TODO capacity setter
T* ptr;
void initialise(){
count = 0;
capacity = 0;
_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);
_capacity = GROW_CAPACITY(oldCapacity);
ptr = GROW_ARRAY!T(ptr, oldCapacity, capacity);
}
ptr[count] = value;
count++;
_count++;
}
auto opSlice(){
ref auto opSlice(){
assert(ptr || !count);
return ptr[0 .. count];
}
auto opSlice(size_t i, size_t o){
ref auto opSlice(size_t i, size_t o){
assert(ptr || !count);
return ptr[i .. o];
}
@ -35,15 +38,19 @@ struct DynArray(T){
size_t opDollar(size_t pos: 0)(){
return count;
}
auto pop(){
assert(count);
return this[--_count];
}
void reset(){
_count = 0;
debug foreach(ref item; this[])
item = T.init;
}
void free(){
if(ptr)
FREE_ARRAY!T(ptr, capacity);
initialise();
}
auto pop(){
assert(count);
count--;
return this[count-1];
}
}