Generated on Thu Feb 14 2013 20:59:45 for Gecode by doxygen 1.8.3.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: 2011-01-27 23:03:05 +1100 (Thu, 27 Jan 2011) $ by $Author: schulte $
17  * $Revision: 11570 $
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  };
83  public:
85 
86 
87  typedef T value_type;
89  typedef T& reference;
91  typedef const T& const_reference;
93  typedef T* pointer;
95  typedef const T* const_pointer;
97  typedef T* iterator;
99  typedef const T* const_iterator;
101  typedef std::reverse_iterator<T*> reverse_iterator;
103  typedef std::reverse_iterator<const T*> const_reverse_iterator;
105 
113  SharedArray(void);
115  SharedArray(int n);
123  void init(int n);
125  SharedArray(const SharedArray& a);
127  SharedArray(const ArgArrayBase<T>& a);
128 
130  T& operator [](int i);
132  const T& operator [](int i) const;
133 
135  int size(void) const;
136 
138 
139 
140  iterator begin(void);
142  const_iterator begin(void) const;
144  iterator end(void);
146  const_iterator end(void) const;
148  reverse_iterator rbegin(void);
150  const_reverse_iterator rbegin(void) const;
152  reverse_iterator rend(void);
154  const_reverse_iterator rend(void) const;
156  };
157 
162  template<class Char, class Traits, class T>
163  std::basic_ostream<Char,Traits>&
164  operator <<(std::basic_ostream<Char,Traits>& os,
165  const SharedArray<T>& x);
166 
167 
168  /*
169  * Implementation
170  *
171  */
172 
173  /*
174  * Shared arrays
175  *
176  */
177  template<class T>
179  SharedArray<T>::SAO::SAO(int n0) : n(n0) {
180  a = (n>0) ? heap.alloc<T>(n) : NULL;
181  }
182 
183  template<class T>
186  SAO* o = new SAO(n);
187  for (int i=n; i--;)
188  o->a[i] = a[i];
189  return o;
190  }
191 
192  template<class T>
194  if (n>0) {
195  heap.free<T>(a,n);
196  }
197  }
198 
199  template<class T>
200  forceinline T&
202  assert((i>=0) && (i<n));
203  return a[i];
204  }
205 
206  template<class T>
207  forceinline const T&
209  assert((i>=0) && (i<n));
210  return a[i];
211  }
212 
213  template<class T>
214  forceinline int
216  return n;
217  }
218 
219 
220 
221  template<class T>
224 
225  template<class T>
228  : SharedHandle(new SAO(n)) {}
229 
230  template<class T>
233  : SharedHandle(sa) {}
234 
235  template<class T>
236  forceinline void
238  assert(object() == NULL);
239  object(new SAO(n));
240  }
241 
242  template<class T>
243  forceinline T&
245  assert(object() != NULL);
246  return (*static_cast<SAO*>(object()))[i];
247  }
248 
249  template<class T>
250  forceinline const T&
252  assert(object() != NULL);
253  return (*static_cast<SAO*>(object()))[i];
254  }
255 
256  template<class T>
259  : SharedHandle(new SAO(a.size())) {
260  for (int i=a.size(); i--; )
261  operator [](i)=a[i];
262  }
263 
264  template<class T>
265  forceinline int
266  SharedArray<T>::size(void) const {
267  assert(object() != NULL);
268  return static_cast<SAO*>(object())->size();
269  }
270 
271  template<class T>
274  assert(object() != NULL);
275  return &(*static_cast<SAO*>(object()))[0];
276  }
277 
278  template<class T>
280  SharedArray<T>::begin(void) const {
281  assert(object() != NULL);
282  return &(*static_cast<SAO*>(object()))[0];
283  }
284 
285  template<class T>
288  assert(object() != NULL);
289  return &(*static_cast<SAO*>(object()))[0] + size();
290  }
291 
292  template<class T>
294  SharedArray<T>::end(void) const {
295  assert(object() != NULL);
296  return &(*static_cast<SAO*>(object()))[0] + size();
297  }
298 
299  template<class T>
302  assert(object() != NULL);
303  return reverse_iterator(&(*static_cast<SAO*>(object()))[0] + size());
304  }
305 
306  template<class T>
309  assert(object() != NULL);
310  return const_reverse_iterator(&(*static_cast<SAO*>(object()))[0] + size());
311  }
312 
313  template<class T>
316  assert(object() != NULL);
317  return reverse_iterator(&(*static_cast<SAO*>(object()))[0]);
318  }
319 
320  template<class T>
322  SharedArray<T>::rend(void) const {
323  assert(object() != NULL);
324  return const_reverse_iterator(&(*static_cast<SAO*>(object()))[0]);
325  }
326 
327  template<class Char, class Traits, class T>
328  std::basic_ostream<Char,Traits>&
329  operator <<(std::basic_ostream<Char,Traits>& os,
330  const SharedArray<T>& x) {
331  std::basic_ostringstream<Char,Traits> s;
332  s.copyfmt(os); s.width(0);
333  s << '{';
334  if (x.size() > 0) {
335  s << x[0];
336  for (int i=1; i<x.size(); i++)
337  s << ", " << x[i];
338  }
339  s << '}';
340  return os << s.str();
341  }
342 
343 }
344 
345 // STATISTICS: kernel-other