Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valuelist.cpp
Go to the documentation of this 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$
22  *
23  */
24 
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "valuelist.h"
31 
32 namespace qucs {
33 
34 // Constructor creates an instance of the valuelist class.
35 template <class type_t>
37  size = 0;
38  last = root = NULL;
39 }
40 
41 /* This copy constructor creates a instance of the valuelist class based
42  on the given valuelist. */
43 template <class type_t>
45  valentry<type_t> * ptr;
46  size = 0;
47  last = root = NULL;
48  for (ptr = p.root; ptr != NULL; ptr = ptr->next)
49  append (ptr->key, new type_t (*(ptr->value)));
50 }
51 
52 // Destructor deletes a valuelist object.
53 template <class type_t>
55  clear ();
56 }
57 
58 // Resets the value list.
59 template <class type_t>
62  while (root) {
63  next = root->next;
64  delete root;
65  root = next;
66  }
67  size = 0;
68  last = NULL;
69 }
70 
71 // Puts a new entry at the beginning of the value list.
72 template <class type_t>
73 void valuelist<type_t>::add (const char * n, type_t * ptr) {
74  valentry<type_t> * entry = new valentry<type_t> ();
75  if (root) root->prev = entry;
76  entry->key = strdup (n);
77  entry->value = ptr;
78  entry->next = root;
79  entry->prev = NULL;
80  root = entry;
81  if (!last) last = root;
82  size++;
83 }
84 
85 // Appends a new entry at the end of the value list.
86 template <class type_t>
87 void valuelist<type_t>::append (char * n, type_t * ptr) {
88  valentry<type_t> * entry = new valentry<type_t> ();
89  entry->key = strdup (n);
90  entry->value = ptr;
91  entry->next = NULL;
92  if (root) {
93  valentry<type_t> * p;
94  for (p = root; p->next != NULL; p = p->next) ;
95  p->next = entry;
96  entry->prev = p;
97  }
98  else {
99  root = entry;
100  entry->prev = NULL;
101  }
102  last = entry;
103  size++;
104 }
105 
106 // Appends another value list at the end of the value list.
107 template <class type_t>
109  for (valentry<type_t> * ptr = p->root; ptr != NULL; ptr = ptr->next)
110  append (ptr->key, new type_t (*(ptr->value)));
111 }
112 
113 // Returns the size of the value list.
114 template <class type_t>
116  return size;
117 }
118 
119 // Removes any occurrence of the given key from the value list.
120 template <class type_t>
121 void valuelist<type_t>::del (char * n) {
122  valentry<type_t> * next = NULL;
123  for (valentry<type_t> * p = root; p != NULL; p = next) {
124  next = p->next;
125  if (!strcmp (p->key, n)) {
126  if (p == root) {
127  root = p->next;
128  if (root) root->prev = NULL;
129  }
130  else {
131  p->prev->next = p->next;
132  if (p->next) p->next->prev = p->prev;
133  }
134  if (p == last) last = p->prev;
135  delete p;
136  size--;
137  }
138  }
139 }
140 
141 // Returns the number of occurrences of the given key in the list.
142 template <class type_t>
144  int count = 0;
145  for (valentry<type_t> * p = root; p != NULL; p = p->next) {
146  if (!strcmp (p->key, n)) count++;
147  }
148  return count;
149 }
150 
151 // Returns the value associated with the given key.
152 template <class type_t>
153 type_t * valuelist<type_t>::get (const char * n) {
154  for (valentry<type_t> * p = root; p != NULL; p = p->next) {
155  if (!strcmp (p->key, n)) return p->value;
156  }
157  return NULL;
158 }
159 
160 // Constructor for value list iterator.
161 template <class type_t>
163  _valuelist = &v;
164  toLast ();
165  toFirst ();
166 }
167 
168 // Destructor for value list iterator.
169 template <class type_t>
171 }
172 
173 // Returns number of items this iterator operates on.
174 template <class type_t>
176  return _valuelist->size;
177 }
178 
179 // Sets the current to the first item in the iterator list.
180 template <class type_t>
182  _current = _first = _valuelist->root;
183  return _current ? _current->key : NULL;
184 }
185 
186 // Sets the current to the last item in the iterator list.
187 template <class type_t>
189  _current = _last = _valuelist->last;
190  return _current ? _current->key : NULL;
191 }
192 
193 // Makes the succeeding item current and returns the new current item.
194 template <class type_t>
196  _current = _current->next;
197  return _current ? _current->key : NULL;
198 }
199 
200 // Makes the preceding item current and returns the new current item.
201 template <class type_t>
203  _current = _current->prev;
204  return _current ? _current->key : NULL;
205 }
206 
207 // Returns the current iterator item.
208 template <class type_t>
210  return _current ? _current->key : NULL;
211 }
212 
213 // Returns the current iterator items key.
214 template <class type_t>
216  return current ();
217 }
218 
219 // Returns the current iterator items value.
220 template <class type_t>
222  return _current ? _current->value : NULL;
223 }
224 
225 // Returns the first iterator item.
226 template <class type_t>
228  return _first ? _first->key : NULL;
229 }
230 
231 // Returns the last iterator item.
232 template <class type_t>
234  return _last ? _last->key : NULL;
235 }
236 
237 } // namespace qucs
append($3)
size
Definition: parse_vcd.y:203
int contains(char *)
Definition: valuelist.cpp:143
char * key
Definition: valuelist.h:43
type_t * currentVal(void)
Definition: valuelist.cpp:221
n
Definition: parse_citi.y:147
valentry * next
Definition: valuelist.h:45
valuelistiterator(valuelist< type_t > &)
Definition: valuelist.cpp:162
next
Definition: parse_spice.y:859
valentry< type_t > * root
Definition: valuelist.h:70
valentry * prev
Definition: valuelist.h:46
void add(const char *, type_t *)
Definition: valuelist.cpp:73
void append(char *, type_t *)
Definition: valuelist.cpp:87
char * operator--(void)
Definition: valuelist.cpp:202
void clear(void)
Definition: valuelist.cpp:60
type_t * get(const char *)
Definition: valuelist.cpp:153
char * operator++(void)
Definition: valuelist.cpp:195
v
Definition: parse_zvr.y:141
char * currentKey(void)
Definition: valuelist.cpp:215
int length(void)
Definition: valuelist.cpp:115
void del(char *)
Definition: valuelist.cpp:121
type_t * value
Definition: valuelist.h:44