Initial commit
This commit is contained in:
87
core/parray.jai
Normal file
87
core/parray.jai
Normal file
@@ -0,0 +1,87 @@
|
||||
PArray :: struct (Data_Type : Type, Handle_Type : Type) {
|
||||
data: [..] Data_Type;
|
||||
indices: [..] u32;
|
||||
}
|
||||
|
||||
parray_reset :: (using array: *PArray) {
|
||||
array_reset(*array.data);
|
||||
array_reset(*array.indices);
|
||||
}
|
||||
|
||||
parray_free :: (using array: PArray) {
|
||||
array_free(array.data);
|
||||
array_free(array.indices);
|
||||
}
|
||||
|
||||
parray_reserve :: (using parray: *PArray, capacity: s32) {
|
||||
if capacity > 0 {
|
||||
array_reserve(*data, capacity);
|
||||
array_reserve(*indices, capacity);
|
||||
}
|
||||
}
|
||||
|
||||
parray_get :: (using parray: PArray, handle: parray.Handle_Type) -> *parray.Data_Type {
|
||||
index := indices[handle - 1] - 1;
|
||||
return *data[index];
|
||||
}
|
||||
|
||||
parray_get_val :: (using parray: PArray, handle: parray.Handle_Type) -> parray.Data_Type {
|
||||
assert(xx handle > 0 && xx handle <= indices.count);
|
||||
index := indices[handle - 1] - 1;
|
||||
return data[index];
|
||||
}
|
||||
|
||||
parray_add :: (using parray: *PArray, value: parray.Data_Type) -> parray.Handle_Type {
|
||||
array_add(*data, value);
|
||||
index := cast(u32)data.count;
|
||||
|
||||
handle : Handle_Type = 0;
|
||||
|
||||
// find the next empty index
|
||||
for *val, i: indices {
|
||||
if <<val == 0 {
|
||||
<<val = index;
|
||||
handle = cast(Handle_Type)i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if handle == 0 {
|
||||
array_add(*indices, index);
|
||||
handle = cast(Handle_Type)indices.count;
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
parray_remove :: (using parray: *PArray, handle: parray.Handle_Type) {
|
||||
index := indices[handle - 1] - 1;
|
||||
indices[handle - 1] = 0;
|
||||
|
||||
if data.count == 1 {
|
||||
data.count = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data[index] = data[data.count-1];
|
||||
|
||||
for * indices {
|
||||
if <<it == data.count {
|
||||
<<it = index + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
data.count -= 1;
|
||||
}
|
||||
|
||||
for_expansion :: (parray: *PArray, body: Code, flags: For_Flags) #expand {
|
||||
for *value, `it_index: parray.data {
|
||||
#if flags & .POINTER {
|
||||
`it := value;
|
||||
} else {
|
||||
`it := <<value;
|
||||
}
|
||||
#insert body;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user