Local Variables 22
This commit is contained in:
parent
4f2211eb72
commit
72a41e81e6
18 changed files with 1335 additions and 115 deletions
45
src/clox/container/dynarray.d
Normal file
45
src/clox/container/dynarray.d
Normal 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];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue