LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Core.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_DETAIL_Core_h_INCLUDED
24 #define NDARRAY_DETAIL_Core_h_INCLUDED
25 
32 #include <boost/intrusive_ptr.hpp>
33 #include <boost/mpl/int.hpp>
34 #include "ndarray/Vector.h"
35 #include "ndarray/Manager.h"
36 
37 namespace ndarray {
38 namespace detail {
39 
58 template <int N>
59 class Core : public Core<N-1> {
60 public:
61  typedef boost::mpl::int_<N> ND;
62  typedef Core<N-1> Super;
63  typedef boost::intrusive_ptr<Core> Ptr;
64  typedef boost::intrusive_ptr<Core const> ConstPtr;
65 
67  template <int M>
68  static Ptr create(
69  Vector<int,M> const & shape,
70  Vector<int,M> const & strides,
71  Manager::Ptr const & manager = Manager::Ptr()
72  ) {
73  return Ptr(new Core(shape, strides, manager), false);
74  }
75 
77  template <int M>
78  static Ptr create(
79  Vector<int,M> const & shape,
80  DataOrderEnum order,
81  Manager::Ptr const & manager = Manager::Ptr()
82  ) {
83  if (order == ROW_MAJOR) {
84  return Ptr(new Core(shape, manager), false);
85  } else {
86  return Ptr(new Core(shape, 1, manager), false);
87  }
88  }
89 
91  static Ptr create(
92  Manager::Ptr const & manager = Manager::Ptr()
93  ) {
94  return Ptr(new Core(manager), false);
95  }
96 
97  Ptr copy() const { return Ptr(new Core(*this)); }
98 
100  int getSize() const { return _size; }
101 
103  int getStride() const { return _stride; }
104 
106  void setSize(int size) { _size = size; }
107 
109  void setStride(int stride) { _stride = stride; }
110 
112  template <int M>
113  int computeOffset(Vector<int,M> const & index) const {
114  return index[M-N] * this->getStride() + Super::computeOffset(index);
115  }
116 
118  template <int M>
119  void fillShape(Vector<int,M> & shape) const {
120  shape[M-N] = this->getSize();
121  Super::fillShape(shape);
122  }
123 
125  template <int M>
126  void fillStrides(Vector<int,M> & strides) const {
127  strides[M-N] = this->getStride();
128  Super::fillStrides(strides);
129  }
130 
132  int getNumElements() const {
133  return getSize() * Super::getNumElements();
134  }
135 
136 protected:
137 
138  // Explicit strides
139  template <int M>
141  Vector<int,M> const & shape,
142  Vector<int,M> const & strides,
143  Manager::Ptr const & manager
144  ) : Super(shape, strides, manager), _size(shape[M-N]), _stride(strides[M-N]) {}
145 
146  // Row-major strides
147  template <int M>
149  Vector<int,M> const & shape,
150  Manager::Ptr const & manager
151  ) : Super(shape, manager), _size(shape[M-N]), _stride(Super::getStride() * Super::getSize()) {}
152 
153  // Column-major strides
154  template <int M>
156  Vector<int,M> const & shape,
157  int stride,
158  Manager::Ptr const & manager
159  ) : Super(shape, stride * shape[M-N], manager), _size(shape[M-N]), _stride(stride) {}
160 
161  // Zero shape and strides
163  Manager::Ptr const & manager
164  ) : Super(manager), _size(0), _stride(0) {}
165 
166  Core(Core const & other) : Super(other), _size(other._size), _stride(other._stride) {}
167 
168 private:
169  int _size;
170  int _stride;
171 };
172 
182 template <>
183 class Core<0> {
184 public:
185  typedef boost::mpl::int_<0> ND;
186  typedef boost::intrusive_ptr<Core> Ptr;
187  typedef boost::intrusive_ptr<Core const> ConstPtr;
188 
189  friend inline void intrusive_ptr_add_ref(Core const * core) {
190  ++core->_rc;
191  }
192 
193  friend inline void intrusive_ptr_release(Core const * core) {
194  if ((--core->_rc)==0) delete core;
195  }
196 
197  Ptr copy() const { return Ptr(new Core(*this)); }
198 
199  int getSize() const { return 1; }
200  int getStride() const { return 1; }
201 
203  template <int M>
204  int computeOffset(Vector<int,M> const & index) const { return 0; }
205 
207  Manager::Ptr getManager() const { return _manager; }
208 
210  void setManager(Manager::Ptr const & manager) { _manager = manager; }
211 
213  template <int M>
214  void fillShape(Vector<int,M> const & shape) const {}
215 
217  template <int M>
218  void fillStrides(Vector<int,M> const & strides) const {}
219 
221  int getNumElements() const { return 1; }
222 
224  int getRC() const { return _rc; }
225 
227  bool isUnique() const { return (_rc == 1) && (_manager->getRC() == 1) && _manager->isUnique(); }
228 
229 protected:
230 
231  virtual ~Core() {}
232 
233  template <int M>
235  Vector<int,M> const & shape,
236  Vector<int,M> const & strides,
237  Manager::Ptr const & manager
238  ) : _manager(manager), _rc(1) {}
239 
240  template <int M>
242  Vector<int,M> const & shape,
243  Manager::Ptr const & manager
244  ) : _manager(manager), _rc(1) {}
245 
246  template <int M>
248  Vector<int,M> const & shape,
249  int stride,
250  Manager::Ptr const & manager
251  ) : _manager(manager), _rc(1) {}
252 
254  Manager::Ptr const & manager
255  ) : _manager(manager), _rc(1) {}
256 
257  Core(Core const & other) : _manager(other._manager), _rc(1) {}
258 
259 private:
261  mutable int _rc;
262 };
263 
264 
270 template <int P, int N>
271 inline Core<N-P> const &
272 getDimension(Core<N> const & core) { return core; }
273 
279 template <int P, int N>
280 inline typename Core<N-P>::Ptr
281 getDimension(typename Core<N>::Ptr const & core) { return core; }
282 
283 } // namespace detail
284 } // namespace ndarray
285 
286 #endif // !NDARRAY_DETAIL_Core_h_INCLUDED
static Ptr create(Vector< int, M > const &shape, Vector< int, M > const &strides, Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given shape, strides, and manager.
Definition: Core.h:68
Manager::Ptr getManager() const
Return the Manager that determines the lifetime of the array data.
Definition: Core.h:207
Core(Vector< int, M > const &shape, Manager::Ptr const &manager)
Definition: Core.h:241
int getNumElements() const
Recursively determine the total number of elements.
Definition: Core.h:132
Ptr copy() const
Definition: Core.h:197
Definition for Vector.
int getStride() const
Return the stride of the Nth dimension.
Definition: Core.h:103
int getSize() const
Return the size of the Nth dimension.
Definition: Core.h:100
Core(Vector< int, M > const &shape, int stride, Manager::Ptr const &manager)
Definition: Core.h:155
int getRC() const
Return the reference count (for debugging purposes).
Definition: Core.h:224
Core(Vector< int, M > const &shape, int stride, Manager::Ptr const &manager)
Definition: Core.h:247
bool isUnique() const
Return true if the Core and Manager reference counts are 1 and the manager is unique.
Definition: Core.h:227
boost::intrusive_ptr< Core > Ptr
intrusive_ptr to Core
Definition: Core.h:63
boost::mpl::int_< N > ND
number of dimensions
Definition: Core.h:61
boost::intrusive_ptr< Core > Ptr
Definition: Core.h:186
Definition of Manager, which manages the ownership of array data.
void setManager(Manager::Ptr const &manager)
Set the Manager that determines the lifetime of the array data.
Definition: Core.h:210
boost::intrusive_ptr< Core const > ConstPtr
const intrusive_ptr to Core
Definition: Core.h:64
int computeOffset(Vector< int, M > const &index) const
Recursively compute the offset to an element.
Definition: Core.h:204
Ptr copy() const
Definition: Core.h:97
Core(Manager::Ptr const &manager)
Definition: Core.h:253
void fillShape(Vector< int, M > const &shape) const
Recursively fill a shape vector.
Definition: Core.h:214
Core< N-P > const & getDimension(Core< N > const &core)
Definition: Core.h:272
Core(Vector< int, M > const &shape, Vector< int, M > const &strides, Manager::Ptr const &manager)
Definition: Core.h:234
static Ptr create(Vector< int, M > const &shape, DataOrderEnum order, Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given shape and manager with contiguous strides.
Definition: Core.h:78
boost::mpl::int_< 0 > ND
Definition: Core.h:185
static Ptr create(Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given manager and zero shape and strides.
Definition: Core.h:91
A fixed-size 1D array class.
Definition: Vector.h:93
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
void setStride(int stride)
Set the stride of the Nth dimension.
Definition: Core.h:109
Core(Manager::Ptr const &manager)
Definition: Core.h:162
int getStride() const
Definition: Core.h:200
Core(Core const &other)
Definition: Core.h:257
int computeOffset(Vector< int, M > const &index) const
Recursively compute the offset to an element.
Definition: Core.h:113
Core(Vector< int, M > const &shape, Vector< int, M > const &strides, Manager::Ptr const &manager)
Definition: Core.h:140
void setSize(int size)
Set the size of the Nth dimension.
Definition: Core.h:106
Core(Vector< int, M > const &shape, Manager::Ptr const &manager)
Definition: Core.h:148
DataOrderEnum
An enumeration for stride computation.
Definition: ndarray_fwd.h:60
void fillStrides(Vector< int, M > &strides) const
Recursively fill a strides vector.
Definition: Core.h:126
Manager::Ptr _manager
Definition: Core.h:260
Core(Core const &other)
Definition: Core.h:166
void fillShape(Vector< int, M > &shape) const
Recursively fill a shape vector.
Definition: Core.h:119
int getSize() const
Definition: Core.h:199
int getNumElements() const
Recursively determine the total number of elements.
Definition: Core.h:221
friend void intrusive_ptr_release(Core const *core)
Definition: Core.h:193
Core< N-1 > Super
base class
Definition: Core.h:62
friend void intrusive_ptr_add_ref(Core const *core)
Definition: Core.h:189
void fillStrides(Vector< int, M > const &strides) const
Recursively fill a strides vector.
Definition: Core.h:218
boost::intrusive_ptr< Core const > ConstPtr
Definition: Core.h:187