Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
BiDiList.h
1 /*
2  * BiDiList class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2016, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef INCLUDE_ELM_DATA_BIDILIST_H_
22 #define INCLUDE_ELM_DATA_BIDILIST_H_
23 
24 #include <elm/assert.h>
25 #include <elm/inhstruct/DLList.h>
26 #include <elm/PreIterator.h>
27 #include <elm/data/Manager.h>
28 #include "custom.h"
29 
30 namespace elm {
31 
32 template <class T, class E = Equiv<T>, class A = DefaultAlloc>
33 class BiDiList: public E, public A {
34 
35  class Node: public inhstruct::DLNode {
36  public:
37  T val;
38  inline Node(const T& v): val(v) { }
39  inline static void *operator new(size_t s, BiDiList<T, E, A> *l) { return l->A::allocate(s); }
40  inline void free(BiDiList<T, E, A> *l) { this->~Node(); l->A::free(this); }
41  private:
42  inline ~Node(void) { }
43  };
44 
45 public:
46 
47  inline BiDiList(void) { }
48  inline BiDiList(const BiDiList<T>& list) { copy(list); }
49  inline ~BiDiList(void) { clear(); }
50  inline const E& equivalence() const { return *this; }
51  inline E& equivalence() { return *this; }
52  inline const A& allocator() const { return *this; }
53  inline A& allocator() { return *this; }
54 
55  class BackIter;
56  class Iter: public InplacePreIterator<Iter, T> {
57  friend class BiDiList;
58  public:
59  inline Iter(void): n(_(null._list.first())) { }
60  inline Iter(const BiDiList& l): n(_(l._list.first())) { }
61  inline Iter(const BackIter& i) { if(i) n = i; else n = i.n->next(); }
62  inline Iter(const BiDiList& l, bool end): n(_(end ? l._list.tail() : l._list.first())) { }
63  inline bool ended(void) const { return n->atEnd(); }
64  inline const T& item(void) const { return n->val; }
65  inline void next(void) { n = _(n->next()); }
66  inline bool equals(const Iter& i) const { return i.n == n; }
67  private:
68  Node *n;
69  };
70 
71  class BackIter: public InplacePreIterator<BackIter, T> {
72  friend class BiDiList;
73  public:
74  inline BackIter(const BiDiList& l): n(_(l._list.last())) { }
75  inline BackIter(const Iter& i) { if(i) n = i.n; else n = _(i.n->previous()); }
76  inline bool ended(void) const { return n->atBegin()(); }
77  inline const T& item(void) const { return n->val; }
78  inline void next(void) { n = _(n->previous()); }
79  private:
80  Node *n;
81  };
82  inline Iter reversedItems(void) const { return BackIter(*this); }
83 
84  // Collection concept
85  inline int count(void) const { return _list.count(); }
86  inline bool contains(const T &item) const
87  { for(Iter iter(*this); iter(); iter++) if(E::isEqual(item, *iter)) return true; return false; }
88  template <class C> bool containsAll(const C& c) const
89  { for(const auto x: c) if(!contains(x)) return false; return true; }
90  inline bool isEmpty(void) const { return _list.isEmpty(); };
91  inline operator bool(void) const { return !isEmpty(); }
92  inline Iter begin(void) const { return Iter(*this); }
93  inline Iter end(void) const { return Iter(*this, true); }
94  bool equals(const BiDiList<T>& l) const
95  { Iter i1(*this), i2(l); while(i1() && i2()) { if(!E::isEqual(*i1, *i2)) return false; i1++; i2++; } return !i1 && !i2; }
96  inline bool operator==(const BiDiList<T>& l) const { return equals(l); }
97  inline bool operator!=(const BiDiList<T>& l) const { return !equals(l); }
98  static const BiDiList<T, E, A> null;
99 
100  // MutableCollection concept
101  inline void clear(void)
102  { while(!_list.isEmpty()) { Node *node = _(_list.first()); _list.removeFirst(); node->free(this); } }
103  inline void add(const T& value) { addFirst(value); }
104  template <class C> inline void addAll(const C& c)
105  { for(const auto x: c) add(x); }
106  inline void remove(const T& v) { Iter i = find(v); if(i()) remove(i); }
107  template <class C> inline void removeAll(const C& c)
108  { for(const auto x: c) remove(x); }
109  inline void remove(const Iter &i) { ASSERT(i()); _(i.n)->remove(); }
110  inline void remove(Iter &i) { ASSERT(i()); Node *n = _(i.n->next()); ((Node *)i.n)->remove(); i.n = n; }
111  inline BiDiList<T>& operator+=(const T& h) { add(h); return *this; }
112  inline BiDiList<T>& operator+=(const BiDiList<T>& l) { addAll(l); return *this; }
113  inline BiDiList<T>& operator-=(const T& h) { remove(h); return *this; }
114  inline BiDiList<T>& operator-=(const BiDiList<T>& l) { removeAll(l); return *this; }
115  void copy(const BiDiList<T>& l) { clear(); for(Iter i(l); i(); i++) addLast(*i); }
116  inline BiDiList& operator=(const BiDiList& list) { copy(list); return *this; }
117 
118  // List concept
119  inline const T& first(void) const { return _(_list.first())->val; }
120  inline const T& last(void) const { return _(_list.last())->val; }
121  Iter find(const T& item) const
122  { Iter i; for(i = begin(); i(); i++) if(E::isEqual(item, *i)) break; return i; }
123  Iter find(const T& item, const Iter& pos) const
124  { Iter i = pos; for(i++; i(); i++) if(E::isEqual(item, *i)) break; return i; }
125  inline Iter nth(int n) const { Iter i(*this); while(n) { ASSERT(i); i++; n--; } ASSERT(i); return i; };
126  inline const T& operator[](int k) const { return *nth(k); }
127 
128  // MutableList concept
129  inline T& first(void) { return _(_list.first())->val; }
130  inline T& last(void) { return _(_list.first())->val; }
131  inline void addFirst(const T& v) { _list.addFirst(new(this) Node(v)); }
132  inline void addLast(const T& v) { _list.addLast(new(this) Node(v)); }
133  inline void addAfter(const Iter& i, const T& value) { ASSERT(i); i.n->insertAfter(new(this) Node(value)); }
134  inline void addBefore(const Iter& i, const T& v) { ASSERT(i); i.n->insertBefore(new(this) Node(v)); }
135  inline void removeFirst() { Node *node = _(_list.first()); _list.removeFirst(); node->free(this); }
136  inline void removeLast() { Node *node = _(_list.last()); _list.removeLast(); node->free(this); }
137  void removeBefore(const Iter& i) { Node *n = _(i.n->previous()); i.n->removePrevious(); n->free(this); }
138  void removeAfter(const Iter& i) { Node *n = _(i.n->next()); i.n->removeNext(); n->free(this); }
139  inline void set(const Iter &i, const T &v) { ASSERT(i); i.n->val = v; }
140  inline T& operator[](int k) { return nth(k).n->val; }
141 
142  // Stack concept
143  inline const T& top() const { return first(); }
144  inline T& top() { return first(); }
145  inline T pop() { T r = first(); removeFirst(); return r; }
146  inline void push(const T& i) { addFirst(i); }
147  inline void reset(void) { clear(); }
148 
149  // Queue concept
150  inline const T &head() const { return first(); }
151  const T get(void) { ASSERT(!isEmpty()); T r = first(); removeFirst(); return r; }
152  void put(const T &v) { addLast(v); }
153 
154  // deprecated
155  inline Iter operator*(void) const { return begin(); }
156  inline operator Iter(void) const { return begin(); }
157 
158 private:
159  inhstruct::DLList _list;
160  static inline Node *_(inhstruct::DLNode *n) { return static_cast<Node *>(n); }
161 };
162 
163 template <class T, class E, class A> const BiDiList<T, E, A> BiDiList<T, E, A>::null;
164 
165 } // elm
166 
167 #endif /* INCLUDE_ELM_DATA_BIDILIST_H_ */
elm::inhstruct::DLList::first
DLNode * first(void) const
Definition: DLList.h:93
elm::BiDiList::operator!=
bool operator!=(const BiDiList< T > &l) const
Definition: BiDiList.h:97
elm::BiDiList::BackIter::BackIter
BackIter(const Iter &i)
Definition: BiDiList.h:75
elm::iter
Definition: util_WAHVector.cpp:157
elm::BiDiList::Iter::ended
bool ended(void) const
Definition: BiDiList.h:63
elm::BiDiList::BiDiList
BiDiList(const BiDiList< T > &list)
Definition: BiDiList.h:48
elm::BiDiList::addBefore
void addBefore(const Iter &i, const T &v)
Definition: BiDiList.h:134
elm::BiDiList::BackIter::item
const T & item(void) const
Definition: BiDiList.h:77
elm::BiDiList::remove
void remove(const Iter &i)
Definition: BiDiList.h:109
elm::BiDiList
Definition: BiDiList.h:33
elm::BiDiList::count
int count(void) const
Definition: BiDiList.h:85
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::inhstruct::DLList::addFirst
void addFirst(DLNode *node)
Definition: DLList.h:106
elm::BiDiList::Iter::Iter
Iter(const BiDiList &l, bool end)
Definition: BiDiList.h:62
elm::inhstruct::DLList::count
int count(void) const
Definition: DLList.h:99
elm::BiDiList::addLast
void addLast(const T &v)
Definition: BiDiList.h:132
elm::BiDiList::remove
void remove(Iter &i)
Definition: BiDiList.h:110
elm::BiDiList::operator+=
BiDiList< T > & operator+=(const BiDiList< T > &l)
Definition: BiDiList.h:112
elm::BiDiList::equivalence
const E & equivalence() const
Definition: BiDiList.h:50
elm::BiDiList::BackIter::next
void next(void)
Definition: BiDiList.h:78
elm::BiDiList::removeAfter
void removeAfter(const Iter &i)
Definition: BiDiList.h:138
elm::BiDiList::equivalence
E & equivalence()
Definition: BiDiList.h:51
elm::BiDiList::top
const T & top() const
Definition: BiDiList.h:143
elm::BiDiList::operator[]
T & operator[](int k)
Definition: BiDiList.h:140
elm::BiDiList::operator-=
BiDiList< T > & operator-=(const T &h)
Definition: BiDiList.h:113
elm::BiDiList::first
T & first(void)
Definition: BiDiList.h:129
elm::BiDiList::set
void set(const Iter &i, const T &v)
Definition: BiDiList.h:139
elm::BiDiList::BackIter
Definition: BiDiList.h:71
elm::BiDiList::top
T & top()
Definition: BiDiList.h:144
elm::BiDiList::addFirst
void addFirst(const T &v)
Definition: BiDiList.h:131
elm::BiDiList::nth
Iter nth(int n) const
Definition: BiDiList.h:125
elm::BiDiList::Iter::Iter
Iter(void)
Definition: BiDiList.h:59
elm::BiDiList::remove
void remove(const T &v)
Definition: BiDiList.h:106
elm::BiDiList::contains
bool contains(const T &item) const
Definition: BiDiList.h:86
elm::inhstruct::DLList::removeFirst
void removeFirst(void)
Definition: DLList.h:110
value
elm::BiDiList::Iter
Definition: BiDiList.h:56
elm::BiDiList::first
const T & first(void) const
Definition: BiDiList.h:119
bool
elm::BiDiList::find
Iter find(const T &item) const
Definition: BiDiList.h:121
elm::BiDiList::Iter::item
const T & item(void) const
Definition: BiDiList.h:64
elm::BiDiList::clear
void clear(void)
Definition: BiDiList.h:101
elm::BiDiList::put
void put(const T &v)
Definition: BiDiList.h:152
elm::BiDiList::push
void push(const T &i)
Definition: BiDiList.h:146
elm::BiDiList::find
Iter find(const T &item, const Iter &pos) const
Definition: BiDiList.h:123
elm::BiDiList::removeFirst
void removeFirst()
Definition: BiDiList.h:135
elm::BiDiList::equals
bool equals(const BiDiList< T > &l) const
Definition: BiDiList.h:94
elm::inhstruct::DLList
Definition: DLList.h:67
elm::BiDiList::allocator
const A & allocator() const
Definition: BiDiList.h:52
elm::BiDiList::reset
void reset(void)
Definition: BiDiList.h:147
elm::_
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:232
elm::DefaultAllocatorDelegate::free
void free(t::ptr p) const
Definition: custom.h:32
elm::BiDiList::add
void add(const T &value)
Definition: BiDiList.h:103
elm::BiDiList::operator+=
BiDiList< T > & operator+=(const T &h)
Definition: BiDiList.h:111
elm::io::list
ListPrinter< T > list(const T &l, cstring s="", typename ListPrinter< T >::fun_t f=ListPrinter< T >::asis)
Definition: Output.h:321
elm
Definition: adapter.h:26
elm::BiDiList::copy
void copy(const BiDiList< T > &l)
Definition: BiDiList.h:115
elm::BiDiList::allocator
A & allocator()
Definition: BiDiList.h:53
elm::BiDiList::end
Iter end(void) const
Definition: BiDiList.h:93
elm::BiDiList::removeBefore
void removeBefore(const Iter &i)
Definition: BiDiList.h:137
elm::BiDiList::addAll
void addAll(const C &c)
Definition: BiDiList.h:104
elm::inhstruct::DLList::addLast
void addLast(DLNode *node)
Definition: DLList.h:108
elm::BiDiList::operator[]
const T & operator[](int k) const
Definition: BiDiList.h:126
elm::BiDiList::last
const T & last(void) const
Definition: BiDiList.h:120
elm::BiDiList::operator*
Iter operator*(void) const
Definition: BiDiList.h:155
elm::BiDiList::operator-=
BiDiList< T > & operator-=(const BiDiList< T > &l)
Definition: BiDiList.h:114
elm::BiDiList::last
T & last(void)
Definition: BiDiList.h:130
elm::BiDiList::removeAll
void removeAll(const C &c)
Definition: BiDiList.h:107
elm::BiDiList::Iter::Iter
Iter(const BiDiList &l)
Definition: BiDiList.h:60
elm::BiDiList::Iter::Iter
Iter(const BackIter &i)
Definition: BiDiList.h:61
elm::BiDiList::head
const T & head() const
Definition: BiDiList.h:150
elm::BiDiList::isEmpty
bool isEmpty(void) const
Definition: BiDiList.h:90
elm::BiDiList::BackIter::BackIter
BackIter(const BiDiList &l)
Definition: BiDiList.h:74
elm::BiDiList::BiDiList
BiDiList(void)
Definition: BiDiList.h:47
elm::InplacePreIterator
Definition: iter.h:49
elm::BiDiList::removeLast
void removeLast()
Definition: BiDiList.h:136
elm::BiDiList::begin
Iter begin(void) const
Definition: BiDiList.h:92
elm::BiDiList::addAfter
void addAfter(const Iter &i, const T &value)
Definition: BiDiList.h:133
elm::inhstruct::DLList::isEmpty
bool isEmpty(void) const
Definition: DLList.h:95
elm::BiDiList::Iter::next
void next(void)
Definition: BiDiList.h:65
elm::BiDiList::null
static const BiDiList< T, E, A > null
Definition: BiDiList.h:98
elm::BiDiList::operator==
bool operator==(const BiDiList< T > &l) const
Definition: BiDiList.h:96
elm::inhstruct::DLList::last
DLNode * last(void) const
Definition: DLList.h:94
elm::BiDiList::operator=
BiDiList & operator=(const BiDiList &list)
Definition: BiDiList.h:116
elm::BiDiList::pop
T pop()
Definition: BiDiList.h:145
elm::BiDiList::Iter::equals
bool equals(const Iter &i) const
Definition: BiDiList.h:66
elm::BiDiList::containsAll
bool containsAll(const C &c) const
Definition: BiDiList.h:88
elm::inhstruct::DLList::removeLast
void removeLast(void)
Definition: DLList.h:112
elm::BiDiList::~BiDiList
~BiDiList(void)
Definition: BiDiList.h:49
elm::inhstruct::DLNode
Definition: DLList.h:30
elm::BiDiList::BackIter::ended
bool ended(void) const
Definition: BiDiList.h:76
elm::BiDiList::get
const T get(void)
Definition: BiDiList.h:151
elm::BiDiList::reversedItems
Iter reversedItems(void) const
Definition: BiDiList.h:82