ndarray: /home/runner/work/ndarray/ndarray/include/ndarray/detail/Core.h Source File

1

2

3

4

5

6

7

8

9

10

11#ifndef NDARRAY_DETAIL_Core_h_INCLUDED

12#define NDARRAY_DETAIL_Core_h_INCLUDED

13

20#include <boost/intrusive_ptr.hpp>

21#include <boost/mpl/int.hpp>

24

25namespace ndarray {

26namespace detail {

27

46template <int N>

48public:

49 typedef boost::mpl::int_<N> ND;

51 typedef boost::intrusive_ptr<Core> Ptr;

52 typedef boost::intrusive_ptr<Core const> ConstPtr;

53

55 template <int M>

59 Manager::Ptr const & manager = Manager::Ptr()

60 ) {

61 return Ptr(new Core(shape, strides, manager), false);

62 }

63

65 template <int M>

69 Manager::Ptr const & manager = Manager::Ptr()

70 ) {

71 if (order == ROW_MAJOR) {

72 return Ptr(new Core(shape, manager), false);

73 } else {

74 return Ptr(new Core(shape, 1, manager), false);

75 }

76 }

77

80 Manager::Ptr const & manager = Manager::Ptr()

81 ) {

82 return Ptr(new Core(manager), false);

83 }

84

85 Ptr copy() const { return Ptr(new Core(*this)); }

86

88 Size getSize() const { return _size; }

89

91 Offset getStride() const { return _stride; }

92

94 void setSize(Size size) { _size = size; }

95

97 void setStride(Offset stride) { _stride = stride; }

98

100 template <int M>

103 }

104

106 template <int M>

108 shape[M-N] = this->getSize();

110 }

111

113 template <int M>

115 strides[M-N] = this->getStride();

117 }

118

122 }

123

124protected:

125

126

127 template <int M>

131 Manager::Ptr const & manager

132 ) : Super(shape, strides, manager), _size(shape[M-N]), _stride(strides[M-N]) {}

133

134

135 template <int M>

136 Core (

138 Manager::Ptr const & manager

140

141

142 template <int M>

143 Core (

144 Vector<Size,M> const & shape,

145 Offset stride,

146 Manager::Ptr const & manager

147 ) : Super(shape, stride * shape[M-N], manager), _size(shape[M-N]), _stride(stride) {}

148

149

150 Core (

151 Manager::Ptr const & manager

152 ) : Super(manager), _size(0), _stride(0) {}

153

154 Core(Core const & other) : Super(other), _size(other._size), _stride(other._stride) {}

155

156private:

157 Size _size;

158 Offset _stride;

159};

160

170template <>

172public:

173 typedef boost::mpl::int_<0> ND;

174 typedef boost::intrusive_ptr<Core> Ptr;

175 typedef boost::intrusive_ptr<Core const> ConstPtr;

176

177 friend inline void intrusive_ptr_add_ref(Core const * core) {

178 ++core->_rc;

179 }

180

181 friend inline void intrusive_ptr_release(Core const * core) {

182 if ((--core->_rc)==0) delete core;

183 }

184

185 Ptr copy() const { return Ptr(new Core(*this)); }

186

187 Size getSize() const { return 1; }

188 Offset getStride() const { return 1; }

189

191 template <int M>

193

195 Manager::Ptr getManager() const { return _manager; }

196

198 void setManager(Manager::Ptr const & manager) { _manager = manager; }

199

201 template <int M>

203

205 template <int M>

207

210

212 int getRC() const { return _rc; }

213

215 bool isUnique() const { return (_rc == 1) && (_manager->getRC() == 1) && _manager->isUnique(); }

216

217protected:

218

219 virtual ~Core() {}

220

221 template <int M>

222 Core(

225 Manager::Ptr const & manager

226 ) : _manager(manager), _rc(1) {}

227

228 template <int M>

229 Core(

230 Vector<Size,M> const & shape,

231 Manager::Ptr const & manager

232 ) : _manager(manager), _rc(1) {}

233

234 template <int M>

235 Core(

236 Vector<Size,M> const & shape,

237 Offset stride,

238 Manager::Ptr const & manager

239 ) : _manager(manager), _rc(1) {}

240

241 Core(

242 Manager::Ptr const & manager

243 ) : _manager(manager), _rc(1) {}

244

245 Core(Core const & other) : _manager(other._manager), _rc(1) {}

246

247private:

248 Manager::Ptr _manager;

249 mutable int _rc;

250};

251

252

258template <int P, int N>

259inline Core<N-P> const &

260getDimension(Core<N> const & core) { return core; }

261

267template <int P, int N>

268inline typename Core<N-P>::Ptr

269getDimension(typename Core<N>::Ptr const & core) { return core; }

270

271}

272}

273

274#endif

Definition of Manager, which manages the ownership of array data.

bool isUnique() const

Return true if the Core and Manager reference counts are 1 and the manager is unique.

Definition Core.h:215

void setManager(Manager::Ptr const &manager)

Set the Manager that determines the lifetime of the array data.

Definition Core.h:198

Manager::Ptr getManager() const

Return the Manager that determines the lifetime of the array data.

Definition Core.h:195

Offset computeOffset(Vector< Size, M > const &index) const

Recursively compute the offset to an element.

Definition Core.h:192

Size getNumElements() const

Recursively determine the total number of elements.

Definition Core.h:209

void fillShape(Vector< Size, M > const &shape) const

Recursively fill a shape vector.

Definition Core.h:202

int getRC() const

Return the reference count (for debugging purposes).

Definition Core.h:212

void fillStrides(Vector< Offset, M > const &strides) const

Recursively fill a strides vector.

Definition Core.h:206

static Ptr create(Vector< Size, M > const &shape, Vector< Offset, M > const &strides, Manager::Ptr const &manager=Manager::Ptr())

Create a Core::Ptr with the given shape, strides, and manager.

Definition Core.h:56

Size getSize() const

Return the size of the Nth dimension.

Definition Core.h:88

void fillStrides(Vector< Offset, M > &strides) const

Recursively fill a strides vector.

Definition Core.h:114

void fillShape(Vector< Size, M > &shape) const

Recursively fill a shape vector.

Definition Core.h:107

static Ptr create(Vector< Size, 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:66

Offset computeOffset(Vector< Size, M > const &index) const

Recursively compute the offset to an element.

Definition Core.h:101

Size getNumElements() const

Recursively determine the total number of elements.

Definition Core.h:120

void setSize(Size size)

Set the size of the Nth dimension.

Definition Core.h:94

boost::intrusive_ptr< Core > Ptr

intrusive_ptr to Core

Definition Core.h:51

boost::intrusive_ptr< Core const > ConstPtr

const intrusive_ptr to Core

Definition Core.h:52

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:79

boost::mpl::int_< N > ND

number of dimensions

Definition Core.h:49

Core< N-1 > Super

base class

Definition Core.h:50

void setStride(Offset stride)

Set the stride of the Nth dimension.

Definition Core.h:97

Offset getStride() const

Return the stride of the Nth dimension.

Definition Core.h:91

DataOrderEnum

An enumeration for stride computation.

Definition ndarray_fwd.h:51

A fixed-size 1D array class.

Definition Vector.h:82