From: Brendan Hansen Date: Wed, 21 Oct 2020 15:25:51 +0000 (-0500) Subject: added initial version of my array type X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2183200b7d191e320efd6b47b7b7b868d70a56b5;p=csc718.git added initial version of my array type --- diff --git a/include/container.h b/include/container.h new file mode 100644 index 0000000..d71e6c5 --- /dev/null +++ b/include/container.h @@ -0,0 +1,133 @@ +#ifndef CONTAINER_H +#define CONTAINER_H + +#include "types.h" +#include + +template +struct Array +{ + u32 count; + u32 capacity; + T *data; + + Array() + { + count = 0; + capacity = 0; + data = nullptr; + } + + T& operator[](i32 elem) { +#if defined(DEBUG) + assert(elem >= 0 && elem < count); +#endif + return data[elem]; + } + const T& operator[](i32 elem) const { return data[elem]; } + + bool ensure_capacity(u32 min_capacity) + { + if (min_capacity <= capacity) return true; + + u32 new_capacity = 0; + if (data != nullptr) + { + new_capacity = capacity; + while (new_capacity < min_capacity) new_capacity <<= 1; + + data = (T*) realloc(data, new_capacity * sizeof(T)); + } + else + { + new_capacity = min_capacity; + data = (T*) malloc(new_capacity * sizeof(T)); + } + + if (data == nullptr) return false; + + capacity = new_capacity; + return true; + } + + void push(const T& val) + { + if (!ensure_capacity(count + 1)) return; + data[count++] = val; + } + + T pop() + { + return data[--count]; + } + + void clear() + { + count = 0; + } + + void insert(u32 idx, const T& x) + { + if (!ensure_capacity(count + 1)) return; + + count += 1; + for (u32 i = count; i > idx; i--) + data[i] = data[i - 1]; + + data[idx] = x; + } + + void remove(const T& elem) + { + auto move = 0; + + for (u32 i = 0; i < count - move; i++) + { + if (data[i + move] == elem) move += 1; + if (move != 0) data[i] = data[i + move]; + } + + count -= move; + } + + void delete_at(u32 idx) + { + if (idx >= count) return; + + for (u32 i = idx; i < count - 1; i++) + data[i] = data[i + 1]; + + count -= 1; + } + + void fast_delete(u32 idx) + { + if (idx >= count) return; + + data[idx] = data[count - 1]; + count -= 1; + } + + bool contains(const T& x) + { + for (u32 i = 0; i < count; i++) + { + if (data[i] == x) return true; + } + + return false; + } + + + T* begin() + { + return &data[0]; + } + + T* end() + { + return &data[count]; + } +}; + +#endif //CONTAINER_H diff --git a/include/utils.h b/include/utils.h index 4b36e38..6ad3837 100644 --- a/include/utils.h +++ b/include/utils.h @@ -5,7 +5,7 @@ #include // NOTE(Brendan): Only for intptr_t // NOTE(Brendan): Hacky way to get the offset of a struct member. offsetof() is the standard way in C to get it, but it is not guarenteed to be defined in all C implementations. -#define offset_of(S, mem) (intptr_t) &(((S*)(0))->mem) +// #define offset_of(S, mem) (intptr_t) &(((S*)(0))->mem) // NOTE(Brendan): There are so many uses of 'static' in C and they quickly lose their meaning. #define internal static // internal void foo() { ... }