00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __Xabsl2Array_h_
00013 #define __Xabsl2Array_h_
00014
00015 #include <stdlib.h>
00016 #include <string.h>
00017
00018
00019
00020
00021
00022
00023 class Xabsl2NamedItem
00024 {
00025 public:
00026
00027
00028
00029
00030 Xabsl2NamedItem(const char* name)
00031 {
00032 n = new char[strlen(name)+1];
00033 strcpy(n,name);
00034 }
00035
00036
00037 ~Xabsl2NamedItem() { delete[] n; }
00038
00039
00040 char* n;
00041 };
00042
00043
00044
00045
00046
00047
00048
00049
00050 template<class T> class Xabsl2ArrayElement : public Xabsl2NamedItem
00051 {
00052 public:
00053
00054
00055
00056
00057
00058 Xabsl2ArrayElement(const char* name, T element)
00059 : Xabsl2NamedItem(name), e(element) {}
00060
00061
00062
00063
00064
00065 ~Xabsl2ArrayElement() {}
00066
00067
00068 T e;
00069 };
00070
00071
00072
00073
00074
00075
00076
00077
00078 template <class T> class Xabsl2Array
00079 {
00080 public:
00081
00082 Xabsl2Array()
00083 {
00084 usedSize = 0;
00085 allocatedSize = 2;
00086 data = new Xabsl2ArrayElement<T>*[allocatedSize];
00087 }
00088
00089
00090 ~Xabsl2Array()
00091 {
00092 for(int i = 0; i < getSize(); ++i)
00093 delete data[i];
00094 delete[] data;
00095 }
00096
00097
00098 void clear()
00099 {
00100 for(int i = 0; i < getSize(); ++i)
00101 delete data[i];
00102 delete[] data;
00103 usedSize = 0;
00104 allocatedSize = 2;
00105 data = new Xabsl2ArrayElement<T>*[allocatedSize];
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 T getElement(const char* name, T defaultValue) const
00116 {
00117 int pos = find(name);
00118 if(pos < 0)
00119 return defaultValue;
00120 else
00121 return getElement(pos);
00122 }
00123
00124
00125
00126
00127
00128
00129 T getElement(const char* name) const
00130 {
00131 return getElement(find(name));
00132 }
00133
00134
00135
00136
00137
00138
00139 T getElement(int pos) const
00140 {
00141 return data[pos]->e;
00142 }
00143
00144
00145
00146
00147
00148
00149 Xabsl2ArrayElement<T>* getPElement(const char* name)
00150 {
00151 return data[find(name)];
00152 }
00153
00154
00155 const char* getName(int pos) const
00156 {
00157 return data[pos]->n;
00158 }
00159
00160
00161
00162
00163
00164
00165 void append(const char* name, T element)
00166 {
00167 if(usedSize == allocatedSize)
00168 {
00169 allocatedSize += allocatedSize / 2;
00170 Xabsl2ArrayElement<T>** temp = new Xabsl2ArrayElement<T>*[allocatedSize];
00171 for(int i = 0; i < getSize(); ++i)
00172 temp[i] = data[i];
00173 delete[] data;
00174 data = temp;
00175 }
00176 data[usedSize++] = new Xabsl2ArrayElement<T>(name,element);
00177 }
00178
00179
00180
00181
00182
00183
00184
00185 void setElement(const char* name, T value)
00186 {
00187 setElement(find(name),value);
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 void setElement(int pos, T value)
00197 {
00198 data[pos]->e = value;
00199 }
00200
00201
00202
00203
00204
00205 int getSize() const {return usedSize;}
00206
00207
00208
00209
00210
00211
00212 T operator[](int pos) const
00213 {
00214 return getElement(pos);
00215 }
00216
00217
00218 bool exists(const char* name) const
00219 {
00220 return find(name) >= 0;
00221 }
00222
00223 protected:
00224
00225
00226
00227
00228
00229 int find(const char* name) const
00230 {
00231 for(int i = 0; i < getSize(); ++i)
00232 if(!strcmp(getName(i),name))
00233 return i;
00234 return -1;
00235 }
00236
00237
00238 Xabsl2ArrayElement<T>** data;
00239
00240
00241 int usedSize, allocatedSize;
00242 };
00243
00244
00245
00246
00247 #endif // __Xabsl2Array_h_
00248
00249
00250
00251
00252
00253