diff --git a/main.cpp b/main.cpp index 048087a..b4e5706 100644 --- a/main.cpp +++ b/main.cpp @@ -26,6 +26,8 @@ i32 main(void) { EntityID id5 = {5}; PropTable positions = {&arena, 8}; + PropTable numbers = {&arena, 8}; + numbers.add_prop(id5, 1); print_positions(&positions); @@ -67,7 +69,7 @@ void print_index(PropTable *positions) { void print_values(PropTable *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"); } diff --git a/prop_table.hpp b/prop_table.hpp index 263a5bf..fcde66e 100644 --- a/prop_table.hpp +++ b/prop_table.hpp @@ -10,7 +10,7 @@ template 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; - } - } - - if (entity_index != -1) { - index[entity_index] = idx1; - } + index[values[idx1].index_location] = idx1; } - // private: - typedef T *TArray; + struct Value { + T value; + u32 index_location; + }; - I32Array index; - TArray values; + typedef Value *ValueArray; + + I32Array index; + ValueArray values; }; #endif // !PROP_TABLE_H