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
SLList.h
1 /*
2  * $Id$
3  * SLList 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_SLLIST_H
23 #define ELM_GENSTRUCT_SLLIST_H
24 
25 #include <elm/assert.h>
26 #include <elm/PreIterator.h>
27 #include <elm/util/Equiv.h>
28 #include <elm/inhstruct/SLList.h>
29 
30 namespace elm { namespace genstruct {
31 
32 // SLList class
33 template <class T, class E = Equiv<T> >
34 class SLList {
35 
36  // Node class
37  class Node: public inhstruct::SLNode {
38  public:
39  T val;
40  inline Node(const T value): val(value) { }
41  inline Node *next(void) const { return nextNode(); }
42  inline Node *nextNode(void) const { return static_cast<Node *>(SLNode::next()); }
43  inline void removeNext(void) { Node *n = nextNode(); SLNode::removeNext(); delete n; }
44  };
45 
46 public:
47  inline SLList(void) { }
48  inline SLList(const SLList& list) { copy(list); }
49  inline SLList& operator=(const SLList& list) { copy(list); return *this; }
50  inline ~SLList(void) { clear(); }
51 
52  void copy(const SLList<T>& list) {
53  clear(); Iterator item(list); if(!item) return; addFirst(*item); Node *cur = firstNode();
54  for(item++; item; item++) { cur->insertAfter(new Node(*item)); cur = cur->next(); }
55  }
56 
58 
59  // Collection concept
60  inline int count(void) const { return _list.count(); }
61  inline bool contains (const T &item) const
62  { for(Iterator iter(*this); iter; iter++) if(E::equals(item, iter)) return true; return false; }
63  inline bool isEmpty(void) const { return _list.isEmpty(); };
64  inline operator bool(void) const { return !isEmpty(); }
65 
66  // Iterator class
67  class Iterator: public PreIterator<Iterator, const T&> {
68  public:
69  inline Iterator(void): node(0), prev(0) { }
70  inline Iterator(const SLList& _list): node(_list.firstNode()), prev(0) { }
71  inline Iterator(const Iterator& source): node(source.node), prev(source.prev) { }
72  inline Iterator& operator=(const Iterator& i) { node = i.node; prev = i.prev; return *this; }
73 
74  inline bool ended(void) const { return !node; }
75  inline const T& item(void) const { ASSERT(node); return node->val; }
76  inline void next(void)
77  { ASSERT(node); prev = node; node = node->next(); }
78  private:
79  friend class SLList;
80  Node *node, *prev;
81  };
82 
83  // MutableIterator class
84  class MutableIterator: public PreIterator<Iterator, T&> {
85  public:
86  inline MutableIterator(void): node(0), prev(0) { }
87  inline MutableIterator(SLList& _list): node(_list.firstNode()), prev(0) { }
88  inline MutableIterator(const MutableIterator& source): node(source.node), prev(source.prev) { }
89  inline MutableIterator& operator=(const MutableIterator& i) { node = i.node; prev = i.prev; return *this; }
90 
91  inline bool ended(void) const { return !node; }
92  inline T& item(void) const { ASSERT(node); return node->val; }
93  inline void next(void)
94  { ASSERT(node); prev = node; node = node->next(); }
95  private:
96  friend class SLList;
97  Node *node, *prev;
98  };
99 
100  // MutableCollection concept
101  inline void clear(void)
102  { while(!_list.isEmpty()) { Node *node = firstNode(); _list.removeFirst(); delete node; } }
103  inline void add(const T& value) { addFirst(value); }
104  template <class C> inline void addAll(const C& items)
105  { for(typename C::Iterator iter(items); iter; iter++) add(iter); }
106  template <class C> inline void removeAll(const C& items)
107  { for(typename C::Iterator iter(items); iter; iter++) remove(iter); }
108 
109  void remove(const T& value) {
110  if(E::equals(first(), value)) removeFirst(); else
111  for(Node *prev = firstNode(), *cur = prev->nextNode(); cur; prev = cur, cur = cur->nextNode())
112  if(E::equals(cur->val, value)) { prev->removeNext(); return; }
113  }
114 
115  inline void remove(Iterator &iter) { remove(iter.prev, iter.node); }
116  inline void remove(MutableIterator &iter) { remove(iter.prev, iter.node); }
117 
118  // List concept
119  inline T& first(void) { return firstNode()->val; }
120  inline const T& first(void) const { return firstNode()->val; }
121  inline T& last(void) { return lastNode()->val; }
122  inline const T& last(void) const { return lastNode()->val; }
123  Iterator find(const T& item) const
124  { Iterator iter(*this); for(; iter; iter++) if(E::equals(item, iter)) break; return iter; }
125  Iterator find(const T& item, const Iterator& pos) const
126  { Iterator iter(pos); for(iter++; iter; iter++) if(E::equals(item, iter)) break; return iter; }
127 
128  // MutableList concept
129  inline void addFirst(const T& value) { _list.addFirst(new Node(value)); }
130  inline void addLast(const T& value) { _list.addLast(new Node(value)); }
131  inline void addAfter(const Iterator& pos, const T& value)
132  { ASSERT(pos.node); pos.node->insertAfter(new Node(value)); }
133  inline void addBefore(const Iterator& pos, const T& value)
134  { if(!pos.prev) addFirst(value); else pos.prev->insertAfter(new Node(value)); }
135  inline void removeFirst(void) { Node *node = firstNode(); _list.removeFirst(); delete node; }
136  inline void removeLast(void) { Node *node = lastNode(); _list.removeLast(); delete node; }
137  inline void set(const Iterator &pos, const T &item) { ASSERT(pos.node); pos.node->val = item; }
138 
139  // Stack concept
140  inline const T& top(void) const { return first(); }
141  inline T pop(void) { T r = first(); removeFirst(); return r; }
142  inline void push(const T& i) { addFirst(i); }
143  inline void reset(void) { clear(); }
144 
145  // operators
146  inline SLList<T>& operator+=(const T& h) { addFirst(h); return *this; }
147  inline SLList<T>& operator+=(const SLList<T>& l) { addAll(l); return *this; }
148 
149 private:
150  inhstruct::SLList _list;
151 
152  inline Node *firstNode(void) const { return static_cast<Node *>(_list.first()); }
153  inline Node *lastNode(void) const { return static_cast<Node *>(_list.last()); }
154  void remove(Node* prev, Node*& cur) {
155  ASSERT(cur);
156  if(!prev) { removeFirst(); cur = firstNode(); }
157  else { prev->removeNext(); cur = prev->next(); }
158  }
159 };
160 
161 template <class T, class E> SLList<T, E> SLList<T, E>::null;
162 
163 } } // elm::genstruct
164 
165 #endif // ELM_GENSTRUCT_SLLIST_H
void addAfter(const Iterator &pos, const T &value)
Definition: SLList.h:131
void addAll(const C &items)
Definition: SLList.h:104
Iterator & operator=(const Iterator &i)
Definition: SLList.h:72
void removeFirst(void)
Definition: SLList.h:67
void removeAll(const C &items)
Definition: SLList.h:106
void add(const T &value)
Definition: SLList.h:103
Definition: PreIterator.h:29
void reset(void)
Definition: SLList.h:143
T pop(void)
Definition: SLList.h:141
SLNode * last(void) const
Definition: inhstruct_SLList.cpp:59
const T & first(void) const
Definition: SLList.h:120
Definition: SLList.h:34
int count(void) const
Definition: inhstruct_SLList.cpp:74
Iterator find(const T &item, const Iterator &pos) const
Definition: SLList.h:125
bool ended(void) const
Definition: SLList.h:74
void next(void)
Definition: SLList.h:76
void removeFirst(void)
Definition: SLList.h:135
void addFirst(SLNode *node)
Definition: SLList.h:63
SLNode * first(void) const
Definition: SLList.h:60
Iterator(void)
Definition: SLList.h:69
SLList< T > & operator+=(const SLList< T > &l)
Definition: SLList.h:147
T & last(void)
Definition: SLList.h:121
void removeLast(void)
Definition: inhstruct_SLList.cpp:102
Definition: SLList.h:17
value_t value(CString name, int value)
Definition: rtti.h:40
void removeLast(void)
Definition: SLList.h:136
int count(void) const
Definition: SLList.h:60
const T & top(void) const
Definition: SLList.h:140
void addBefore(const Iterator &pos, const T &value)
Definition: SLList.h:133
SLList(void)
Definition: SLList.h:47
MutableIterator(SLList &_list)
Definition: SLList.h:87
Iterator(const SLList &_list)
Definition: SLList.h:70
SLList(const SLList &list)
Definition: SLList.h:48
~SLList(void)
Definition: SLList.h:50
bool isEmpty(void) const
Definition: SLList.h:63
bool ended(void) const
Definition: SLList.h:91
Definition: SLList.h:28
T & first(void)
Definition: SLList.h:119
bool contains(const T &item) const
Definition: SLList.h:61
MutableIterator & operator=(const MutableIterator &i)
Definition: SLList.h:89
SLList & operator=(const SLList &list)
Definition: SLList.h:49
T & item(void) const
Definition: SLList.h:92
Iterator(const Iterator &source)
Definition: SLList.h:71
Iterator find(const T &item) const
Definition: SLList.h:123
static SLList< T, E > null
Definition: SLList.h:57
const T & last(void) const
Definition: SLList.h:122
void addLast(SLNode *node)
Definition: inhstruct_SLList.cpp:90
void next(void)
Definition: SLList.h:93
MutableIterator(const MutableIterator &source)
Definition: SLList.h:88
void set(const Iterator &pos, const T &item)
Definition: SLList.h:137
void addFirst(const T &value)
Definition: SLList.h:129
void clear(void)
Definition: SLList.h:101
void addLast(const T &value)
Definition: SLList.h:130
const T & item(void) const
Definition: SLList.h:75
void copy(const SLList< T > &list)
Definition: SLList.h:52
SLList< T > & operator+=(const T &h)
Definition: SLList.h:146
void push(const T &i)
Definition: SLList.h:142
Definition: SLList.h:67
bool isEmpty(void) const
Definition: SLList.h:70
MutableIterator(void)
Definition: SLList.h:86