#ifndef ARRAY_H
#define ARRAY_H
+#include <inttypes.h>
+#include "common.h"
+
+typedef uint32_t uint;
template<typename type>
class Array {
- public:
- Array() :
- array(NULL),
- size(0) {
- }
-
- Array(uint _size) :
- array((type *) ourcalloc(1, sizeof(type) * _size)),
+public:
+ Array() :
+ array(NULL),
+ size(0) {
+ }
+
+ Array(uint32_t _size) :
+ array((type *) ourcalloc(1, sizeof(type) * _size)),
size(_size)
- {
- }
-
- Array(type *_array, uint _size) :
- array((type *) ourmalloc(sizeof(type) * _size)),
+ {
+ }
+
+ Array(type *_array, uint _size) :
+ array((type *) ourmalloc(sizeof(type) * _size)),
size(_size) {
- memcpy(array, _array, _size * sizeof(type));
- }
+ memcpy(array, _array, _size * sizeof(type));
+ }
- Array(Array<type> *_array) :
- array((type *) ourmalloc(sizeof(type) * _array->size)),
+ Array(Array<type> *_array) :
+ array((type *) ourmalloc(sizeof(type) * _array->size)),
size(_array->size) {
memcpy(array, _array->array, size * sizeof(type));
}
-
+
void init(uint _size) {
array = (type *) ourcalloc(1, sizeof(type) * _size);
size = _size;
size = _size;
memcpy(array, _array, _size * sizeof(type));
}
-
+
void init(Array<type> *_array) {
array = (type *) ourmalloc(sizeof(type) * _array->size);
size = _array->size;
memcpy(array, _array->array, size * sizeof(type));
}
-
+
~Array() {
if (array)
ourfree(array);
}
-
+
+ bool equals(Array<type> *_array) {
+ if (_array->size != size)
+ return false;
+ int cmp = memcmp(array, _array->array, size * sizeof(type));
+ return cmp == 0;
+ }
+
type get(uint index) const {
return array[index];
}
-
+
void set(uint index, type item) {
array[index] = item;
}
-
+
uint length() const {
return size;
}
type *internalArray() {
return array;
}
-
- private:
+
+private:
type *array;
uint size;
};
+
+template<typename type>
+void System_arraycopy(Array<type> *src, int32_t srcPos, Array<type> *dst, int32_t dstPos, int32_t len) {
+ if (srcPos + len > src->length() ||
+ dstPos + len > dst->length())
+ ASSERT(0);
+ uint bytesToCopy = len * sizeof(type);
+ memcpy(&dst->internalArray()[dstPos], &src->internalArray()[srcPos], bytesToCopy);
+}
#endif