collect1.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*_############################################################################
  2. _##
  3. _## collect1.h
  4. _##
  5. _## SNMP++v3.2.23
  6. _## -----------------------------------------------
  7. _## Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  8. _##
  9. _## This software is based on SNMP++2.6 from Hewlett Packard:
  10. _##
  11. _## Copyright (c) 1996
  12. _## Hewlett-Packard Company
  13. _##
  14. _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  15. _## Permission to use, copy, modify, distribute and/or sell this software
  16. _## and/or its documentation is hereby granted without fee. User agrees
  17. _## to display the above copyright notice and this license notice in all
  18. _## copies of the software and any documentation of the software. User
  19. _## agrees to assume all liability for the use of the software;
  20. _## Hewlett-Packard and Jochen Katz make no representations about the
  21. _## suitability of this software for any purpose. It is provided
  22. _## "AS-IS" without warranty of any kind, either express or implied. User
  23. _## hereby grants a royalty-free license to any and all derivatives based
  24. _## upon this software code base.
  25. _##
  26. _## Stuttgart, Germany, Sun Nov 11 15:10:59 CET 2007
  27. _##
  28. _##########################################################################*/
  29. // $Id: collect1.h 287 2007-03-22 22:37:09Z katz $
  30. #ifdef SNMP_PP_NAMESPACE
  31. namespace Snmp_pp {
  32. #endif
  33. template <class T> class SnmpCollection
  34. {
  35. class cBlock
  36. {
  37. public:
  38. cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
  39. T *item[MAXT];
  40. cBlock *prev;
  41. cBlock *next;
  42. };
  43. public:
  44. /**
  45. * Create an empty collection.
  46. */
  47. SnmpCollection()
  48. : count(0), data(0,0) {};
  49. /**
  50. * Create a collection using a single template object.
  51. */
  52. SnmpCollection(const T &t)
  53. : count(1), data(0, 0)
  54. {
  55. data.item[0] = (T*) (t.clone());
  56. };
  57. /**
  58. * Create a collection with another collection (copy constructor).
  59. */
  60. SnmpCollection(const SnmpCollection<T> &c)
  61. : count(0), data(0, 0)
  62. {
  63. if (c.count == 0) return;
  64. // load up the new collection
  65. cBlock *current = &data;
  66. cBlock *nextBlock;
  67. int cn = 0;
  68. while (count < c.count)
  69. {
  70. if (cn >= MAXT)
  71. {
  72. nextBlock = new cBlock(current, 0);
  73. current->next = nextBlock;
  74. current = nextBlock;
  75. cn=0;
  76. }
  77. T *tmp;
  78. c.get_element(tmp, count);
  79. current->item[cn] = (T*) (tmp->clone());
  80. count++;
  81. cn++;
  82. }
  83. };
  84. /**
  85. * Destroy the collection.
  86. */
  87. ~SnmpCollection()
  88. {
  89. clear(); // just delete the data
  90. };
  91. /**
  92. * Get the size of the collection.
  93. */
  94. int size() const
  95. {
  96. return count;
  97. };
  98. /**
  99. * Append an item to the collection.
  100. */
  101. SnmpCollection& operator +=(const T &i)
  102. {
  103. cBlock *current = &data;
  104. int cn = (int) count % MAXT;
  105. while (current->next)
  106. current = current->next;
  107. if ((count > 0) && ((count % MAXT) == 0))
  108. {
  109. cBlock *add = new cBlock(current, 0);
  110. if (!add) return *this;
  111. current->next = add;
  112. add->item[0] = (T*) (i.clone());
  113. }
  114. else
  115. {
  116. current->item[cn] = (T*) (i.clone());
  117. }
  118. count++;
  119. return *this;
  120. };
  121. /**
  122. * Assign one collection to another.
  123. */
  124. SnmpCollection &operator =(const SnmpCollection<T> &c)
  125. {
  126. if (this == &c) return *this; // check for self assignment
  127. clear(); // delete the data
  128. if (c.count == 0) return *this;
  129. // load up the new collection
  130. cBlock *current = &data;
  131. cBlock *nextBlock;
  132. int cn = 0;
  133. count = 0;
  134. while (count < c.count)
  135. {
  136. if (cn >= MAXT)
  137. {
  138. nextBlock = new cBlock(current, 0);
  139. current->next = nextBlock;
  140. current = nextBlock;
  141. cn=0;
  142. }
  143. T *tmp;
  144. c.get_element(tmp, count);
  145. current->item[cn] = (T*) (tmp->clone());
  146. count++;
  147. cn++;
  148. }
  149. return *this;
  150. };
  151. /**
  152. * Access an element in the collection.
  153. *
  154. * @return The requestet element or an empty element if out of bounds.
  155. */
  156. T operator[](const int p) const
  157. {
  158. if ((p < count) && (p >= 0))
  159. {
  160. cBlock const *current = &data;
  161. int bn = (int) (p / MAXT);
  162. int cn = (int) p % MAXT;
  163. for (int z=0; z<bn; z++)
  164. current = current->next;
  165. return *(current->item[cn]);
  166. }
  167. else
  168. {
  169. // return an instance of nothing!!
  170. T t;
  171. return t;
  172. }
  173. };
  174. /**
  175. * Set an element in the collection.
  176. *
  177. * @return 0 on success and -1 on failure.
  178. */
  179. int set_element( const T& i, const int p)
  180. {
  181. if ((p < 0) || (p > count)) return -1; // not found!
  182. cBlock *current = &data;
  183. int bn = (int) p / MAXT;
  184. int cn = (int) p % MAXT;
  185. for (int z=0; z<bn; z++)
  186. current = current->next;
  187. delete current->item[cn];
  188. current->item[cn] = (T*) (i.clone());
  189. return 0;
  190. };
  191. /**
  192. * Get an element in the collection.
  193. *
  194. * @return 0 on success and -1 on failure.
  195. */
  196. int get_element(T &t, const int p) const
  197. {
  198. if ((p < 0) || (p > count)) return -1; // not found!
  199. cBlock const *current = &data;
  200. int bn = (int) p / MAXT;
  201. int cn = (int) p % MAXT;
  202. for (int z=0; z<bn; z++)
  203. current = current->next;
  204. t = *(current->item[cn]);
  205. return 0;
  206. };
  207. /**
  208. * Get a pointer to an element in the collection.
  209. *
  210. * @return 0 on success and -1 on failure.
  211. */
  212. int get_element(T *&t, const int p) const
  213. {
  214. if ((p < 0) || (p > count)) return -1; // not found!
  215. cBlock const *current = &data;
  216. int bn = (int) p / MAXT;
  217. int cn = (int) p % MAXT;
  218. for (int z=0; z<bn; z++)
  219. current = current->next;
  220. t = current->item[cn];
  221. return 0;
  222. };
  223. /**
  224. * Apply an function to the entire collection, iterator.
  225. */
  226. void apply(void f(T&))
  227. {
  228. T temp;
  229. for ( int z=0; z<count; z++)
  230. {
  231. this->get_element(temp, z);
  232. f(temp);
  233. }
  234. };
  235. /**
  236. * Looks for an element in the collection.
  237. *
  238. * @return TRUE if found.
  239. */
  240. int find(const T& i, int &pos) const
  241. {
  242. T temp;
  243. for (int z=0; z<count; z++)
  244. {
  245. this->get_element(temp, z);
  246. if ( temp == i) {
  247. pos = z;
  248. return TRUE;
  249. }
  250. }
  251. return FALSE;
  252. };
  253. /**
  254. * Delete an element in the collection.
  255. */
  256. int remove(const T& i)
  257. {
  258. // first see if we have it
  259. int pos;
  260. if (find(i, pos))
  261. {
  262. SnmpCollection<T> newCollection;
  263. for (int z=0; z<count; z++)
  264. {
  265. if (z != pos)
  266. {
  267. T item;
  268. get_element(item, z);
  269. newCollection += item;
  270. }
  271. }
  272. // assign new collection to 'this'
  273. operator =(newCollection);
  274. return TRUE;
  275. }
  276. return FALSE; // not found thus not removed
  277. };
  278. /**
  279. * Delete all elements within the collection.
  280. */
  281. void clear()
  282. {
  283. if (count == 0) return;
  284. cBlock *current = &data;
  285. int z=0;
  286. int cn=0;
  287. while ( z< count)
  288. {
  289. if (cn >= MAXT)
  290. {
  291. cn =0;
  292. current = current->next;
  293. }
  294. delete current->item[cn];
  295. cn++;
  296. z++;
  297. }
  298. // delete the blocks
  299. while (current->next)
  300. current = current->next;
  301. while (current->prev)
  302. {
  303. current = current->prev;
  304. delete current->next;
  305. }
  306. count = 0;
  307. data.next=0;
  308. data.prev=0;
  309. };
  310. private:
  311. int count;
  312. cBlock data;
  313. };
  314. #ifdef SNMP_PP_NAMESPACE
  315. } // end of namespace Snmp_pp
  316. #endif