Generated on Sun Aug 9 2020 05:34:08 for Gecode by doxygen 1.8.18
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 
43 namespace 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  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 
115  SharedArray(void);
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  iterator begin(void);
147  const_iterator begin(void) const;
149  iterator end(void);
151  const_iterator end(void) const;
161  };
162 
167  template<class Char, class Traits, class T>
168  std::basic_ostream<Char,Traits>&
169  operator <<(std::basic_ostream<Char,Traits>& os,
170  const SharedArray<T>& x);
171 
172 
173  /*
174  * Implementation
175  *
176  */
177 
178  /*
179  * Shared arrays
180  *
181  */
182  template<class T>
184  SharedArray<T>::SAO::SAO(int n0) : n(n0) {
185  a = (n>0) ? heap.alloc<T>(n) : NULL;
186  }
187 
188  template<class T>
190  if (n>0) {
191  heap.free<T>(a,n);
192  }
193  }
194 
195  template<class T>
196  forceinline T&
198  assert((i>=0) && (i<n));
199  return a[i];
200  }
201 
202  template<class T>
203  forceinline const T&
205  assert((i>=0) && (i<n));
206  return a[i];
207  }
208 
209  template<class T>
210  forceinline int
212  return n;
213  }
214 
215  template<class T>
216  forceinline T*
218  return a;
219  }
220 
221  template<class T>
222  forceinline const T*
224  return a;
225  }
226 
227  template<class T>
228  forceinline T*
230  return a+n;
231  }
232 
233  template<class T>
234  forceinline const T*
236  return a+n;
237  }
238 
239 
240  template<class T>
243 
244  template<class T>
247  : SharedHandle(new SAO(n)) {}
248 
249  template<class T>
252  : SharedHandle(sa) {}
253 
254  template<class T>
255  forceinline void
257  assert(object() == NULL);
258  object(new SAO(n));
259  }
260 
261  template<class T>
262  forceinline T&
264  assert(object() != NULL);
265  return (*static_cast<SAO*>(object()))[i];
266  }
267 
268  template<class T>
269  forceinline const T&
271  assert(object() != NULL);
272  return (*static_cast<SAO*>(object()))[i];
273  }
274 
275  template<class T>
276  inline bool
278  if (size() != sa.size())
279  return false;
280  if (object()==sa.object())
281  return true;
282  for (int i=0; i<size(); i++) {
283  if ((*this)[i] != sa[i])
284  return false;
285  }
286  return true;
287  }
288 
289  template<class T>
292  : SharedHandle(new SAO(a.size())) {
293  for (int i=0; i<a.size(); i++)
294  operator [](i)=a[i];
295  }
296 
297  template<class T>
298  forceinline int
299  SharedArray<T>::size(void) const {
300  assert(object() != NULL);
301  return static_cast<SAO*>(object())->size();
302  }
303 
304  template<class T>
307  assert(object() != NULL);
308  return static_cast<SAO*>(object())->begin();
309  }
310 
311  template<class T>
313  SharedArray<T>::begin(void) const {
314  assert(object() != NULL);
315  return static_cast<SAO*>(object())->begin();
316  }
317 
318  template<class T>
321  assert(object() != NULL);
322  return static_cast<SAO*>(object())->end();
323  }
324 
325  template<class T>
327  SharedArray<T>::end(void) const {
328  assert(object() != NULL);
329  return static_cast<SAO*>(object())->end();
330  }
331 
332  template<class T>
335  assert(object() != NULL);
336  return reverse_iterator(static_cast<SAO*>(object())->end());
337  }
338 
339  template<class T>
342  assert(object() != NULL);
343  return const_reverse_iterator(static_cast<SAO*>(object())->end());
344  }
345 
346  template<class T>
349  assert(object() != NULL);
350  return reverse_iterator(static_cast<SAO*>(object())->begin());
351  }
352 
353  template<class T>
355  SharedArray<T>::rend(void) const {
356  assert(object() != NULL);
357  return const_reverse_iterator(static_cast<SAO*>(object())->begin());
358  }
359 
360  template<class Char, class Traits, class T>
361  std::basic_ostream<Char,Traits>&
362  operator <<(std::basic_ostream<Char,Traits>& os,
363  const SharedArray<T>& x) {
364  std::basic_ostringstream<Char,Traits> s;
365  s.copyfmt(os); s.width(0);
366  s << '{';
367  if (x.size() > 0) {
368  s << x[0];
369  for (int i=1; i<x.size(); i++)
370  s << ", " << x[i];
371  }
372  s << '}';
373  return os << s.str();
374  }
375 
376 }
377 
378 // STATISTICS: kernel-other
Post propagator for SetVar x
Definition: set.hh:767
const_iterator end(void) const
Return a read-only iterator past the end of the array.
T & reference
Type of a reference to the value type.
const_reverse_iterator rend(void) const
Return a reverse and read-only iterator past the beginning of the array.
unsigned int size(I &i)
Size of all ranges of range iterator i.
int size(void) const
Return number of elements.
SharedArray(const SharedArray &a)
Initialize from shared array a (share elements)
SharedArray(const ArgArrayBase< T > &a)
Initialize from argument array a.
std::reverse_iterator< T * > reverse_iterator
Type of the iterator used to iterate backwards through this array's elements.
std::reverse_iterator< const T * > const_reverse_iterator
Type of the iterator used to iterate backwards and read-only through this array's elements.
T * begin(void)
Return beginning of array (for iterators)
T * iterator
Type of the iterator used to iterate through this array's elements.
virtual ~SAO(void)
Delete object.
SharedHandle::Object * object(void) const
Access to the shared object.
reverse_iterator rend(void)
Return a reverse iterator past the beginning of the array.
The shared object.
const T * const_pointer
Type of a read-only pointer to the value type.
SharedArray(void)
Construct as not yet intialized.
T * alloc(long unsigned int n)
Allocate block of n objects of type T from heap.
Definition: heap.hpp:431
Gecode toplevel namespace
iterator begin(void)
Return an iterator at the beginning of the array.
void init(int n)
Initialize as array with n elements.
const T & const_reference
Type of a constant reference to the value type.
T & operator[](int i)
Access element at position i.
reverse_iterator rbegin(void)
Return a reverse iterator at the end of the array.
T * end(void)
Return end of array (for iterators)
Base-class for argument arrays.
Definition: array.hpp:537
bool operator==(const SharedArray< T > &sa) const
Test equality with sa.
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Heap heap
The single global heap.
Definition: heap.cpp:44
const_reverse_iterator rbegin(void) const
Return a reverse and read-only iterator at the end of the array.
const_iterator begin(void) const
Return a read-only iterator at the beginning of the array.
T & operator[](int i)
Access element at position i.
SAO(int n)
Allocate for n elements.
const T * const_iterator
Type of the iterator used to iterate read-only through this array's elements.
void free(T *b, long unsigned int n)
Delete n objects starting at b.
Definition: heap.hpp:457
#define forceinline
Definition: config.hpp:185
SharedArray(int n)
Initialize as array with n elements.
int size(void) const
Return number of elements.
The shared handle.
iterator end(void)
Return an iterator past the end of the array.
T * pointer
Type of a pointer to the value type.
Shared array with arbitrary number of elements.
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
Implementation of object for shared arrays.
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition: val-sel.hpp:39
T value_type
Type of the view stored in this array.