From 0e7a4a15aa555a1a392ec3587ddd4ab4195b9777 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 8 Jul 2022 12:48:25 +0200 Subject: [PATCH] Array list iterator --- main.cpp | 65 +++++++++++++++++++++++++++++++------------------------- ui.cpp | 2 +- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/main.cpp b/main.cpp index 906aafc..35ee1ff 100644 --- a/main.cpp +++ b/main.cpp @@ -761,6 +761,7 @@ UI_SIGNAL_CALLBACK(scene_callback) { const int ARRAY_LIST_DEFAULT_CAP = 32; const int ARRAY_LIST_DEFAULT_ALLOCATION_MUL = 2; template struct Array_List; +template struct Array_List_Iter; template void array_add_free_node(Arena *arena, Array_List *array, int size); template @@ -778,6 +779,9 @@ struct Array_List{ Array_Node *first = 0; Array_Node *last = 0; Array_Node *first_free = 0; + + // Iterator method + Array_List_Iter iter(); }; template @@ -786,39 +790,39 @@ struct Array_List_Iter{ int index; Array_Node *node; int node_index; + + // Methods + void next(); + force_inline B32 is_valid(); }; template -void iter_next(Array_List_Iter *iter){ - if(iter->node_index + 1 >= iter->node->len){ - iter->node = iter->node->next; - iter->node_index = -1; - iter->item = 0; - } +void Array_List_Iter::next(){ + if(node_index + 1 >= node->len){ + node = node->next; + node_index = -1; + item = 0; + } - if(iter->node){ - iter->node_index += 1; - iter->index += 1; - iter->item = iter->node->data + iter->node_index; + if(node){ + node_index += 1; + index += 1; + item = node->data + node_index; + } } -} template -Array_List_Iter iter_make(Array_List *array){ +B32 Array_List_Iter::is_valid(){ return item != 0; } + +template +Array_List_Iter Array_List::iter(){ Array_List_Iter result = {}; - result.node = array->first; + result.node = this->first; result.index = result.node_index = -1; - iter_next(&result); + result.next(); return result; } -template -void array_remove_from_free_list(Array_List *array, Array_Node *it){ - if(it->prev) it->prev->next = it->next; - if(it->next) it->next->prev = it->prev; - if(array->first_free == it) array->first_free = array->first_free->next; -} - template Array_Node *array_allocate_node(Arena *arena, int size){ auto node = (Array_Node *)arena_push_size(arena, sizeof(Array_Node) + size*sizeof(T)); @@ -840,9 +844,9 @@ void make_sure_there_is_room_for_item_count(Arena *arena, Array_List *array, Array_Node *node = 0; // Iterate the free list to check if we have a block of required size there - for(Array_Node *it = array->first_free; it; it=it->next){ + For_List(array->first_free){ if(it->cap >= item_count){ - array_remove_from_free_list(array, it); + DLLFreeListRemove(array->first_free, it); node = it; node->len = 0; break; @@ -858,14 +862,14 @@ void make_sure_there_is_room_for_item_count(Arena *arena, Array_List *array, } assert(node); - DLLQueuePushLast(array->first, array->last, node); + DLLQueueAddLast(array->first, array->last, node); } } template T *array_get(Array_List *array, int index){ int i = 0; - for(Array_Node *it = array->first; it; it=it->next){ + For_List(array->first){ int lookup_i = index - i; if(lookup_i < it->len) return it->data + lookup_i; i += it->cap; @@ -885,7 +889,7 @@ void array_free_node(Array_List *array, Array_Node *node){ #if 1 // Make sure it's actually in array list B32 found = false; - for(Array_Node *it = array->first; it; it=it->next){ + For_List(array->first){ if(it == node){ found = true; break; @@ -963,13 +967,13 @@ void array_print(Array_List *array){ function void test_array_list(){ Scratch scratch; - Array_List array{4, 2}; + Array_List array{32, 1}; for(int i = 0; i < 512; i++){ array_add(scratch, &array, i); } - for(Array_List_Iter i = iter_make(&array); i.item; iter_next(&i)){ - assert(i.index == *i.item); + For_It(array){ + assert(it.index == *it.item); } assert(*array_get(&array, 22) == 22); assert(*array_get(&array, 65) == 65); @@ -977,6 +981,9 @@ test_array_list(){ array_print(&array); array_free_node(&array, array.last->prev); + array_free_node(&array, array.last->prev); + array_free_node(&array, array.last->prev); + array_free_node(&array, array.last->prev); array_free_node(&array, array.last->prev->prev); array_print(&array); diff --git a/ui.cpp b/ui.cpp index cfbea66..7691750 100644 --- a/ui.cpp +++ b/ui.cpp @@ -62,7 +62,7 @@ function UIWidget *ui_new_widget(Allocator *arena, UIWidgetKind kind) { } function void ui_push_child(UIWidget *widget, UIWidget *child) { - DLLQueuePush(widget->first_child, widget->last_child, child); + DLLQueueAdd(widget->first_child, widget->last_child, child); } function UIWidget *ui_push_child(Arena *arena, UIWidget *widget, UIWidgetKind kind) {