Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnOrderedMap.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnOrderedMap.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 #include <map>
17 
18 using namespace std;
19 
20 // drwnOrderedMap -----------------------------------------------------------
38 
39 template<typename KeyType, typename ValueType>
41  protected:
42  vector<pair<KeyType, ValueType> *> _data;
43  map<KeyType, unsigned> _index;
44 
45  public:
51  ~drwnOrderedMap();
52 
54  void clear();
56  size_t size() const { return _data.size(); };
60  void insert(const KeyType& key, const ValueType& v);
62  void erase(const KeyType& key);
64  int find(const KeyType& key) const;
65 
68 
70  const ValueType& operator[](unsigned int indx) const;
72  ValueType& operator[](unsigned int indx);
74  const ValueType& operator[](const KeyType& key) const;
76  ValueType& operator[](const KeyType& key);
77 };
78 
79 // implementation -----------------------------------------------------------
80 
81 template<typename KeyType, typename ValueType>
83 {
84  // do nothing
85 }
86 
87 template<typename KeyType, typename ValueType>
89  _index(m._index)
90 {
91  // deep copy data
92  _data.reserve(m._data.size());
93  for (typename vector<pair<KeyType, ValueType> *>::const_iterator it = m._data.begin();
94  it != m._data.end(); it++) {
95  _data.push_back(new pair<KeyType, ValueType>(*it));
96  }
97 }
98 
99 template<typename KeyType, typename ValueType>
101 {
102  clear(); // delete entries
103 }
104 
105 template<typename KeyType, typename ValueType>
107 {
108  for (typename vector<pair<KeyType, ValueType> *>::iterator it = _data.begin();
109  it != _data.end(); it++) {
110  delete *it;
111  }
112  _data.clear();
113  _index.clear();
114 }
115 
116 
117 template<typename KeyType, typename ValueType>
118 void drwnOrderedMap<KeyType, ValueType>::insert(const KeyType& key, const ValueType& v)
119 {
120  typename map<KeyType, unsigned>::iterator it = _index.find(key);
121  if (it != _index.end()) {
122  _data[it->second]->second = v;
123  } else {
124  _index.insert(it, make_pair(key, (unsigned)_data.size()));
125  _data.push_back(new pair<KeyType, ValueType>(key, v));
126  }
127 }
128 
129 template<typename KeyType, typename ValueType>
131 {
132  typename map<KeyType, unsigned>::iterator it = _index.find(key);
133  DRWN_ASSERT(it != _index.end());
134 
135  // re-order vector
136  delete _data[it->second];
137  for (unsigned i = it->second + 1; i < _data.size(); i++) {
138  _data[i - 1] = _data[i];
139  }
140  _data.pop_back();
141 
142  // re-assign mapping
143  for (typename map<KeyType, unsigned>::iterator jt = _index.begin();
144  jt != _index.end(); jt++) {
145  if (jt->second > it->second)
146  jt->second -= 1;
147  }
148  _index.erase(it);
149 }
150 
151 template<typename KeyType, typename ValueType>
152 int drwnOrderedMap<KeyType, ValueType>::find(const KeyType& key) const
153 {
154  typename map<KeyType, unsigned>::const_iterator it = _index.find(key);
155  if (it == _index.end()) {
156  return -1;
157  } else {
158  return (int)it->second;
159  }
160 }
161 
162 template<typename KeyType, typename ValueType>
164 {
165  if (&m != this) {
166  clear();
167  _data.reserve(m._data.size());
168  for (typename vector<pair<KeyType, ValueType> *>::const_iterator it = m._data.begin();
169  it != m._data.end(); it++) {
170  _data.push_back(new pair<KeyType, ValueType>(*it));
171  }
172  _index = m._index;
173  }
174 
175  return *this;
176 }
177 
178 template<typename KeyType, typename ValueType>
179 const ValueType& drwnOrderedMap<KeyType, ValueType>::operator[](unsigned int indx) const
180 {
181  return _data[indx]->second;
182 }
183 
184 template<typename KeyType, typename ValueType>
186 {
187  return _data[indx]->second;
188 }
189 
190 template<typename KeyType, typename ValueType>
191 const ValueType& drwnOrderedMap<KeyType, ValueType>::operator[](const KeyType& key) const
192 {
193  typename map<KeyType, unsigned>::const_iterator it = _index.find(key);
194  return _data[it->second]->second;
195 }
196 
197 template<typename KeyType, typename ValueType>
199 {
200  typename map<KeyType, unsigned>::iterator it = _index.find(key);
201  if (it == _index.end()) {
202  _index.insert(it, make_pair(key, (unsigned)_data.size()));
203  _data.push_back(new pair<KeyType, ValueType>(key, ValueType(0)));
204  return _data.back()->second;
205  }
206 
207  return _data[it->second]->second;
208 }
209 
void clear()
clear all entries from the map
Definition: drwnOrderedMap.h:106
size_t size() const
returns the number of entries in the map
Definition: drwnOrderedMap.h:56
~drwnOrderedMap()
destructor
Definition: drwnOrderedMap.h:100
void erase(const KeyType &key)
remove the entry in the map corresponding to key key
Definition: drwnOrderedMap.h:130
drwnOrderedMap()
default constructor
Definition: drwnOrderedMap.h:82
drwnOrderedMap< KeyType, ValueType > & operator=(const drwnOrderedMap< KeyType, ValueType > &m)
assignment operator
Definition: drwnOrderedMap.h:163
void insert(const KeyType &key, const ValueType &v)
Inserts value v into the map at the end position with key key. If the key already exists then the val...
Definition: drwnOrderedMap.h:118
int find(const KeyType &key) const
find the index of an entry in the map (-1 if the key does not exist)
Definition: drwnOrderedMap.h:152
Provides a datastructure for that can be indexed by a KeyType (usually a string) or unsigned integer...
Definition: drwnOrderedMap.h:40
const ValueType & operator[](unsigned int indx) const
index the indx-th entry in the map
Definition: drwnOrderedMap.h:179