BDS Public
Beam-lib  2.16.3
This is the Beam C++ class library.
BList_func.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BList_func.h BEAM List implementation based upong stdc++ lists
3  * T.Barnaby, BEAM Ltd, 4/8/00
4  * Copyright (c) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5  *******************************************************************************
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <memory.h>
11 
12 template <class T> BList<T>::BList() {
13  onodes = nodeCreate();
14  onodes->next = onodes;
15  onodes->prev = onodes;
16  olength = 0;
17 }
18 
19 template <class T> BList<T>::BList(const BList<T>& l) {
20  onodes = nodeCreate();
21  onodes->next = onodes;
22  onodes->prev = onodes;
23  olength = 0;
24  append((BList<T>&)l);
25 }
26 
27 template <class T> BList<T>::~BList() {
28  clear();
29  delete [] (char*)onodes;
30 }
31 
32 template <class T> void BList<T>::start(BIter& i) const {
33  i = onodes->next;
34 }
35 
36 template <class T> BIter BList<T>::begin() const {
37  return onodes->next;
38 }
39 
40 template <class T> BIter BList<T>::end() const {
41  return onodes->prev;
42 }
43 
44 template <class T> BIter BList<T>::end(BIter& i) const {
45  i = onodes->prev;
46  return onodes->prev;
47 }
48 
49 template <class T> void BList<T>::next(BIter& i) const {
50  BNode* n = i;
51 
52  if(n != onodes)
53  n = n->next;
54  i = n;
55 }
56 
57 template <class T> void BList<T>::prev(BIter& i) {
58  BNode* n = i;
59 
60  if(n != onodes)
61  n = n->prev;
62  i = n;
63 }
64 
65 template <class T> BIter BList<T>::goTo(int pos) const {
66  BIter i;
67  int c;
68 
69  for(i = begin(), c = 0; (c < pos) && !isEnd(i); next(i), c++);
70  return i;
71 }
72 
73 template <class T> int BList<T>::position(BIter i) {
74  BIter ii;
75  int c;
76 
77  for(ii = begin(), c = 0; !isEnd(ii); next(ii), c++){
78  if(ii == i)
79  return c;
80  }
81  return -1;
82 }
83 
84 template <class T> unsigned int BList<T>::number() const {
85  return olength;
86 }
87 
88 template <class T> unsigned int BList<T>::size() const {
89  return number();
90 }
91 
92 template <class T> int BList<T>::isStart(BIter& i) const {
93  Node* n = (Node*)(BNode*)i;
94 
95  return (n == onodes->next);
96 }
97 
98 template <class T> int BList<T>::isEnd(BIter& i) const {
99  Node* n = (Node*)(BNode*)i;
100 
101  return (n == onodes);
102 }
103 
104 template <class T> void BList<T>::clear(){
105  BIter i;
106 
107  for(start(i); !isEnd(i); )
108  del(i);
109 }
110 
111 template <class T> T& BList<T>::front(){
112  return get(begin());
113 }
114 
115 template <class T> T& BList<T>::rear(){
116  return get(end());
117 }
118 
119 template <class T> T& BList<T>::get(BIter i){
120  return nodeGet(i)->item;
121 }
122 
123 template <class T> const T& BList<T>::get(BIter i) const {
124  return nodeGet(i)->item;
125 }
126 
127 template <class T> void BList<T>::append(const T& item){
128  BIter i = end();
129 
130  insertAfter(i, item);
131 }
132 
133 template <class T> void BList<T>::insert(BIter& i, const T& item){
134  Node* c = (Node*)(BNode*)i;
135  Node* n = nodeCreate(item);
136 
137  n->next = c;
138  n->prev = c->prev;
139  c->prev->next = n;
140  c->prev = n;
141  olength++;
142  i = n;
143 }
144 
145 template <class T> void BList<T>::insertAfter(BIter& i, const T& item){
146  next(i);
147  insert(i, item);
148 }
149 
150 template <class T> void BList<T>::del(BIter& i){
151  Node* n = (Node*)(BNode*)i;
152 
153  if(olength){
154  i = n->next;
155  n->prev->next = n->next;
156  n->next->prev = n->prev;
157  delete n;
158  olength--;
159  }
160 }
161 
162 template <class T> void BList<T>::deleteLast(){
163  BIter i = end();
164 
165  del(i);
166 }
167 
168 template <class T> void BList<T>::deleteFirst(){
169  BIter i = begin();
170  del(i);
171 }
172 
173 template <class T> void BList<T>::push(const T& item){
174  append(item);
175 }
176 
177 template <class T> T BList<T>::pop(){
178  T t = rear();
179 
180  deleteLast();
181  return t;
182 }
183 
184 template <class T> void BList<T>::queueAdd(const T& i){
185  append(i);
186 }
187 
188 template <class T> T BList<T>::queueGet(){
189  T t = front();
190 
191  deleteFirst();
192  return t;
193 }
194 
195 template <class T> void BList<T>::append(const BList<T>& l){
196  BIter i;
197 
198  for(l.start(i); !l.isEnd(i); l.next(i)){
199  append(l.get(i));
200  }
201 }
202 
203 template <class T> int BList<T>::has(const T& v) const {
204  BIter i;
205 
206  for(start(i); !isEnd(i); next(i)){
207  if(get(i) == v)
208  return 1;
209  }
210  return 0;
211 }
212 
213 template <class T> void BList<T>::swap(BIter i1, BIter i2){
214  BNode* n1 = (BNode*)i1;
215  BNode* n2 = (BNode*)i2;
216  BNode* n1p = n1->prev;
217  BNode* n1n = n1->next;
218  BNode* n2p = n2->prev;
219  BNode* n2n = n2->next;
220 
221  if(n1->next == n2){
222  n1p->next = n2;
223  n2n->prev = n1;
224 
225  n1->prev = n2;
226  n2->prev = n1p;
227  n1->next = n2n;
228  n2->next = n1;
229  }
230  else if(n1->prev == n2){
231  n2p->next = n1;
232  n1n->prev = n2;
233 
234  n1->prev = n2p;
235  n2->prev = n1;
236  n1->next = n2;
237  n2->next = n1n;
238  }
239  else {
240  n1p->next = n2;
241  n1n->prev = n2;
242  n2p->next = n1;
243  n2n->prev = n1;
244 
245  n1->prev = n2p;
246  n2->prev = n1p;
247  n1->next = n2n;
248  n2->next = n1n;
249  }
250 }
251 
252 template <class T> void BList<T>::sort(){
253  BIter i;
254  BIter s;
255  int n = number();
256  int c;
257 
258  while(n){
259  for(c = n - 1, i = begin(); c > 0; c--, next(i)){
260  s = i;
261  next(s);
262  if(get(i) > get(s)){
263  swap(i, s);
264  i = s;
265  }
266  }
267  n--;
268  }
269 }
270 
271 template <class T> void BList<T>::sort(SortFunc func){
272  BIter i;
273  BIter s;
274  int n = number();
275  int c;
276 
277  while(n){
278  for(c = n - 1, i = begin(); c > 0; c--, next(i)){
279  s = i;
280  next(s);
281  if(func(get(i), get(s)) > 0){
282  swap(i, s);
283  i = s;
284  }
285  }
286  n--;
287  }
288 }
289 
290 template <class T> T& BList<T>::operator[](BIter i){
291  return get(i);
292 }
293 
294 template <class T> const T& BList<T>::operator[](const BIter& i) const {
295  return get(i);
296 }
297 
298 template <class T> T& BList<T>::operator[](int i){
299  BIter ii;
300 
301  if((ii = goTo(i))){
302  return get(ii);
303  }
304  else {
305  fprintf(stderr, "BList over range\n");
306  exit(1);
307  }
308 }
309 
310 template <class T> const T& BList<T>::operator[](int i) const {
311  BIter ii;
312 
313  if((ii = goTo(i))){
314  return get(ii);
315  }
316  else {
317  fprintf(stderr, "BList over range\n");
318  exit(1);
319  }
320 }
321 
322 template <class T> BList<T>& BList<T>::operator=(const BList<T>& l){
323  BIter i;
324 
325  if(this != &l){
326  clear();
327  for(l.start(i); !l.isEnd(i); l.next(i)){
328  append(l[i]);
329  }
330  }
331 
332  return *this;
333 }
334 
335 template <class T> BList<T> BList<T>::operator+(const BList<T>& l) const {
336  BList<T> rl = *this;
337  BIter i;
338 
339  for(l.start(i); !l.isEnd(i); l.next(i)){
340  rl.append(l[i]);
341  }
342  return rl;
343 }
344 
345 template <class T> typename BList<T>::Node* BList<T>::nodeGet(BIter i){
346  return (Node*)(BNode*)i;
347 }
348 
349 template <class T> const typename BList<T>::Node* BList<T>::nodeGet(BIter i) const{
350  return (Node*)(BNode*)i;
351 }
352 
353 template <class T> typename BList<T>::Node* BList<T>::nodeCreate(const T& item){
354  return new Node(item);
355 }
356 template <class T> typename BList<T>::Node* BList<T>::nodeCreate(){
357  Node* n;
358 
359  n = (Node*)new char [ sizeof(Node) ];
360  // WARNING: This is not ideal. However only used for the list start/end Node where the object is not valid anyway.
361  memset((void*)n, 0, sizeof(Node));
362  return n;
363 }
void queueAdd(const T &i)
Add item to end of list.
Definition: BList_func.h:184
BList< T > operator+(const BList< T > &l) const
Definition: BList_func.h:335
void prev(BIter &i)
Iterator for previous item in list.
Definition: BList_func.h:57
void sort()
Sort list based on get(i) values.
Definition: BList_func.h:252
T & rear()
Get last item in list.
Definition: BList_func.h:115
Iterator for BList.
Definition: BList.h:18
virtual void insert(BIter &i, const T &item)
Insert item before item.
Definition: BList_func.h:133
BNode * prev
Definition: BList.h:14
virtual void clear()
Clear the list.
Definition: BList_func.h:104
Template based list class.
Definition: BList.h:30
int isEnd(BIter &i) const
True if iterator refers to last item.
Definition: BList_func.h:98
T pop()
Pop item from list deleteing item.
Definition: BList_func.h:177
BIter end() const
Iterator for end of list.
Definition: BList_func.h:40
BIter goTo(int pos) const
Iterator for pos item in list.
Definition: BList_func.h:65
void deleteFirst()
Delete fisrt item.
Definition: BList_func.h:168
int isStart(BIter &i) const
True if iterator refers to first item.
Definition: BList_func.h:92
BList< T > & operator=(const BList< T > &l)
Definition: BList_func.h:322
void insertAfter(BIter &i, const T &item)
Insert item after item.
Definition: BList_func.h:145
unsigned int size() const
Number of items in list.
Definition: BList_func.h:88
Definition: BList.h:32
BIter begin() const
Iterator for start of list.
Definition: BList_func.h:36
void next(BIter &i) const
Iterator for next item in list.
Definition: BList_func.h:49
unsigned int number() const
Number of items in list.
Definition: BList_func.h:84
virtual Node * nodeGet(BIter i)
Definition: BList_func.h:345
BList()
Definition: BList_func.h:12
T & operator[](int i)
Definition: BList_func.h:298
void start(BIter &i) const
Iterator to start of list.
Definition: BList_func.h:32
void push(const T &i)
Push item onto list.
Definition: BList_func.h:173
void append(const T &item)
Append item to list.
Definition: BList_func.h:127
BNode * next
Definition: BList.h:13
BInt16 number
The error number.
Definition: BoapMc1.h:19
virtual ~BList()
Definition: BList_func.h:27
T & get(BIter i)
Get item specified by iterator in list.
Definition: BList_func.h:119
T queueGet()
Get item from front of list deleteing item.
Definition: BList_func.h:188
T & front()
Get first item in list.
Definition: BList_func.h:111
virtual void del(BIter &i)
Delete specified item.
Definition: BList_func.h:150
Definition: BList.h:10
void deleteLast()
Delete last item.
Definition: BList_func.h:162
int position(BIter i)
Postition in list item with iterator i.
Definition: BList_func.h:73
void swap(BIter i1, BIter i2)
Swap two items in list.
Definition: BList_func.h:213
int has(const T &i) const
Checks if the item is in the list.
Definition: BList_func.h:203