Array list iterator

This commit is contained in:
Krzosa Karol
2022-07-08 12:48:25 +02:00
parent 70d1006ece
commit 0e7a4a15aa
2 changed files with 37 additions and 30 deletions

View File

@@ -761,6 +761,7 @@ UI_SIGNAL_CALLBACK(scene_callback) {
const int ARRAY_LIST_DEFAULT_CAP = 32; const int ARRAY_LIST_DEFAULT_CAP = 32;
const int ARRAY_LIST_DEFAULT_ALLOCATION_MUL = 2; const int ARRAY_LIST_DEFAULT_ALLOCATION_MUL = 2;
template<class T> struct Array_List; template<class T> struct Array_List;
template<class T> struct Array_List_Iter;
template<class T> void array_add_free_node(Arena *arena, Array_List<T> *array, int size); template<class T> void array_add_free_node(Arena *arena, Array_List<T> *array, int size);
template<class T> template<class T>
@@ -778,6 +779,9 @@ struct Array_List{
Array_Node<T> *first = 0; Array_Node<T> *first = 0;
Array_Node<T> *last = 0; Array_Node<T> *last = 0;
Array_Node<T> *first_free = 0; Array_Node<T> *first_free = 0;
// Iterator method
Array_List_Iter<T> iter();
}; };
template<class T> template<class T>
@@ -786,39 +790,39 @@ struct Array_List_Iter{
int index; int index;
Array_Node<T> *node; Array_Node<T> *node;
int node_index; int node_index;
// Methods
void next();
force_inline B32 is_valid();
}; };
template<class T> template<class T>
void iter_next(Array_List_Iter<T> *iter){ void Array_List_Iter<T>::next(){
if(iter->node_index + 1 >= iter->node->len){ if(node_index + 1 >= node->len){
iter->node = iter->node->next; node = node->next;
iter->node_index = -1; node_index = -1;
iter->item = 0; item = 0;
} }
if(iter->node){ if(node){
iter->node_index += 1; node_index += 1;
iter->index += 1; index += 1;
iter->item = iter->node->data + iter->node_index; item = node->data + node_index;
}
} }
}
template<class T> template<class T>
Array_List_Iter<T> iter_make(Array_List<T> *array){ B32 Array_List_Iter<T>::is_valid(){ return item != 0; }
template<class T>
Array_List_Iter<T> Array_List<T>::iter(){
Array_List_Iter<T> result = {}; Array_List_Iter<T> result = {};
result.node = array->first; result.node = this->first;
result.index = result.node_index = -1; result.index = result.node_index = -1;
iter_next(&result); result.next();
return result; return result;
} }
template<class T>
void array_remove_from_free_list(Array_List<T> *array, Array_Node<T> *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<class T> template<class T>
Array_Node<T> *array_allocate_node(Arena *arena, int size){ Array_Node<T> *array_allocate_node(Arena *arena, int size){
auto node = (Array_Node<T> *)arena_push_size(arena, sizeof(Array_Node<T>) + size*sizeof(T)); auto node = (Array_Node<T> *)arena_push_size(arena, sizeof(Array_Node<T>) + size*sizeof(T));
@@ -840,9 +844,9 @@ void make_sure_there_is_room_for_item_count(Arena *arena, Array_List<T> *array,
Array_Node<T> *node = 0; Array_Node<T> *node = 0;
// Iterate the free list to check if we have a block of required size there // Iterate the free list to check if we have a block of required size there
for(Array_Node<T> *it = array->first_free; it; it=it->next){ For_List(array->first_free){
if(it->cap >= item_count){ if(it->cap >= item_count){
array_remove_from_free_list(array, it); DLLFreeListRemove(array->first_free, it);
node = it; node = it;
node->len = 0; node->len = 0;
break; break;
@@ -858,14 +862,14 @@ void make_sure_there_is_room_for_item_count(Arena *arena, Array_List<T> *array,
} }
assert(node); assert(node);
DLLQueuePushLast(array->first, array->last, node); DLLQueueAddLast(array->first, array->last, node);
} }
} }
template<class T> template<class T>
T *array_get(Array_List<T> *array, int index){ T *array_get(Array_List<T> *array, int index){
int i = 0; int i = 0;
for(Array_Node<T> *it = array->first; it; it=it->next){ For_List(array->first){
int lookup_i = index - i; int lookup_i = index - i;
if(lookup_i < it->len) return it->data + lookup_i; if(lookup_i < it->len) return it->data + lookup_i;
i += it->cap; i += it->cap;
@@ -885,7 +889,7 @@ void array_free_node(Array_List<T> *array, Array_Node<T> *node){
#if 1 #if 1
// Make sure it's actually in array list // Make sure it's actually in array list
B32 found = false; B32 found = false;
for(Array_Node<T> *it = array->first; it; it=it->next){ For_List(array->first){
if(it == node){ if(it == node){
found = true; found = true;
break; break;
@@ -963,13 +967,13 @@ void array_print(Array_List<int> *array){
function void function void
test_array_list(){ test_array_list(){
Scratch scratch; Scratch scratch;
Array_List<int> array{4, 2}; Array_List<int> array{32, 1};
for(int i = 0; i < 512; i++){ for(int i = 0; i < 512; i++){
array_add(scratch, &array, i); array_add(scratch, &array, i);
} }
for(Array_List_Iter<int> i = iter_make(&array); i.item; iter_next(&i)){ For_It(array){
assert(i.index == *i.item); assert(it.index == *it.item);
} }
assert(*array_get(&array, 22) == 22); assert(*array_get(&array, 22) == 22);
assert(*array_get(&array, 65) == 65); assert(*array_get(&array, 65) == 65);
@@ -977,6 +981,9 @@ test_array_list(){
array_print(&array); 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);
array_free_node(&array, array.last->prev->prev); array_free_node(&array, array.last->prev->prev);
array_print(&array); array_print(&array);

2
ui.cpp
View File

@@ -62,7 +62,7 @@ function UIWidget *ui_new_widget(Allocator *arena, UIWidgetKind kind) {
} }
function void ui_push_child(UIWidget *widget, UIWidget *child) { 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) { function UIWidget *ui_push_child(Arena *arena, UIWidget *widget, UIWidgetKind kind) {