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
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_SORTEDLIST_H_
22 #define ELM_SORTEDLIST_H_
23 
24 #include <elm/genstruct/SLList.h>
25 #include <elm/compare.h>
26 #include <elm/adapter.h>
27 
28 namespace elm {
29 
30 // SortedSLList class
31 template <class T, class C = Comparator<T>, class A = IdAdapter<T> >
32 class SortedList {
34 public:
35  typedef typename A::key_t key_t;
36  typedef typename A::val_t val_t;
37 
38  inline SortedList(void) { }
39  inline SortedList(const C& c): _c(c) { }
40  SortedList(SortedList & source): list(source.list) { }
41 
42  inline void removeFirst(void) { list.removeFirst(); }
43  inline void removeLast(void) { list.removeLast(); }
44 
45  // Collection concept
46  inline int count (void) const { return list.count(); }
47 
48  bool contains(const key_t &item) const {
49  for (Iterator current(*this); current; current++) {
50  int cmp = _c.doCompare(item, A::key(*current));
51  if(cmp > 0) continue; else if(!cmp) return true; else break;
52  }
53  return false;
54  }
55 
56  inline bool isEmpty(void) const { return list.isEmpty(); }
57  inline operator bool(void) const { return !list.isEmpty(); }
58 
59  class Iterator: public list_t::Iterator {
60  public:
61  inline Iterator(void) { }
62  inline Iterator(const SortedList& _list): list_t::Iterator(_list.list) { }
63  private:
64  friend class SortedList;
65  inline Iterator(const typename list_t::Iterator& iter)
66  : list_t::Iterator(iter) { }
67  };
68 
69  // MutableCollection concept
70  inline void clear(void) { list.clear(); }
71 
72  void add (const T &value) {
73  for(Iterator current(*this); current; current++)
74  if(_c.doCompare(A::key(value), A::key(*current)) < 0) {
75  list.addBefore(current, value);
76  return;
77  }
78  list.addLast(value);
79  }
80 
81  template <template <class _> class CC> inline void addAll (const CC<T> &items)
82  { for(typename CC<T>::Iterator item(items); item; item++) add(item); }
83  inline void remove(const T &item) { list.remove(item); }
84  template <template <class _> class CC> inline void removeAll(const CC<T> &items)
85  { list.removeAll(items); }
86  void remove(const Iterator &iter) { list.remove(iter); }
87 
88  // List concept
89  inline const T& first(void) const { return list.first(); }
90  inline const T& last(void) const { return list.last(); }
91  inline Iterator find(const T& item) const
92  { return Iterator(list.find(item)); }
93  inline Iterator find(const T& item, const Iterator& iter) const
94  { return list.find(item, iter); }
95 
96 private:
97  list_t list;
98  C _c;
99 };
100 
101 } // elm
102 
103 #endif /* ELM_SORTEDLIST_H_ */
SortedList(const C &c)
Definition: SortedList.h:39
void removeAll(const C &items)
Definition: SLList.h:106
void clear(void)
Definition: SortedList.h:70
Iterator find(const T &item) const
Definition: SortedList.h:91
bool contains(const key_t &item) const
Definition: SortedList.h:48
Iterator(void)
Definition: SortedList.h:61
Definition: SLList.h:34
bool isEmpty(void) const
Definition: SortedList.h:56
void add(const T &value)
Definition: SortedList.h:72
void remove(const T &value)
Definition: SLList.h:109
void removeFirst(void)
Definition: SLList.h:135
void addAll(const CC< T > &items)
Definition: SortedList.h:81
const T & last(void) const
Definition: SortedList.h:90
T & last(void)
Definition: SLList.h:121
Iterator find(const T &item, const Iterator &iter) const
Definition: SortedList.h:93
value_t value(CString name, int value)
Definition: rtti.h:40
SortedList(SortedList &source)
Definition: SortedList.h:40
void removeLast(void)
Definition: SLList.h:136
int count(void) const
Definition: SLList.h:60
void addBefore(const Iterator &pos, const T &value)
Definition: SLList.h:133
SortedList(void)
Definition: SortedList.h:38
Definition: SortedList.h:32
bool isEmpty(void) const
Definition: SLList.h:63
const T & first(void) const
Definition: SortedList.h:89
T & first(void)
Definition: SLList.h:119
Iterator(const SortedList &_list)
Definition: SortedList.h:62
Iterator find(const T &item) const
Definition: SLList.h:123
int count(void) const
Definition: SortedList.h:46
void removeAll(const CC< T > &items)
Definition: SortedList.h:84
void clear(void)
Definition: SLList.h:101
void removeFirst(void)
Definition: SortedList.h:42
void addLast(const T &value)
Definition: SLList.h:130
void removeLast(void)
Definition: SortedList.h:43
A::val_t val_t
Definition: SortedList.h:36
Definition: SortedList.h:59
A::key_t key_t
Definition: SortedList.h:35