Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DLList.h
1 /*
2  * $Id$
3  * DLList class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_GENSTRUCT_DLLIST_H
23 #define ELM_GENSTRUCT_DLLIST_H
24 
25 #include <elm/util/Equiv.h>
26 #include <elm/PreIterator.h>
27 #include <elm/inhstruct/DLList.h>
28 
29 namespace elm { namespace genstruct {
30 
31 // DLList<T> class
32 template <class T, class E = Equiv<T> >
33 class DLList {
34 
35  // DLNode class
36  class DLNode: public inhstruct::DLNode {
37  T val;
38  public:
39  inline DLNode(const T value);
40  inline const T& value(void) const;
41  inline T& value(void) { return val; }
42  };
43 
44  inhstruct::DLList list;
45 public:
46  inline ~DLList(void);
47 
49  public:
50  inline AbstractIterator(const DLList<T>& _list, DLNode *_cur)
51  : list(&_list.list), cur(_cur) { }
52  inline AbstractIterator(const AbstractIterator& iter)
53  : list(iter.list), cur(iter.cur) { }
55  { list = iter.list; cur = iter.cur; return *this; }
56  protected:
58  DLNode *cur;
59  };
60 
61  // Collection concept
62  inline int count(void) const;
63  inline bool contains(const T& value) const;
64  inline bool isEmpty(void) const;
65  inline operator bool(void) const { return !isEmpty(); }
66 
67  class Iterator: public PreIterator<Iterator, const T&>, public AbstractIterator {
68  public:
69  inline Iterator(const DLList& _list)
70  : AbstractIterator(_list, (DLNode *)_list.list.first()) { }
71  inline Iterator(const AbstractIterator& iter)
72  : AbstractIterator(iter) { }
73  inline Iterator& operator=(const AbstractIterator& iter)
74  { AbstractIterator::operator=(iter); return *this; }
75 
76  inline bool ended(void) const { return this->cur->atEnd(); }
77  inline const T& item(void) const { return this->cur->value(); }
78  inline void next(void) { this->cur = (DLNode *)this->cur->next(); }
79  };
80 
81  // MutableCollection concept
82  inline void clear(void);
83  inline void add(const T& item) { addLast(item); }
84  template <template <class _> class C> inline void addAll(const C<T>& items);
85  inline void remove(const T& value);
86  template <template <class _> class C> inline void removeAll(const C<T>& items);
87  void remove(const AbstractIterator &iter);
88 
89  // List concept
90  inline const T& first(void) const;
91  inline const T& last(void) const;
92  inline Iterator find(const T& item) const {
93  Iterator iter(*this);
94  for(; iter; iter++) if(E::equals(item, iter)) break;
95  return iter;
96  }
97  inline Iterator find(const T& item, const Iterator& iter) const {
98  for(iter++; iter; iter++) if(E::equals(item, iter)) break;
99  return iter;
100  }
101 
102  // MutableList concept
103  inline void addFirst(const T& value);
104  inline void addLast(const T& value);
105  inline void removeFirst(void);
106  inline void removeLast(void);
107  inline void addAfter(const AbstractIterator &pos, const T& item)
108  { ASSERTP(!pos.cur->atEnd(), "insert after end");
109  pos.cur->insertAfter(new DLNode(item)); }
110  inline void addBefore(const AbstractIterator &pos, const T& item)
111  { ASSERTP(!pos.cur->atBegin(), "insert before begin");
112  pos.cur->insertBefore(new DLNode(item)); }
113  inline void set(const AbstractIterator &pos, const T& item)
114  { ASSERTP(!pos.cur->atBegin() && !pos.cur->atEnd(), "bad position");
115  pos.cur->val = item; }
116 
117  // BiDiList concept
118  class BackIterator: public PreIterator<Iterator, const T&>, public AbstractIterator {
119  public:
120  inline BackIterator(const DLList& _list)
121  : AbstractIterator(_list, (DLNode *)_list.list.last()) { }
122  inline BackIterator(const AbstractIterator& iter)
123  : AbstractIterator(iter) { }
124  inline BackIterator& operator=(const BackIterator& iter)
125  { AbstractIterator::operator=(iter); return *this; }
126 
127  inline bool ended(void) const { return this->cur->atBegin(); }
128  inline const T& item(void) const { return this->cur->value(); }
129  inline void next(void) { this->cur = (DLNode *)this->cur->previous(); }
130  };
131 
132  // Queue concept
133  inline const T &head(void) const { return first(); }
134  inline T get(void) { T res = first(); removeFirst(); return res; }
135  inline void put(const T &item) { addLast(item); }
136  inline void reset(void) { clear(); }
137 };
138 
139 
140 // DLList<T>::DLNode methods
141 template <class T, class E>
142 DLList<T, E>::DLNode::DLNode(const T value): val(value) { }
143 template <class T, class E>
144 const T& DLList<T, E>::DLNode::value(void) const { return val; }
145 
146 
147 // DLList<T> methods
148 template <class T, class E> inline DLList<T, E>::~DLList(void) { clear(); }
149 
150 template <class T, class E> const T& DLList<T, E>::first(void) const
151  { return ((DLNode *)list.first())->value(); }
152 
153 template <class T, class E> const T& DLList<T, E>::last(void) const
154  { return ((DLNode *)list.last())->value(); }
155 
156 template <class T, class E> bool DLList<T, E>::isEmpty(void) const
157  { return list.isEmpty(); }
158 
159 template <class T, class E> int DLList<T, E>::count(void) const {
160  return list.count();
161 }
162 template <class T, class E> void DLList<T, E>::addFirst(const T& value)
163  { list.addFirst(new DLNode(value)); }
164 
165 template <class T, class E> void DLList<T, E>::addLast(const T& value)
166  { list.addLast(new DLNode(value)); }
167 
168 template <class T, class E> void DLList<T, E>::removeFirst(void)
169  { list.removeFirst(); }
170 
171 template <class T, class E> void DLList<T, E>::removeLast(void)
172  { list.removeLast(); }
173 
174 template <class T, class E> void DLList<T, E>::remove(const T& value) {
175  for(DLNode *cur = (DLNode *)list.first(); !cur->atEnd(); cur = (DLNode *)cur->next())
176  if(E::equals(cur->value(), value)) {
177  cur->remove();
178  delete cur;
179  break;
180  }
181 }
182 
183 template <class T, class E> bool DLList<T, E>::contains(const T& value) const {
184  for(DLNode *cur = (DLNode *)list.first(); !cur->atEnd(); cur = (DLNode *)cur->next())
185  if(E::equals(cur->value(), value))
186  return true;
187  return false;
188 }
189 
190 template <class T, class E> void DLList<T, E>::clear(void) {
191  while(!list.isEmpty()) {
192  DLNode *node = (DLNode *)list.first();
193  list.removeFirst();
194  delete node;
195  }
196 }
197 
198 template <class T, class E> template <template <class _> class C>
199 inline void DLList<T, E>::addAll(const C<T>& items) {
200  for(typename C<T>::Iterator iter(items); iter; iter++)
201  add(iter);
202 }
203 
204 template <class T, class E> template <template <class _> class C>
205 inline void DLList<T, E>::removeAll(const C<T>& items) {
206  for(typename C<T>::Iterator iter(items); iter; iter++)
207  remove(iter);
208 }
209 
210 template <class T, class E>
212  { iter.cur.remove(); }
213 
214 } } // elm::genstruct
215 
216 
217 #endif // ELM_GENSTRUCT_DLLIST_H
void next(void)
Definition: DLList.h:78
void set(const AbstractIterator &pos, const T &item)
Definition: DLList.h:113
const inhstruct::DLList * list
Definition: DLList.h:57
const T & item(void) const
Definition: DLList.h:128
BackIterator(const DLList &_list)
Definition: DLList.h:120
Definition: PreIterator.h:29
Iterator & operator=(const AbstractIterator &iter)
Definition: DLList.h:73
AbstractIterator & operator=(const AbstractIterator &iter)
Definition: DLList.h:54
void next(void)
Definition: DLList.h:129
void remove(const T &value)
Definition: DLList.h:174
Iterator find(const T &item, const Iterator &iter) const
Definition: DLList.h:97
Iterator(const AbstractIterator &iter)
Definition: DLList.h:71
void removeAll(const C< T > &items)
Definition: DLList.h:205
AbstractIterator(const DLList< T > &_list, DLNode *_cur)
Definition: DLList.h:50
Definition: DLList.h:16
void addLast(const T &value)
Definition: DLList.h:165
void removeFirst(void)
Definition: DLList.h:168
bool ended(void) const
Definition: DLList.h:127
DLNode * cur
Definition: DLList.h:58
Iterator(const DLList &_list)
Definition: DLList.h:69
const T & item(void) const
Definition: DLList.h:77
BackIterator(const AbstractIterator &iter)
Definition: DLList.h:122
value_t value(CString name, int value)
Definition: rtti.h:40
void put(const T &item)
Definition: DLList.h:135
void addFirst(const T &value)
Definition: DLList.h:162
const T & last(void) const
Definition: DLList.h:153
const T & first(void) const
Definition: DLList.h:150
Definition: DLList.h:67
void removeLast(void)
Definition: DLList.h:171
int count(void) const
Definition: DLList.h:159
AbstractIterator(const AbstractIterator &iter)
Definition: DLList.h:52
void addAfter(const AbstractIterator &pos, const T &item)
Definition: DLList.h:107
~DLList(void)
Definition: DLList.h:148
void clear(T *target, int size)
Definition: array.h:65
bool contains(const T &value) const
Definition: DLList.h:183
void addAll(const C< T > &items)
Definition: DLList.h:199
Definition: DLList.h:33
void add(const T &item)
Definition: DLList.h:83
Definition: DLList.h:35
void clear(void)
Definition: DLList.h:190
Definition: DLList.h:118
bool ended(void) const
Definition: DLList.h:76
bool isEmpty(void) const
Definition: DLList.h:156
BackIterator & operator=(const BackIterator &iter)
Definition: DLList.h:124
void addBefore(const AbstractIterator &pos, const T &item)
Definition: DLList.h:110
void reset(void)
Definition: DLList.h:136
Iterator find(const T &item) const
Definition: DLList.h:92
const T & head(void) const
Definition: DLList.h:133