Reverse index lookup

This commit is contained in:
Abdelrahman Said
2026-06-04 00:18:06 +01:00
parent 2d99546572
commit 8e1602d1cf
2 changed files with 16 additions and 20 deletions
+3 -1
View File
@@ -26,6 +26,8 @@ i32 main(void) {
EntityID id5 = {5};
PropTable<Pos> positions = {&arena, 8};
PropTable<i32> numbers = {&arena, 8};
numbers.add_prop(id5, 1);
print_positions(&positions);
@@ -67,7 +69,7 @@ void print_index(PropTable<Pos> *positions) {
void print_values(PropTable<Pos> *positions) {
printf("==========VALUES==========\n");
for (u64 i = 0; i < wapp_array_count(positions->values); ++i) {
printf("%" PRIu64 ": (%f, %f, %f)\n", i, positions->values[i].x, positions->values[i].y, positions->values[i].z);
printf("%" PRIu64 ": (%f, %f, %f)\n", i, positions->values[i].value.x, positions->values[i].value.y, positions->values[i].value.z);
}
printf("\n");
}
+11 -17
View File
@@ -10,7 +10,7 @@ template<typename T>
struct PropTable {
PropTable(const Allocator *allocator, u64 capacity) {
index = wapp_array_alloc_capacity(i32, allocator, capacity, ARRAY_INIT_FILLED);
values = wapp_array_alloc_capacity(T, allocator, capacity, ARRAY_INIT_NONE);
values = wapp_array_alloc_capacity(Value, allocator, capacity, ARRAY_INIT_NONE);
for (u64 i = 0; i < wapp_array_count(index); ++i) {
index[i] = -1;
@@ -21,8 +21,9 @@ struct PropTable {
if (IS_INVALID_ID(id)) { return; }
i32 idx = (i32)wapp_array_count(values);
Value v = {prop, id.index};
wapp_array_set(i32, index, id.index, &idx);
wapp_array_append_capped(T, values, &prop);
wapp_array_append_capped(Value, values, &v);
}
void remove_prop(EntityID id) {
@@ -34,29 +35,22 @@ struct PropTable {
// Swap values
values[idx1] = values[idx2];
wapp_array_pop(T, values);
wapp_array_pop(Value, values);
// Update index
index[id.index] = -1;
i64 entity_index = -1;
for (u64 i = 0; i < wapp_array_count(index); ++i) {
if (index[i] == idx2) {
entity_index = (i64)i;
break;
}
index[values[idx1].index_location] = idx1;
}
if (entity_index != -1) {
index[entity_index] = idx1;
}
}
struct Value {
T value;
u32 index_location;
};
// private:
typedef T *TArray;
typedef Value *ValueArray;
I32Array index;
TArray values;
ValueArray values;
};
#endif // !PROP_TABLE_H