00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifndef __ZMMF_BASE_ARRAY_H__
00033 #define __ZMMF_BASE_ARRAY_H__
00034
00035 #include "zmm/zmm.h"
00036 #include "memory.h"
00037
00038 #define DEFAULT_ARRAY_CAPACITY 16
00039
00040 namespace zmm
00041 {
00042
00043 template <class T>
00044 class BaseArray : public Object
00045 {
00046 public:
00047 BaseArray() : Object()
00048 {
00049 _init(DEFAULT_ARRAY_CAPACITY);
00050 }
00051
00052 BaseArray(int capacity) : Object()
00053 {
00054 _init(capacity);
00055 }
00056
00057 void _init(int capacity)
00058 {
00059 this->capacity = capacity;
00060 siz = 0;
00061 arr = (T *)MALLOC(capacity * sizeof(T));
00062 }
00063
00064 ~BaseArray()
00065 {
00066 if (arr)
00067 FREE(arr);
00068 }
00069
00070 void append(T el)
00071 {
00072 resize(siz+1);
00073 arr[siz++] = el;
00074 }
00075
00076 void set(T el, int index)
00077 {
00078 arr[index] = el;
00079 }
00080
00081 T get(int index)
00082 {
00083 return arr[index];
00084 }
00085
00086 void remove(int index, int count=1)
00087 {
00088 if (index < 0 || index >= siz)
00089 return;
00090 int max = index + count;
00091 if (max > siz)
00092 max = siz;
00093 if (max <= index)
00094 return;
00095 int move = siz - max;
00096 if (move)
00097 {
00098 memmove(
00099 (void *)(arr + index),
00100 (void *)(arr + index + count),
00101 move * sizeof(T)
00102 );
00103 }
00104 siz -= count;
00105 }
00106
00107 void removeUnordered(int index)
00108 {
00109 if (index < 0 || index >= siz)
00110 return;
00111 arr[index] = arr[--siz];
00112 }
00113
00114 void insert(int index, T el)
00115 {
00116 resize(siz + 1);
00117 memmove(
00118 (void *)(arr + (index + 1)),
00119 (void *)(arr + index),
00120 (siz - index) * sizeof(T)
00121 );
00122 arr[index] = el;
00123 siz++;
00124 }
00125
00126 int size()
00127 {
00128 return siz;
00129 }
00130
00131 void resize(int requiredSize)
00132 {
00133 if(requiredSize > capacity)
00134 {
00135 int newCapacity = siz + (siz / 2);
00136 if(requiredSize > newCapacity)
00137 newCapacity = requiredSize;
00138 capacity = newCapacity;
00139 arr = (T *)REALLOC(arr, capacity * sizeof(T));
00140 }
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 protected:
00157 T* arr;
00158 int siz;
00159 int capacity;
00160 };
00161
00162 class IntArray : public BaseArray<int>
00163 {
00164 public:
00165 String toCSV(char sep = ',')
00166 {
00167 Ref<StringBuffer> buf(new StringBuffer());
00168 for (int i = 0; i < siz; i++)
00169 *buf << sep << get(i);
00170 if (buf->length() <= 0)
00171 return _("");
00172 return buf->toString(1);
00173 }
00174
00175 void addCSV(String csv, char sep = ',')
00176 {
00177 char *data = csv.c_str();
00178 char *dataEnd = data + csv.length();
00179 while (data < dataEnd)
00180 {
00181 char *endptr;
00182 int val = (int)strtol(data, &endptr, 10);
00183 if (endptr == data)
00184 throw _Exception(_("illegal csv given to IntArray"));
00185 append(val);
00186 if (endptr >= dataEnd)
00187 break;
00188 if (*endptr == sep)
00189 data = endptr + 1;
00190 else
00191 throw _Exception(_("illegal csv given to IntArray"));
00192 }
00193 }
00194 };
00195
00196 }
00197
00198 #endif // __ZMMF_BASE_ARRAY_H__