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
AssocList.h
1 /*
2  * $Id$
3  * Comparator class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2008, 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_ASSOCLIST_H_
23 #define ELM_GENSTRUCT_ASSOCLIST_H_
24 
25 #include <elm/PreIterator.h>
26 #include <elm/type_info.h>
27 #include <elm/util/Option.h>
28 #include <elm/compare.h>
29 #include <elm/genstruct/SortedSLList.h>
30 
31 namespace elm { namespace genstruct {
32 
33 // AssocList class
34 template <class K, class T, class C = Comparator<K>, class E = Equiv<T>, class N = type_info<T> >
35 class AssocList {
36  typedef Pair<K, T> pair_t;
38 
39 public:
40  inline AssocList(void) { }
41  inline AssocList(const AssocList& alist): list(alist.list) { }
42 
43  class PairIterator: public list_t::Iterator {
44  public:
45  PairIterator(const AssocList& list): list_t::Iterator(list.list) { }
46  PairIterator(const PairIterator& iter): list_t::Iterator(iter) { }
48  { list_t::Iterator::operator=(iter); return *this; }
49  };
50 
51  // Collection concept
52  inline int count(void) const { return list.count(); }
53  inline bool contains(const T& item) const {
54  for(Iterator iter(*this); iter; iter++)
55  if(E::equals(item, iter)) return true;
56  return false;
57  }
58  inline bool isEmpty(void) const { return list.isEmpty(); }
59  inline operator bool (void) const { return !list.isEmpty(); }
60 
61  class Iterator: public PreIterator<Iterator, T> {
62  public:
63  inline Iterator(const AssocList& list): iter(list) { }
64  inline Iterator(const Iterator& iterator): iter(iterator.iter) { }
65  inline Iterator& operator=(const Iterator& iterator)
66  { iter = iterator.iter; return *this; }
67 
68  inline bool ended(void) const { return iter.ended(); }
69  inline void next(void) { iter.next(); }
70  inline T item(void) const { return iter.item().snd; }
71  private:
72  PairIterator iter;
73  };
74 
75  // List concept
76  inline const T& first(void) const { return list.first().snd; }
77  inline const T& last (void) const { return list.last().snd; }
78  inline Iterator find (const T& item) {
79  Iterator iter(*this);
80  for(; iter; iter++) if(E::equals(iter, item)) break;
81  return iter;
82  }
83  inline Iterator find (const T& item, const Iterator &iter) {
84  for(iter++; iter; iter++) if(E::equals(iter, item)) break;
85  return iter;
86  }
87 
88  // Map concept
89  Option<T> get(const K &key) const
90  { typename list_t::Iterator item = list.find(pair(key, N::null));
91  if(!item.ended()) return some((*item).snd); else return none; }
92  T get(const K& key, const T& def) const
93  { typename list_t::Iterator item = list.find(pair(key, N::null));
94  if(item) return (*item).snd; else return def; }
95  bool hasKey(const K &key) const
96  { return list.contains(pair(key, N::null)); }
97 
98  class KeyIterator: public PreIterator<KeyIterator, const K&> {
99  public:
100  inline KeyIterator(const AssocList& list): iter(list) { }
101  inline KeyIterator(const KeyIterator& iterator): iter(iterator.iter) { }
102  inline KeyIterator& operator=(const KeyIterator& iterator)
103  { iter = iterator.iter; return *this; }
104 
105  inline bool ended(void) const { return iter.ended(); }
106  inline void next(void) { iter.next(); }
107  inline const K& item(void) const { return iter.item().fst; }
108  private:
109  PairIterator iter;
110  };
111 
112  // MutableMap class
113  inline void put(const K& key, const T& value)
114  { list.add(pair(key, value)); }
115  inline void remove(const K& key)
116  { list.remove(pair(key, N::null)); }
117  inline void remove(const PairIterator& iter)
118  { list.remove(iter); }
119 
120 private:
121  list_t list;
122 };
123 
124 } } // elm::genstruct
125 
126 #endif /* ELM_GENSTRUCT_ASSOCLIST_H_ */
const T & first(void) const
Definition: AssocList.h:76
int count(void) const
Definition: AssocList.h:52
Definition: PreIterator.h:29
AssocList(const AssocList &alist)
Definition: AssocList.h:41
Iterator find(const T &item) const
Definition: SortedList.h:91
Option< T > some(T val)
Definition: Option.h:49
bool contains(const key_t &item) const
Definition: SortedList.h:48
void put(const K &key, const T &value)
Definition: AssocList.h:113
PairIterator(const AssocList &list)
Definition: AssocList.h:45
Iterator find(const T &item, const Iterator &iter)
Definition: AssocList.h:83
PairIterator(const PairIterator &iter)
Definition: AssocList.h:46
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:41
void remove(const T &item)
Definition: SortedList.h:83
bool hasKey(const K &key) const
Definition: AssocList.h:95
void next(void)
Definition: AssocList.h:106
Definition: AssocList.h:98
bool isEmpty(void) const
Definition: SortedList.h:56
Iterator(const AssocList &list)
Definition: AssocList.h:63
void add(const T &value)
Definition: SortedList.h:72
Definition: AssocList.h:61
const T & last(void) const
Definition: SortedList.h:90
Iterator & operator=(const Iterator &iterator)
Definition: AssocList.h:65
Definition: AssocList.h:35
value_t value(CString name, int value)
Definition: rtti.h:40
KeyIterator & operator=(const KeyIterator &iterator)
Definition: AssocList.h:102
const T & last(void) const
Definition: AssocList.h:77
Definition: Option.h:22
Definition: SortedSLList.h:31
Iterator find(const T &item)
Definition: AssocList.h:78
PairIterator & operator=(const PairIterator &iter)
Definition: AssocList.h:47
bool isEmpty(void) const
Definition: AssocList.h:58
const K & item(void) const
Definition: AssocList.h:107
const T & first(void) const
Definition: SortedList.h:89
Definition: AssocList.h:43
bool contains(const T &item) const
Definition: AssocList.h:53
Iterator(const Iterator &iterator)
Definition: AssocList.h:64
KeyIterator(const AssocList &list)
Definition: AssocList.h:100
KeyIterator(const KeyIterator &iterator)
Definition: AssocList.h:101
void next(void)
Definition: AssocList.h:69
bool ended(void) const
Definition: AssocList.h:68
T2 snd
Definition: Pair.h:19
bool ended(void) const
Definition: AssocList.h:105
AssocList(void)
Definition: AssocList.h:40
int count(void) const
Definition: SortedList.h:46
T item(void) const
Definition: AssocList.h:70
Definition: Pair.h:16
const OptionalNone none
Definition: util_Option.cpp:134