9 #include "imstkAbstractDataArray.h" 10 #include "imstkMath.h" 11 #include "imstkMacros.h" 28 static constexpr
int NumComponents = 1;
35 using iterator_category = std::forward_iterator_tag;
36 using difference_type = std::ptrdiff_t;
41 iterator(pointer ptr, pointer end) : ptr_(ptr), end_(end) { }
47 #ifdef IMSTK_CHECK_ARRAY_RANGE 48 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
56 #ifdef IMSTK_CHECK_ARRAY_RANGE 57 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
62 reference operator*() {
return *ptr_; }
64 pointer operator->() {
return ptr_; }
66 bool operator==(
const self_type& rhs)
const {
return ptr_ == rhs.ptr_; }
68 bool operator!=(
const self_type& rhs)
const {
return ptr_ != rhs.ptr_; }
80 using iterator_category = std::forward_iterator_tag;
81 using difference_type = std::ptrdiff_t;
83 using reference =
const T&;
86 const_iterator(pointer ptr, pointer end) : ptr_(ptr), end_(end) { }
92 #ifdef IMSTK_CHECK_ARRAY_RANGE 93 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
101 #ifdef IMSTK_CHECK_ARRAY_RANGE 102 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
109 const pointer operator->() {
return ptr_; }
113 bool operator!=(
const self_type& rhs)
const {
return ptr_ != rhs.ptr_; }
127 setType(TypeTemplateMacro(T));
136 setType(TypeTemplateMacro(T));
151 setType(TypeTemplateMacro(T));
152 m_size = m_capacity =
static_cast<int>(list.size());
162 m_mapped = other.m_mapped;
163 m_size = other.m_size;
164 m_capacity = other.m_capacity;
165 m_scalarType = other.m_scalarType;
168 m_data = other.m_data;
172 m_data =
new T[m_size];
173 std::copy_n(other.m_data, m_size, m_data);
179 m_mapped = other.m_mapped;
180 m_size = other.m_size;
181 m_capacity = other.m_capacity;
182 m_scalarType = other.m_scalarType;
183 m_data = other.m_data;
184 other.m_mapped =
true;
208 if (size <= m_capacity)
214 const T* oldData = m_data;
215 m_data =
new T[
size];
216 std::copy_n(oldData, m_size, m_data);
218 m_size = m_capacity =
size;
225 inline void fill(
const T& val) { std::fill_n(m_data, m_size, val); }
232 const T* oldData = m_data;
233 m_data =
new T[m_size];
234 std::copy_n(oldData, m_size, m_data);
250 const int newSize = m_size + 1;
251 if (newSize > m_capacity)
256 m_data[newSize - 1] = val;
267 const int newSize = m_size + 1;
268 if (newSize > m_capacity)
273 m_data[newSize - 1] = val;
279 iterator
begin() {
return iterator(m_data, m_data + m_size); }
281 const_iterator
begin()
const {
return const_iterator(m_data, m_data + m_size); }
283 iterator end() {
return iterator(m_data + m_size, m_data + m_size); }
285 const_iterator end()
const {
return const_iterator(m_data + m_size, m_data + m_size); }
287 const_iterator cbegin()
const {
return const_iterator(m_data, m_data + m_size); }
289 const_iterator cend()
const {
return const_iterator(m_data + m_size, m_data + m_size); }
295 inline void reserve(
const int capacity)
override 297 if (m_mapped) {
return; }
298 if (capacity <= m_capacity) {
return; }
300 const int currSize = m_size;
305 inline T* getPointer() {
return m_data; }
308 inline T& operator[](
const size_t pos)
310 #ifdef IMSTK_CHECK_ARRAY_RANGE 311 if (pos >= static_cast<size_t>(m_size)) {
throw std::out_of_range(
"Index out of range"); }
316 inline const T& operator[](
const size_t pos)
const 318 #ifdef IMSTK_CHECK_ARRAY_RANGE 319 if (pos >= static_cast<size_t>(m_size)) {
throw std::out_of_range(
"Index out of range"); }
328 inline T&
at(
const size_t pos)
330 #ifdef IMSTK_CHECK_ARRAY_RANGE 331 if (pos >= static_cast<size_t>(m_size)) {
throw std::out_of_range(
"Index out of range"); }
340 inline const T&
at(
const size_t pos)
const 342 #ifdef IMSTK_CHECK_ARRAY_RANGE 343 if (pos >= static_cast<size_t>(m_size)) {
throw std::out_of_range(
"Index out of range"); }
359 m_data =
new T[list.size()];
366 m_size = m_capacity =
static_cast<int>(list.size());
390 std::copy_n(other.m_data, other.m_size, m_data);
391 m_size = other.m_size;
422 m_size = m_capacity =
size;
435 throw(std::runtime_error(
"Can't cast a mapped array"));
439 for (
auto& i : *
this)
449 std::shared_ptr<AbstractDataArray>
cast(ScalarTypeId type)
override 451 if (type == AbstractDataArray::m_scalarType)
453 return std::make_shared<DataArray<T>>(*this);
459 throw(std::runtime_error(
"Unknown scalar type"));
467 std::unique_ptr<DataArray<T>>
clone()
469 return std::unique_ptr<DataArray<T>>(cloneImplementation());
const T & at(const size_t pos) const
int size() const
Get number of values/tuples.
std::shared_ptr< AbstractDataArray > cast(ScalarTypeId type) override
Cast array to the IMSTK type on the abstract interface.
DataArray(const int size)
Constructs a data array.
DataArray< N > cast()
Cast array to specific c++ type.
virtual int getNumberOfComponents() const override
Returns the number of components.
virtual void squeeze()
Resize to current size.
DataArray(std::initializer_list< U > list)
Constructs from intializer list.
void fill(const T &val)
Fill the array with the specified value.
std::unique_ptr< DataArray< T > > clone()
Polymorphic clone, shadows the declaration in the superclasss but returns own type.
This class serves as the base class of DataArray, for typeless use.
DataArray()
Constructs an empty data array DataArray will never have capacity < 1.
void reserve(const int capacity) override
Allocates extra capacity, for the number of values, conservative reallocate.
void * getVoidPointer() override
Returns void pointer to data.
void resize(const int size) override
Resize data array to hold exactly size number of values.
void push_back(const T &val)
Append the data array to hold the new value, resizes if neccesary.
iterator begin()
begin(), end() to mirror std::vector
void setData(T *ptr, const int size)
Computes the range of a component of the vectors elements.
DataArray(const DataArray &other)
Copy Constructor.
DataArray< T > & operator=(std::initializer_list< U > list)
Allow initialization from initializer list, ie: DataArray<int> arr = { 1, 2 }.
bool operator==(const Color &color_lhs, const Color &color_rhs)
Comparison operator.
Color operator*(const Color &color_lhs, const Color &color_rhs)
Multiplication operators.
Simple dynamic array implementation that also supports event posting and viewing/facade.