My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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: ptrlist.cpp 1825 2011-03-11 20:42:14Z ela $
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 // Constructor creates an unnamed instance of the ptrlist class.
37 template <class type_t>
39  size = 0;
40  root = NULL;
41 }
42 
43 /* This copy constructor creates a instance of the ptrlist class based
44  on the given ptrlist. */
45 template <class type_t>
47  ptrentry<type_t> * ptr;
48  size = 0;
49  root = NULL;
50  for (ptr = p.root; ptr != NULL; ptr = ptr->next) append (ptr->data);
51 }
52 
53 // Destructor deletes a ptrlist object.
54 template <class type_t>
56  ptrentry<type_t> * next;
57  while (root) {
58  next = root->next;
59  delete root;
60  size--;
61  root = next;
62  }
63 }
64 
65 // Puts a new entry at the beginning of the pointer list.
66 template <class type_t>
67 void ptrlist<type_t>::add (type_t * ptr) {
68  ptrentry<type_t> * entry = new ptrentry<type_t> ();
69  if (root) root->prev = entry;
70  entry->data = ptr;
71  entry->next = root;
72  entry->prev = NULL;
73  root = entry;
74  size++;
75 }
76 
77 // Appends a new entry at the end of the pointer list.
78 template <class type_t>
79 void ptrlist<type_t>::append (type_t * ptr) {
80  ptrentry<type_t> * entry = new ptrentry<type_t> ();
81  entry->data = ptr;
82  entry->next = NULL;
83  if (root) {
84  ptrentry<type_t> * p;
85  for (p = root; p->next != NULL; p = p->next) ;
86  p->next = entry;
87  entry->prev = p;
88  }
89  else {
90  root = entry;
91  entry->prev = NULL;
92  }
93  size++;
94 }
95 
96 // Returns the size of the pointer list.
97 template <class type_t>
99  return size;
100 }
101 
102 // Removes any occurrence of the given pointer from the pointer list.
103 template <class type_t>
104 void ptrlist<type_t>::del (type_t * ptr) {
105  ptrentry<type_t> * next = NULL;
106  for (ptrentry<type_t> * p = root; p != NULL; p = next) {
107  next = p->next;
108  if (p->data == ptr) {
109  if (p == root) {
110  root = p->next;
111  if (root) root->prev = NULL;
112  }
113  else {
114  p->prev->next = p->next;
115  if (p->next) p->next->prev = p->prev;
116  }
117  delete p;
118  size--;
119  }
120  }
121 }
122 
123 // Returns the number of occurrences of the given pointer in the list.
124 template <class type_t>
125 int ptrlist<type_t>::contains (type_t * ptr) {
126  int count = 0;
127  for (ptrentry<type_t> * p = root; p != NULL; p = p->next) {
128  if (p->data == ptr) count++;
129  }
130  return count;
131 }
132 
133 // Returns the first position of the given pointer in the list.
134 template <class type_t>
135 int ptrlist<type_t>::index (type_t * ptr) {
136  int idx = -1;
137  for (ptrentry<type_t> * p = root; p != NULL; p = p->next, idx++) {
138  if (p->data == ptr) break;
139  }
140  return idx;
141 }
142 
143 // Returns the pointer at the given position.
144 template <class type_t>
145 type_t * ptrlist<type_t>::get (int idx) {
146  ptrentry<type_t> * ptr = root;
147  for (int i = 0 ; i < idx && ptr != NULL; ptr = ptr->next, i++) ;
148  return ptr ? ptr->data : NULL;
149 }
150 
151 // Constructor for pointer list iterator.
152 template <class type_t>
154  _ptrlist = &p;
155  toLast ();
156  toFirst ();
157 }
158 
159 // Default constructor for pointer list iterator.
160 template <class type_t>
162  _ptrlist = NULL;
163  _first = _last = _current = NULL;
164 }
165 
166 // Destructor for pointer list iterator.
167 template <class type_t>
169 }
170 
171 // Returns number of items this iterator operates on.
172 template <class type_t>
174  return _ptrlist->size;
175 }
176 
177 // Sets the current to the first item in the iterator list.
178 template <class type_t>
180  _current = _first = _ptrlist->root;
181  return _current ? _current->data : NULL;
182 }
183 
184 // Sets the current to the last item in the iterator list.
185 template <class type_t>
187  for (_last = _ptrlist->root; _last && _last->next; _last = _last->next) ;
188  _current = _last;
189  return _current ? _current->data : NULL;
190 }
191 
192 // Makes the succeeding item current and returns the new current item.
193 template <class type_t>
195  _current = _current->next;
196  return _current ? _current->data : NULL;
197 }
198 
199 // Makes the preceding item current and returns the new current item.
200 template <class type_t>
202  _current = _current->prev;
203  return _current ? _current->data : NULL;
204 }
205 
206 // Returns the current iterator item.
207 template <class type_t>
209  return _current ? _current->data : NULL;
210 }
211 
212 // Returns the first iterator item.
213 template <class type_t>
215  return _first ? _first->data : NULL;
216 }
217 
218 // Returns the last iterator item.
219 template <class type_t>
221  return _last ? _last->data : NULL;
222 }