1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
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_
10
#if defined(_LUCENE_PRAGMA_ONCE)
18
* A template to encapsulate various map type classes
21
template<typename _kt, typename _vt,
23
typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
24
typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
25
class __CLMap:public _base,LUCENE_BASE {
31
DEFINE_MUTEX(THIS_LOCK)
33
typedef typename _base::iterator iterator;
34
typedef typename _base::const_iterator const_iterator;
35
typedef CL_NS_STD(pair)<_kt, _vt> _pair;
37
///Default constructor for the __CLMap
44
///Deconstructor for the __CLMap
49
void setDeleteKey(bool val){ dk = val; }
50
void setDeleteValue(bool val){ dv = val; }
52
///Construct the VoidMap and set the deleteTypes to the specified values
53
///\param deleteKey if true then the key variable is deleted when an object is deleted
54
///\param keyDelType delete the key variable using the specified type
55
///\param deleteValue if true then the value variable is deleted when an object is deleted
56
///\param valueDelType delete the value variable using the specified type
57
/*__CLMap ( const bool deleteKey, const bool deleteValue ):
63
///checks to see if the specified key exists
64
///\param k the key to check for
65
///\returns true if the key exists
66
bool exists(_kt k)const{
67
const_iterator itr = base::find(k);
68
bool ret = itr!=base::end();
72
///put the specified pair into the map. remove any old items first
75
void put(_kt k,_vt v){
76
//todo: check if this is always right!
77
//must should look through code, for
78
//cases where map is not unique!!!
82
//todo: replacing the old item might be quicker...
84
base::insert(_pair(k,v));
88
///using a non-const key, get a non-const value
89
_vt get( _kt k) const {
90
const_iterator itr = base::find(k);
91
if ( itr==base::end() )
96
///using a non-const key, get the actual key
97
_kt getKey( _kt k) const {
98
const_iterator itr = base::find(k);
99
if ( itr==base::end() )
105
void removeitr (iterator itr, const bool dontDeleteKey = false, const bool dontDeleteValue = false){
106
//delete key&val first. This prevents potential loops (deleting object removes itself)
107
_kt key = itr->first;
108
_vt val = itr->second;
111
//keys & vals need to be deleted after erase, because the hashvalue is still needed
112
if ( dk && !dontDeleteKey )
113
_KeyDeletor::doDelete(key);
114
if ( dv && !dontDeleteValue )
115
_ValueDeletor::doDelete(val);
117
///delete and optionally delete the specified key and associated value
118
void remove(_kt key, const bool dontDeleteKey = false, const bool dontDeleteValue = false){
119
iterator itr = base::find(key);
120
if ( itr!=base::end() )
121
removeitr(itr,dontDeleteKey,dontDeleteValue);
124
///clear all keys and values in the map
127
iterator itr = base::begin();
128
while ( itr!=base::end() ){
129
#ifdef _CL_HAVE_EXT_HASH_MAP
135
_KeyDeletor::doDelete(itr->first);
137
_ValueDeletor::doDelete(itr->second);
147
// makes no guarantees as to the order of the map
148
// cannot contain duplicate keys; each key can map to at most one value
149
#define CLHashtable CLHashMap
151
#if defined(_CL_HAVE_GOOGLE_DENSE_HASH_MAP)
152
//do nothing, implementations is done later...
153
#elif defined(LUCENE_DISABLE_HASHING)
155
//a CLSet with CLHashMap traits
156
template<typename _kt, typename _vt,
158
typename _EqualDummy,
159
typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
160
typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
161
class CLHashMap:public __CLMap<_kt,_vt,
162
CL_NS_STD(map)<_kt,_vt, _Compare>,
163
_KeyDeletor,_ValueDeletor>
165
typedef typename CL_NS_STD(map)<_kt,_vt,_Compare> _base;
166
typedef __CLMap<_kt, _vt, CL_NS_STD(map)<_kt,_vt, _Compare>,
167
_KeyDeletor,_ValueDeletor> _this;
169
CLHashMap ( const bool deleteKey=false, const bool deleteValue=false )
171
_this::setDeleteKey(deleteKey);
172
_this::setDeleteValue(deleteValue);
175
#elif defined(_CL_HAVE_EXT_HASH_MAP)
176
//ext/hash_map syntax
177
//HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
178
template<typename _kt, typename _vt,
181
typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
182
typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
183
class CLHashMap:public __CLMap<_kt,_vt,
184
CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher,_Equals>,
185
_KeyDeletor,_ValueDeletor>
187
typedef __CLMap<_kt,_vt, CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher,_Equals>,
188
_KeyDeletor,_ValueDeletor> _this;
190
CLHashMap ( const bool deleteKey=false, const bool deleteValue=false )
192
_this::setDeleteKey(deleteKey);
193
_this::setDeleteValue(deleteValue);
198
//HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
199
template<typename _kt, typename _vt,
202
typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
203
typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
204
class CLHashMap:public __CLMap<_kt,_vt,
205
CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher>,
206
_KeyDeletor,_ValueDeletor>
208
typedef __CLMap<_kt,_vt, CL_NS_HASHING(hash_map)<_kt,_vt, _Hasher>,
209
_KeyDeletor,_ValueDeletor> _this;
211
CLHashMap ( const bool deleteKey=false, const bool deleteValue=false )
213
_this::setDeleteKey(deleteKey);
214
_this::setDeleteValue(deleteValue);
219
//A collection that contains no duplicates
220
//does not guarantee that the order will remain constant over time
221
template<typename _kt, typename _vt,
223
typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
224
typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
225
class CLSet:public __CLMap<_kt,_vt,
226
CL_NS_STD(map)<_kt,_vt, _Compare>,
227
_KeyDeletor,_ValueDeletor>
229
typedef typename CL_NS_STD(map)<_kt,_vt,_Compare> _base;
230
typedef __CLMap<_kt, _vt, CL_NS_STD(map)<_kt,_vt, _Compare>,
231
_KeyDeletor,_ValueDeletor> _this;
233
CLSet ( const bool deleteKey=false, const bool deleteValue=false )
235
_this::setDeleteKey(deleteKey);
236
_this::setDeleteValue(deleteValue);
241
//A collection that can contains duplicates
242
template<typename _kt, typename _vt,
244
typename _KeyDeletor=CL_NS(util)::Deletor::Dummy,
245
typename _ValueDeletor=CL_NS(util)::Deletor::Dummy>
246
class CLMultiMap:public __CLMap<_kt,_vt,
247
CL_NS_STD(multimap)<_kt,_vt>,
248
_KeyDeletor,_ValueDeletor>
250
typedef typename CL_NS_STD(multimap)<_kt,_vt> _base;
251
typedef __CLMap<_kt, _vt, CL_NS_STD(multimap)<_kt,_vt>,
252
_KeyDeletor,_ValueDeletor> _this;
254
CLMultiMap ( const bool deleteKey=false, const bool deleteValue=false )
256
_this::setDeleteKey(deleteKey);
257
_this::setDeleteValue(deleteValue);
262
//*** need to create a class that allows duplicates - use <set>
263
//#define CLSet __CLMap
266
#ifdef _CL_HAVE_GOOGLE_DENSE_HASH_MAP
267
#include "GoogleSparseMap.h"