-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
executable file
·162 lines (132 loc) · 3.77 KB
/
array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// APK - Copyright (c) 2024 by Robin Southern. https://github.com/betajaen/apk
// Licensed under the MIT License; see LICENSE file.
#pragma once
#include "pod.h"
#include "assert.h"
#include "compat.h"
namespace apk {
void memcpy(void* dst, const void* src, APK_SIZE_TYPE size);
template<typename T>
class Array {
T* m_data;
uint32 m_size, m_capacity;
public:
Array()
: m_data(NULL), m_size(0), m_capacity(0)
{
}
~Array() {
release();
}
Array(const Array& ary) {
copyFrom(ary);
}
Array(Array&& ary) {
moveFrom(ary);
}
Array& operator=(const Array& ary) {
if (&ary == this) {
return *this;
}
copyFrom(ary);
return *this;
}
Array& operator=(Array&& ary) {
if (&ary == this) {
return *this;
}
moveFrom(ary);
return *this;
}
void copyFrom(const Array& other) {
release();
capacity(other.m_size);
m_size = other.m_size;
for(uint32_t i=0;i < m_size;i++) {
new ((void*) &m_data[i]) T(other[i]);
}
}
void moveFrom(Array& other) {
release();
m_capacity = other.m_capacity;
m_size = other.m_size;
m_data = other.m_data;
other.m_capacity = 0;
other.m_size = 0;
other.m_data = NULL;
}
void release() {
if (m_data) {
for(uint32_t i=0; i < m_size;i++) {
m_data[i].~T();
}
apk_deallocate(m_data);
m_capacity = 0;
m_size = 0;
m_data = NULL;
}
}
void clear() {
for(uint32 i=0; i < m_size;i++) {
m_data[i].~T();
}
m_size = 0;
}
void push_back(const T& v) {
if (m_size == m_capacity) {
capacity(m_capacity == 0 ? 8 : m_capacity * 2);
}
new ((void*) &m_data[m_size]) T(v);
m_size++;
}
void pop_back() {
assert(m_size > 0);
m_size--;
m_data[m_size].~T();
}
T& operator[](APK_SIZE_TYPE index) {
assert(index < m_size);
return m_data[index];
}
const T& operator[](APK_SIZE_TYPE index) const {
assert(index < m_size);
return m_data[index];
}
void capacity(APK_SIZE_TYPE newCapacity) {
if (newCapacity == m_capacity)
return;
if (newCapacity == 0) {
release();
return;
}
T* tmp = (T*) apk_allocate(sizeof(T) * newCapacity);
if (m_size && m_data) {
APK_SIZE_TYPE amountToCopy = 0;
if (newCapacity < m_size) {
amountToCopy = newCapacity;
m_size = amountToCopy;
}
else {
amountToCopy = m_size;
}
for(uint32_t i=0;i < m_size;i++) {
new ((void*) &tmp[i]) T(m_data[i]);
}
}
if (m_data) {
for(uint32_t i=0; i < m_size;i++) {
m_data[i].~T();
}
apk_deallocate(m_data);
}
m_data = tmp;
m_capacity = newCapacity;
}
APK_SIZE_TYPE capacity() const {
return m_capacity;
}
APK_SIZE_TYPE size() const {
return m_size;
}
};
}