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; size_t capacity(size_t newCapacity){ ptr = GROW_ARRAY!T(ptr, _capacity, newCapacity); _capacity = newCapacity; return newCapacity; } T* ptr; void initialise(){ _count = 0; _capacity = 0; ptr = null; } void opOpAssign(string op: "~")(T value){ if(capacity < count + 1) capacity = GROW_CAPACITY(_capacity); ptr[count] = value; _count++; } ref auto opSlice(){ assert(ptr || !count); return ptr[0 .. count]; } ref auto opSlice(size_t i, size_t o){ assert(ptr || !count); return ptr[i .. o]; } ref auto opIndex(size_t i){ assert(ptr && count); return ptr[i]; } size_t opDollar(size_t pos: 0)(){ return count; } auto pop(){ assert(count > 0); return ptr[--_count]; } void reset(){ _count = 0; debug foreach(ref item; this[]) item = T.init; } void free(){ if(ptr) FREE_ARRAY!T(ptr, capacity); initialise(); } }