OpenWareLaboratory
SimpleArray.h
Go to the documentation of this file.
1 #ifndef __SimpleArray_h__
2 #define __SimpleArray_h__
3 
4 #include <cstddef>
5 #include <string.h> /* for memcpy and memmov */
6 #include "message.h"
7 
13 template<typename T>
14 class SimpleArray {
15 protected:
16  T* data;
17  size_t size;
18 public:
19  SimpleArray() : data(NULL), size(0){}
20  SimpleArray(T* data, size_t size) : data(data), size(size){}
21  /* virtual ~SimpleArray(){} No virtual destructor to prevent adding a vtable to the object size */
22 
27  T* getData(){
28  return data;
29  }
30 
31  size_t getSize() const {
32  return size;
33  }
34 
35  bool isEmpty() const {
36  return size == 0;
37  }
38 
43  T getElement(size_t index){
44  return data[index];
45  }
46 
50  void setElement(size_t index, T value){
51  data[index] = value;
52  }
53 
61  bool equals(const SimpleArray<T>& other) const {
62  if(size != other.size)
63  return false;
64  for(size_t n=0; n<size; n++){
65  if(data[n] != other.data[n])
66  return false;
67  }
68  return true;
69  }
70 
76  void copyTo(SimpleArray<T> destination){
77  ASSERT(destination.size >= size, "Array too small");
78  memcpy((void*)destination.data, (void*)data, size*sizeof(T));
79  }
80 
86  void copyFrom(SimpleArray<T> source){
87  ASSERT(size >= source.size, "Array too small");
88  memcpy((void*)data, (void*)source.data, source.size*sizeof(T));
89  }
90 
99  void insert(SimpleArray<T> source, int destinationOffset, size_t len){
100  insert(source, 0, destinationOffset, len);
101  }
102 
111  void insert(SimpleArray<T> source, int sourceOffset, int destinationOffset, size_t len){
112  ASSERT(size >= destinationOffset+len, "Array too small");
113  ASSERT(source.size >= sourceOffset+len, "Array too small");
114  memcpy((void*)(data+destinationOffset), (void*)(source.data+sourceOffset), len*sizeof(T));
115  }
116 
124  void move(int fromIndex, int toIndex, size_t len){
125  ASSERT(size >= toIndex+len, "Array too small");
126  memmove((void*)(data+toIndex), (void*)(data+fromIndex), len*sizeof(T));
127  }
128 
134  static void copy(T* dst, T* src, size_t len){
135  size_t blocks = len >> 2u;
136  T a, b, c, d;
137  while(blocks--){
138  a = *src++;
139  b = *src++;
140  c = *src++;
141  d = *src++;
142  *dst++ = a;
143  *dst++ = b;
144  *dst++ = c;
145  *dst++ = d;
146  }
147  blocks = len & 0x3;
148  while(blocks--){
149  *dst++ = *src++;
150  }
151  }
152 
157  operator T*(){
158  return data;
159  }
160 
161  // /**
162  // * Allows to index the array using array-style brackets.
163  // * @param index the index of the element
164  // * @return the value of the **index** element of the array
165  // */
166  // T& operator [](size_t index){
167  // return data[index];
168  // }
169 
170  // /**
171  // * Allows to index the array using array-style brackets.
172  // * **const** version of operator[]
173  // */
174  // const T& operator [](size_t index) const {
175  // return data[index];
176  // }
177 
178 };
179 
180 
181 #endif // __SimpleArray_h__
SimpleArray holds a pointer to an array and the array size, and is designed to be passed by value.
Definition: SimpleArray.h:14
SimpleArray(T *data, size_t size)
Definition: SimpleArray.h:20
void copyFrom(SimpleArray< T > source)
Copies the content of another array into this array.
Definition: SimpleArray.h:86
size_t getSize() const
Definition: SimpleArray.h:31
bool equals(const SimpleArray< T > &other) const
Compares two arrays.
Definition: SimpleArray.h:61
void insert(SimpleArray< T > source, int destinationOffset, size_t len)
Copies the content of an array into a subset of the array.
Definition: SimpleArray.h:99
size_t size
Definition: SimpleArray.h:17
void copyTo(SimpleArray< T > destination)
Copies the content of this array to another array.
Definition: SimpleArray.h:76
void insert(SimpleArray< T > source, int sourceOffset, int destinationOffset, size_t len)
Copies the content of an array into a subset of the array.
Definition: SimpleArray.h:111
void setElement(size_t index, T value)
Set a single value in the array.
Definition: SimpleArray.h:50
bool isEmpty() const
Definition: SimpleArray.h:35
static void copy(T *dst, T *src, size_t len)
Optimised array copy for datatype T.
Definition: SimpleArray.h:134
T * getData()
Get the data stored in the Array.
Definition: SimpleArray.h:27
void move(int fromIndex, int toIndex, size_t len)
Copies values within an array.
Definition: SimpleArray.h:124
T getElement(size_t index)
Get a single value stored in the array.
Definition: SimpleArray.h:43
#define ASSERT(cond, msg)
Definition: message.h:16