ANNA Suite  2020b
Multipurpose development suite for Telco applications
MultiMap.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_util_MultiMap_hpp
10 #define anna_core_util_MultiMap_hpp
11 
12 // Solaris std::multimap is missing !!
13 
14 #include <algorithm>
15 #include <functional>
16 #include <vector>
17 
19 #include <anna/config/defines.hpp>
20 
21 namespace anna {
22 
33 template < typename T, typename SortBy, typename TKey = int > class MultiMap : public std::vector <T*> {
34  struct LessT : public std::binary_function <T*, T*, bool> {
35  bool operator()(const T* first, const T* second) const {
36  return SortBy::value(first) < SortBy::value(second);
37  }
38  };
39 
40  // El orden de los operandos está impuesto por cómo se invoca al operator() desde stl_algo.h (2685).
41  struct LessKey : public std::binary_function <T*, TKey, bool> {
42  bool operator()(const T* _vv, const TKey& searched) const {
43  return SortBy::value(_vv) < searched;
44  }
45  };
46 
47 public:
48  typedef typename std::vector <T*> container;
49  typedef typename container::iterator iterator;
50  typedef typename container::const_iterator const_iterator;
51  typedef typename container::value_type value_type;
52 
56  MultiMap() {;}
57 
62  explicit MultiMap(const MultiMap& other) : container(other) {}
63 
67  virtual ~MultiMap() { this->clear(); }
68 
74  bool add(T* _vv)
75  noexcept(false) {
76  if(_vv == NULL)
77  return false;
78 
79  iterator maxii = this->end();
80  iterator ii = std::upper_bound(this->begin(), maxii, _vv, a_lessT);
81 
82  if(ii == maxii)
83  this->push_back(_vv);
84  else
85  this->insert(ii, _vv);
86 
87  return true;
88  }
89 
97  bool contains(const T* _vv) const { return (_vv == NULL) ? false : (find(SortBy::value(_vv)) != NULL); }
98 
103  for(iterator ii = this->begin(), maxii = this->end(); ii != maxii; ii ++)
104  delete data(ii);
105 
106  this->clear();
107  }
108 
114  T* erase(const T* _vv)
115  noexcept(false) {
116  if(_vv == NULL)
117  return NULL;
118 
119  iterator maxii = this->end();
120  iterator ii = std::lower_bound(this->begin(), this->end(), _vv, a_lessT);
121 
122  if(ii == maxii)
123  return NULL;
124 
125  T* dd = data(ii);
126 
127  if(!(SortBy::value(_vv) == SortBy::value(dd)))
128  return NULL;
129 
130  container::erase(ii);
131  return dd;
132  }
133 
140  iterator erase_iterator(iterator ii) { return container::erase(ii); }
141 
147  T* find(const TKey& key)
148  {
149  iterator maxii = this->end();
150  iterator ii = lower_bound(this->begin(), maxii, key, a_lessKey);
151 
152  if(ii == maxii)
153  return NULL;
154 
155  T* pos = data(ii);
156  return (SortBy::value(pos) == key) ? pos : NULL;
157  }
158 
163  iterator find_iterator(const TKey& key)
164  {
165  return lower_bound(this->begin(), this->end(), key, a_lessKey);
166  }
167 
173  const T* find(const TKey& key) const {
174  return const_cast <MultiMap <T, SortBy, TKey>*>(this)->find(key);
175  }
176 
181  static T* data(iterator ii) { return *ii; }
182 
187  static const T* data(const_iterator ii) { return *ii; }
188 
189 private:
190  LessT a_lessT;
191  LessKey a_lessKey;
192 };
193 
194 }
195 
196 #endif
197 
iterator erase_iterator(iterator ii)
Definition: MultiMap.hpp:140
static const T * data(const_iterator ii)
Definition: MultiMap.hpp:187
T * find(const TKey &key)
Definition: MultiMap.hpp:147
static T * data(iterator ii)
Definition: MultiMap.hpp:181
virtual ~MultiMap()
Definition: MultiMap.hpp:67
container::const_iterator const_iterator
Definition: MultiMap.hpp:50
bool add(T *_vv) noexcept(false)
Definition: MultiMap.hpp:74
Definition: MultiMap.hpp:33
bool contains(const T *_vv) const
Definition: MultiMap.hpp:97
Definition: app.hpp:12
MultiMap()
Definition: MultiMap.hpp:56
container::value_type value_type
Definition: MultiMap.hpp:51
MultiMap(const MultiMap &other)
Definition: MultiMap.hpp:62
T * erase(const T *_vv) noexcept(false)
Definition: MultiMap.hpp:114
void clearAndDestroy()
Definition: MultiMap.hpp:102
const T * find(const TKey &key) const
Definition: MultiMap.hpp:173
container::iterator iterator
Definition: MultiMap.hpp:49
iterator find_iterator(const TKey &key)
Definition: MultiMap.hpp:163
std::vector< T * > container
Definition: MultiMap.hpp:48