PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/project_plan/tools/qucs-code/qucs-core/src/valuelist.cpp

https://bitbucket.org/JonSRomero/arduino_motorcycle_gauges
C++ | 237 lines | 164 code | 26 blank | 47 comment | 27 complexity | 88e936f592eedc83b969670bf107b8f0 MD5 | raw file
  1. /*
  2. * valuelist.cpp - value list template class implementation
  3. *
  4. * Copyright (C) 2006 Stefan Jahn <stefan@lkcc.org>
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this package; see the file COPYING. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. *
  21. * $Id: valuelist.cpp 1825 2011-03-11 20:42:14Z ela $
  22. *
  23. */
  24. #if HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "valuelist.h"
  32. // Constructor creates an instance of the valuelist class.
  33. template <class type_t>
  34. valuelist<type_t>::valuelist () {
  35. size = 0;
  36. last = root = NULL;
  37. }
  38. /* This copy constructor creates a instance of the valuelist class based
  39. on the given valuelist. */
  40. template <class type_t>
  41. valuelist<type_t>::valuelist (const valuelist<type_t> & p) {
  42. valentry<type_t> * ptr;
  43. size = 0;
  44. last = root = NULL;
  45. for (ptr = p.root; ptr != NULL; ptr = ptr->next)
  46. append (ptr->key, new type_t (*(ptr->value)));
  47. }
  48. // Destructor deletes a valuelist object.
  49. template <class type_t>
  50. valuelist<type_t>::~valuelist () {
  51. clear ();
  52. }
  53. // Resets the value list.
  54. template <class type_t>
  55. void valuelist<type_t>::clear (void) {
  56. valentry<type_t> * next;
  57. while (root) {
  58. next = root->next;
  59. delete root;
  60. root = next;
  61. }
  62. size = 0;
  63. last = NULL;
  64. }
  65. // Puts a new entry at the beginning of the value list.
  66. template <class type_t>
  67. void valuelist<type_t>::add (const char * n, type_t * ptr) {
  68. valentry<type_t> * entry = new valentry<type_t> ();
  69. if (root) root->prev = entry;
  70. entry->key = strdup (n);
  71. entry->value = ptr;
  72. entry->next = root;
  73. entry->prev = NULL;
  74. root = entry;
  75. if (!last) last = root;
  76. size++;
  77. }
  78. // Appends a new entry at the end of the value list.
  79. template <class type_t>
  80. void valuelist<type_t>::append (char * n, type_t * ptr) {
  81. valentry<type_t> * entry = new valentry<type_t> ();
  82. entry->key = strdup (n);
  83. entry->value = ptr;
  84. entry->next = NULL;
  85. if (root) {
  86. valentry<type_t> * p;
  87. for (p = root; p->next != NULL; p = p->next) ;
  88. p->next = entry;
  89. entry->prev = p;
  90. }
  91. else {
  92. root = entry;
  93. entry->prev = NULL;
  94. }
  95. last = entry;
  96. size++;
  97. }
  98. // Appends another value list at the end of the value list.
  99. template <class type_t>
  100. void valuelist<type_t>::append (valuelist<type_t> * p) {
  101. for (valentry<type_t> * ptr = p->root; ptr != NULL; ptr = ptr->next)
  102. append (ptr->key, new type_t (*(ptr->value)));
  103. }
  104. // Returns the size of the value list.
  105. template <class type_t>
  106. int valuelist<type_t>::length (void) {
  107. return size;
  108. }
  109. // Removes any occurrence of the given key from the value list.
  110. template <class type_t>
  111. void valuelist<type_t>::del (char * n) {
  112. valentry<type_t> * next = NULL;
  113. for (valentry<type_t> * p = root; p != NULL; p = next) {
  114. next = p->next;
  115. if (!strcmp (p->key, n)) {
  116. if (p == root) {
  117. root = p->next;
  118. if (root) root->prev = NULL;
  119. }
  120. else {
  121. p->prev->next = p->next;
  122. if (p->next) p->next->prev = p->prev;
  123. }
  124. if (p == last) last = p->prev;
  125. delete p;
  126. size--;
  127. }
  128. }
  129. }
  130. // Returns the number of occurrences of the given key in the list.
  131. template <class type_t>
  132. int valuelist<type_t>::contains (char * n) {
  133. int count = 0;
  134. for (valentry<type_t> * p = root; p != NULL; p = p->next) {
  135. if (!strcmp (p->key, n)) count++;
  136. }
  137. return count;
  138. }
  139. // Returns the value associated with the given key.
  140. template <class type_t>
  141. type_t * valuelist<type_t>::get (const char * n) {
  142. for (valentry<type_t> * p = root; p != NULL; p = p->next) {
  143. if (!strcmp (p->key, n)) return p->value;
  144. }
  145. return NULL;
  146. }
  147. // Constructor for value list iterator.
  148. template <class type_t>
  149. valuelistiterator<type_t>::valuelistiterator (valuelist<type_t> & v) {
  150. _valuelist = &v;
  151. toLast ();
  152. toFirst ();
  153. }
  154. // Destructor for value list iterator.
  155. template <class type_t>
  156. valuelistiterator<type_t>::~valuelistiterator () {
  157. }
  158. // Returns number of items this iterator operates on.
  159. template <class type_t>
  160. int valuelistiterator<type_t>::count (void) {
  161. return _valuelist->size;
  162. }
  163. // Sets the current to the first item in the iterator list.
  164. template <class type_t>
  165. char * valuelistiterator<type_t>::toFirst (void) {
  166. _current = _first = _valuelist->root;
  167. return _current ? _current->key : NULL;
  168. }
  169. // Sets the current to the last item in the iterator list.
  170. template <class type_t>
  171. char * valuelistiterator<type_t>::toLast (void) {
  172. _current = _last = _valuelist->last;
  173. return _current ? _current->key : NULL;
  174. }
  175. // Makes the succeeding item current and returns the new current item.
  176. template <class type_t>
  177. char * valuelistiterator<type_t>::operator++ (void) {
  178. _current = _current->next;
  179. return _current ? _current->key : NULL;
  180. }
  181. // Makes the preceding item current and returns the new current item.
  182. template <class type_t>
  183. char * valuelistiterator<type_t>::operator-- (void) {
  184. _current = _current->prev;
  185. return _current ? _current->key : NULL;
  186. }
  187. // Returns the current iterator item.
  188. template <class type_t>
  189. char * valuelistiterator<type_t>::current (void) {
  190. return _current ? _current->key : NULL;
  191. }
  192. // Returns the current iterator items key.
  193. template <class type_t>
  194. char * valuelistiterator<type_t>::currentKey (void) {
  195. return current ();
  196. }
  197. // Returns the current iterator items value.
  198. template <class type_t>
  199. type_t * valuelistiterator<type_t>::currentVal (void) {
  200. return _current ? _current->value : NULL;
  201. }
  202. // Returns the first iterator item.
  203. template <class type_t>
  204. char * valuelistiterator<type_t>::first (void) {
  205. return _first ? _first->key : NULL;
  206. }
  207. // Returns the last iterator item.
  208. template <class type_t>
  209. char * valuelistiterator<type_t>::last (void) {
  210. return _last ? _last->key : NULL;
  211. }