Generated on Tue Jun 16 2015 12:49:07 for Gecode by doxygen 1.8.9.1
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: 2013-03-13 06:01:49 +0100 (Wed, 13 Mar 2013) $ by $Author: tack $
17  * $Revision: 13521 $
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 SharedHandle::Object* copy(void) const;
73  virtual ~SAO(void);
74 
76  T& operator [](int i);
78  const T& operator [](int i) const;
79 
81  int size(void) const;
82 
84  T* begin(void);
86  const T* begin(void) const;
88  T* end(void);
90  const T* end(void) const;
91  };
92  public:
94 
95  typedef T value_type;
98  typedef T& reference;
100  typedef const T& const_reference;
102  typedef T* pointer;
104  typedef const T* const_pointer;
106  typedef T* iterator;
108  typedef const T* const_iterator;
110  typedef std::reverse_iterator<T*> reverse_iterator;
112  typedef std::reverse_iterator<const T*> const_reverse_iterator;
114 
122  SharedArray(void);
124  SharedArray(int n);
132  void init(int n);
134  SharedArray(const SharedArray& a);
136  SharedArray(const ArgArrayBase<T>& a);
137 
139  T& operator [](int i);
141  const T& operator [](int i) const;
142 
144  int size(void) const;
145 
147 
148  iterator begin(void);
151  const_iterator begin(void) const;
153  iterator end(void);
155  const_iterator end(void) const;
157  reverse_iterator rbegin(void);
159  const_reverse_iterator rbegin(void) const;
161  reverse_iterator rend(void);
163  const_reverse_iterator rend(void) const;
165  };
166 
171  template<class Char, class Traits, class T>
172  std::basic_ostream<Char,Traits>&
173  operator <<(std::basic_ostream<Char,Traits>& os,
174  const SharedArray<T>& x);
175 
176 
177  /*
178  * Implementation
179  *
180  */
181 
182  /*
183  * Shared arrays
184  *
185  */
186  template<class T>
188  SharedArray<T>::SAO::SAO(int n0) : n(n0) {
189  a = (n>0) ? heap.alloc<T>(n) : NULL;
190  }
191 
192  template<class T>
195  SAO* o = new SAO(n);
196  for (int i=n; i--;)
197  o->a[i] = a[i];
198  return o;
199  }
200 
201  template<class T>
203  if (n>0) {
204  heap.free<T>(a,n);
205  }
206  }
207 
208  template<class T>
209  forceinline T&
211  assert((i>=0) && (i<n));
212  return a[i];
213  }
214 
215  template<class T>
216  forceinline const T&
218  assert((i>=0) && (i<n));
219  return a[i];
220  }
221 
222  template<class T>
223  forceinline int
225  return n;
226  }
227 
228  template<class T>
229  forceinline T*
231  return a;
232  }
233 
234  template<class T>
235  forceinline const T*
237  return a;
238  }
239 
240  template<class T>
241  forceinline T*
243  return a+n;
244  }
245 
246  template<class T>
247  forceinline const T*
249  return a+n;
250  }
251 
252 
253  template<class T>
256 
257  template<class T>
260  : SharedHandle(new SAO(n)) {}
261 
262  template<class T>
265  : SharedHandle(sa) {}
266 
267  template<class T>
268  forceinline void
270  assert(object() == NULL);
271  object(new SAO(n));
272  }
273 
274  template<class T>
275  forceinline T&
277  assert(object() != NULL);
278  return (*static_cast<SAO*>(object()))[i];
279  }
280 
281  template<class T>
282  forceinline const T&
284  assert(object() != NULL);
285  return (*static_cast<SAO*>(object()))[i];
286  }
287 
288  template<class T>
291  : SharedHandle(new SAO(a.size())) {
292  for (int i=a.size(); i--; )
293  operator [](i)=a[i];
294  }
295 
296  template<class T>
297  forceinline int
298  SharedArray<T>::size(void) const {
299  assert(object() != NULL);
300  return static_cast<SAO*>(object())->size();
301  }
302 
303  template<class T>
306  assert(object() != NULL);
307  return static_cast<SAO*>(object())->begin();
308  }
309 
310  template<class T>
312  SharedArray<T>::begin(void) const {
313  assert(object() != NULL);
314  return static_cast<SAO*>(object())->begin();
315  }
316 
317  template<class T>
320  assert(object() != NULL);
321  return static_cast<SAO*>(object())->end();
322  }
323 
324  template<class T>
326  SharedArray<T>::end(void) const {
327  assert(object() != NULL);
328  return static_cast<SAO*>(object())->end();
329  }
330 
331  template<class T>
334  assert(object() != NULL);
335  return reverse_iterator(static_cast<SAO*>(object())->end());
336  }
337 
338  template<class T>
341  assert(object() != NULL);
342  return const_reverse_iterator(static_cast<SAO*>(object())->end());
343  }
344 
345  template<class T>
348  assert(object() != NULL);
349  return reverse_iterator(static_cast<SAO*>(object())->begin());
350  }
351 
352  template<class T>
354  SharedArray<T>::rend(void) const {
355  assert(object() != NULL);
356  return const_reverse_iterator(static_cast<SAO*>(object())->begin());
357  }
358 
359  template<class Char, class Traits, class T>
360  std::basic_ostream<Char,Traits>&
361  operator <<(std::basic_ostream<Char,Traits>& os,
362  const SharedArray<T>& x) {
363  std::basic_ostringstream<Char,Traits> s;
364  s.copyfmt(os); s.width(0);
365  s << '{';
366  if (x.size() > 0) {
367  s << x[0];
368  for (int i=1; i<x.size(); i++)
369  s << ", " << x[i];
370  }
371  s << '}';
372  return os << s.str();
373  }
374 
375 }
376 
377 // STATISTICS: kernel-other
T & operator[](int i)
Access element at position i.
The shared handle.
Definition: core.hpp:79
const T * const_iterator
Type of the iterator used to iterate read-only through this array's elements.
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
T * end(void)
Return end of array (for iterators)
Implementation of object for shared arrays.
iterator end(void)
Return an iterator past the end of the array.
const T & const_reference
Type of a constant reference to the value type.
T value_type
Type of the view stored in this array.
Heap heap
The single global heap.
Definition: heap.cpp:49
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:400
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's elements.
T & operator[](int i)
Access element at position i.
unsigned int size(I &i)
Size of all ranges of range iterator i.
struct Gecode::@519::NNF::@60::@62 a
For atomic nodes.
int size(void) const
Return number of elements.
iterator begin(void)
Return an iterator at the beginning of the array.
reverse_iterator rbegin(void)
Return a reverse iterator at the end of the array.
virtual SharedHandle::Object * copy(void) const
Create copy of elements.
T & reference
Type of a reference to the value type.
const T * const_pointer
Type of a read-only pointer to the value type.
int size(void) const
Return number of elements.
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:426
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
T * pointer
Type of a pointer to the value type.
Base-class for argument arrays.
Definition: array.hpp:521
#define forceinline
Definition: config.hpp:132
The shared object.
Definition: core.hpp:88
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'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's elements...