ANNA Suite  2020b
Multipurpose development suite for Telco applications
SortedVector.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_SortedVector_hpp
10 #define anna_core_util_SortedVector_hpp
11 
12 #include <map>
13 
15 #include <anna/config/defines.hpp>
16 
17 namespace anna {
18 
30 template < typename T, typename SortBy, typename TKey = int > class SortedVector : public std::map <TKey, T*> {
31 public:
32  typedef typename std::map <TKey, T*> container;
33  typedef typename container::iterator iterator;
34  typedef typename container::const_iterator const_iterator;
35  typedef typename container::value_type value_type;
36 
41 
46  explicit SortedVector(const SortedVector& other) :
47  container(other) {}
48 
52  virtual ~SortedVector() { container::clear(); }
53 
61  bool contains(const T* t) const
62  {
63  if(t == NULL)
64  return false;
65 
66  TKey key(SortBy::value(t));
67  return find(key) != NULL;
68  }
69 
75  bool add(T* t)
76  noexcept(false) {
77  if(t == NULL)
78  return false;
79 
80  TKey key(SortBy::value(t));
81  value_type v(key, t);
82  std::pair <iterator, bool> result = container::insert(v);
83  return result.second;
84  }
85 
91  bool erase(T* t)
92  noexcept(false) {
93  if(t == NULL)
94  return false;
95 
96  bool result = false;
97  TKey key(SortBy::value(t));
98  iterator ii = container::find(key);
99 
100  if(ii != container::end()) {
101  container::erase(ii);
102  result = true;
103  }
104 
105  return result;
106  }
107 
113  T* find(const TKey key)
114  {
115  iterator ii = container::find(key);
116  return (ii == container::end()) ? NULL : ii->second;
117  }
118 
124  const T* find(const TKey key) const {
125  return const_cast <SortedVector <T, SortBy, TKey>*>(this)->find(key);
126  }
127 
132  static T* data(iterator ii) { return ii->second; }
133 
138  static const T* data(const_iterator ii) { return ii->second; }
139 };
140 
141 }
142 
143 #endif
container::iterator iterator
Definition: SortedVector.hpp:33
container::value_type value_type
Definition: SortedVector.hpp:35
const T * find(const TKey key) const
Definition: SortedVector.hpp:124
std::map< TKey, T * > container
Definition: SortedVector.hpp:32
static T * data(iterator ii)
Definition: SortedVector.hpp:132
SortedVector()
Definition: SortedVector.hpp:40
bool contains(const T *t) const
Definition: SortedVector.hpp:61
Definition: SortedVector.hpp:30
bool erase(T *t) noexcept(false)
Definition: SortedVector.hpp:91
static const T * data(const_iterator ii)
Definition: SortedVector.hpp:138
SortedVector(const SortedVector &other)
Definition: SortedVector.hpp:46
virtual ~SortedVector()
Definition: SortedVector.hpp:52
container::const_iterator const_iterator
Definition: SortedVector.hpp:34
Definition: app.hpp:12
bool add(T *t) noexcept(false)
Definition: SortedVector.hpp:75
T * find(const TKey key)
Definition: SortedVector.hpp:113