Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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  * Last modified:
16  * $Date$ by $Author$
17  * $Revision$
18  *
19  * This file is part of Gecode, the generic constraint
20  * development environment:
21  * http://www.gecode.org
22  *
23  * Permission is hereby granted, free of charge, to any person obtaining
24  * a copy of this software and associated documentation files (the
25  * "Software"), to deal in the Software without restriction, including
26  * without limitation the rights to use, copy, modify, merge, publish,
27  * distribute, sublicense, and/or sell copies of the Software, and to
28  * permit persons to whom the Software is furnished to do so, subject to
29  * the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be
32  * included in all copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
38  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
39  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41  *
42  */
43 
44 #include <cstdarg>
45 #include <iostream>
46 #include <sstream>
47 
48 namespace Gecode {
49 
57  template<class T>
58  class SharedArray : public SharedHandle {
59  protected:
61  class SAO : public SharedHandle::Object {
62  private:
64  T* a;
66  int n;
67  public:
69  SAO(int n);
71  virtual ~SAO(void);
72 
74  T& operator [](int i);
76  const T& operator [](int i) const;
77 
79  int size(void) const;
80 
82  T* begin(void);
84  const T* begin(void) const;
86  T* end(void);
88  const T* end(void) const;
89  };
90  public:
92 
93  typedef T value_type;
96  typedef T& reference;
98  typedef const T& const_reference;
100  typedef T* pointer;
102  typedef const T* const_pointer;
104  typedef T* iterator;
106  typedef const T* const_iterator;
108  typedef std::reverse_iterator<T*> reverse_iterator;
110  typedef std::reverse_iterator<const T*> const_reverse_iterator;
112 
120  SharedArray(void);
122  SharedArray(int n);
130  void init(int n);
132  SharedArray(const SharedArray& a);
134  SharedArray(const ArgArrayBase<T>& a);
135 
137  T& operator [](int i);
139  const T& operator [](int i) const;
140 
142  int size(void) const;
143 
145 
146  iterator begin(void);
149  const_iterator begin(void) const;
151  iterator end(void);
153  const_iterator end(void) const;
155  reverse_iterator rbegin(void);
157  const_reverse_iterator rbegin(void) const;
159  reverse_iterator rend(void);
161  const_reverse_iterator rend(void) const;
163  };
164 
169  template<class Char, class Traits, class T>
170  std::basic_ostream<Char,Traits>&
171  operator <<(std::basic_ostream<Char,Traits>& os,
172  const SharedArray<T>& x);
173 
174 
175  /*
176  * Implementation
177  *
178  */
179 
180  /*
181  * Shared arrays
182  *
183  */
184  template<class T>
186  SharedArray<T>::SAO::SAO(int n0) : n(n0) {
187  a = (n>0) ? heap.alloc<T>(n) : NULL;
188  }
189 
190  template<class T>
192  if (n>0) {
193  heap.free<T>(a,n);
194  }
195  }
196 
197  template<class T>
198  forceinline T&
200  assert((i>=0) && (i<n));
201  return a[i];
202  }
203 
204  template<class T>
205  forceinline const T&
207  assert((i>=0) && (i<n));
208  return a[i];
209  }
210 
211  template<class T>
212  forceinline int
214  return n;
215  }
216 
217  template<class T>
218  forceinline T*
220  return a;
221  }
222 
223  template<class T>
224  forceinline const T*
226  return a;
227  }
228 
229  template<class T>
230  forceinline T*
232  return a+n;
233  }
234 
235  template<class T>
236  forceinline const T*
238  return a+n;
239  }
240 
241 
242  template<class T>
245 
246  template<class T>
249  : SharedHandle(new SAO(n)) {}
250 
251  template<class T>
254  : SharedHandle(sa) {}
255 
256  template<class T>
257  forceinline void
259  assert(object() == NULL);
260  object(new SAO(n));
261  }
262 
263  template<class T>
264  forceinline T&
266  assert(object() != NULL);
267  return (*static_cast<SAO*>(object()))[i];
268  }
269 
270  template<class T>
271  forceinline const T&
273  assert(object() != NULL);
274  return (*static_cast<SAO*>(object()))[i];
275  }
276 
277  template<class T>
280  : SharedHandle(new SAO(a.size())) {
281  for (int i=a.size(); i--; )
282  operator [](i)=a[i];
283  }
284 
285  template<class T>
286  forceinline int
287  SharedArray<T>::size(void) const {
288  assert(object() != NULL);
289  return static_cast<SAO*>(object())->size();
290  }
291 
292  template<class T>
295  assert(object() != NULL);
296  return static_cast<SAO*>(object())->begin();
297  }
298 
299  template<class T>
301  SharedArray<T>::begin(void) const {
302  assert(object() != NULL);
303  return static_cast<SAO*>(object())->begin();
304  }
305 
306  template<class T>
309  assert(object() != NULL);
310  return static_cast<SAO*>(object())->end();
311  }
312 
313  template<class T>
315  SharedArray<T>::end(void) const {
316  assert(object() != NULL);
317  return static_cast<SAO*>(object())->end();
318  }
319 
320  template<class T>
323  assert(object() != NULL);
324  return reverse_iterator(static_cast<SAO*>(object())->end());
325  }
326 
327  template<class T>
330  assert(object() != NULL);
331  return const_reverse_iterator(static_cast<SAO*>(object())->end());
332  }
333 
334  template<class T>
337  assert(object() != NULL);
338  return reverse_iterator(static_cast<SAO*>(object())->begin());
339  }
340 
341  template<class T>
343  SharedArray<T>::rend(void) const {
344  assert(object() != NULL);
345  return const_reverse_iterator(static_cast<SAO*>(object())->begin());
346  }
347 
348  template<class Char, class Traits, class T>
349  std::basic_ostream<Char,Traits>&
350  operator <<(std::basic_ostream<Char,Traits>& os,
351  const SharedArray<T>& x) {
352  std::basic_ostringstream<Char,Traits> s;
353  s.copyfmt(os); s.width(0);
354  s << '{';
355  if (x.size() > 0) {
356  s << x[0];
357  for (int i=1; i<x.size(); i++)
358  s << ", " << x[i];
359  }
360  s << '}';
361  return os << s.str();
362  }
363 
364 }
365 
366 // STATISTICS: kernel-other
T & operator[](int i)
Access element at position i.
The shared handle.
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1657
const T * const_iterator
Type of the iterator used to iterate read-only through this array&#39;s elements.
T * end(void)
Return end of array (for iterators)
Implementation of object for shared arrays.
#define forceinline
Definition: config.hpp:182
const T & const_reference
Type of a constant reference to the value type.
T value_type
Type of the view stored in this array.
SAO(int n)
Allocate for n elements.
T * alloc(long unsigned int n)
Allocate block of n objects of type T from heap.
Definition: heap.hpp:435
Gecode::IntArgs i(4, 1, 2, 3, 4)
void init(int n)
Initialize as array with n elements.
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
T * iterator
Type of the iterator used to iterate through this array&#39;s elements.
unsigned int size(I &i)
Size of all ranges of range iterator i.
int size(void) const
Return number of elements.
reverse_iterator rbegin(void)
Return a reverse iterator at the end of the array.
T & reference
Type of a reference to the value type.
const T * const_pointer
Type of a read-only pointer to the value type.
T * begin(void)
Return beginning of array (for iterators)
virtual ~SAO(void)
Delete object.
void free(T *b, long unsigned int n)
Delete n objects starting at b.
Definition: heap.hpp:461
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
T * pointer
Type of a pointer to the value type.
Base-class for argument arrays.
Definition: array.hpp:512
Heap heap
The single global heap.
Definition: heap.cpp:48
Post propagator for SetVar x
Definition: set.hh:769
reverse_iterator rend(void)
Return a reverse iterator past the beginning of the array.
std::reverse_iterator< T * > reverse_iterator
Type of the iterator used to iterate backwards through this array&#39;s elements.
Gecode toplevel namespace
Shared array with arbitrary number of elements.
SharedArray(void)
Construct as not yet intialized.
std::reverse_iterator< const T * > const_reverse_iterator
Type of the iterator used to iterate backwards and read-only through this array&#39;s elements...