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
00030 #ifdef SNMP_PP_NAMESPACE
00031 namespace Snmp_pp {
00032 #endif
00033
00034 template <class T> class SnmpCollection
00035 {
00036 class cBlock
00037 {
00038 public:
00039 cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
00040 T *item[MAXT];
00041 cBlock *prev;
00042 cBlock *next;
00043 };
00044
00045 public:
00046
00047
00048
00049
00050 SnmpCollection()
00051 : count(0), data(0,0) {};
00052
00053
00054
00055
00056 SnmpCollection(const T &t)
00057 : count(1), data(0, 0)
00058 {
00059 data.item[0] = (T*) (t.clone());
00060 };
00061
00062
00063
00064
00065 SnmpCollection(const SnmpCollection<T> &c)
00066 : count(0), data(0, 0)
00067 {
00068 if (c.count == 0) return;
00069
00070
00071 cBlock *current = &data;
00072 cBlock *nextBlock;
00073 int cn = 0;
00074
00075 while (count < c.count)
00076 {
00077 if (cn >= MAXT)
00078 {
00079 nextBlock = new cBlock(current, 0);
00080 current->next = nextBlock;
00081 current = nextBlock;
00082 cn=0;
00083 }
00084 T *tmp;
00085 c.get_element(tmp, count);
00086 current->item[cn] = (T*) (tmp->clone());
00087 count++;
00088 cn++;
00089 }
00090 };
00091
00092
00093
00094
00095 ~SnmpCollection()
00096 {
00097 clear();
00098 };
00099
00100
00101
00102
00103 int size() const
00104 {
00105 return count;
00106 };
00107
00108
00109
00110
00111 SnmpCollection& operator +=(const T &i)
00112 {
00113 cBlock *current = &data;
00114 int cn = (int) count % MAXT;
00115 while (current->next)
00116 current = current->next;
00117 if ((count > 0) && ((count % MAXT) == 0))
00118 {
00119 cBlock *add = new cBlock(current, 0);
00120 if (!add) return *this;
00121 current->next = add;
00122 add->item[0] = (T*) (i.clone());
00123 }
00124 else
00125 {
00126 current->item[cn] = (T*) (i.clone());
00127 }
00128 count++;
00129
00130 return *this;
00131 };
00132
00133
00134
00135
00136 SnmpCollection &operator =(const SnmpCollection<T> &c)
00137 {
00138 if (this == &c) return *this;
00139
00140 clear();
00141
00142 if (c.count == 0) return *this;
00143
00144
00145 cBlock *current = &data;
00146 cBlock *nextBlock;
00147 int cn = 0;
00148 count = 0;
00149 while (count < c.count)
00150 {
00151 if (cn >= MAXT)
00152 {
00153 nextBlock = new cBlock(current, 0);
00154 current->next = nextBlock;
00155 current = nextBlock;
00156 cn=0;
00157 }
00158 T *tmp;
00159 c.get_element(tmp, count);
00160 current->item[cn] = (T*) (tmp->clone());
00161 count++;
00162 cn++;
00163 }
00164
00165 return *this;
00166 };
00167
00168
00169
00170
00171
00172
00173 T operator[](const int p) const
00174 {
00175 if ((p < count) && (p >= 0))
00176 {
00177 cBlock const *current = &data;
00178 int bn = (int) (p / MAXT);
00179 int cn = (int) p % MAXT;
00180 for (int z=0; z<bn; z++)
00181 current = current->next;
00182 return *(current->item[cn]);
00183 }
00184 else
00185 {
00186
00187 T t;
00188 return t;
00189 }
00190 };
00191
00192
00193
00194
00195
00196
00197 int set_element( const T& i, const int p)
00198 {
00199 if ((p < 0) || (p > count)) return -1;
00200
00201 cBlock *current = &data;
00202 int bn = (int) p / MAXT;
00203 int cn = (int) p % MAXT;
00204 for (int z=0; z<bn; z++)
00205 current = current->next;
00206 delete current->item[cn];
00207 current->item[cn] = (T*) (i.clone());
00208 return 0;
00209 };
00210
00211
00212
00213
00214
00215
00216 int get_element(T &t, const int p) const
00217 {
00218 if ((p < 0) || (p > count)) return -1;
00219
00220 cBlock const *current = &data;
00221 int bn = (int) p / MAXT;
00222 int cn = (int) p % MAXT;
00223 for (int z=0; z<bn; z++)
00224 current = current->next;
00225 t = *(current->item[cn]);
00226 return 0;
00227 };
00228
00229
00230
00231
00232
00233
00234 int get_element(T *&t, const int p) const
00235 {
00236 if ((p < 0) || (p > count)) return -1;
00237
00238 cBlock const *current = &data;
00239 int bn = (int) p / MAXT;
00240 int cn = (int) p % MAXT;
00241 for (int z=0; z<bn; z++)
00242 current = current->next;
00243 t = current->item[cn];
00244 return 0;
00245 };
00246
00247
00248
00249
00250 void apply(void f(T&))
00251 {
00252 T temp;
00253 for ( int z=0; z<count; z++)
00254 {
00255 this->get_element(temp, z);
00256 f(temp);
00257 }
00258 };
00259
00260
00261
00262
00263
00264
00265 int find(const T& i, int &pos) const
00266 {
00267 T temp;
00268 for (int z=0; z<count; z++)
00269 {
00270 this->get_element(temp, z);
00271 if ( temp == i) {
00272 pos = z;
00273 return TRUE;
00274 }
00275 }
00276 return FALSE;
00277 };
00278
00279
00280
00281
00282 int remove(const T& i)
00283 {
00284
00285 int pos;
00286 if (find(i, pos))
00287 {
00288 SnmpCollection<T> newCollection;
00289
00290 for (int z=0; z<count; z++)
00291 {
00292 if (z != pos)
00293 {
00294 T item;
00295 get_element(item, z);
00296 newCollection += item;
00297 }
00298 }
00299
00300
00301 operator =(newCollection);
00302
00303 return TRUE;
00304 }
00305 return FALSE;
00306 };
00307
00308
00309
00310
00311 void clear()
00312 {
00313 if (count == 0) return;
00314
00315 cBlock *current = &data;
00316 int z=0;
00317 int cn=0;
00318 while ( z< count)
00319 {
00320 if (cn >= MAXT)
00321 {
00322 cn =0;
00323 current = current->next;
00324 }
00325 delete current->item[cn];
00326 cn++;
00327 z++;
00328 }
00329
00330
00331 while (current->next)
00332 current = current->next;
00333 while (current->prev)
00334 {
00335 current = current->prev;
00336 delete current->next;
00337 }
00338
00339 count = 0;
00340 data.next=0;
00341 data.prev=0;
00342 };
00343
00344 private:
00345 int count;
00346 cBlock data;
00347 };
00348
00349 #ifdef SNMP_PP_NAMESPACE
00350 };
00351 #endif