11#include <initializer_list>
29template <
typename T,
typename S =
size_t>
class Array :
public Object {
35 this->_releasecb = NULL;
42 this->
set(array, size);
62 if (this->_releasecb) {
63 for (S i = 0; i < this->_count; i++) {
64 this->_releasecb((this->_address)[i]);
68 this->deallocate(this->_address);
76 void set(T * array, S size) {
77 this->saveArray(array, size);
83 void set(std::initializer_list<T> list) {
84 this->saveArray(list);
92 [[deprecated(
"allocation is no longer configurable")]]
102 [[deprecated(
"allocation is no longer configurable")]]
114 for (S i = 0; i < this->_count; i++) {
115 if ( this->_callback((this->_address)[i],
object)
126 if ((this->_address == 0) || (this->_count == 0)) {
128 }
else if (index >= this->_count) {
131 return this->_address[index];
141 for (S i = 0; i < this->_count; i++) {
142 if ( this->_callback(this->_address[i],
object)
159 for (S i = 0; i < this->_count; i++) {
160 std::cout << this->_address[i];
163 std::cout <<
"]" << std::endl;
170 this->_callback = callback;
177 this->_releasecb = callback;
185 this->_address = (T *) this->allocate(arr->
count());
186 this->_count = arr->
count();
187 memcpy(this->_address, arr->
address(), this->_count);
194 this->_address = this->reallocate(this->_address, this->_count + 1);
195 if (this->_address == NULL) {
201 this->_address[this->_count - 1] = obj;
206 this->_address = this->reallocate(this->_address, this->_count + 1);
207 if (this->_address == NULL) {
214 for (S i = this->_count - 1; i > index; i--) {
215 this->_address[i] = this->_address[i - 1];
218 this->_address[index] = obj;
230 for (S i = index; (i+1) < this->_count; i++) {
231 this->_address[i] = this->_address[i + 1];
236 this->_address = this->reallocate(this->_address, this->_count);
239 if (this->_count && (this->_address == NULL)) {
252 T *
address()
const {
return this->_address; }
259 static T * allocate(S size) {
260 return (T *) malloc(
sizeof(T) * size);
266 static T * reallocate(T * addr, S newsize) {
267 return (T *) realloc(addr,
sizeof(T) * newsize);
274 static void deallocate(T * value) {
275 free((
void *) value);
281 void saveArray(T * array, S size) {
283 this->_address = (T *) this->allocate(size);
286 if (this->_address) {
288 for (S i = 0; i < size; i++) {
289 this->_address[i] = array[i];
297 void saveArray(std::initializer_list<T> list) {
299 typename std::initializer_list<T>::iterator itr;
301 this->_count = list.size();
302 this->_address = (T *) this->allocate(this->_count);
304 if (this->_address) {
306 for (itr = list.begin(); itr != list.end(); ++itr) {
307 this->_address[i] = *itr;
326 int (* _callback) (T a, T b);
334 void (* _releasecb) (T obj);
343 this->saveArray(list);
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