Garbage Collection 26

This commit is contained in:
nazrin 2025-06-12 06:07:25 +00:00
parent dc4e6d33b2
commit 6d5dff6e3d
10 changed files with 444 additions and 211 deletions

View file

@ -1,13 +1,20 @@
module clox.container.dynarray;
public import std.typecons : Flag, Yes, No;
import clox.memory;
struct DynArray(T){
private size_t _count;
private size_t _capacity;
size_t count() => _count;
size_t count(size_t c) => _count = c;
size_t capacity() => _capacity;
// TODO capacity setter
size_t capacity(size_t newCapacity){
ptr = GROW_ARRAY!T(ptr, _capacity, newCapacity);
_capacity = newCapacity;
return newCapacity;
}
T* ptr;
void initialise(){
_count = 0;
@ -15,11 +22,8 @@ struct DynArray(T){
ptr = null;
}
void opOpAssign(string op: "~")(T value){
if(capacity < count + 1){
size_t oldCapacity = capacity;
_capacity = GROW_CAPACITY(oldCapacity);
ptr = GROW_ARRAY!T(ptr, oldCapacity, capacity);
}
if(capacity < count + 1)
capacity = GROW_CAPACITY(_capacity);
ptr[count] = value;
_count++;
}
@ -39,8 +43,8 @@ struct DynArray(T){
return count;
}
auto pop(){
assert(count);
return this[--_count];
assert(count > 0);
return ptr[--_count];
}
void reset(){
_count = 0;