tabbing
[iotcloud.git] / version2 / src / C / array.h
index 274982212aac4190fe5fec2bb85a8833635a113b..e2e2a67f462c8659ff2272f98c4323c5acd01b33 100644 (file)
@@ -1,32 +1,36 @@
 #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;
@@ -37,26 +41,33 @@ class Array {
                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;
        }
@@ -64,9 +75,18 @@ class Array {
        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