18 #ifndef itkVariableLengthVector_h
19 #define itkVariableLengthVector_h
23 #include <type_traits>
33 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
90 template <
typename TValue>
129 operator()(
unsigned int itkNotUsed(newSize),
unsigned int itkNotUsed(oldSize))
const
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...");
187 return newSize != oldSize;
223 return newSize > oldSize;
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);
302 template <
typename TValue2>
305 unsigned int itkNotUsed(oldSize),
306 TValue2 * itkNotUsed(oldBuffer),
307 TValue2 * itkNotUsed(newBuffer))
const
396 template <
typename T>
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);
407 this->m_Data[i] = static_cast<ValueType>(v[i]);
438 itkAssertInDebugAndIgnoreInReleaseMacro(m_LetArrayManageMemory == v.m_LetArrayManageMemory);
440 swap(v.m_Data, m_Data);
441 swap(v.m_NumElements, m_NumElements);
463 operator=(
Self && v) noexcept;
481 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
503 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
511 Fill(
const TValue & v);
525 template <
typename T>
540 this->m_Data[i] = static_cast<ValueType>(v[i]);
562 operator=(
const Self & v);
573 FastAssign(
const Self & v);
583 operator=(
const TValue & v);
589 return m_NumElements;
594 return m_NumElements;
599 return m_NumElements;
607 return this->m_Data[i];
614 return this->m_Data[i];
664 template <
typename TReallocatePolicy,
typename TKeepValuesPolicy>
666 SetSize(
unsigned int sz, TReallocatePolicy reallocatePolicy, TKeepValuesPolicy keepValues);
679 SetSize(
unsigned int sz,
bool destroyExistingData =
true)
684 if (destroyExistingData)
698 DestroyExistingData();
713 SetData(TValue * datain,
bool LetArrayManageMemory =
false);
730 SetData(TValue * datain,
unsigned int sz,
bool LetArrayManageMemory =
false);
759 Reserve(ElementIdentifier size);
767 AllocateElements(ElementIdentifier size)
const;
782 this->m_Data[i] -= static_cast<ValueType>(1.0);
794 this->m_Data[i] += static_cast<ValueType>(1.0);
831 template <
typename T>
835 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.
GetSize());
838 m_Data[i] -= static_cast<ValueType>(v[i]);
864 template <
typename T>
868 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.
GetSize());
871 m_Data[i] += static_cast<ValueType>(v[i]);
898 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
902 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
905 m_Data[i] += static_cast<ValueType>(rhs[i]);
920 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
924 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
927 m_Data[i] -= static_cast<ValueType>(rhs[i]);
938 template <
typename T>
942 const ValueType & sc = static_cast<ValueType>(s);
971 template <
typename T>
978 m_Data[i] = static_cast<ValueType>(static_cast<RealValueType>(m_Data[i]) / sc);
994 ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(
Self);
1002 GetSquaredNorm()
const;
1008 return !m_LetArrayManageMemory;
1012 bool m_LetArrayManageMemory{
true };
1030 template <
typename T>
1031 struct IsArray : FalseType
1035 template <
typename T>
1036 struct IsArray<
itk::VariableLengthVector<T>> : TrueType
1039 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1061 template <
typename TExpr>
1070 Load(
const Type & v,
unsigned int itkNotUsed(idx))
1087 template <
typename TExpr1,
typename TExpr2>
1088 inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>::Value,
unsigned int>
1089 GetSize(
const TExpr1 & lhs, [[maybe_unused]]
const TExpr2 & rhs)
1091 itkAssertInDebugAndIgnoreInReleaseMacro(lhs.Size() == rhs.Size());
1106 template <
typename TExpr1,
typename TExpr2>
1107 inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr1>, mpl::Not<mpl::IsArray<TExpr2>>>::Value,
unsigned int>
1108 GetSize(
const TExpr1 & lhs,
const TExpr2 & itkNotUsed(rhs))
1122 template <
typename TExpr1,
typename TExpr2>
1123 inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr2>, mpl::Not<mpl::IsArray<TExpr1>>>::Value,
unsigned int>
1124 GetSize(
const TExpr1 & itkNotUsed(lhs),
const TExpr2 & rhs)
1129 template <
typename T>
1130 struct GetType<VariableLengthVector<T>>
1134 Load(
const VariableLengthVector<T> & v,
unsigned int idx)
1139 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1165 template <
typename TExpr1,
typename TExpr2>
1167 : mpl::Or<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>,
1168 mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>,
1169 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2>>>
1183 template <
typename TExpr1,
typename TExpr2>
1185 : mpl::Or<mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>,
1186 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2>>>
1200 template <
typename TExpr1,
typename TExpr2>
1201 struct CanBeDivided : mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>
1232 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1241 static_assert(std::is_base_of_v<Details::op::BinaryOperationConcept, TBinaryOp>,
1242 "The Binary Operation shall inherit from BinaryOperationConcept");
1249 return Details::GetSize(m_lhs, m_rhs);
1254 typename mpl::PromoteType<typename Details::GetType<TExpr1>::Type,
typename Details::GetType<TExpr2>::Type>::Type;
1271 itkAssertInDebugAndIgnoreInReleaseMacro(idx < Size());
1272 return TBinaryOp::Apply(Details::GetType<TExpr1>::Load(m_lhs, idx), Details::GetType<TExpr2>::Load(m_rhs, idx));
1298 template <
typename TExpr1,
typename TExpr2>
1299 inline std::enable_if_t<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>::Value,
1315 template <
typename TExpr1,
typename TExpr2>
1316 inline std::enable_if_t<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>::Value,
1331 template <
typename TExpr1,
typename TExpr2>
1332 inline std::enable_if_t<Details::op::CanBeMultiplied<TExpr1, TExpr2>::Value,
1346 template <
typename TExpr1,
typename TExpr2>
1347 inline std::enable_if_t<Details::op::CanBeDivided<TExpr1, TExpr2>::Value,
1357 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1365 for (
unsigned int i = 1, N = v.Size(); i != N; ++i)
1379 template <
typename TExpr>
1380 inline std::enable_if_t<mpl::IsArray<TExpr>::Value,
typename TExpr::RealValueType>
1383 return static_cast<typename TExpr::RealValueType>(std::sqrt(static_cast<double>(GetSquaredNorm(v))));
1391 template <
typename TExpr>
1392 inline std::enable_if_t<mpl::IsArray<TExpr>::Value,
typename TExpr::RealValueType>
1395 using RealValueType =
typename TExpr::RealValueType;
1396 RealValueType sum = 0.0;
1397 for (
unsigned int i = 0, N = v.Size(); i < N; ++i)
1399 const RealValueType value = v[i];
1400 sum += value * value;
1412 template <
typename TValue>
1416 const unsigned int length = arr.
Size();
1417 const int last = static_cast<unsigned int>(length) - 1;
1421 for (
int i = 0; i < last; ++i)
1423 os << arr[i] <<
", ";
1454 template <
typename T>
1467 #ifndef ITK_MANUAL_INSTANTIATION
1468 # include "itkVariableLengthVector.hxx"