| @@ -1,5 +1,6 @@ | |||||
| #pragma once | #pragma once | ||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <initializer_list> | |||||
| #include "types.hpp" | #include "types.hpp" | ||||
| template <typename type> | template <typename type> | ||||
| @@ -16,10 +17,9 @@ struct Array_List { | |||||
| void dealloc() { | void dealloc() { | ||||
| free(data); | free(data); | ||||
| data = 0; | |||||
| data = nullptr; | |||||
| } | } | ||||
| void clear() { | void clear() { | ||||
| next_index = 0; | next_index = 0; | ||||
| } | } | ||||
| @@ -68,6 +68,16 @@ struct Array_List { | |||||
| return data[index]; | return data[index]; | ||||
| } | } | ||||
| static Array_List<type> alloc_from(std::initializer_list<type> l) { | |||||
| Array_List<type> 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) { | void _merge(u32 start, u32 mid, u32 end) { | ||||
| u32 start2 = mid + 1; | u32 start2 = mid + 1; | ||||
| @@ -137,3 +147,21 @@ struct Array_List { | |||||
| return middle; | return middle; | ||||
| } | } | ||||
| }; | }; | ||||
| template <typename type> | |||||
| struct Auto_Array_List : public Array_List<type> { | |||||
| Auto_Array_List() = default; | |||||
| Auto_Array_List(std::initializer_list<type> l) { | |||||
| this->alloc(l.size()); | |||||
| for (type e : l) { | |||||
| this->append(e); | |||||
| } | |||||
| } | |||||
| ~Auto_Array_List() { | |||||
| free(this->data); | |||||
| this->data = nullptr; | |||||
| } | |||||
| }; | |||||