Lucene++ - a full-featured, c++ search engine
API Documentation


 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
HashMap.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6 
7 #ifndef HASHMAP_H
8 #define HASHMAP_H
9 
10 #include <boost/unordered_map.hpp>
11 #include "LuceneSync.h"
12 
13 namespace Lucene
14 {
16  template < class KEY, class VALUE, class HASH = boost::hash<KEY>, class EQUAL = std::equal_to<KEY> >
17  class HashMap : public LuceneSync
18  {
19  public:
21  typedef std::pair<KEY, VALUE> key_value;
22  typedef boost::unordered_map< KEY, VALUE, HASH, EQUAL, LuceneAllocator<key_value> > map_type;
23  typedef typename map_type::iterator iterator;
24  typedef typename map_type::const_iterator const_iterator;
25  typedef KEY key_type;
26  typedef VALUE value_type;
27 
28  virtual ~HashMap()
29  {
30  }
31 
32  protected:
33  boost::shared_ptr<map_type> mapContainer;
34 
35  public:
37  {
38  this_type instance;
39  instance.mapContainer = Lucene::newInstance<map_type>();
40  return instance;
41  }
42 
43  void reset()
44  {
45  mapContainer.reset();
46  }
47 
48  int32_t size() const
49  {
50  return (int32_t)mapContainer->size();
51  }
52 
53  bool empty() const
54  {
55  return mapContainer->empty();
56  }
57 
58  void clear()
59  {
60  mapContainer->clear();
61  }
62 
64  {
65  return mapContainer->begin();
66  }
67 
69  {
70  return mapContainer->end();
71  }
72 
74  {
75  return mapContainer->begin();
76  }
77 
79  {
80  return mapContainer->end();
81  }
82 
83  operator bool() const
84  {
85  return mapContainer;
86  }
87 
88  bool operator! () const
89  {
90  return !mapContainer;
91  }
92 
93  map_type& operator= (const map_type& other)
94  {
95  mapContainer = other.mapContainer;
96  return *this;
97  }
98 
99  void put(const KEY& key, const VALUE& value)
100  {
101  (*mapContainer)[key] = value;
102  }
103 
104  template <class ITER>
105  void putAll(ITER first, ITER last)
106  {
107  for (iterator current = first; current != last; ++current)
108  (*mapContainer)[current->first] = current->second;
109  }
110 
111  template <class ITER>
112  void remove(ITER pos)
113  {
114  mapContainer->erase(pos);
115  }
116 
117  template <class ITER>
118  ITER remove(ITER first, ITER last)
119  {
120  return mapContainer->erase(first, last);
121  }
122 
123  bool remove(const KEY& key)
124  {
125  return (mapContainer->erase(key) > 0);
126  }
127 
128  iterator find(const KEY& key)
129  {
130  return mapContainer->find(key);
131  }
132 
133  VALUE get(const KEY& key) const
134  {
135  iterator findValue = mapContainer->find(key);
136  return findValue == mapContainer->end() ? VALUE() : findValue->second;
137  }
138 
139  bool contains(const KEY& key) const
140  {
141  return (mapContainer->find(key) != mapContainer->end());
142  }
143 
144  VALUE& operator[] (const KEY& key)
145  {
146  return (*mapContainer)[key];
147  }
148  };
149 
151  template < class KEY, class VALUE, class HASH = boost::hash<KEY>, class EQUAL = std::equal_to<KEY> >
152  class WeakHashMap : public HashMap<KEY, VALUE, HASH, EQUAL>
153  {
154  public:
156  typedef std::pair<KEY, VALUE> key_value;
157  typedef typename boost::unordered_map< KEY, VALUE, HASH, EQUAL, LuceneAllocator<key_value> > map_type;
158  typedef typename map_type::iterator iterator;
159 
161  {
162  this_type instance;
163  instance.mapContainer = Lucene::newInstance<map_type>();
164  return instance;
165  }
166 
167  void removeWeak()
168  {
169  if (!this->mapContainer || this->mapContainer->empty())
170  return;
171  map_type clearCopy;
172  for (iterator key = this->mapContainer->begin(); key != this->mapContainer->end(); ++key)
173  {
174  if (!key->first.expired())
175  clearCopy.insert(*key);
176  }
177  this->mapContainer->swap(clearCopy);
178  }
179 
180  VALUE get(const KEY& key)
181  {
182  iterator findValue = this->mapContainer->find(key);
183  if (findValue != this->mapContainer->end())
184  return findValue->second;
185  removeWeak();
186  return VALUE();
187  }
188  };
189 }
190 
191 #endif

clucene.sourceforge.net