Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
shared-array.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 * Guido Tack <tack@gecode.org>
6 *
7 * Contributing authors:
8 * Gregory Crosswhite <gcross@phys.washington.edu>
9 *
10 * Copyright:
11 * Gregory Crosswhite, 2011
12 * Christian Schulte, 2003
13 * Guido Tack, 2004
14 *
15 * This file is part of Gecode, the generic constraint
16 * development environment:
17 * http://www.gecode.org
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 *
38 */
39
40#include <iostream>
41#include <sstream>
42
43namespace Gecode {
44
52 template<class T>
53 class SharedArray : public SharedHandle {
54 protected:
56 class SAO : public SharedHandle::Object {
57 private:
59 T* a;
61 int n;
62 public:
64 SAO(int n);
66 virtual ~SAO(void);
67
69 T& operator [](int i);
71 const T& operator [](int i) const;
72
74 int size(void) const;
75
77 T* begin(void);
79 const T* begin(void) const;
81 T* end(void);
83 const T* end(void) const;
84 };
85 public:
87
88
89 typedef T value_type;
91 typedef T& reference;
93 typedef const T& const_reference;
95 typedef T* pointer;
97 typedef const T* const_pointer;
99 typedef T* iterator;
101 typedef const T* const_iterator;
103 typedef std::reverse_iterator<T*> reverse_iterator;
105 typedef std::reverse_iterator<const T*> const_reverse_iterator;
107
125 void init(int n);
130
132 T& operator [](int i);
134 const T& operator [](int i) const;
135
137 int size(void) const;
138
140 bool operator ==(const SharedArray<T>& sa) const;
141
143
144
151 const_iterator end(void) const;
161
164 };
165
170 template<class Char, class Traits, class T>
171 std::basic_ostream<Char,Traits>&
172 operator <<(std::basic_ostream<Char,Traits>& os,
173 const SharedArray<T>& x);
174
175
176 /*
177 * Implementation
178 *
179 */
180
181 /*
182 * Shared arrays
183 *
184 */
185 template<class T>
187 SharedArray<T>::SAO::SAO(int n0) : n(n0) {
188 a = (n>0) ? heap.alloc<T>(n) : NULL;
189 }
190
191 template<class T>
193 if (n>0) {
194 heap.free<T>(a,n);
195 }
196 }
197
198 template<class T>
199 forceinline T&
201 assert((i>=0) && (i<n));
202 return a[i];
203 }
204
205 template<class T>
206 forceinline const T&
208 assert((i>=0) && (i<n));
209 return a[i];
210 }
211
212 template<class T>
213 forceinline int
215 return n;
216 }
217
218 template<class T>
219 forceinline T*
221 return a;
222 }
223
224 template<class T>
225 forceinline const T*
227 return a;
228 }
229
230 template<class T>
231 forceinline T*
233 return a+n;
234 }
235
236 template<class T>
237 forceinline const T*
239 return a+n;
240 }
241
242
243 template<class T>
246
247 template<class T>
251
252 template<class T>
256
257 template<class T>
258 forceinline void
260 assert(object() == NULL);
261 object(new SAO(n));
262 }
263
264 template<class T>
265 forceinline T&
267 assert(object() != NULL);
268 return (*static_cast<SAO*>(object()))[i];
269 }
270
271 template<class T>
272 forceinline const T&
274 assert(object() != NULL);
275 return (*static_cast<SAO*>(object()))[i];
276 }
277
278 template<class T>
279 inline bool
281 if (size() != sa.size())
282 return false;
283 if (object()==sa.object())
284 return true;
285 for (int i=0; i<size(); i++) {
286 if ((*this)[i] != sa[i])
287 return false;
288 }
289 return true;
290 }
291
292 template<class T>
295 : SharedHandle(new SAO(a.size())) {
296 for (int i=0; i<a.size(); i++)
297 operator [](i)=a[i];
298 }
299
300 template<class T>
301 forceinline int
303 assert(object() != NULL);
304 return static_cast<SAO*>(object())->size();
305 }
306
307 template<class T>
310 assert(object() != NULL);
311 return static_cast<SAO*>(object())->begin();
312 }
313
314 template<class T>
317 assert(object() != NULL);
318 return static_cast<SAO*>(object())->begin();
319 }
320
321 template<class T>
324 assert(object() != NULL);
325 return static_cast<SAO*>(object())->end();
326 }
327
328 template<class T>
331 assert(object() != NULL);
332 return static_cast<SAO*>(object())->end();
333 }
334
335 template<class T>
338 assert(object() != NULL);
339 return reverse_iterator(static_cast<SAO*>(object())->end());
340 }
341
342 template<class T>
345 assert(object() != NULL);
346 return const_reverse_iterator(static_cast<SAO*>(object())->end());
347 }
348
349 template<class T>
352 assert(object() != NULL);
353 return reverse_iterator(static_cast<SAO*>(object())->begin());
354 }
355
356 template<class T>
359 assert(object() != NULL);
360 return const_reverse_iterator(static_cast<SAO*>(object())->begin());
361 }
362
363 template<class Char, class Traits, class T>
364 std::basic_ostream<Char,Traits>&
365 operator <<(std::basic_ostream<Char,Traits>& os,
366 const SharedArray<T>& x) {
367 std::basic_ostringstream<Char,Traits> s;
368 s.copyfmt(os); s.width(0);
369 s << '{';
370 if (x.size() > 0) {
371 s << x[0];
372 for (int i=1; i<x.size(); i++)
373 s << ", " << x[i];
374 }
375 s << '}';
376 return os << s.str();
377 }
378
379}
380
381// STATISTICS: kernel-other
Base-class for argument arrays.
Definition array.hpp:537
FloatNum size(void) const
Return size of float value (distance between maximum and minimum)
Definition val.hpp:78
Implementation of object for shared arrays.
T * end(void)
Return end of array (for iterators)
T * begin(void)
Return beginning of array (for iterators)
SAO(int n)
Allocate for n elements.
int size(void) const
Return number of elements.
T & operator[](int i)
Access element at position i.
virtual ~SAO(void)
Delete object.
Shared array with arbitrary number of elements.
const_iterator begin(void) const
Return a read-only iterator at the beginning of the array.
const_reverse_iterator rend(void) const
Return a reverse and read-only iterator past the beginning of the array.
std::reverse_iterator< const int * > const_reverse_iterator
reverse_iterator rend(void)
Return a reverse iterator past the beginning of the array.
const_iterator end(void) const
Return a read-only iterator past the end of the array.
bool operator==(const SharedArray< T > &sa) const
Test equality with sa.
SharedArray(const ArgArrayBase< T > &a)
Initialize from argument array a.
SharedArray(int n)
Initialize as array with n elements.
reverse_iterator rbegin(void)
Return a reverse iterator at the end of the array.
iterator begin(void)
Return an iterator at the beginning of the array.
iterator end(void)
Return an iterator past the end of the array.
SharedArray(const SharedArray &a)
Initialize from shared array a (share elements)
std::reverse_iterator< int * > reverse_iterator
SharedArray(void)
Construct as not yet intialized.
int size(void) const
Return number of elements.
const_reverse_iterator rbegin(void) const
Return a reverse and read-only iterator at the end of the array.
T & operator[](int i)
Access element at position i.
void init(int n)
Initialize as array with n elements.
SharedArray & operator=(const SharedArray &)=default
Assignment operator.
SharedHandle(void)
Create shared handle with no object pointing to.
SharedHandle::Object * object(void) const
Access to the shared object.
Heap heap
The single global heap.
Definition heap.cpp:44
Gecode toplevel namespace
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition val-sel.hpp:39
Post propagator for SetVar x
Definition set.hh:773
#define forceinline
Definition config.hpp:194