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


HashMap.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 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 public:
20  typedef std::pair<KEY, VALUE> key_value;
21  typedef boost::unordered_map<KEY, VALUE, HASH, EQUAL> map_type;
22  typedef typename map_type::iterator iterator;
23  typedef typename map_type::const_iterator const_iterator;
24  typedef KEY key_type;
25  typedef VALUE value_type;
26 
27  virtual ~HashMap() {
28  }
29 
30 protected:
31  boost::shared_ptr<map_type> mapContainer;
32 
33 public:
34  static this_type newInstance() {
35  this_type instance;
36  instance.mapContainer = Lucene::newInstance<map_type>();
37  return instance;
38  }
39 
40  void reset() {
41  mapContainer.reset();
42  }
43 
44  int32_t size() const {
45  return (int32_t)mapContainer->size();
46  }
47 
48  bool empty() const {
49  return mapContainer->empty();
50  }
51 
52  void clear() {
53  mapContainer->clear();
54  }
55 
56  iterator begin() {
57  return mapContainer->begin();
58  }
59 
60  iterator end() {
61  return mapContainer->end();
62  }
63 
64  const_iterator begin() const {
65  return mapContainer->begin();
66  }
67 
68  const_iterator end() const {
69  return mapContainer->end();
70  }
71 
72  operator bool() const {
73  return mapContainer.get() != NULL;
74  }
75 
76  bool operator! () const {
77  return !mapContainer;
78  }
79 
80  map_type& operator= (const map_type& other) {
81  mapContainer = other.mapContainer;
82  return *this;
83  }
84 
85  void put(const KEY& key, const VALUE& value) {
86  (*mapContainer)[key] = value;
87  }
88 
89  template <class ITER>
90  void putAll(ITER first, ITER last) {
91  for (iterator current = first; current != last; ++current) {
92  (*mapContainer)[current->first] = current->second;
93  }
94  }
95 
96  template <class ITER>
97  void remove(ITER pos) {
98  mapContainer->erase(pos);
99  }
100 
101  template <class ITER>
102  ITER remove(ITER first, ITER last) {
103  return mapContainer->erase(first, last);
104  }
105 
106  bool remove(const KEY& key) {
107  return (mapContainer->erase(key) > 0);
108  }
109 
110  iterator find(const KEY& key) {
111  return mapContainer->find(key);
112  }
113 
114  VALUE get(const KEY& key) const {
115  iterator findValue = mapContainer->find(key);
116  return findValue == mapContainer->end() ? VALUE() : findValue->second;
117  }
118 
119  bool contains(const KEY& key) const {
120  return (mapContainer->find(key) != mapContainer->end());
121  }
122 
123  VALUE& operator[] (const KEY& key) {
124  return (*mapContainer)[key];
125  }
126 };
127 
129 template < class KEY, class VALUE, class HASH = boost::hash<KEY>, class EQUAL = std::equal_to<KEY> >
130 class WeakHashMap : public HashMap<KEY, VALUE, HASH, EQUAL> {
131 public:
133  typedef std::pair<KEY, VALUE> key_value;
134  typedef typename boost::unordered_map<KEY, VALUE, HASH, EQUAL> map_type;
135  typedef typename map_type::iterator iterator;
136 
137  static this_type newInstance() {
138  this_type instance;
139  instance.mapContainer = Lucene::newInstance<map_type>();
140  return instance;
141  }
142 
143  void removeWeak() {
144  if (!this->mapContainer || this->mapContainer->empty()) {
145  return;
146  }
147  map_type clearCopy;
148  for (iterator key = this->mapContainer->begin(); key != this->mapContainer->end(); ++key) {
149  if (!key->first.expired()) {
150  clearCopy.insert(*key);
151  }
152  }
153  this->mapContainer->swap(clearCopy);
154  }
155 
156  VALUE get(const KEY& key) {
157  iterator findValue = this->mapContainer->find(key);
158  if (findValue != this->mapContainer->end()) {
159  return findValue->second;
160  }
161  removeWeak();
162  return VALUE();
163  }
164 };
165 
166 }
167 
168 #endif
iterator find(const KEY &key)
Definition: HashMap.h:110
const_iterator begin() const
Definition: HashMap.h:64
WeakHashMap< KEY, VALUE, HASH, EQUAL > this_type
Definition: HashMap.h:132
HashMap< KEY, VALUE, HASH, EQUAL > this_type
Definition: HashMap.h:19
Utility template class to handle hash maps that can be safely copied and shared.
Definition: HashMap.h:17
void put(const KEY &key, const VALUE &value)
Definition: HashMap.h:85
boost::shared_ptr< map_type > mapContainer
Definition: HashMap.h:31
static this_type newInstance()
Definition: HashMap.h:137
std::pair< KEY, VALUE > key_value
Definition: HashMap.h:133
Utility template class to handle weak keyed maps.
Definition: HashMap.h:130
map_type::iterator iterator
Definition: HashMap.h:135
void clear()
Definition: HashMap.h:52
Base class for all Lucene synchronised classes.
Definition: LuceneSync.h:15
void putAll(ITER first, ITER last)
Definition: HashMap.h:90
void reset()
Definition: HashMap.h:40
map_type & operator=(const map_type &other)
Definition: HashMap.h:80
iterator end()
Definition: HashMap.h:60
static this_type newInstance()
Definition: HashMap.h:34
VALUE value_type
Definition: HashMap.h:25
bool contains(const KEY &key) const
Definition: HashMap.h:119
const_iterator end() const
Definition: HashMap.h:68
Definition: AbstractAllTermDocs.h:12
int32_t size() const
Definition: HashMap.h:44
boost::unordered_map< KEY, VALUE, HASH, EQUAL > map_type
Definition: HashMap.h:21
KEY key_type
Definition: HashMap.h:24
std::pair< KEY, VALUE > key_value
Definition: HashMap.h:20
iterator begin()
Definition: HashMap.h:56
void removeWeak()
Definition: HashMap.h:143
map_type::iterator iterator
Definition: HashMap.h:22
map_type::const_iterator const_iterator
Definition: HashMap.h:23
virtual ~HashMap()
Definition: HashMap.h:27
bool operator!() const
Definition: HashMap.h:76
bool empty() const
Definition: HashMap.h:48
VALUE & operator[](const KEY &key)
Definition: HashMap.h:123
boost::unordered_map< KEY, VALUE, HASH, EQUAL > map_type
Definition: HashMap.h:134

clucene.sourceforge.net