Skip to content

Commit fdc0a9f

Browse files
authored
Containers (#429)
* vector and string view draft * string * minor changes * container rewrite * again * more changes * more changes again
1 parent d80ffad commit fdc0a9f

File tree

10 files changed

+833
-38
lines changed

10 files changed

+833
-38
lines changed

ZEngine/ZEngine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ target_include_directories (zEngineLib
2323
.
2424
./Core
2525
./Core/Memory
26+
./Core/Container
2627
./Hardwares
2728
./Helpers
2829
./Layers
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#pragma once
2+
#include <Allocator.h>
3+
#include <type_traits>
4+
5+
// using span ?
6+
7+
using namespace ZEngine::Core::Memory;
8+
9+
namespace ZEngine::Core::Container
10+
{
11+
12+
template <typename T>
13+
struct Array
14+
{
15+
using value_type = T;
16+
using size_type = size_t;
17+
using reference = T&;
18+
using const_reference = const T&;
19+
using pointer = T*;
20+
using const_pointer = const T*;
21+
using iterator = T*;
22+
using const_iterator = const T*;
23+
24+
void init(Memory::ArenaAllocator* allocator, size_type initial_capacity, size_type initial_size)
25+
{
26+
m_allocator = allocator;
27+
m_size = initial_size;
28+
m_capacity = 0;
29+
m_data = nullptr;
30+
reserve(initial_capacity);
31+
}
32+
33+
const_reference operator[](size_type index) const
34+
{
35+
ZENGINE_VALIDATE_ASSERT(index < m_size, "Index out of range")
36+
return m_data[index];
37+
}
38+
39+
iterator begin()
40+
{
41+
return m_data;
42+
}
43+
44+
const_iterator begin() const
45+
{
46+
return m_data;
47+
}
48+
49+
iterator end()
50+
{
51+
return m_data + m_size;
52+
}
53+
54+
const_iterator end() const
55+
{
56+
return m_data + m_size;
57+
}
58+
59+
reference front()
60+
{
61+
ZENGINE_VALIDATE_ASSERT(m_size > 0, "Index out of range")
62+
return m_data[0];
63+
}
64+
65+
const_reference front() const
66+
{
67+
ZENGINE_VALIDATE_ASSERT(m_size > 0, "Index out of range")
68+
return m_data[0];
69+
}
70+
71+
reference back()
72+
{
73+
ZENGINE_VALIDATE_ASSERT(m_size > 0, "Index out of range")
74+
return m_data[m_size - 1];
75+
}
76+
77+
const_reference back() const
78+
{
79+
ZENGINE_VALIDATE_ASSERT(m_size > 0, "Index out of range")
80+
return m_data[m_size - 1];
81+
}
82+
83+
pointer data()
84+
{
85+
return m_data;
86+
}
87+
88+
const_pointer data() const
89+
{
90+
return m_data;
91+
}
92+
93+
bool empty() const
94+
{
95+
return m_size == 0;
96+
}
97+
98+
size_type size() const
99+
{
100+
return m_size;
101+
}
102+
103+
size_type capacity() const
104+
{
105+
return m_capacity;
106+
}
107+
108+
void clear()
109+
{
110+
m_size = 0;
111+
}
112+
113+
void push(const T& value)
114+
{
115+
if (m_size == m_capacity)
116+
{
117+
size_type new_capacity = m_capacity == 0 ? 4 : m_capacity * 2;
118+
reserve(new_capacity);
119+
}
120+
m_data[m_size] = value;
121+
++m_size;
122+
}
123+
124+
void push(T&& value)
125+
{
126+
if (m_size == m_capacity)
127+
{
128+
size_type new_capacity = m_capacity == 0 ? 4 : m_capacity * 2;
129+
reserve(new_capacity);
130+
}
131+
m_data[m_size] = std::move(value);
132+
++m_size;
133+
}
134+
135+
reference push_use(const T& value)
136+
{
137+
if (m_size == m_capacity)
138+
{
139+
size_type new_capacity = m_capacity == 0 ? 4 : m_capacity * 2;
140+
reserve(new_capacity);
141+
}
142+
m_data[m_size] = value;
143+
++m_size;
144+
return back();
145+
}
146+
147+
void pop()
148+
{
149+
ZENGINE_VALIDATE_ASSERT(m_size > 0, "Index out of range")
150+
--m_size;
151+
}
152+
153+
~Array()
154+
{
155+
clear();
156+
m_data = nullptr;
157+
m_capacity = 0;
158+
}
159+
160+
void reserve(size_type new_capacity)
161+
{
162+
if (new_capacity <= m_capacity)
163+
{
164+
return;
165+
}
166+
167+
size_t old_alloc_size = m_capacity * sizeof(T);
168+
size_t new_alloc_size = new_capacity * sizeof(T);
169+
m_data = static_cast<pointer>(ZResize(m_allocator, m_data, old_alloc_size, new_alloc_size, ZAlignof(value_type)));
170+
m_capacity = new_capacity;
171+
}
172+
173+
Memory::ArenaAllocator* m_allocator;
174+
size_type m_size;
175+
size_type m_capacity;
176+
pointer m_data;
177+
};
178+
179+
template <typename T>
180+
struct ArrayView
181+
{
182+
ArrayView(T* data, size_t size) : m_data(data), m_size(size) {}
183+
184+
void set(T* data, size_t size)
185+
{
186+
m_data = data;
187+
m_size = size;
188+
}
189+
190+
T& operator[](size_t index)
191+
{
192+
ZENGINE_VALIDATE_ASSERT(index < m_size, "Index out of range")
193+
return m_data[index];
194+
}
195+
const T& operator[](size_t index) const
196+
{
197+
ZENGINE_VALIDATE_ASSERT(index < m_size, "Index out of range")
198+
return m_data[index];
199+
}
200+
201+
T* m_data;
202+
size_t m_size;
203+
};
204+
} // namespace ZEngine::Core::Container

0 commit comments

Comments
 (0)