BFLibCPP 0.1
CPP Library
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1
6#ifndef ARRAY_HPP
7#define ARRAY_HPP
8
9#include <stdbool.h>
10#include <stdint.h>
11#include <initializer_list>
12#include <iostream>
13#include "delete.hpp"
14#include "access.hpp"
15#include "object.hpp"
16#include <string.h>
17
18namespace BF {
19
29template <typename T, typename S = size_t> class Array : public Object {
30public:
31 Array() : Object() {
32 this->_address = 0;
33 this->_count = 0;
34 this->_callback = Array::comparisonDefault;
35 this->_releasecb = NULL;
36 }
37
41 Array(T * array, S size) : Array() {
42 this->set(array, size);
43 }
44
48 Array(std::initializer_list<T> list) : Array() {
49 this->set(list);
50 }
51
52 virtual ~Array() {
53 this->removeAll();
54 }
55
56 void removeAll() {
57 // if there is a release callback
58 // then let's call this
59 //
60 // this callback handles the memory of each element. This
61 // is set by the owner of this object
62 if (this->_releasecb) {
63 for (S i = 0; i < this->_count; i++) {
64 this->_releasecb((this->_address)[i]);
65 }
66 }
67
68 this->deallocate(this->_address);
69 this->_address = 0;
70 this->_count = 0;
71 }
72
76 void set(T * array, S size) {
77 this->saveArray(array, size);
78 }
79
83 void set(std::initializer_list<T> list) {
84 this->saveArray(list);
85 }
86
92 [[deprecated("allocation is no longer configurable")]]
93 void setAllocationCallback(T * (* cb) (S size)) {
94 //this->_allocationCallback = cb;
95 }
96
102 [[deprecated("allocation is no longer configurable")]]
103 void setDeallocationCallback(void (* cb) (T * value)) {
104 //this->_deallocationCallback = cb;
105 }
106
113 virtual bool contains(T object) {
114 for (S i = 0; i < this->_count; i++) {
115 if ( this->_callback((this->_address)[i], object)
116 == 0)
117 return true;
118 }
119 return false;
120 }
121
125 T objectAtIndex(S index) const {
126 if ((this->_address == 0) || (this->_count == 0)) {
127 return (T) 0;
128 } else if (index >= this->_count) {
129 return (T) 0;
130 } else {
131 return this->_address[index];
132 }
133 }
134
140 S indexForObject(T object) const {
141 for (S i = 0; i < this->_count; i++) {
142 if ( this->_callback(this->_address[i], object)
143 == 0)
144 return i;
145 }
146 return -1;
147 }
148
150 virtual S count() const {
151 return this->_count;
152 }
153
157 virtual void print() {
158 std::cout << "[ ";
159 for (S i = 0; i < this->_count; i++) {
160 std::cout << this->_address[i];
161 std::cout << " ";
162 }
163 std::cout << "]" << std::endl;
164 }
165
169 void setComparator(int (* callback) (T a, T b)) {
170 this->_callback = callback;
171 }
172
176 void setReleaseCallback(void (* callback) (T obj)) {
177 this->_releasecb = callback;
178 }
179
183 void copyFromArray(const Array<T> * arr) {
184 this->removeAll();
185 this->_address = (T *) this->allocate(arr->count());
186 this->_count = arr->count();
187 memcpy(this->_address, arr->address(), this->_count);
188 }
189
193 int add(T obj) {
194 this->_address = this->reallocate(this->_address, this->_count + 1);
195 if (this->_address == NULL) {
196 this->_count = 0;
197 return -3;
198 }
199
200 this->_count++;
201 this->_address[this->_count - 1] = obj;
202 return 0;
203 }
204
205 int insertObjectAtIndex(T obj, S index) {
206 this->_address = this->reallocate(this->_address, this->_count + 1);
207 if (this->_address == NULL) {
208 this->_count = 0;
209 return -4;
210 }
211
212 // shift
213 this->_count++;
214 for (S i = this->_count - 1; i > index; i--) {
215 this->_address[i] = this->_address[i - 1];
216 }
217
218 this->_address[index] = obj;
219
220 return 0;
221 }
222
228 int removeObjectAtIndex(S index) {
229 // shift objects
230 for (S i = index; (i+1) < this->_count; i++) {
231 this->_address[i] = this->_address[i + 1];
232 }
233
234 // adjust array
235 this->_count--;
236 this->_address = this->reallocate(this->_address, this->_count);
237
238 // if count == 0, then realloc will return NULL
239 if (this->_count && (this->_address == NULL)) {
240 this->_count = 0;
241 return -5;
242 }
243
244 return 0;
245 }
246
247protected:
248
252 T * address() const { return this->_address; }
253
254private:
255
259 static T * allocate(S size) {
260 return (T *) malloc(sizeof(T) * size);
261 }
262
266 static T * reallocate(T * addr, S newsize) {
267 return (T *) realloc(addr, sizeof(T) * newsize);
268 }
269
274 static void deallocate(T * value) {
275 free((void *) value);
276 }
277
281 void saveArray(T * array, S size) {
282 this->removeAll();
283 this->_address = (T *) this->allocate(size);
284 this->_count = size;
285
286 if (this->_address) {
287 // Load into array
288 for (S i = 0; i < size; i++) {
289 this->_address[i] = array[i];
290 }
291 }
292 }
293
297 void saveArray(std::initializer_list<T> list) {
298 this->removeAll();
299 typename std::initializer_list<T>::iterator itr;
300
301 this->_count = list.size();
302 this->_address = (T *) this->allocate(this->_count);
303
304 if (this->_address) {
305 S i = 0;
306 for (itr = list.begin(); itr != list.end(); ++itr) {
307 this->_address[i] = *itr;
308 i++;
309 }
310 }
311 }
312
318 T * _address;
319
321 S _count;
322
326 int (* _callback) (T a, T b);
327
334 void (* _releasecb) (T obj);
335
336public:
337
338 T operator[](S index) const {
339 return this->objectAtIndex(index);
340 }
341
342 void operator=(const std::initializer_list<T> & list) {
343 this->saveArray(list);
344 }
345
349 Array<T> & operator=(const Array<T> & arr) {
350 this->copyFromArray(&arr);
351 return *this;
352 }
353
354// Comparators
355public:
359 static int comparisonDefault(T a, T b) {
360 if (a < b) {
361 return -1;
362 } else if (a > b) {
363 return 1;
364 } else {
365 return 0;
366 }
367 }
368};
369
370} // namespace BF
371
372#endif // ARRAY_HPP
373
Definition array.hpp:29
void setReleaseCallback(void(*callback)(T obj))
Definition array.hpp:176
void copyFromArray(const Array< T > *arr)
Definition array.hpp:183
void set(std::initializer_list< T > list)
Definition array.hpp:83
static int comparisonDefault(T a, T b)
Definition array.hpp:359
void setDeallocationCallback(void(*cb)(T *value))
Definition array.hpp:103
virtual bool contains(T object)
Definition array.hpp:113
int insertObjectAtIndex(T obj, S index)
Definition array.hpp:205
virtual S count() const
Returns _count.
Definition array.hpp:150
T operator[](S index) const
Definition array.hpp:338
void operator=(const std::initializer_list< T > &list)
Definition array.hpp:342
T objectAtIndex(S index) const
Definition array.hpp:125
int add(T obj)
Definition array.hpp:193
virtual void print()
Definition array.hpp:157
T * address() const
Definition array.hpp:252
void set(T *array, S size)
Definition array.hpp:76
void setComparator(int(*callback)(T a, T b))
Definition array.hpp:169
Array(T *array, S size)
Definition array.hpp:41
int removeObjectAtIndex(S index)
Definition array.hpp:228
S indexForObject(T object) const
Definition array.hpp:140
Array()
Definition array.hpp:31
Array(std::initializer_list< T > list)
Definition array.hpp:48
virtual ~Array()
Definition array.hpp:52
void removeAll()
Definition array.hpp:56
Array< T > & operator=(const Array< T > &arr)
Definition array.hpp:349
void setAllocationCallback(T *(*cb)(S size))
Definition array.hpp:93
Definition object.hpp:21
Definition array.hpp:18