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

Collection.h
Go to the documentation of this file.
1 // 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
13 namespace Lucene {
14
16 template <class TYPE>
17 class Collection : public LuceneSync {
18 public:
19 typedef Collection<TYPE> this_type;
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;
24 typedef TYPE value_type;
25
26 virtual ~Collection() {
27 }
28
29 protected:
30 boost::shared_ptr<collection_type> container;
31
32 public:
33 static this_type newInstance(int32_t size = 0) {
34 this_type instance;
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) {
41 this_type instance;
42 instance.container = Lucene::newInstance<collection_type>(first, last);
43 return instance;
44 }
45
46 void reset() {
47 resize(0);
48 }
49
50 void resize(int32_t size) {
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
70 iterator begin() {
71 return container->begin();
72 }
73
74 iterator end() {
75 return container->end();
76 }
77
78 const_iterator begin() const {
79 return container->begin();
80 }
81
82 const_iterator end() const {
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>
105 ITER remove(ITER pos) {
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>
119 void remove_if(PRED comp) {
120 container->erase(std::remove_if(container->begin(), container->end(), comp), container->end());
121 }
122
123 TYPE removeFirst() {
124 TYPE front = container->front();
125 container->erase(container->begin());
126 return front;
127 }
128
129 TYPE removeLast() {
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>
140 iterator find_if(PRED comp) {
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
165 int32_t hashCode() {
166 return (int32_t)(int64_t)container.get();
167 }
168
169 void swap(this_type& other) {
170 container.swap(other->container);
171 }
172
173 TYPE& operator[] (int32_t pos) {
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
189 bool operator== (const this_type& other) {
190 return (container == other.container);
191 }
192
193 bool operator!= (const this_type& other) {
194 return (container != other.container);
195 }
196 };
197
198 template <typename TYPE>
199 Collection<TYPE> newCollection(const TYPE& a1) {
200 Collection<TYPE> result = Collection<TYPE>::newInstance();
201 result.add(a1);
202 return result;
203 }
204
205 template <typename TYPE>
206 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2) {
207 Collection<TYPE> result = newCollection(a1);
208 result.add(a2);
209 return result;
210 }
211
212 template <typename TYPE>
213 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3) {
214 Collection<TYPE> result = newCollection(a1, a2);
215 result.add(a3);
216 return result;
217 }
218
219 template <typename TYPE>
220 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4) {
221 Collection<TYPE> result = newCollection(a1, a2, a3);
222 result.add(a4);
223 return result;
224 }
225
226 template <typename TYPE>
227 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5) {
228 Collection<TYPE> result = newCollection(a1, a2, a3, a4);
229 result.add(a5);
230 return result;
231 }
232
233 template <typename TYPE>
234 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6) {
235 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5);
236 result.add(a6);
237 return result;
238 }
239
240 template <typename TYPE>
241 Collection<TYPE> newCollection(const TYPE& a1, const TYPE& a2, const TYPE& a3, const TYPE& a4, const TYPE& a5, const TYPE& a6, const TYPE& a7) {
242 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6);
243 result.add(a7);
244 return result;
245 }
246
247 template <typename TYPE>
248 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) {
249 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6, a7);
250 result.add(a8);
251 return result;
252 }
253
254 template <typename TYPE>
255 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) {
256 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6, a7, a8);
257 result.add(a9);
258 return result;
259 }
260
261 template <typename TYPE>
262 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) {
263 Collection<TYPE> result = newCollection(a1, a2, a3, a4, a5, a6, a7, a8, a9);
264 result.add(a10);
265 return result;
266 }
267
268 }
269
270 #endif
Lucene::Collection::const_iterator
collection_type::const_iterator const_iterator
Definition: Collection.h:23
Lucene::Collection::this_type
Collection< TYPE > this_type
Definition: Collection.h:19
Lucene::newCollection
Collection< TYPE > newCollection(const TYPE &a1)
Definition: Collection.h:199
Lucene::Collection::newInstance
static this_type newInstance(int32_t size=0)
Definition: Collection.h:33
Lucene::Collection::find
iterator find(const TYPE &type)
Definition: Collection.h:135
Lucene::Collection::insert
void insert(ITER pos, const TYPE &type)
Definition: Collection.h:100
Lucene::Collection::contains_if
bool contains_if(PRED comp) const
Definition: Collection.h:149
Lucene::Collection::operator!=
bool operator!=(const this_type &other)
Definition: Collection.h:193
Lucene::Collection::add
void add(int32_t pos, const TYPE &type)
Definition: Collection.h:90
Lucene::Collection::end
iterator end()
Definition: Collection.h:74
Lucene::LuceneSync
Base class for all Lucene synchronised classes.
Definition: LuceneSync.h:15
Lucene::Collection::begin
const_iterator begin() const
Definition: Collection.h:78
Lucene::Collection::clear
void clear()
Definition: Collection.h:66
Lucene::Collection::newInstance
static this_type newInstance(ITER first, ITER last)
Definition: Collection.h:40
Lucene::Collection::value_type
TYPE value_type
Definition: Collection.h:24
Lucene::Collection::iterator
collection_type::iterator iterator
Definition: Collection.h:22
Lucene::Collection::hashCode
int32_t hashCode()
Definition: Collection.h:165
Lucene::Collection::~Collection
virtual ~Collection()
Definition: Collection.h:26
Lucene::Collection::shared_ptr
boost::shared_ptr< this_type > shared_ptr
Definition: Collection.h:20
Lucene::Collection::begin
iterator begin()
Definition: Collection.h:70
Lucene::Collection::reset
void reset()
Definition: Collection.h:46
Lucene::Collection::find_if
iterator find_if(PRED comp)
Definition: Collection.h:140
Lucene::Collection::collection_type
std::vector< TYPE > collection_type
Definition: Collection.h:21
Lucene::Collection::add
void add(const TYPE &type)
Definition: Collection.h:86
Lucene::Collection::container
boost::shared_ptr< collection_type > container
Definition: Collection.h:30
Lucene::Collection::end
const_iterator end() const
Definition: Collection.h:82
Lucene::Collection::equals
bool equals(const this_type &other) const
Definition: Collection.h:153
Lucene::Collection::removeLast
TYPE removeLast()
Definition: Collection.h:129
Lucene
Definition: AbstractAllTermDocs.h:12
Lucene::Collection::resize
void resize(int32_t size)
Definition: Collection.h:50
Lucene::Collection::equals
bool equals(const this_type &other, PRED comp) const
Definition: Collection.h:158
Lucene::Collection::empty
bool empty() const
Definition: Collection.h:62
LuceneSync.h
Lucene::Collection::size
int32_t size() const
Definition: Collection.h:58
Lucene::Collection
Utility template class to handle collections that can be safely copied and shared.
Definition: Collection.h:17
Lucene::Collection::addAll
void addAll(ITER first, ITER last)
Definition: Collection.h:95
Lucene::Collection::removeFirst
TYPE removeFirst()
Definition: Collection.h:123
Lucene::Collection::contains
bool contains(const TYPE &type) const
Definition: Collection.h:144
Lucene::Collection::operator!
bool operator!() const
Definition: Collection.h:185
Lucene::Collection::operator==
bool operator==(const this_type &other)
Definition: Collection.h:189
Lucene::Collection::swap
void swap(this_type &other)
Definition: Collection.h:169
Lucene::Collection::remove_if
void remove_if(PRED comp)
Definition: Collection.h:119
Lucene::Collection::operator[]
TYPE & operator[](int32_t pos)
Definition: Collection.h:173

clucene.sourceforge.net