Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
ListMap.h
1 /*
2  * ListSet class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2017, 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_LISTMAP_H_
22 #define ELM_DATA_LISTMAP_H_
23 
24 #include "SortedList.h"
25 
26 namespace elm {
27 
28 template <class K, class T, class C = Comparator<K>, class E = Equiv<T>, class A = DefaultAlloc >
29 class ListMap: private SortedList<Pair<K, T>, AssocComparator<K, T, C>, A>, public E {
31 public:
33  typedef typename base_t::Iter PairIter;
34 
35  inline ListMap() { }
36  inline ListMap(const ListMap<K, T>& l): base_t::type_t(l) { }
37  inline E& equivalence() { return *this; }
38 
39  class Iter: public PreIterator<Iter, T> {
40  friend class ListMap;
41  public:
42  inline Iter() { }
43  inline Iter(const ListMap<K, T>& l): i(l) { }
44  inline bool ended(void) const { return i.ended(); }
45  inline const T& item(void) const { return (*i).snd; }
46  inline void next(void) { i.next(); }
47  inline bool equals(const Iter& ii) const { return i.equals(ii.i); }
48  private:
49  PairIter i;
50  };
51 
52  class KeyIter: public PreIterator<KeyIter, K> {
53  public:
54  inline KeyIter(void) { }
55  inline KeyIter(const ListMap<K, T>& l): i(l) { }
56  inline bool ended(void) const { return i.ended(); }
57  inline const K& item(void) const { return (*i).fst; }
58  inline void next(void) { i.next(); }
59  inline bool equals(const KeyIter& ii) { return i.equals(ii.i); }
60  private:
61  PairIter i;
62  };
63 
64  // Collection concept
65  inline Iter begin(void) const { return Iter(*this); }
66  inline Iter end(void) const { return Iter(); }
67  inline int count(void) const { return base_t::count(); }
68  inline bool contains(const T& v) const
69  { for(const auto& i: *this) if(E::isEqual(i, v)) return true; return false; }
70  template <class CC> inline bool containsAll(const CC& c) const
71  { for(const auto i: c) if(!contains(i)) return false; return true; }
72  inline bool isEmpty(void) const { return base_t::isEmpty(); }
73  inline operator bool(void) const { return !isEmpty(); }
74  inline Iter items(void) const { return begin(); }
75  inline Iter operator*(void) const { return begin(); }
76  inline operator Iter(void) const { return begin(); }
77  inline bool equals(const self_t& m) const { return base_t::equals(m); }
78  inline bool operator==(const self_t& m) const { return equals(m); }
79  inline bool operator!=(const self_t& m) const { return !equals(m); }
80  inline bool contains(const self_t& m) const { return base_t::contains(m); }
81  inline bool operator<=(const self_t& m) const { return m.contains(*this); }
82  inline bool operator<(const self_t& m) const { return m.contains(*this) && !equals(m); }
83  inline bool operator>=(const self_t& m) const { return contains(m); }
84  inline bool operator>(const self_t& m) const { return contains(m) && !equals(m); }
85 
86  // Map concept
87  inline Option<T> get(const K &k) const
88  { PairIter i = lookup(k); if(i()) return some((*i).snd); else return none; }
89  inline const T &get(const K &k, const T &d) const
90  { PairIter i = lookup(k); if(i()) return (*i).snd; else return d; }
91  inline bool hasKey(const K &k) const { return lookup(k)(); }
92 
93  inline Iterable<KeyIter> keys() const { return subiter(KeyIter(*this), KeyIter()); }
94  inline Iterable<PairIter> pairs() const { return subiter(PairIter(*this), PairIter()); }
95 
96  // MutableMap concept
97  void put(const K& k, const T& v) {
98  PairIter i = lookup(k);
99  if(i())
100  base_t::set(i, pair(k, v));
101  else
102  base_t::add(pair(k, v));
103  }
104  inline void remove(const Iter& i) { base_t::remove(i.i); }
105  inline void remove(const K& k)
106  { PairIter i = lookup(k); if(i()) base_t::remove(*i); }
107 
108 private:
109  PairIter lookup(const K& k) const {
110  for(PairIter i = base_t::begin(); i(); i++) {
111  int cmp = base_t::comparator().compareKey(k, (*i).fst);
112  if(cmp == 0)
113  return i;
114  else if(cmp < 0)
115  break;
116  }
117  return base_t::end();
118  }
119 };
120 
121 template <class K, class T, class M>
122 inline bool operator<=(const T& v, const ListMap<K, T, M>& m) { return m.contains(v); }
123 
124 } // elm
125 
126 #endif /* ELM_DATA_LISTMAP_H_ */
elm::List::Iter::next
void next(void)
Definition: List.h:72
elm::Option
Definition: Option.h:35
elm::ListMap::begin
Iter begin(void) const
Definition: ListMap.h:65
elm::ListMap
Definition: ListMap.h:29
elm::ListMap::Iter
Definition: ListMap.h:39
elm::operator<=
bool operator<=(const T &v, const FragTable< T, E, A > &t)
Definition: FragTable.h:158
elm::subiter
Iterable< I > subiter(const I &b, const I &e)
Definition: util.h:231
elm::ListMap::operator<
bool operator<(const self_t &m) const
Definition: ListMap.h:82
elm::ListMap::operator<=
bool operator<=(const self_t &m) const
Definition: ListMap.h:81
elm::ListMap::isEmpty
bool isEmpty(void) const
Definition: ListMap.h:72
elm::List::Iter::equals
bool equals(const Iter &i) const
Definition: List.h:73
elm::ListMap::KeyIter::KeyIter
KeyIter(const ListMap< K, T > &l)
Definition: ListMap.h:55
elm::ListMap::PairIter
base_t::Iter PairIter
Definition: ListMap.h:33
elm::ListMap::Iter::Iter
Iter(const ListMap< K, T > &l)
Definition: ListMap.h:43
elm::type_t
Definition: type_info.h:79
elm::SortedList::isEmpty
bool isEmpty(void) const
Definition: SortedList.h:57
elm::ListMap::items
Iter items(void) const
Definition: ListMap.h:74
elm::ListMap::KeyIter::item
const K & item(void) const
Definition: ListMap.h:57
elm::pair
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:63
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::ListMap::KeyIter::next
void next(void)
Definition: ListMap.h:58
elm::SortedList
Definition: SortedList.h:34
elm::ListMap::operator==
bool operator==(const self_t &m) const
Definition: ListMap.h:78
elm::ListMap::equivalence
E & equivalence()
Definition: ListMap.h:37
elm::ListMap::Iter::ended
bool ended(void) const
Definition: ListMap.h:44
elm::SortedList::end
Iter end(void) const
Definition: SortedList.h:69
elm::ListMap::ListMap
ListMap()
Definition: ListMap.h:35
elm::ListMap::KeyIter
Definition: ListMap.h:52
elm::ListMap::KeyIter::KeyIter
KeyIter(void)
Definition: ListMap.h:54
elm::ListMap::containsAll
bool containsAll(const CC &c) const
Definition: ListMap.h:70
elm::some
Option< T > some(const T &val)
Definition: Option.h:81
elm::Iterable
Definition: util.h:220
bool
elm::ListMap::self_t
ListMap< K, T, C, E, A > self_t
Definition: ListMap.h:32
elm::SortedList::remove
void remove(const T &item)
Definition: SortedList.h:96
elm::ListMap::pairs
Iterable< PairIter > pairs() const
Definition: ListMap.h:94
elm::ListMap::Iter::equals
bool equals(const Iter &ii) const
Definition: ListMap.h:47
elm::ListMap::Iter::item
const T & item(void) const
Definition: ListMap.h:45
elm::ListMap::ListMap
ListMap(const ListMap< K, T > &l)
Definition: ListMap.h:36
elm::ListMap::KeyIter::equals
bool equals(const KeyIter &ii)
Definition: ListMap.h:59
elm::ListMap::Iter::next
void next(void)
Definition: ListMap.h:46
elm::none
const OptionalNone none
Definition: util_Option.cpp:154
elm
Definition: adapter.h:26
elm::SortedList::count
int count(void) const
Definition: SortedList.h:51
elm::ListMap::operator>=
bool operator>=(const self_t &m) const
Definition: ListMap.h:83
elm::SortedList::equals
bool equals(const SortedList< T > &l) const
Definition: SortedList.h:71
elm::ListMap::count
int count(void) const
Definition: ListMap.h:67
elm::ListMap::keys
Iterable< KeyIter > keys() const
Definition: ListMap.h:93
elm::ListMap::end
Iter end(void) const
Definition: ListMap.h:66
elm::ListMap::get
const T & get(const K &k, const T &d) const
Definition: ListMap.h:89
elm::ListMap::put
void put(const K &k, const T &v)
Definition: ListMap.h:97
elm::ListMap::operator>
bool operator>(const self_t &m) const
Definition: ListMap.h:84
elm::ListMap::KeyIter::ended
bool ended(void) const
Definition: ListMap.h:56
elm::ListMap::get
Option< T > get(const K &k) const
Definition: ListMap.h:87
elm::SortedList::set
void set(Iter i, const T &val)
Definition: SortedList.h:125
elm::AssocComparator
Definition: compare.h:64
elm::ListMap::remove
void remove(const Iter &i)
Definition: ListMap.h:104
elm::ListMap::remove
void remove(const K &k)
Definition: ListMap.h:105
elm::ListMap::hasKey
bool hasKey(const K &k) const
Definition: ListMap.h:91
elm::ListMap::operator*
Iter operator*(void) const
Definition: ListMap.h:75
elm::ListMap::equals
bool equals(const self_t &m) const
Definition: ListMap.h:77
elm::PreIterator
Definition: iter.h:28
elm::ListMap::contains
bool contains(const T &v) const
Definition: ListMap.h:68
elm::ListMap::Iter::Iter
Iter()
Definition: ListMap.h:42
elm::ListMap::contains
bool contains(const self_t &m) const
Definition: ListMap.h:80
elm::List::Iter::ended
bool ended(void) const
Definition: List.h:70
elm::SortedList::Iter
Definition: SortedList.h:60
elm::ListMap::operator!=
bool operator!=(const self_t &m) const
Definition: ListMap.h:79
elm::SortedList::begin
Iter begin(void) const
Definition: SortedList.h:68
elm::SortedList::comparator
C & comparator()
Definition: SortedList.h:43