diff --git a/arraylist.hpp b/arraylist.hpp index 25a99fc..4774a82 100644 --- a/arraylist.hpp +++ b/arraylist.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include "types.hpp" template @@ -16,10 +17,9 @@ struct Array_List { void dealloc() { free(data); - data = 0; + data = nullptr; } - void clear() { next_index = 0; } @@ -68,6 +68,16 @@ struct Array_List { return data[index]; } + static Array_List alloc_from(std::initializer_list l) { + Array_List ret; + ret.alloc(l.size()); + for (auto e : l) { + ret.append(e); + } + ret.auto_free = true; + return ret; + } + void _merge(u32 start, u32 mid, u32 end) { u32 start2 = mid + 1; @@ -137,3 +147,21 @@ struct Array_List { return middle; } }; + + +template +struct Auto_Array_List : public Array_List { + Auto_Array_List() = default; + + Auto_Array_List(std::initializer_list l) { + this->alloc(l.size()); + for (type e : l) { + this->append(e); + } + } + + ~Auto_Array_List() { + free(this->data); + this->data = nullptr; + } +};