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
SortedBinMap.h
1 /*
2  * $Id$
3  * SortedBinMap class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-07, 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_SORTEDBINMAP_H
23 #define ELM_GENSTRUCT_SORTEDBINMAP_H
24 
25 #include <elm/utility.h>
26 #include <elm/util/Pair.h>
27 #include <elm/genstruct/SortedBinTree.h>
28 
29 namespace elm { namespace genstruct {
30 
31 // SortedBinMap class
32 template <class K, class T, class C = Comparator<K> >
33 class SortedBinMap {
34  typedef Pair<K, T> value_t;
36 public:
37  inline SortedBinMap(void) { }
38  inline SortedBinMap(const SortedBinMap& map): tree(map.tree) { }
39 
40  // Collection concept
41  inline int count(void) const { return tree.count(); }
42  inline bool contains(const K &item) const { return tree.look(item); }
43  inline bool isEmpty(void) const { return tree.isEmpty(); }
44  inline operator bool(void) const { return !isEmpty(); }
45 
46  // Iterator class
47  class Iterator: public PreIterator<Iterator, const T&> {
48  public:
49  inline Iterator(const SortedBinMap& map): iter(map.tree) { }
50  inline Iterator(const Iterator& _): iter(_) { }
51  inline bool ended(void) const { return iter.ended(); }
52  inline void next(void) { iter.next(); }
53  const T &item(void) const { return iter.item().snd; }
54  private:
55  typename tree_t::Iterator iter;
56  };
57 
58  // Map concept
59  inline const T& get(const K &key, const T &def) const {
60  const value_t *val = tree.look(key);
61  return val ? val->snd : def;
62  }
63  inline Option<T> get(const K &key) const {
64  const value_t *res = tree.look(key);
65  return res ? Option<T>(res->snd) : none;
66  }
67  inline bool hasKey(const K &key) const { return tree.look(key); }
68 
69  // KeyIterator class
70  class KeyIterator: public PreIterator<KeyIterator, const K&> {
71  public:
72  inline KeyIterator(const SortedBinMap& map): iter(map.tree) { }
73  inline KeyIterator(const KeyIterator& _): iter(_) { }
74  inline bool ended(void) const { return iter.ended(); }
75  inline void next(void) { iter.next(); }
76  const K &item(void) const { return iter.item().fst; }
77  private:
78  typename tree_t::Iterator iter;
79  };
80 
81  // PairIterator class
82  class PairIterator: public PreIterator<PairIterator, const value_t&> {
83  public:
84  inline PairIterator(const SortedBinMap& map): iter(map.tree) { }
85  inline PairIterator(const PairIterator& _): iter(_) { }
86  inline bool ended(void) const { return iter.ended(); }
87  inline void next(void) { iter.next(); }
88  const value_t &item(void) const { return iter.item(); }
89  private:
90  typename tree_t::Iterator iter;
91  };
92 
93  // MutableMap concept
94  inline void put(const K& key, const T& value)
95  { tree.add(value_t(key, value)); }
96  inline void remove(const K& key)
97  { value_t *val = tree.look(key); if(val) tree.remove(*val); }
98  inline void remove(const PairIterator& iter)
99  { tree.remove(iter.iter); }
100 
101 private:
102  tree_t tree;
103 };
104 
105 } } // elm::genstruct
106 
107 #endif // ELM_GENSTRUCT_SORTEDBINMAP_H
void add(const T &value)
Definition: SortedBinTree.h:93
Definition: SortedBinTree.h:38
SortedBinMap(const SortedBinMap &map)
Definition: SortedBinMap.h:38
int count(void) const
Definition: SortedBinMap.h:41
const T & item(void) const
Definition: SortedBinMap.h:53
PairIterator(const SortedBinMap &map)
Definition: SortedBinMap.h:84
bool isEmpty(void) const
Definition: SortedBinMap.h:43
Definition: PreIterator.h:29
bool hasKey(const K &key) const
Definition: SortedBinMap.h:67
Definition: SortedBinMap.h:47
Definition: SortedBinMap.h:82
Iterator(const SortedBinMap &map)
Definition: SortedBinMap.h:49
bool ended(void) const
Definition: SortedBinMap.h:74
const K & item(void) const
Definition: SortedBinMap.h:76
Definition: SortedBinMap.h:70
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:221
bool contains(const K &item) const
Definition: SortedBinMap.h:42
KeyIterator(const KeyIterator &_)
Definition: SortedBinMap.h:73
value_t value(CString name, int value)
Definition: rtti.h:40
Definition: Option.h:22
void next(void)
Definition: SortedBinMap.h:52
PairIterator(const PairIterator &_)
Definition: SortedBinMap.h:85
bool ended(void) const
Definition: SortedBinMap.h:51
bool isEmpty(void) const
Definition: SortedBinTree.h:55
void remove(const T &value)
Definition: SortedBinTree.h:125
T2 snd
Definition: Pair.h:19
void next(void)
Definition: SortedBinMap.h:87
void put(const K &key, const T &value)
Definition: SortedBinMap.h:94
const T * look(const typename K::key_t &key) const
Definition: SortedBinTree.h:161
Iterator(const Iterator &_)
Definition: SortedBinMap.h:50
Definition: Pair.h:16
int count(void) const
Definition: SortedBinTree.h:53
Definition: SortedBinMap.h:33
bool ended(void) const
Definition: SortedBinMap.h:86
const OptionalNone none
Definition: util_Option.cpp:134
KeyIterator(const SortedBinMap &map)
Definition: SortedBinMap.h:72
SortedBinMap(void)
Definition: SortedBinMap.h:37
void next(void)
Definition: SortedBinMap.h:75
const value_t & item(void) const
Definition: SortedBinMap.h:88