added initial version of my array type
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 21 Oct 2020 15:25:51 +0000 (10:25 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 21 Oct 2020 15:25:51 +0000 (10:25 -0500)
include/container.h [new file with mode: 0644]
include/utils.h

diff --git a/include/container.h b/include/container.h
new file mode 100644 (file)
index 0000000..d71e6c5
--- /dev/null
@@ -0,0 +1,133 @@
+#ifndef CONTAINER_H
+#define CONTAINER_H
+
+#include "types.h"
+#include <assert.h>
+
+template <typename T>
+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
index 4b36e38deaaa5db9c5a647daa17e5e8b07ae9e5f..6ad3837ff0bdccb30e9bba0ae033f7fcd448111c 100644 (file)
@@ -5,7 +5,7 @@
 #include <stdint.h> // 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() { ... }