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

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Collection.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 COLLECTION_H
8 #define COLLECTION_H
9
10 #include <vector>
11 #include "LuceneSync.h"
12
13 namespace Lucene
14 {
16 template <class TYPE>
17 class Collection : public LuceneSync
18 {
19 public:
20 typedef Collection<TYPE> this_type;
21 typedef boost::shared_ptr<this_type> shared_ptr;
22 typedef std::vector< TYPE, LuceneAllocator<TYPE> > collection_type;
23 typedef typename collection_type::iterator iterator;
24 typedef typename collection_type::const_iterator const_iterator;
25 typedef TYPE value_type;
26
27 virtual ~Collection()
28 {
29 }
30
31 protected:
32 boost::shared_ptr<collection_type> container;
33
34 public:
35 static this_type newInstance(int32_t size = 0)
36 {
37 this_type instance;
38 instance.container = Lucene::newInstance<collection_type>(size);
39 return instance;
40 }
41
42 template <class ITER>
43 static this_type newInstance(ITER first, ITER last)
44 {
45 this_type instance;
46 instance.container = Lucene::newInstance<collection_type>(first, last);
47 return instance;
48 }
49
50 void reset()
51 {
52 resize(0);
53 }
54
55 void resize(int32_t size)
56 {
57 if (size == 0)
58 container.reset();
59 else
60 container->resize(size);
61 }
62
63 int32_t size() const
64 {
65 return (int32_t)container->size();
66 }
67
68 bool empty() const
69 {
70 return container->empty();
71 }
72
73 void clear()
74 {
75 container->clear();
76 }
77
78 iterator begin()
79 {
80 return container->begin();
81 }
82
83 iterator end()
84 {
85 return container->end();
86 }
87
88 const_iterator begin() const
89 {
90 return container->begin();
91 }
92
93 const_iterator end() const
94 {
95 return container->end();
96 }
97
98 void add(const TYPE& type)
99 {
100 container->push_back(type);
101 }
102
103 void add(int32_t pos, const TYPE& type)
104 {
105 container->insert(container->begin() + pos, type);
106 }
107
108 template <class ITER>
109 void addAll(ITER first, ITER last)
110 {
111 container->insert(container->end(), first, last);
112 }
113
114 template <class ITER>
115 void insert(ITER pos, const TYPE& type)
116 {
117 container->insert(pos, type);
118 }
119
120 template <class ITER>
121 ITER remove(ITER pos)
122 {
123 return container->erase(pos);
124 }
125
126 template <class ITER>
127 ITER remove(ITER first, ITER last)
128 {
129 return container->erase(first, last);
130 }
131
132 void remove(const TYPE& type)
133 {
134 container->erase(std::remove(container->begin(), container->end(), type), container->end());
135 }
136
137 template <class PRED>
138 void remove_if(PRED comp)
139 {
140 container->erase(std::remove_if(container->begin(), container->end(), comp), container->end());
141 }
142
143 TYPE removeFirst()
144 {
145 TYPE front = container->front();
146 container->erase(container->begin());
147 return front;
148 }
149
150 TYPE removeLast()
151 {
152 TYPE back = container->back();
153 container->pop_back();
154 return back;
155 }
156
157 iterator find(const TYPE& type)
158 {
159 return std::find(container->begin(), container->end(), type);
160 }
161
162 template <class PRED>
163 iterator find_if(PRED comp)
164 {
165 return std::find_if(container->begin(), container->end(), comp);
166 }
167
168 bool contains(const TYPE& type) const
169 {
170 return (std::find(container->begin(), container->end(), type) != container->end());
171 }
172
173 template <class PRED>
174 bool contains_if(PRED comp) const
175 {
176 return (std::find_if(container->begin(), container->end(), comp) != container->end());
177 }
178
179 bool equals(const this_type& other) const
180 {
181 return equals(other, std::equal_to<TYPE>());
182 }
183
184 template <class PRED>
185 bool equals(const this_type& other, PRED comp) const
186 {
187 if (container->size() != other.container->size())
188 return false;
189 return std::equal(container->begin(), container->end(), other.container->begin(), comp);
190 }
191
192 int32_t hashCode()
193 {
194 return (int32_t)(int64_t)container.get();
195 }
196
197 void swap(this_type& other)
198 {
199 container.swap(other->container);
200 }
201
202 TYPE& operator[] (int32_t pos)
203 {
204 return (*container)[pos];
205 }
206
207 const TYPE& operator[] (int32_t pos) const
208 {
209 return (*container)[pos];
210 }
211
212 operator bool() const
213 {
214 return container;
215 }
216
217 bool operator! () const
218 {
219 return !container;
220 }
221
222 bool operator== (const this_type& other)
223 {
224 return (container == other.container);
225 }
226
227 bool operator!= (const this_type& other)
228 {
229 return (container != other.container);
230 }
231 };
232
233 template <typename TYPE>
234 Collection<TYPE> newCollection(const TYPE& a1)
235 {
236 Collection<TYPE> result = Collection<TYPE>::newInstance();
237 result.add(a1);
238 return result;
239 }
240
241 template <typename TYPE>
242 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2)
243 {
244 Collection<TYPE> result = newCollection(a1);
245 result.add(a2);
246 return result;
247 }
248
249 template <typename TYPE>
250 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3)
251 {
252 Collection<TYPE> result = newCollection(a1, a2);
253 result.add(a3);
254 return result;
255 }
256
257 template <typename TYPE>
258 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4)
259 {
260 Collection<TYPE> result = newCollection(a1, a2, a3);
261 result.add(a4);
262 return result;
263 }
264
265 template <typename TYPE>
266 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5)
267 {
268 Collection<TYPE> result = newCollection(a1, a2, a3, a4);
269 result.add(a5);
270 return result;
271 }
272
273 template <typename TYPE>
274 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6)
275 {
276 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5);
277 result.add(a6);
278 return result;
279 }
280
281 template <typename TYPE>
282 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6, const TYPE& a7)
283 {
284 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6);
285 result.add(a7);
286 return result;
287 }
288
289 template <typename TYPE>
290 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6, const TYPE& a7, const TYPE& a8)
291 {
292 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6, a7);
293 result.add(a8);
294 return result;
295 }
296
297 template <typename TYPE>
298 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6, const TYPE& a7, const TYPE& a8, const TYPE& a9)
299 {
300 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6, a7, a8);
301 result.add(a9);
302 return result;
303 }
304
305 template <typename TYPE>
306 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6, const TYPE& a7, const TYPE& a8, const TYPE& a9, const TYPE& a10)
307 {
308 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6, a7, a8, a9);
309 result.add(a10);
310 return result;
311 }
312 }
313
314 #endif

clucene.sourceforge.net