Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ptrlist.cpp
Go to the documentation of this file.
1 /*
2  * ptrlist.cpp - pointer list template class implementation
3  *
4  * Copyright (C) 2005, 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 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "ptrlist.h"
35 
36 namespace qucs {
37 
38 // Constructor creates an unnamed instance of the ptrlist class.
39 template <class type_t>
41  size = 0;
42  root = NULL;
43 }
44 
45 /* This copy constructor creates a instance of the ptrlist class based
46  on the given ptrlist. */
47 template <class type_t>
49  ptrentry<type_t> * ptr;
50  size = 0;
51  root = NULL;
52  for (ptr = p.root; ptr != NULL; ptr = ptr->next) append (ptr->data);
53 }
54 
55 // Destructor deletes a ptrlist object.
56 template <class type_t>
58  ptrentry<type_t> * next;
59  while (root) {
60  next = root->next;
61  delete root;
62  size--;
63  root = next;
64  }
65 }
66 
67 // Puts a new entry at the beginning of the pointer list.
68 template <class type_t>
69 void ptrlist<type_t>::add (type_t * ptr) {
70  ptrentry<type_t> * entry = new ptrentry<type_t> ();
71  if (root) root->prev = entry;
72  entry->data = ptr;
73  entry->next = root;
74  entry->prev = NULL;
75  root = entry;
76  size++;
77 }
78 
79 // Appends a new entry at the end of the pointer list.
80 template <class type_t>
81 void ptrlist<type_t>::append (type_t * ptr) {
82  ptrentry<type_t> * entry = new ptrentry<type_t> ();
83  entry->data = ptr;
84  entry->next = NULL;
85  if (root) {
86  ptrentry<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  size++;
96 }
97 
98 // Returns the size of the pointer list.
99 template <class type_t>
101  return size;
102 }
103 
104 // Removes any occurrence of the given pointer from the pointer list.
105 template <class type_t>
106 void ptrlist<type_t>::del (type_t * ptr) {
107  ptrentry<type_t> * next = NULL;
108  for (ptrentry<type_t> * p = root; p != NULL; p = next) {
109  next = p->next;
110  if (p->data == ptr) {
111  if (p == root) {
112  root = p->next;
113  if (root) root->prev = NULL;
114  }
115  else {
116  p->prev->next = p->next;
117  if (p->next) p->next->prev = p->prev;
118  }
119  delete p;
120  size--;
121  }
122  }
123 }
124 
125 // Returns the number of occurrences of the given pointer in the list.
126 template <class type_t>
127 int ptrlist<type_t>::contains (type_t * ptr) {
128  int count = 0;
129  for (ptrentry<type_t> * p = root; p != NULL; p = p->next) {
130  if (p->data == ptr) count++;
131  }
132  return count;
133 }
134 
135 // Returns the first position of the given pointer in the list.
136 template <class type_t>
137 int ptrlist<type_t>::index (type_t * ptr) {
138  int idx = -1;
139  for (ptrentry<type_t> * p = root; p != NULL; p = p->next, idx++) {
140  if (p->data == ptr) break;
141  }
142  return idx;
143 }
144 
145 // Returns the pointer at the given position.
146 template <class type_t>
147 type_t * ptrlist<type_t>::get (int idx) {
148  ptrentry<type_t> * ptr = root;
149  for (int i = 0 ; i < idx && ptr != NULL; ptr = ptr->next, i++) ;
150  return ptr ? ptr->data : NULL;
151 }
152 
153 // Constructor for pointer list iterator.
154 template <class type_t>
156  _ptrlist = &p;
157  toLast ();
158  toFirst ();
159 }
160 
161 // Default constructor for pointer list iterator.
162 template <class type_t>
164  _ptrlist = NULL;
165  _first = _last = _current = NULL;
166 }
167 
168 // Destructor for pointer 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 _ptrlist->size;
177 }
178 
179 // Sets the current to the first item in the iterator list.
180 template <class type_t>
182  _current = _first = _ptrlist->root;
183  return _current ? _current->data : NULL;
184 }
185 
186 // Sets the current to the last item in the iterator list.
187 template <class type_t>
189  for (_last = _ptrlist->root; _last && _last->next; _last = _last->next) ;
190  _current = _last;
191  return _current ? _current->data : NULL;
192 }
193 
194 // Makes the succeeding item current and returns the new current item.
195 template <class type_t>
197  _current = _current->next;
198  return _current ? _current->data : NULL;
199 }
200 
201 // Makes the preceding item current and returns the new current item.
202 template <class type_t>
204  _current = _current->prev;
205  return _current ? _current->data : NULL;
206 }
207 
208 // Returns the current iterator item.
209 template <class type_t>
211  return _current ? _current->data : NULL;
212 }
213 
214 // Returns the first iterator item.
215 template <class type_t>
217  return _first ? _first->data : NULL;
218 }
219 
220 // Returns the last iterator item.
221 template <class type_t>
223  return _last ? _last->data : NULL;
224 }
225 
226 } // namespace qucs
type_t * current(void)
Definition: ptrlist.cpp:210
append($3)
type_t * first(void)
Definition: ptrlist.cpp:216
size
Definition: parse_vcd.y:203
type_t * operator++(void)
Definition: ptrlist.cpp:196
void add(type_t *)
Definition: ptrlist.cpp:69
type_t * get(int)
Definition: ptrlist.cpp:147
int index(type_t *)
Definition: ptrlist.cpp:137
i
Definition: parse_mdl.y:516
next
Definition: parse_spice.y:859
int contains(type_t *)
Definition: ptrlist.cpp:127
ptrentry< type_t > * root
Definition: ptrlist.h:69
type_t * toLast(void)
Definition: ptrlist.cpp:188
void append(type_t *)
Definition: ptrlist.cpp:81
void del(type_t *)
Definition: ptrlist.cpp:106
int length(void)
Definition: ptrlist.cpp:100
type_t * operator--(void)
Definition: ptrlist.cpp:203
type_t * last(void)
Definition: ptrlist.cpp:222
type_t * toFirst(void)
Definition: ptrlist.cpp:181