18#ifndef itkVariableLengthVector_h
19#define itkVariableLengthVector_h
33template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
34struct VariableLengthVectorExpression;
90template <
typename TValue>
91class ITK_TEMPLATE_EXPORT VariableLengthVector
110 struct AllocateRootPolicy
126 struct AlwaysReallocate : AllocateRootPolicy
129 operator()(
unsigned int itkNotUsed(newSize),
unsigned int itkNotUsed(oldSize))
const
153 struct NeverReallocate : AllocateRootPolicy
156 operator()([[maybe_unused]]
unsigned int newSize, [[maybe_unused]]
unsigned int oldSize)
const
158 itkAssertInDebugAndIgnoreInReleaseMacro(newSize == oldSize &&
159 "SetSize is expected to never change the VariableLengthVector size...");
182 struct ShrinkToFit : AllocateRootPolicy
185 operator()(
unsigned int newSize,
unsigned int oldSize)
const
187 return newSize != oldSize;
218 struct DontShrinkToFit : AllocateRootPolicy
221 operator()(
unsigned int newSize,
unsigned int oldSize)
const
223 return newSize > oldSize;
246 struct KeepValuesRootPolicy
270 struct KeepOldValues : KeepValuesRootPolicy
272 template <
typename TValue2>
274 operator()(
unsigned int newSize,
unsigned int oldSize, TValue2 * oldBuffer, TValue2 * newBuffer)
const
276 itkAssertInDebugAndIgnoreInReleaseMacro(newBuffer);
277 const size_t nb = std::min(newSize, oldSize);
278 itkAssertInDebugAndIgnoreInReleaseMacro(nb == 0 || (nb > 0 && oldBuffer !=
nullptr));
279 std::copy_n(oldBuffer, nb, newBuffer);
300 struct DumpOldValues : KeepValuesRootPolicy
302 template <
typename TValue2>
304 operator()(
unsigned int itkNotUsed(newSize),
305 unsigned int itkNotUsed(oldSize),
306 TValue2 * itkNotUsed(oldBuffer),
307 TValue2 * itkNotUsed(newBuffer))
const
315 using ValueType = TValue;
316 using ComponentType = TValue;
318 using Self = VariableLengthVector;
321 using ElementIdentifier =
unsigned int;
329 VariableLengthVector() =
default;
339 explicit VariableLengthVector(
unsigned int length);
354 VariableLengthVector(ValueType * datain,
unsigned int sz,
bool LetArrayManageMemory =
false);
375 VariableLengthVector(
const ValueType * datain,
unsigned int sz,
bool LetArrayManageMemory =
false);
396 template <
typename T>
397 VariableLengthVector(
const VariableLengthVector<T> & v)
399 m_NumElements = v.Size();
400 m_LetArrayManageMemory =
true;
401 if (m_NumElements != 0)
403 m_Data = this->AllocateElements(m_NumElements);
404 itkAssertInDebugAndIgnoreInReleaseMacro(m_Data !=
nullptr);
405 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
407 this->m_Data[i] =
static_cast<ValueType
>(v[i]);
425 VariableLengthVector(
const VariableLengthVector<TValue> & v);
436 Swap(
Self & v)
noexcept
438 itkAssertInDebugAndIgnoreInReleaseMacro(m_LetArrayManageMemory == v.m_LetArrayManageMemory);
440 swap(v.m_Data, m_Data);
441 swap(v.m_NumElements, m_NumElements);
452 VariableLengthVector(
Self && v)
noexcept;
463 operator=(
Self && v)
noexcept;
481 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
482 VariableLengthVector(VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const & rhs);
503 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
505 operator=(VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const & rhs);
511 Fill(TValue
const & v);
525 template <
typename T>
527 operator=(
const VariableLengthVector<T> & v)
536 ElementIdentifier
const N = v.Size();
537 this->SetSize(N, DontShrinkToFit(), DumpOldValues());
538 for (ElementIdentifier i = 0; i < N; ++i)
540 this->m_Data[i] =
static_cast<ValueType
>(v[i]);
562 operator=(
const Self & v);
573 FastAssign(
const Self & v);
583 operator=(TValue
const & v);
589 return m_NumElements;
594 return m_NumElements;
597 GetNumberOfElements()
const
599 return m_NumElements;
604 TValue & operator[](
unsigned int i) {
return this->m_Data[i]; }
607 TValue
const & operator[](
unsigned int i)
const {
return this->m_Data[i]; }
611 GetElement(
unsigned int i)
const
618 SetElement(
unsigned int i,
const TValue & value)
656 template <
typename TReallocatePolicy,
typename TKeepValuesPolicy>
658 SetSize(
unsigned int sz, TReallocatePolicy reallocatePolicy, TKeepValuesPolicy keepValues);
671 SetSize(
unsigned int sz,
bool destroyExistingData =
true)
676 if (destroyExistingData)
678 SetSize(sz, AlwaysReallocate(), KeepOldValues());
682 SetSize(sz, ShrinkToFit(), KeepOldValues());
690 DestroyExistingData();
705 SetData(TValue * datain,
bool LetArrayManageMemory =
false);
722 SetData(TValue * datain,
unsigned int sz,
bool LetArrayManageMemory =
false);
734 ~VariableLengthVector();
751 Reserve(ElementIdentifier size);
759 AllocateElements(ElementIdentifier size)
const;
762 GetDataPointer()
const
772 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
774 this->m_Data[i] -=
static_cast<ValueType
>(1.0);
784 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
786 this->m_Data[i] +=
static_cast<ValueType
>(1.0);
823 template <
typename T>
825 operator-=(
const VariableLengthVector<T> & v)
827 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.GetSize());
828 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
830 m_Data[i] -=
static_cast<ValueType
>(v[i]);
840 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
856 template <
typename T>
858 operator+=(
const VariableLengthVector<T> & v)
860 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.GetSize());
861 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
863 m_Data[i] +=
static_cast<ValueType
>(v[i]);
873 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
890 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
892 operator+=(VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const & rhs)
894 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() == Size());
895 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
897 m_Data[i] +=
static_cast<ValueType
>(rhs[i]);
912 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
914 operator-=(VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const & rhs)
916 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() == Size());
917 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
919 m_Data[i] -=
static_cast<ValueType
>(rhs[i]);
930 template <
typename T>
934 const ValueType & sc =
static_cast<ValueType
>(s);
935 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
949 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
963 template <
typename T>
967 const RealValueType sc = s;
968 for (ElementIdentifier i = 0; i < m_NumElements; ++i)
970 m_Data[i] =
static_cast<ValueType
>(
static_cast<RealValueType
>(m_Data[i]) / sc);
986 ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(
Self);
994 GetSquaredNorm()
const;
1000 return !m_LetArrayManageMemory;
1004 bool m_LetArrayManageMemory{
true };
1007 ElementIdentifier m_NumElements{ 0 };
1022template <
typename T>
1023struct IsArray : FalseType
1027template <
typename T>
1028struct IsArray<
itk::VariableLengthVector<T>> : TrueType
1031template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1032struct IsArray<VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>> : TrueType
1053template <
typename TExpr>
1062 Load(Type
const & v,
unsigned int itkNotUsed(idx))
1079template <
typename TExpr1,
typename TExpr2>
1080inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>::Value,
unsigned int>
1081GetSize(TExpr1
const & lhs, [[maybe_unused]] TExpr2
const & rhs)
1083 itkAssertInDebugAndIgnoreInReleaseMacro(lhs.Size() == rhs.Size());
1098template <
typename TExpr1,
typename TExpr2>
1099inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr1>, mpl::Not<mpl::IsArray<TExpr2>>>::Value,
unsigned int>
1100GetSize(TExpr1
const & lhs, TExpr2
const & itkNotUsed(rhs))
1114template <
typename TExpr1,
typename TExpr2>
1115inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr2>, mpl::Not<mpl::IsArray<TExpr1>>>::Value,
unsigned int>
1116GetSize(TExpr1
const & itkNotUsed(lhs), TExpr2
const & rhs)
1121template <
typename T>
1122struct GetType<VariableLengthVector<T>>
1126 Load(VariableLengthVector<T>
const & v,
unsigned int idx)
1131template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1132struct GetType<VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>>
1134 using Type =
typename VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>::ResType;
1136 Load(VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const & v,
unsigned int idx)
1157template <
typename TExpr1,
typename TExpr2>
1158struct CanBeAddedOrSubtracted
1159 : mpl::Or<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>,
1160 mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>,
1161 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2>>>
1175template <
typename TExpr1,
typename TExpr2>
1176struct CanBeMultiplied
1177 : mpl::Or<mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>,
1178 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2>>>
1192template <
typename TExpr1,
typename TExpr2>
1193struct CanBeDivided : mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>
1224template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1225struct VariableLengthVectorExpression
1227 VariableLengthVectorExpression(TExpr1
const & lhs, TExpr2
const & rhs)
1233 static_assert(std::is_base_of_v<Details::op::BinaryOperationConcept, TBinaryOp>,
1234 "The Binary Operation shall inherit from BinaryOperationConcept");
1241 return Details::GetSize(m_lhs, m_rhs);
1246 typename mpl::PromoteType<typename Details::GetType<TExpr1>::Type,
typename Details::GetType<TExpr2>::Type>::Type;
1260 ResType operator[](
unsigned int idx)
const
1262 itkAssertInDebugAndIgnoreInReleaseMacro(idx < Size());
1263 return TBinaryOp::Apply(Details::GetType<TExpr1>::Load(m_lhs, idx), Details::GetType<TExpr2>::Load(m_rhs, idx));
1273 GetSquaredNorm()
const;
1276 TExpr1
const & m_lhs;
1277 TExpr2
const & m_rhs;
1289template <
typename TExpr1,
typename TExpr2>
1290inline std::enable_if_t<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>::Value,
1291 VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Plus>>
1292operator+(TExpr1
const & lhs, TExpr2
const & rhs)
1294 return VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Plus>(lhs, rhs);
1306template <
typename TExpr1,
typename TExpr2>
1307inline std::enable_if_t<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>::Value,
1308 VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Sub>>
1309operator-(TExpr1
const & lhs, TExpr2
const & rhs)
1311 return VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Sub>(lhs, rhs);
1322template <
typename TExpr1,
typename TExpr2>
1323inline std::enable_if_t<Details::op::CanBeMultiplied<TExpr1, TExpr2>::Value,
1324 VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Mult>>
1325operator*(TExpr1
const & lhs, TExpr2
const & rhs)
1327 return VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Mult>(lhs, rhs);
1337template <
typename TExpr1,
typename TExpr2>
1338inline std::enable_if_t<Details::op::CanBeDivided<TExpr1, TExpr2>::Value,
1339 VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Div>>
1340operator/(TExpr1
const & lhs, TExpr2
const & rhs)
1342 return VariableLengthVectorExpression<TExpr1, TExpr2, Details::op::Div>(lhs, rhs);
1348template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1350operator<<(std::ostream & os, VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const & v)
1356 for (
unsigned int i = 1, N = v.Size(); i != N; ++i)
1370template <
typename TExpr>
1371inline std::enable_if_t<mpl::IsArray<TExpr>::Value,
typename TExpr::RealValueType>
1372GetNorm(TExpr
const & v)
1374 return static_cast<typename TExpr::RealValueType
>(std::sqrt(
static_cast<double>(GetSquaredNorm(v))));
1382template <
typename TExpr>
1383inline std::enable_if_t<mpl::IsArray<TExpr>::Value,
typename TExpr::RealValueType>
1384GetSquaredNorm(TExpr
const & v)
1386 using RealValueType =
typename TExpr::RealValueType;
1387 RealValueType sum = 0.0;
1388 for (
unsigned int i = 0, N = v.Size(); i < N; ++i)
1390 const RealValueType value = v[i];
1391 sum += value * value;
1403template <
typename TValue>
1405operator<<(std::ostream & os,
const VariableLengthVector<TValue> & arr)
1407 const unsigned int length = arr.Size();
1408 const int last =
static_cast<unsigned int>(length) - 1;
1412 for (
int i = 0; i < last; ++i)
1414 os << arr[i] <<
", ";
1445template <
typename T>
1447swap(VariableLengthVector<T> & l_, VariableLengthVector<T> & r_)
noexcept
1458#ifndef ITK_MANUAL_INSTANTIATION
1459# include "itkVariableLengthVector.hxx"
Pixel-wise addition of two images.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)