X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=blobdiff_plain;f=version2%2Fsrc%2FC%2Farray.h;h=e2e2a67f462c8659ff2272f98c4323c5acd01b33;hp=274982212aac4190fe5fec2bb85a8833635a113b;hb=2d567b75be4055f6a40ffe1cedb5cdc9be262d86;hpb=f0a95f7a96e808259ced5e9f61e029773c7a85e6;ds=sidebyside diff --git a/version2/src/C/array.h b/version2/src/C/array.h index 2749822..e2e2a67 100644 --- a/version2/src/C/array.h +++ b/version2/src/C/array.h @@ -1,32 +1,36 @@ #ifndef ARRAY_H #define ARRAY_H +#include +#include "common.h" + +typedef uint32_t uint; template 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 *_array) : - array((type *) ourmalloc(sizeof(type) * _array->size)), + Array(Array *_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; @@ -37,26 +41,33 @@ class Array { size = _size; memcpy(array, _array, _size * sizeof(type)); } - + void init(Array *_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 *_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; } @@ -64,9 +75,18 @@ class Array { type *internalArray() { return array; } - - private: + +private: type *array; uint size; }; + +template +void System_arraycopy(Array *src, int32_t srcPos, Array *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