i6engine  1.0
sequence_map.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_SEQUENCEMAP_H__
26 #define __I6ENGINE_UTILS_SEQUENCEMAP_H__
27 
28 #include <cassert>
29 #include <list>
30 #include <unordered_map>
31 
32 namespace i6e {
33 namespace utils {
34 
46  template<typename KeyType, typename ValueType, typename Hash = std::hash<KeyType>>
47  class sequence_map {
48  public:
49  typedef typename std::list<std::pair<KeyType, ValueType>>::const_iterator const_iterator;
50  typedef typename std::list<std::pair<KeyType, ValueType>>::iterator iterator;
51 
55  sequence_map() : _map(), _list() {
56  }
57 
61  ValueType & operator[](const KeyType & key) {
62  if (_map.find(key) == _map.end()) {
63  _list.push_back(std::make_pair(key, ValueType()));
64  _map[key] = &(_list.back().second);
65  }
66  return *_map[key];
67  }
68 
72  std::size_t size() const {
73  return _map.size();
74  }
75 
79  std::size_t erase(const KeyType & key) {
80  _map.erase(key);
81  for (auto it = _list.begin(); it != _list.end(); it++) {
82  if (it->first == key) {
83  _list.erase(it);
84  return 1;
85  }
86  }
87  return 0;
88  }
89 
93  const_iterator erase(const_iterator position) {
94  _map.erase(position->first);
95 #if ISIXE_MPLATFORM == ISIXE_MPLATFORM_WIN32
96  return _list.erase(position);
97 #elif ISIXE_MPLATFORM == ISIXE_MPLATFORM_LINUX
98  // This is O(n) but as of a bug in libstdc++, there is no list::erase for const_iterator
99  // See http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011 Section 23.3.5 for current support
100  for (iterator it = _list.begin(); it != _list.end(); ++it) {
101  if (it->first == position->first) {
102  return _list.erase(it);
103  }
104  }
105  return _list.end();
106 #endif
107  }
108 
112  typename std::list<std::pair<KeyType, ValueType>>::iterator find(const KeyType & key) {
113  if (_map.find(key) == _map.end()) {
114  return _list.end();
115  }
116  for (typename std::list<std::pair<KeyType, ValueType>>::iterator it = _list.begin(); it != _list.end(); ++it) {
117  if (it->first == key) {
118  return it;
119  }
120  }
121  assert(false); // key appears in map but not in list
122  return _list.end();
123  }
124 
125  typename std::list<std::pair<KeyType, ValueType>>::const_iterator find(const KeyType & key) const {
126  if (_map.find(key) == _map.end()) {
127  return _list.end();
128  }
129  for (typename std::list<std::pair<KeyType, ValueType>>::const_iterator it = _list.begin(); it != _list.end(); ++it) {
130  if (it->first == key) {
131  return it;
132  }
133  }
134  assert(false); // key appears in map but not in list
135  return _list.end();
136  }
137 
141  typename std::list<std::pair<KeyType, ValueType>>::iterator begin() {
142  return _list.begin();
143  }
144 
148  typename std::list<std::pair<KeyType, ValueType>>::const_iterator begin() const {
149  return _list.begin();
150  }
151 
155  typename std::list<std::pair<KeyType, ValueType>>::iterator end() {
156  return _list.end();
157  }
158 
162  typename std::list<std::pair<KeyType, ValueType>>::const_iterator end() const {
163  return _list.end();
164  }
165 
169  typename std::list<std::pair<KeyType, ValueType>>::const_iterator cbegin() const {
170  return _list.cbegin();
171  }
172 
176  typename std::list<std::pair<KeyType, ValueType>>::const_iterator cend() const {
177  return _list.cend();
178  }
179 
183  void clear() {
184  _map.clear();
185  _list.clear();
186  }
187 
188  private:
189  std::unordered_map<KeyType, ValueType *, Hash> _map;
190 
191  std::list<std::pair<KeyType, ValueType>> _list;
192  };
193 
194 } /* namespace utils */
195 } /* namespace i6e */
196 
197 #endif /* __I6ENGINE_UTILS_SEQUENCEMAP_H__ */
198 
A map with linear access time and an iterator iterating through the elements in creation time...
Definition: sequence_map.h:47
std::list< std::pair< KeyType, ValueType > >::const_iterator begin() const
returns const_iterator to the begin of the list
Definition: sequence_map.h:148
std::list< std::pair< KeyType, ValueType > >::iterator find(const KeyType &key)
returns iterator to found entry for key, otherwise end()
Definition: sequence_map.h:112
std::list< std::pair< KeyType, ValueType > >::const_iterator cend() const
returns const_iterator to the end of the list
Definition: sequence_map.h:176
std::size_t size() const
returns size of the map
Definition: sequence_map.h:72
std::list< std::pair< int64_t, ComPtr > >::iterator iterator
Definition: sequence_map.h:50
std::list< std::pair< KeyType, ValueType > >::const_iterator find(const KeyType &key) const
Definition: sequence_map.h:125
const_iterator erase(const_iterator position)
remove value contained by iterator
Definition: sequence_map.h:93
void clear()
clears map
Definition: sequence_map.h:183
std::size_t erase(const KeyType &key)
removes value for given key, returns 1 if successful, otherwise 0
Definition: sequence_map.h:79
std::list< std::pair< int64_t, ComPtr > >::const_iterator const_iterator
Definition: sequence_map.h:49
std::list< std::pair< KeyType, ValueType > >::const_iterator end() const
returns const_iterator to the end of the list
Definition: sequence_map.h:162
sequence_map()
constructor
Definition: sequence_map.h:55
std::list< std::pair< KeyType, ValueType > >::iterator end()
returns iterator to the end of the list
Definition: sequence_map.h:155
std::list< std::pair< KeyType, ValueType > >::const_iterator cbegin() const
returns const_iterator to the begin of the list
Definition: sequence_map.h:169
std::list< std::pair< KeyType, ValueType > >::iterator begin()
returns iterator to the begin of the list
Definition: sequence_map.h:141
ValueType & operator[](const KeyType &key)
acess operator with [] using key, returns
Definition: sequence_map.h:61