diff --git a/.gitignore b/.gitignore index cc3ae56..290dfbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /bin/* *.report /ftb -vgcore.* \ No newline at end of file +vgcore.* diff --git a/arraylist.hpp b/arraylist.hpp index e41ad5b..1619cea 100644 --- a/arraylist.hpp +++ b/arraylist.hpp @@ -2,6 +2,7 @@ #include #include #include "types.hpp" +#include "macros.hpp" template struct Array_List { @@ -15,6 +16,16 @@ struct Array_List { length = initial_capacity; } + void alloc_from(std::initializer_list l) { + length = max(l.size(), 1); // alloc at least one + + data = (type*)malloc(length * sizeof(type)); + count = 0; + for (type t : l) { + data[count++] = t; + } + } + void dealloc() { free(data); data = nullptr; @@ -68,15 +79,6 @@ 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; @@ -151,7 +153,14 @@ struct Array_List { template struct Auto_Array_List : public Array_List { - Auto_Array_List() = default; + + Auto_Array_List(u32 length) { + this->alloc(length); + } + + Auto_Array_List() { + this->alloc(16); + } Auto_Array_List(std::initializer_list l) { this->alloc(l.size()); diff --git a/macros.hpp b/macros.hpp index 37e2760..94e9975 100644 --- a/macros.hpp +++ b/macros.hpp @@ -6,6 +6,9 @@ #define label(x, y) concat(x, y) #define line_label(x) label(x, __LINE__) +#define min(a, b) ((a) < (b)) ? (a) : (b) +#define max(a, b) ((a) > (b)) ? (a) : (b) + /** * Defer *