NumCpp  2.1.0
A C++ implementation of the Python Numpy library
DataCube.hpp
Go to the documentation of this file.
1 #pragma once
30 
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <deque>
36 #include <limits>
37 #include <string>
38 
39 namespace nc
40 {
41  //================================================================================
43  template<typename dtype>
44  class DataCube
45  {
46  public:
47  //================================Typedefs==================================
48  using iterator = typename std::deque<NdArray<dtype> >::iterator;
49  using const_iterator = typename std::deque<NdArray<dtype> >::const_iterator;
50 
51  //============================================================================
54  DataCube() = default;
55 
56  //============================================================================
61  explicit DataCube(uint32 inSize) :
62  cube_(inSize)
63  {};
64 
65  //============================================================================
73  {
74  return cube_.at(inIndex);
75  }
76 
77  //============================================================================
84  const NdArray<dtype>& at(uint32 inIndex) const
85  {
86  return cube_.at(inIndex);
87  }
88 
89  //============================================================================
94  NdArray<dtype>& back() noexcept
95  {
96  return cube_.back();
97  }
98 
99  //============================================================================
104  iterator begin() noexcept
105  {
106  return cube_.begin();
107  }
108 
109  //============================================================================
114  const_iterator cbegin() const noexcept
115  {
116  return cube_.cbegin();
117  }
118 
119  //============================================================================
124  void dump(const std::string& inFilename) const
125  {
126  filesystem::File f(inFilename);
127  if (!f.hasExt())
128  {
129  f.withExt("bin");
130  }
131 
132  std::ofstream ofile(f.fullName().c_str(), std::ios::binary);
133  if (!ofile.good())
134  {
135  THROW_RUNTIME_ERROR("Could not open the input file:\n\t" + inFilename);
136  }
137 
138  for (auto& ndarray : cube_)
139  {
140  ofile.write(reinterpret_cast<const char*>(ndarray.data()), ndarray.size() * sizeof(dtype));
141  }
142 
143  ofile.close();
144  }
145 
146  //============================================================================
151  bool isempty() noexcept
152  {
153  return cube_.empty();
154  }
155 
156  //============================================================================
161  iterator end() noexcept
162  {
163  return cube_.end();
164  }
165 
166  //============================================================================
171  const_iterator cend() const noexcept
172  {
173  return cube_.cend();
174  }
175 
176  //============================================================================
181  NdArray<dtype>& front() noexcept
182  {
183  return cube_.front();
184  }
185 
186  //============================================================================
191  const Shape& shape() const noexcept
192  {
193  return elementShape_;
194  }
195 
196  //============================================================================
201  uint32 size() const noexcept
202  {
203  return static_cast<uint32>(cube_.size());
204  }
205 
206  //============================================================================
209  void pop_back() noexcept
210  {
211  cube_.pop_back();
212  }
213 
214  //============================================================================
217  void pop_front() noexcept
218  {
219  cube_.pop_front();
220  }
221 
222  //============================================================================
227  void push_back(const NdArray<dtype>& inArray)
228  {
229  const Shape inputShape = inArray.shape();
230 
231  if (elementShape_.rows == 0 && elementShape_.cols == 0)
232  {
233  // initialize to the first input array size
234  elementShape_.rows = inputShape.rows;
235  elementShape_.cols = inputShape.cols;
236  }
237 
238  if (inputShape != elementShape_)
239  {
240  THROW_INVALID_ARGUMENT_ERROR("element arrays must all be the same shape");
241  }
242 
243  cube_.push_back(inArray);
244  }
245 
246  //============================================================================
251  void push_front(const NdArray<dtype>& inArray)
252  {
253  const Shape inputShape = inArray.shape();
254 
255  if (elementShape_.rows == 0 && elementShape_.cols == 0)
256  {
257  // initialize to the first input array size
258  elementShape_.rows = inputShape.rows;
259  elementShape_.cols = inputShape.cols;
260  }
261 
262  if (inputShape != elementShape_)
263  {
264  THROW_INVALID_ARGUMENT_ERROR("element arrays must all be the same shape.");
265  }
266 
267  cube_.push_front(inArray);
268  }
269 
270  //============================================================================
277  NdArray<dtype>& operator[](uint32 inIndex) noexcept
278  {
279  return cube_[inIndex];
280  }
281 
282  //============================================================================
289  const NdArray<dtype>& operator[](uint32 inIndex) const noexcept
290  {
291  return cube_[inIndex];
292  }
293 
294  private:
295  //================================Attributes==================================
296  std::deque<NdArray<dtype> > cube_{};
297  Shape elementShape_{ 0, 0 };
298  };
299 } // namespace nc
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
nc::DataCube::DataCube
DataCube(uint32 inSize)
Definition: DataCube.hpp:61
nc::DataCube::DataCube
DataCube()=default
nc::DataCube::end
iterator end() noexcept
Definition: DataCube.hpp:161
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::DataCube::back
NdArray< dtype > & back() noexcept
Definition: DataCube.hpp:94
nc::DataCube::isempty
bool isempty() noexcept
Definition: DataCube.hpp:151
NdArray.hpp
nc::DataCube::dump
void dump(const std::string &inFilename) const
Definition: DataCube.hpp:124
nc::DataCube::pop_front
void pop_front() noexcept
Definition: DataCube.hpp:217
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::DataCube::size
uint32 size() const noexcept
Definition: DataCube.hpp:201
nc::DataCube::front
NdArray< dtype > & front() noexcept
Definition: DataCube.hpp:181
nc::DataCube::const_iterator
typename std::deque< NdArray< dtype > >::const_iterator const_iterator
Definition: DataCube.hpp:49
nc::DataCube::operator[]
const NdArray< dtype > & operator[](uint32 inIndex) const noexcept
Definition: DataCube.hpp:289
nc::DataCube
Convenience container for holding a uniform array of NdArrays.
Definition: DataCube.hpp:44
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::DataCube::at
const NdArray< dtype > & at(uint32 inIndex) const
Definition: DataCube.hpp:84
nc::DataCube::cend
const_iterator cend() const noexcept
Definition: DataCube.hpp:171
THROW_RUNTIME_ERROR
#define THROW_RUNTIME_ERROR(msg)
Definition: Error.hpp:38
Filesystem.hpp
nc::DataCube::shape
const Shape & shape() const noexcept
Definition: DataCube.hpp:191
nc::DataCube::push_front
void push_front(const NdArray< dtype > &inArray)
Definition: DataCube.hpp:251
nc
Definition: Coordinate.hpp:45
nc::DataCube::push_back
void push_back(const NdArray< dtype > &inArray)
Definition: DataCube.hpp:227
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
nc::DataCube::operator[]
NdArray< dtype > & operator[](uint32 inIndex) noexcept
Definition: DataCube.hpp:277
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
nc::DataCube::iterator
typename std::deque< NdArray< dtype > >::iterator iterator
Definition: DataCube.hpp:48
Types.hpp
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:58
nc::DataCube::cbegin
const_iterator cbegin() const noexcept
Definition: DataCube.hpp:114
nc::DataCube::at
NdArray< dtype > & at(uint32 inIndex)
Definition: DataCube.hpp:72
nc::filesystem::File
Provides simple filesystem functions.
Definition: Filesystem.hpp:40
nc::DataCube::begin
iterator begin() noexcept
Definition: DataCube.hpp:104
nc::DataCube::pop_back
void pop_back() noexcept
Definition: DataCube.hpp:209