OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HandleMap.h
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/HandleMap.h
3  *
4  * Copyright (C) 2002-2007 Novamente LLC
5  * All Rights Reserved
6  *
7  * Written by Thiago Maia <thiago@vettatech.com>
8  * Andre Senna <senna@vettalabs.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License v3 as
12  * published by the Free Software Foundation and including the exceptions
13  * at http://opencog.org/wiki/Licenses
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program; if not, write to:
22  * Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
30 #ifndef _OPENCOG_HANDLE_MAP_H
31 #define _OPENCOG_HANDLE_MAP_H
32 
33 #include <map>
34 #include <memory>
35 #include <mutex>
36 
38 #include <opencog/util/exceptions.h>
39 
40 namespace opencog
41 {
46 template<class T> class HandleMapIterator;
47 
49 
52 template<class T>
53 class HandleMap
54  : public std::enable_shared_from_this<HandleMap<T>>
55 {
56  friend class HandleMapIterator<T>;
57 
58 private:
59 
63  typedef typename std::map< Handle, T, std::less<Handle> > InternalMap;
64  typedef typename InternalMap::iterator InternalIterator;
65 
70 
74  std::mutex _mtx;
75 
76 public:
77 
78  typedef std::shared_ptr<HandleMap<T>> MapPtr;
79  HandleMap() {}
80 
87  void add(Handle key, T element) throw (RuntimeException)
88  {
89  std::lock_guard<std::mutex> lck(_mtx);
90 
91  // check if the element is already in the hash table. If not, it
92  // is added to the head of the list for that position
93  InternalIterator ti = _handle_map.find(key);
94  if (ti == _handle_map.end()) {
95  (_handle_map)[key] = element;
96  } else {
97  throw RuntimeException(TRACE_INFO,
98  "attempting to insert duplicated key %ld in hash map",
99  key.value());
100  }
101  }
102 
109  T get(Handle key)
110  {
111  std::lock_guard<std::mutex> lck(_mtx);
112 
113  // assert the key exists. Otherwise throws an exception.
114  InternalIterator ti = _handle_map.find(key);
115  if (ti == _handle_map.end())
116  {
117  throw AssertionException(
118  "HandleMap: key (%ld) does not exist in this map",
119  key.value());
120  }
121  return ti->second;
122  }
123 
124 
131  bool contains(Handle key)
132  {
133  std::lock_guard<std::mutex> lck(_mtx);
134  return _handle_map.find(key) != _handle_map.end();
135  }
136 
144  T remove(Handle key)
145  {
146  std::lock_guard<std::mutex> lck(_mtx);
147 
148  T ret = NULL;
149  InternalIterator ti = _handle_map.find(key);
150  if (ti != _handle_map.end()) {
151  ret = ti->second;
152  _handle_map.erase(ti);
153  }
154 
155  // returns the removed element.
156  return ret;
157  }
158 
164  void resize(int newSize)
165  {
166  std::lock_guard<std::mutex> lck(_mtx);
167  _handle_map.resize(newSize);
168  }
169 
175  int get_count()
176  {
177  std::lock_guard<std::mutex> lck(_mtx);
178  return _handle_map.size();
179  }
180 
187  int get_size()
188  {
189  std::lock_guard<std::mutex> lck(_mtx);
190 
191  // max_size = handle_map->bucket_count();
192  return _handle_map.size();
193  }
194 
201  {
202  MapPtr mp = std::enable_shared_from_this<HandleMap<T>>::shared_from_this();
203  return new HandleMapIterator<T>(mp);
204  }
205 
206 };
207 
208 template<class T>
209 class HandleMapIterator
210 {
211  friend class HandleMap<T>;
212 
213 private:
214 
218  typedef typename std::map<Handle, T>::iterator iter_type;
220 
224  typedef std::shared_ptr<HandleMap<T>> MapPtr;
226 
233  {
234  map = m;
235  std::lock_guard<std::mutex> lck(map->_mtx);
236  current = map->_handle_map.begin();
237  }
238 
239 public:
240 
246  bool has_next()
247  {
248  std::lock_guard<std::mutex> lck(map->_mtx);
249  return current != map->_handle_map.end();
250  }
251 
252 
258  Handle next() throw (IndexErrorException)
259  {
260  if (!has_next()) {
261  throw IndexErrorException(TRACE_INFO, "HandleMapIterator out of bounds");
262  }
263 
264  std::lock_guard<std::mutex> lck(map->_mtx);
265 
266  Handle ret = current->first;
267  ++current;
268  return ret;
269  }
270 
271 };
272 
274 } // namespace opencog
275 
276 #endif // _OPENCOG_HANDLE_MAP_H
bool contains(Handle key)
Definition: HandleMap.h:131
std::mutex _mtx
Definition: HandleMap.h:74
void resize(int newSize)
Definition: HandleMap.h:164
std::shared_ptr< HandleMap< T > > MapPtr
Definition: HandleMap.h:78
red-black tree maps Handle keys to type T elements
Definition: HandleMap.h:53
HandleMapIterator< T > * keys()
Definition: HandleMap.h:200
void add(Handle key, T element)
Definition: HandleMap.h:87
InternalMap _handle_map
Definition: HandleMap.h:69
std::shared_ptr< HandleMap< T > > MapPtr
Definition: HandleMap.h:224
std::map< Handle, T, std::less< Handle > > InternalMap
Definition: HandleMap.h:63
InternalMap::iterator InternalIterator
Definition: HandleMap.h:64
std::map< Handle, T >::iterator iter_type
Definition: HandleMap.h:218