Generated on Sat Jun 2 2018 07:17:44 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  * 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 <cstdarg>
41 #include <iostream>
42 #include <sstream>
43 
44 namespace Gecode {
45 
53  template<class T>
54  class SharedArray : public SharedHandle {
55  protected:
57  class SAO : public SharedHandle::Object {
58  private:
60  T* a;
62  int n;
63  public:
65  SAO(int n);
67  virtual ~SAO(void);
68 
70  T& operator [](int i);
72  const T& operator [](int i) const;
73 
75  int size(void) const;
76 
78  T* begin(void);
80  const T* begin(void) const;
82  T* end(void);
84  const T* end(void) const;
85  };
86  public:
88 
89  typedef T value_type;
92  typedef T& reference;
94  typedef const T& const_reference;
96  typedef T* pointer;
98  typedef const T* const_pointer;
100  typedef T* iterator;
102  typedef const T* const_iterator;
104  typedef std::reverse_iterator<T*> reverse_iterator;
106  typedef std::reverse_iterator<const T*> const_reverse_iterator;
108 
116  SharedArray(void);
118  SharedArray(int n);
126  void init(int n);
128  SharedArray(const SharedArray& a);
130  SharedArray(const ArgArrayBase<T>& a);
131 
133  T& operator [](int i);
135  const T& operator [](int i) const;
136 
138  int size(void) const;
139 
141 
142  iterator begin(void);
145  const_iterator begin(void) const;
147  iterator end(void);
149  const_iterator end(void) const;
151  reverse_iterator rbegin(void);
153  const_reverse_iterator rbegin(void) const;
155  reverse_iterator rend(void);
157  const_reverse_iterator rend(void) const;
159  };
160 
165  template<class Char, class Traits, class T>
166  std::basic_ostream<Char,Traits>&
167  operator <<(std::basic_ostream<Char,Traits>& os,
168  const SharedArray<T>& x);
169 
170 
171  /*
172  * Implementation
173  *
174  */
175 
176  /*
177  * Shared arrays
178  *
179  */
180  template<class T>
182  SharedArray<T>::SAO::SAO(int n0) : n(n0) {
183  a = (n>0) ? heap.alloc<T>(n) : NULL;
184  }
185 
186  template<class T>
188  if (n>0) {
189  heap.free<T>(a,n);
190  }
191  }
192 
193  template<class T>
194  forceinline T&
196  assert((i>=0) && (i<n));
197  return a[i];
198  }
199 
200  template<class T>
201  forceinline const T&
203  assert((i>=0) && (i<n));
204  return a[i];
205  }
206 
207  template<class T>
208  forceinline int
210  return n;
211  }
212 
213  template<class T>
214  forceinline T*
216  return a;
217  }
218 
219  template<class T>
220  forceinline const T*
222  return a;
223  }
224 
225  template<class T>
226  forceinline T*
228  return a+n;
229  }
230 
231  template<class T>
232  forceinline const T*
234  return a+n;
235  }
236 
237 
238  template<class T>
241 
242  template<class T>
245  : SharedHandle(new SAO(n)) {}
246 
247  template<class T>
250  : SharedHandle(sa) {}
251 
252  template<class T>
253  forceinline void
255  assert(object() == NULL);
256  object(new SAO(n));
257  }
258 
259  template<class T>
260  forceinline T&
262  assert(object() != NULL);
263  return (*static_cast<SAO*>(object()))[i];
264  }
265 
266  template<class T>
267  forceinline const T&
269  assert(object() != NULL);
270  return (*static_cast<SAO*>(object()))[i];
271  }
272 
273  template<class T>
276  : SharedHandle(new SAO(a.size())) {
277  for (int i=a.size(); i--; )
278  operator [](i)=a[i];
279  }
280 
281  template<class T>
282  forceinline int
283  SharedArray<T>::size(void) const {
284  assert(object() != NULL);
285  return static_cast<SAO*>(object())->size();
286  }
287 
288  template<class T>
291  assert(object() != NULL);
292  return static_cast<SAO*>(object())->begin();
293  }
294 
295  template<class T>
297  SharedArray<T>::begin(void) const {
298  assert(object() != NULL);
299  return static_cast<SAO*>(object())->begin();
300  }
301 
302  template<class T>
305  assert(object() != NULL);
306  return static_cast<SAO*>(object())->end();
307  }
308 
309  template<class T>
311  SharedArray<T>::end(void) const {
312  assert(object() != NULL);
313  return static_cast<SAO*>(object())->end();
314  }
315 
316  template<class T>
319  assert(object() != NULL);
320  return reverse_iterator(static_cast<SAO*>(object())->end());
321  }
322 
323  template<class T>
326  assert(object() != NULL);
327  return const_reverse_iterator(static_cast<SAO*>(object())->end());
328  }
329 
330  template<class T>
333  assert(object() != NULL);
334  return reverse_iterator(static_cast<SAO*>(object())->begin());
335  }
336 
337  template<class T>
339  SharedArray<T>::rend(void) const {
340  assert(object() != NULL);
341  return const_reverse_iterator(static_cast<SAO*>(object())->begin());
342  }
343 
344  template<class Char, class Traits, class T>
345  std::basic_ostream<Char,Traits>&
346  operator <<(std::basic_ostream<Char,Traits>& os,
347  const SharedArray<T>& x) {
348  std::basic_ostringstream<Char,Traits> s;
349  s.copyfmt(os); s.width(0);
350  s << '{';
351  if (x.size() > 0) {
352  s << x[0];
353  for (int i=1; i<x.size(); i++)
354  s << ", " << x[i];
355  }
356  s << '}';
357  return os << s.str();
358  }
359 
360 }
361 
362 // 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:1653
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:185
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:431
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:234
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:457
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
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:508
Heap heap
The single global heap.
Definition: heap.cpp:44
Post propagator for SetVar x
Definition: set.hh:765
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...