Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
SortedList.h
1 /*
2  * SortedList class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2012, IRIT UPS.
6  *
7  * ELM 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  * ELM 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 ELM; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_DATA_SORTEDLIST_H_
22 #define ELM_DATA_SORTEDLIST_H_
23 
24 #include "Adapter.h"
25 #include "List.h"
26 #include "util.h"
27 #include <elm/compare.h>
28 #include <elm/util/Option.h>
29 
30 namespace elm {
31 
32 // SortedList class
33 template <class T, class C = Comparator<T>, class A = DefaultAlloc >
34 class SortedList {
35 protected:
37 public:
38  typedef T t;
40 
41  inline SortedList(void) { }
42  inline SortedList(const SortedList<T, C, A> &l): list(l.list) { }
43  inline C& comparator() { return list.equivalence(); }
44  inline const C& comparator() const { return list.equivalence(); }
45  inline A& allocator() { return list.allocator(); }
46 
47  inline void removeFirst(void) { list.removeFirst(); }
48  inline void removeLast(void) { list.removeLast(); }
49 
50  // Collection concept
51  inline int count (void) const { return list.count(); }
52 
53  inline bool contains(const T &item) const { return find(item) != end(); }
54  template <class CC> bool containsAll(const CC& c) const
55  { for(const auto i: c) if(!contains(i)) return false; return true; }
56 
57  inline bool isEmpty(void) const { return list.isEmpty(); }
58  inline operator bool(void) const { return !list.isEmpty(); }
59 
60  class Iter: public list_t::Iter {
61  public:
62  inline Iter(void) { }
63  inline Iter(const self_t& _list): list_t::Iter(_list.list) { }
64  };
65  inline Iter items(void) const { return Iter(*this); }
66  inline Iter operator*(void) const { return items(); }
67  inline operator Iter(void) const { return items(); }
68  inline Iter begin(void) const { return items(); }
69  inline Iter end(void) const { return Iter(); }
70 
71  bool equals(const SortedList<T>& l) const {
72  Iter i = begin(), j = l.begin();
73  for(; i() && j(); i++, j++)
74  if(comparator().doCompare(*i, *j) != 0)
75  return false;
76  return !i && !j;
77  }
78  inline bool operator==(const SortedList<T>& l) const { return equals(l); }
79  inline bool operator!=(const SortedList<T>& l) const { return !equals(l); }
80 
81  // MutableCollection concept
82  inline void clear(void) { list.clear(); }
83  inline void copy(const SortedList<T>& l) { list.copy(l.list); }
84 
85  void add(const T &value) {
86  for(typename list_t::PrecIter current(list); current(); current++)
87  if(comparator().doCompare(value, *current) < 0) {
88  list.addBefore(current, value);
89  return;
90  }
92  }
93 
94  template <class CC> inline void addAll (const CC &c)
95  { list.addAll(c); }
96  inline void remove(const T &item) { list.remove(item); }
97  template <class CC> inline void removeAll(const CC &c)
98  { list.removeAll(c); }
99  inline void remove(const Iter &iter) { list.remove(*iter); }
100  inline SortedList<T>& operator+=(const T& v) { add(v); return *this; }
101  inline SortedList<T>& operator-=(const T& v) { remove(v); return *this; }
102 
103  // List concept
104  inline const T& first(void) const { return list.first(); }
105  inline const T& last(void) const { return list.last(); }
106  Iter find(const T& item, const Iter& iter) const {
107  Iter i = iter; for(; i(); i++) {
108  int cmp = comparator().doCompare(item, *i);
109  if(cmp > 0) continue; else if(!cmp) return i; else return end();
110  }
111  return i;
112  }
113  inline Iter find(const T& item) const { return find(item, begin()); }
114  inline T& at(const Iter& i) { return list.at(i); }
115  inline Iter nth(int n) const { Iter i(*this); while(n != 0 && i()) { n--; i++; }; return i; }
116 
117  // operators
118  inline SortedList<T, C>& operator=(const SortedList<T, C>& sl) { list.copy(sl.list); return *this; }
119  inline bool operator&(const T& e) const { return list.contains(e); }
120  inline T& operator[](int k) { return list[k]; }
121  inline const T& operator[](int k) const { return list[k]; }
122 
123 
124 protected:
125  inline void set(Iter i, const T& val) { list.set(i, val); }
127 };
128 
129 } // elm
130 
131 #endif /* ELM_DATA_SORTEDLIST_H_ */
elm::SortedList::at
T & at(const Iter &i)
Definition: SortedList.h:114
elm::List::clear
void clear(void)
Definition: List.h:129
elm::SortedList::Iter::Iter
Iter(void)
Definition: SortedList.h:62
elm::iter
Definition: util_WAHVector.cpp:157
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::List::addLast
void addLast(const T &value)
Definition: List.h:158
elm::List::contains
bool contains(const T &item) const
Definition: List.h:120
elm::SortedList::list_t
List< T, CompareEquiv< C >, A > list_t
Definition: SortedList.h:36
elm::SortedList::items
Iter items(void) const
Definition: SortedList.h:65
elm::SortedList::isEmpty
bool isEmpty(void) const
Definition: SortedList.h:57
elm::SortedList::find
Iter find(const T &item) const
Definition: SortedList.h:113
elm::SortedList::add
void add(const T &value)
Definition: SortedList.h:85
elm::SortedList::contains
bool contains(const T &item) const
Definition: SortedList.h:53
elm::SortedList
Definition: SortedList.h:34
elm::SortedList::operator&
bool operator&(const T &e) const
Definition: SortedList.h:119
elm::SortedList::addAll
void addAll(const CC &c)
Definition: SortedList.h:94
elm::SortedList::removeLast
void removeLast(void)
Definition: SortedList.h:48
elm::List::Iter
Definition: List.h:64
elm::SortedList::end
Iter end(void) const
Definition: SortedList.h:69
elm::List::last
T & last(void)
Definition: List.h:147
elm::SortedList::list
list_t list
Definition: SortedList.h:126
elm::List::removeAll
void removeAll(const C &items)
Definition: List.h:134
value
elm::SortedList::operator==
bool operator==(const SortedList< T > &l) const
Definition: SortedList.h:78
elm::SortedList::operator[]
const T & operator[](int k) const
Definition: SortedList.h:121
bool
elm::SortedList::copy
void copy(const SortedList< T > &l)
Definition: SortedList.h:83
elm::SortedList::remove
void remove(const T &item)
Definition: SortedList.h:96
elm::SortedList::comparator
const C & comparator() const
Definition: SortedList.h:44
elm::SortedList::t
T t
Definition: SortedList.h:38
elm::SortedList::find
Iter find(const T &item, const Iter &iter) const
Definition: SortedList.h:106
elm::List::equivalence
E & equivalence()
Definition: List.h:54
elm::SortedList::clear
void clear(void)
Definition: SortedList.h:82
elm::SortedList::operator!=
bool operator!=(const SortedList< T > &l) const
Definition: SortedList.h:79
elm::SortedList::Iter::Iter
Iter(const self_t &_list)
Definition: SortedList.h:63
elm::SortedList::last
const T & last(void) const
Definition: SortedList.h:105
elm::List::copy
void copy(const List< T, E, A > &list)
Definition: List.h:58
elm::SortedList::self_t
SortedList< T, C, A > self_t
Definition: SortedList.h:39
elm::SortedList::operator+=
SortedList< T > & operator+=(const T &v)
Definition: SortedList.h:100
elm::List::allocator
A & allocator()
Definition: List.h:56
elm
Definition: adapter.h:26
elm::SortedList::count
int count(void) const
Definition: SortedList.h:51
elm::SortedList::equals
bool equals(const SortedList< T > &l) const
Definition: SortedList.h:71
elm::List::count
int count(void) const
Definition: List.h:119
elm::SortedList::nth
Iter nth(int n) const
Definition: SortedList.h:115
elm::SortedList::allocator
A & allocator()
Definition: SortedList.h:45
elm::List::removeFirst
void removeFirst(void)
Definition: List.h:164
elm::List::isEmpty
bool isEmpty(void) const
Definition: List.h:122
elm::List::set
void set(const Iter &pos, const T &item)
Definition: List.h:166
elm::List::removeLast
void removeLast(void)
Definition: List.h:165
elm::SortedList::removeFirst
void removeFirst(void)
Definition: SortedList.h:47
elm::SortedList::remove
void remove(const Iter &iter)
Definition: SortedList.h:99
elm::SortedList::operator[]
T & operator[](int k)
Definition: SortedList.h:120
elm::List::remove
void remove(const T &value)
Definition: List.h:136
elm::List::addBefore
void addBefore(PrecIter &pos, const T &value)
Definition: List.h:161
elm::List::at
const T & at(const Iter &i) const
Definition: List.h:126
elm::SortedList::containsAll
bool containsAll(const CC &c) const
Definition: SortedList.h:54
elm::List::first
T & first(void)
Definition: List.h:145
elm::SortedList::set
void set(Iter i, const T &val)
Definition: SortedList.h:125
elm::SortedList::removeAll
void removeAll(const CC &c)
Definition: SortedList.h:97
elm::SortedList::operator-=
SortedList< T > & operator-=(const T &v)
Definition: SortedList.h:101
elm::SortedList::operator*
Iter operator*(void) const
Definition: SortedList.h:66
elm::SortedList::first
const T & first(void) const
Definition: SortedList.h:104
elm::List::addAll
void addAll(const C &items)
Definition: List.h:132
elm::SortedList::operator=
SortedList< T, C > & operator=(const SortedList< T, C > &sl)
Definition: SortedList.h:118
elm::List
Definition: List.h:34
elm::SortedList::SortedList
SortedList(void)
Definition: SortedList.h:41
elm::SortedList::Iter
Definition: SortedList.h:60
elm::SortedList::begin
Iter begin(void) const
Definition: SortedList.h:68
elm::SortedList::comparator
C & comparator()
Definition: SortedList.h:43
elm::SortedList::SortedList
SortedList(const SortedList< T, C, A > &l)
Definition: SortedList.h:42