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_VoidList_
8 #define _lucene_util_VoidList_
9
10 #if defined(_LUCENE_PRAGMA_ONCE)
11 # pragma once
12 #endif
13
14 #include "Equators.h"
15
CL_NS_DEF(util)16 CL_NS_DEF(util)
17
18 /**
19 * A template to encapsulate various list type classes
20 * @internal
21 */
22 template<typename _kt,typename base,typename _valueDeletor>
23 class __CLList:public base,LUCENE_BASE {
24 private:
25 bool dv;
26 public:
27 DEFINE_MUTEX(THIS_LOCK)
28
29 typedef typename base::const_iterator const_iterator;
30 typedef typename base::iterator iterator;
31
32 virtual ~__CLList(){
33 clear();
34 }
35
36 __CLList ( const bool deleteValue ):
37 dv(deleteValue)
38 {
39 }
40
41 void setDoDelete(bool val){ dv=val; }
42
43 //sets array to the contents of this array.
44 //array must be size+1, otherwise memory may be overwritten
45 void toArray(_kt* into) const{
46 int i=0;
47 for ( const_iterator itr=base::begin();itr!=base::end();itr++ ){
48 into[i] = *itr;
49 i++;
50 }
51 into[i] = NULL;
52 }
53
54 void set(int32_t i, _kt val) {
55 if ( dv )
56 _valueDeletor::doDelete((*this)[i]);
57 (*this)[i] = val;
58 }
59
60 //todo: check this
61 void delete_back(){
62 if ( base::size() > 0 ){
63 iterator itr = base::end();
64 if ( itr != base::begin())
65 itr --;
66 _kt key = *itr;
67 base::erase(itr);
68 if ( dv )
69 _valueDeletor::doDelete(key);
70 }
71 }
72
73 void delete_front(){
74 if ( base::size() > 0 ){
75 iterator itr = base::begin();
76 _kt key = *itr;
77 base::erase(itr);
78 if ( dv )
79 _valueDeletor::doDelete(key);
80 }
81 }
82
83 void clear(){
84 if ( dv ){
85 iterator itr = base::begin();
86 while ( itr != base::end() ){
87 _valueDeletor::doDelete(*itr);
88 ++itr;
89 }
90 }
91 base::clear();
92 }
93
94 void remove(int32_t i, bool dontDelete=false){
95 iterator itr=base::begin();
96 itr+=i;
97 _kt key = *itr;
98 base::erase( itr );
99 if ( dv && !dontDelete )
100 _valueDeletor::doDelete(key);
101 }
102 void remove(iterator itr, bool dontDelete=false){
103 _kt key = *itr;
104 base::erase( itr );
105 if ( dv && !dontDelete )
106 _valueDeletor::doDelete(key);
107 }
108
109 };
110
111 //growable arrays of Objects (like a collection or list)
112 //a list, so can contain duplicates
113 //it grows in chunks... todo: check jlucene for initial size of array, and growfactors
114 template<typename _kt, typename _valueDeletor=CL_NS(util)::Deletor::Dummy>
115 class CLVector:public __CLList<_kt, CL_NS_STD(vector)<_kt> , _valueDeletor>
116 {
117 public:
118 CLVector ( const bool deleteValue=true ):
119 __CLList<_kt, CL_NS_STD(vector)<_kt> , _valueDeletor>(deleteValue)
120 {
121 }
122 };
123
124 //An array-backed implementation of the List interface
125 //a list, so can contain duplicates
126 //*** a very simple list - use <valarray>
127 //(This class is roughly equivalent to Vector, except that it is unsynchronized.)
128 #define CLArrayList CLVector
129 #define CLHashSet CLHashList
130
131 //implementation of the List interface, provides access to the first and last list elements in O(1)
132 //no comparator is required... and so can contain duplicates
133 //a simple list with no comparator
134 //*** a very simple list - use <list>
135 #ifdef LUCENE_DISABLE_HASHING
136 #define CLHashList CLSetList
137 #else
138
139 template<typename _kt,
140 typename _Comparator=CL_NS(util)::Compare::TChar,
141 typename _valueDeletor=CL_NS(util)::Deletor::Dummy>
142 class CLHashList:public __CLList<_kt, CL_NS_HASHING(hash_set)<_kt,_Comparator> , _valueDeletor>
143 {
144 public:
145 CLHashList ( const bool deleteValue=true ):
146 __CLList<_kt, CL_NS_HASHING(hash_set)<_kt,_Comparator> , _valueDeletor>(deleteValue)
147 {
148 }
149 };
150 #endif
151
152 template<typename _kt, typename _valueDeletor=CL_NS(util)::Deletor::Dummy>
153 class CLLinkedList:public __CLList<_kt, CL_NS_STD(list)<_kt> , _valueDeletor>
154 {
155 public:
156 CLLinkedList ( const bool deleteValue=true ):
157 __CLList<_kt, CL_NS_STD(list)<_kt> , _valueDeletor>(deleteValue)
158 {
159 }
160 };
161 template<typename _kt,
162 typename _Comparator=CL_NS(util)::Compare::TChar,
163 typename _valueDeletor=CL_NS(util)::Deletor::Dummy>
164 class CLSetList:public __CLList<_kt, CL_NS_STD(set)<_kt,_Comparator> , _valueDeletor>
165 {
166 public:
167 CLSetList ( const bool deleteValue=true ):
168 __CLList<_kt, CL_NS_STD(set)<_kt,_Comparator> , _valueDeletor>(deleteValue)
169 {
170 }
171 };
172
173 CL_NS_END
174 #endif
175