-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastCont.cpp
More file actions
187 lines (166 loc) · 5.2 KB
/
FastCont.cpp
File metadata and controls
187 lines (166 loc) · 5.2 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "FastCont.h"
template <class T, class id_data_type>
id_data_type FastCont<T, id_data_type>::push_back(T _a) {
// FastContElement<T, id_data_type> a(_a, rollingID++);
if (alloc_size == 0) {
alloc_size = 1;
p = (FastContElement<T, id_data_type> *)malloc(sizeof(FastContElement<T, id_data_type>));
} else if (alloc_size <= _size) {
alloc_size *= 2;
p = (FastContElement<T, id_data_type> *)realloc(p, sizeof(FastContElement<T, id_data_type>) * alloc_size);
}
(p + _size)->id = rollingID++;
// (p + _size)->data = _a;
memcpy(&(p + _size)->data, &_a, sizeof(T));
// #pragma message("talele memcpy je novi, dela kr dobr zaenkat...")
++_size;
return rollingID - 1;
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::pop_back() {
if (_size == 0) return;
--_size;
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::remove_index(uint32_t at) {
if (at >= _size) return;
for (uint32_t i = at + 1; i < _size; ++i)
*(p + i - 1) = *(p + i);
--_size;
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::remove_id(id_data_type id) {
for (uint32_t i = 0; i < _size; ++i) {
if ((p + i)->id == id) {
remove_index(i);
}
}
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::insert(T a, uint32_t at) {
if (at > _size) return;
if (alloc_size == 0) {
alloc_size = 1;
p = (FastContElement<T, id_data_type> *)malloc(sizeof(FastContElement<T, id_data_type>));
} else if (alloc_size <= _size) {
alloc_size *= 2;
p = (FastContElement<T, id_data_type> *)realloc(p, sizeof(FastContElement<T, id_data_type>) * alloc_size);
}
for (int64_t i = _size - 1; i >= at; --i)
*(p + i + 1) = *(p + i);
(p + at)->data = a;
++_size;
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::clear() {
if (p != nullptr)
free(p);
p = nullptr;
alloc_size = 0;
_size = 0;
}
template <class T, class id_data_type>
T *FastCont<T, id_data_type>::at_index(uint32_t at) {
if (at >= _size) throw invalid_argument("\"at\" out of bounds");
return &((p + at)->data);
}
template <class T, class id_data_type>
T *FastCont<T, id_data_type>::at_id(id_data_type searchForID) {
for (uint32_t i = 0; i < _size; ++i) {
if ((p + i)->id == searchForID) {
return &((p + i)->data);
}
}
#ifdef CONSOLE_LOGGING_ID_NOT_FOUND
cout << "E: FastCont::at_id\tno ID found!\n";
#endif
return nullptr;
}
template <class T, class id_data_type>
id_data_type FastCont<T, id_data_type>::get_id_at_index(uint32_t index) {
return (p + index)->id;
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::reserve_n_spots(uint32_t n) {
if (alloc_size == 0) alloc_size = 1;
while (alloc_size < n)
alloc_size *= 2;
p = (FastContElement<T, id_data_type> *)realloc(p, sizeof(FastContElement<T, id_data_type>) * alloc_size);
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::reset() {
rollingID = 0;
}
template <class T, class id_data_type>
int64_t FastCont<T, id_data_type>::find_and_return_index(T a) {
for (uint32_t i = 0; i < _size; ++i) {
if ((p + i)->data == a) return i;
}
return -1;
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::set_memory_leak_safety(bool a) {
memory_leak_safety = a;
}
template <class T, class id_data_type>
FastCont<T, id_data_type>::FastCont(bool _memory_leak_safety) {
memory_leak_safety = _memory_leak_safety;
p = nullptr;
rollingID = 0;
alloc_size = 0;
_size = 0;
}
template <class T, class id_data_type>
FastCont<T, id_data_type>::FastCont() {
memory_leak_safety = true;
p = nullptr;
rollingID = 0;
alloc_size = 0;
_size = 0;
}
template <class T, class id_data_type>
template <typename... Args>
FastCont<T, id_data_type>::FastCont(T a, Args... data) {
memory_leak_safety = true;
p = nullptr;
rollingID = 0;
alloc_size = 0;
_size = 0;
push_back(a);
handleInfArgs(data...);
}
template <class T, class id_data_type>
template <typename... Args>
void FastCont<T, id_data_type>::handleInfArgs(T a, Args... data) {
push_back(a);
handleInfArgs(data...);
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::handleInfArgs() {
return;
}
template <class T, class id_data_type>
FastCont<T, id_data_type>::~FastCont() {
if (memory_leak_safety && p != nullptr)
free(p);
}
template <class T, class id_data_type>
void FastCont<T, id_data_type>::force_import(id_data_type id, T _a) {
T *ex = at_id(id);
if (ex != nullptr) {
*ex = _a;
return;
}
if (alloc_size == 0) {
alloc_size = 1;
p = (FastContElement<T, id_data_type> *)malloc(sizeof(FastContElement<T, id_data_type>));
} else if (alloc_size <= _size) {
alloc_size *= 2;
p = (FastContElement<T, id_data_type> *)realloc(p, sizeof(FastContElement<T, id_data_type>) * alloc_size);
}
(p + _size)->id = id;
// (p + _size)->data = _a;
memcpy(&(p + _size)->data, &_a, sizeof(T));
++_size;
if (id > rollingID) rollingID = id;
}