1/*------------------------------------------------------------------------------
2* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3*
4* Distributable under the terms of either the Apache License (Version 2.0) or
5* the GNU Lesser General Public License, as specified in the COPYING file.
6------------------------------------------------------------------------------*/
7#ifndef _lucene_util_VoidMap_
8#define _lucene_util_VoidMap_
9
10#if defined(_LUCENE_PRAGMA_ONCE)
11# pragma once
12#endif
13
14
15CL_NS_DEF(util)
16
17/**
18* A template to encapsulate various map type classes
19* @internal
20*/
21template<typename _kt, typename _vt,
22 typename base,
23 typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
24 typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
25class __CLMap: public base, LUCENE_BASE {
26private:
27 bool dk;
28 bool dv;
29public:
30 DEFINE_MUTEX(THIS_LOCK)
31
32 typedef typename base::iterator iterator;
33 typedef typename base::const_iterator const_iterator;
34 typedef CL_NS_STD(pair)<_kt, _vt> _pair;
35
36 ///Default constructor for the __CLMap
37 __CLMap ():
38 dk(true),
39 dv(true)
40 {
41 }
42
43 ///Deconstructor for the __CLMap
44 ~__CLMap (){
45 clear();
46 }
47
48 void setDeleteKey(bool val){ dk = val; }
49 void setDeleteValue(bool val){ dv = val; }
50
51 ///Construct the VoidMap and set the deleteTypes to the specified values
52 ///\param deleteKey if true then the key variable is deleted when an object is deleted
53 ///\param keyDelType delete the key variable using the specified type
54 ///\param deleteValue if true then the value variable is deleted when an object is deleted
55 ///\param valueDelType delete the value variable using the specified type
56 /*__CLMap ( const bool deleteKey, const bool deleteValue ):
57 dk(deleteKey),
58 dv(deleteValue)
59 {
60 }*/
61
62 ///checks to see if the specified key exists
63 ///\param k the key to check for
64 ///\returns true if the key exists
65 bool exists(_kt k)const{
66 const_iterator itr = base::find(k);
67 bool ret = itr!=base::end();
68 return ret;
69 }
70
71 ///put the specified pair into the map. remove any old items first
72 ///\param k the key
73 ///\param v the value
74 void put(_kt k,_vt v){
75 //todo: check if this is always right!
76 //must should look through code, for
77 //cases where map is not unique!!!
78 if ( dk || dv )
79 remove(k);
80
81 //todo: replacing the old item might be quicker...
82
83 base::insert(_pair(k,v));
84 }
85
86
87 ///using a non-const key, get a non-const value
88 _vt get( _kt k) const {
89 const_iterator itr = base::find(k);
90 if ( itr==base::end() )
91 return _vt();
92 else
93 return itr->second;
94 }
95 ///using a non-const key, get the actual key
96 _kt getKey( _kt k) const {
97 const_iterator itr = base::find(k);
98 if ( itr==base::end() )
99 return _kt();
100 else
101 return itr->first;
102 }
103
104 void removeitr (iterator itr, const bool dontDeleteKey = false, const bool dontDeleteValue = false){
105 //delete key&val first. This prevents potential loops (deleting object removes itself)
106 _kt key = itr->first;
107 _vt val = itr->second;
108 base::erase(itr);
109
110 //keys & vals need to be deleted after erase, because the hashvalue is still needed
111 if ( dk && !dontDeleteKey )
112 _KeyDeletor::doDelete(key);
113 if ( dv && !dontDeleteValue )
114 _ValueDeletor::doDelete(val);
115 }
116 ///delete and optionally delete the specified key and associated value
117 void remove(_kt key, const bool dontDeleteKey = false, const bool dontDeleteValue = false){
118 iterator itr = base::find(key);
119 if ( itr!=base::end() )
120 removeitr(itr,dontDeleteKey,dontDeleteValue);
121 }
122
123 ///clear all keys and values in the map
124 void clear(){
125 if ( dk || dv ){
126 iterator itr = base::begin();
127 while ( itr!=base::end() ){
128 #ifdef _CL_HAVE_EXT_HASH_MAP
129 removeitr(itr);
130 itr = base::begin();
131
132 #else
133 if ( dk )
134 _KeyDeletor::doDelete(itr->first);
135 if ( dv )
136 _ValueDeletor::doDelete(itr->second);
137 ++itr;
138
139 #endif
140 }
141 }
142 base::clear();
143 }
144};
145
146// makes no guarantees as to the order of the map
147// cannot contain duplicate keys; each key can map to at most one value
148#define CLHashtable CLHashMap
149
150#if defined(_CL_HAVE_GOOGLE_DENSE_HASH_MAP)
151//do nothing
152#elif defined(LUCENE_DISABLE_HASHING)
153
154 //a CLSet with CLHashMap traits
155template<typename _kt, typename _vt,
156 typename _Compare,
157 typename _EqualDummy,
158 typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
159 typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
160class CLHashMap:public __CLMap<_kt,_vt,
161 CL_NS_STD(map)<_kt,_vt, _Compare>,
162 _KeyDeletor,_ValueDeletor>
163{
164 typedef typename CL_NS_STD(map)<_kt,_vt,_Compare> _base;
165 typedef __CLMap<_kt, _vt, CL_NS_STD(map)<_kt,_vt, _Compare>,
166 _KeyDeletor,_ValueDeletor> _this;
167public:
168 CLHashMap ( const bool deleteKey=false, const bool deleteValue=false )
169 {
170 _this::setDeleteKey(deleteKey);
171 _this::setDeleteValue(deleteValue);
172 }
173};
174#elif defined(_CL_HAVE_EXT_HASH_MAP)
175 //ext/hash_map syntax
176//HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
177template<typename _kt, typename _vt,
178 typename _Hasher,
179 typename _Equals,
180 typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
181 typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
182class CLHashMap:public __CLMap<_kt,_vt,
183 CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher,_Equals>,
184 _KeyDeletor,_ValueDeletor>
185{
186 typedef __CLMap<_kt,_vt, CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher,_Equals>,
187 _KeyDeletor,_ValueDeletor> _this;
188public:
189 CLHashMap ( const bool deleteKey=false, const bool deleteValue=false )
190 {
191 _this::setDeleteKey(deleteKey);
192 _this::setDeleteValue(deleteValue);
193 }
194};
195
196#else
197//HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
198template<typename _kt, typename _vt,
199 typename _Hasher,
200 typename _Equals,
201 typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
202 typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
203class CLHashMap:public __CLMap<_kt,_vt,
204 CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher>,
205 _KeyDeletor,_ValueDeletor>
206{
207 typedef __CLMap<_kt,_vt, CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher>,
208 _KeyDeletor,_ValueDeletor> _this;
209public:
210 CLHashMap ( const bool deleteKey=false, const bool deleteValue=false )
211 {
212 _this::setDeleteKey(deleteKey);
213 _this::setDeleteValue(deleteValue);
214 }
215};
216#endif
217
218//A collection that contains no duplicates
219//does not guarantee that the order will remain constant over time
220template<typename _kt, typename _vt,
221 typename _Compare,
222 typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
223 typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
224class CLSet:public __CLMap<_kt,_vt,
225 CL_NS_STD(map)<_kt,_vt, _Compare>,
226 _KeyDeletor,_ValueDeletor>
227{
228 typedef typename CL_NS_STD(map)<_kt,_vt,_Compare> _base;
229 typedef __CLMap<_kt, _vt, CL_NS_STD(map)<_kt,_vt, _Compare>,
230 _KeyDeletor,_ValueDeletor> _this;
231public:
232 CLSet ( const bool deleteKey=false, const bool deleteValue=false )
233 {
234 _this::setDeleteKey(deleteKey);
235 _this::setDeleteValue(deleteValue);
236 }
237};
238
239
240//A collection that can contains duplicates
241template<typename _kt, typename _vt,
242 typename _Compare,
243 typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
244 typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
245class CLMultiMap:public __CLMap<_kt,_vt,
246 CL_NS_STD(multimap)<_kt,_vt>,
247 _KeyDeletor,_ValueDeletor>
248{
249 typedef typename CL_NS_STD(multimap)<_kt,_vt> _base;
250 typedef __CLMap<_kt, _vt, CL_NS_STD(multimap)<_kt,_vt>,
251 _KeyDeletor,_ValueDeletor> _this;
252public:
253 CLMultiMap ( const bool deleteKey=false, const bool deleteValue=false )
254 {
255 _this::setDeleteKey(deleteKey);
256 _this::setDeleteValue(deleteValue);
257 }
258};
259
260
261//*** need to create a class that allows duplicates - use <set>
262//#define CLSet __CLMap
263CL_NS_END
264
265#ifdef _CL_HAVE_GOOGLE_DENSE_HASH_MAP
266#include "GoogleSparseMap.h"
267#endif
268
269#endif
270