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


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

clucene.sourceforge.net