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
161 itkAssertInDebugAndIgnoreInReleaseMacro(newSize == oldSize &&
162 "SetSize is expected to never change the VariableLengthVector size...");
190 return newSize != oldSize;
226 return newSize > oldSize;
275 template <
typename TValue2>
277 operator()(
unsigned int newSize,
unsigned int oldSize, TValue2 * oldBuffer, TValue2 * newBuffer)
const
279 itkAssertInDebugAndIgnoreInReleaseMacro(newBuffer);
280 const size_t nb = std::min(newSize, oldSize);
281 itkAssertInDebugAndIgnoreInReleaseMacro(nb == 0 || (nb > 0 && oldBuffer !=
nullptr));
282 std::copy_n(oldBuffer, nb, newBuffer);
305 template <
typename TValue2>
308 unsigned int itkNotUsed(oldSize),
309 TValue2 * itkNotUsed(oldBuffer),
310 TValue2 * itkNotUsed(newBuffer))
const
399 template <
typename T>
402 m_NumElements = v.
Size();
403 m_LetArrayManageMemory =
true;
404 if (m_NumElements != 0)
406 m_Data = this->AllocateElements(m_NumElements);
407 itkAssertInDebugAndIgnoreInReleaseMacro(m_Data !=
nullptr);
410 this->m_Data[i] = static_cast<ValueType>(v[i]);
441 itkAssertInDebugAndIgnoreInReleaseMacro(m_LetArrayManageMemory == v.m_LetArrayManageMemory);
443 swap(v.m_Data, m_Data);
444 swap(v.m_NumElements, m_NumElements);
466 operator=(
Self && v) noexcept;
484 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
506 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
514 Fill(TValue
const & v);
528 template <
typename T>
543 this->m_Data[i] = static_cast<ValueType>(v[i]);
565 operator=(
const Self & v);
576 FastAssign(
const Self & v);
586 operator=(TValue
const & v);
592 return m_NumElements;
597 return m_NumElements;
602 return m_NumElements;
607 TValue &
operator[](
unsigned int i) {
return this->m_Data[i]; }
610 TValue
const &
operator[](
unsigned int i)
const {
return this->m_Data[i]; }
659 template <
typename TReallocatePolicy,
typename TKeepValuesPolicy>
661 SetSize(
unsigned int sz, TReallocatePolicy reallocatePolicy, TKeepValuesPolicy keepValues);
674 SetSize(
unsigned int sz,
bool destroyExistingData =
true)
679 if (destroyExistingData)
693 DestroyExistingData();
708 SetData(TValue * datain,
bool LetArrayManageMemory =
false);
725 SetData(TValue * datain,
unsigned int sz,
bool LetArrayManageMemory =
false);
754 Reserve(ElementIdentifier size);
762 AllocateElements(ElementIdentifier size)
const;
777 this->m_Data[i] -= static_cast<ValueType>(1.0);
789 this->m_Data[i] += static_cast<ValueType>(1.0);
826 template <
typename T>
830 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.
GetSize());
833 m_Data[i] -= static_cast<ValueType>(v[i]);
859 template <
typename T>
863 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.
GetSize());
866 m_Data[i] += static_cast<ValueType>(v[i]);
893 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
897 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
900 m_Data[i] += static_cast<ValueType>(rhs[i]);
915 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
919 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
922 m_Data[i] -= static_cast<ValueType>(rhs[i]);
933 template <
typename T>
937 const ValueType & sc = static_cast<ValueType>(s);
966 template <
typename T>
973 m_Data[i] = static_cast<ValueType>(static_cast<RealValueType>(m_Data[i]) / sc);
989 ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(
Self);
997 GetSquaredNorm()
const;
1003 return !m_LetArrayManageMemory;
1007 bool m_LetArrayManageMemory{
true };
1025 template <
typename T>
1026 struct IsArray : FalseType
1030 template <
typename T>
1031 struct IsArray<
itk::VariableLengthVector<T>> : TrueType
1034 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1056 template <
typename TExpr>
1065 Load(Type
const & v,
unsigned int idx)
1083 template <
typename TExpr1,
typename TExpr2>
1084 inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>::Value,
unsigned int>
1085 GetSize(TExpr1
const & lhs, TExpr2
const & rhs)
1088 itkAssertInDebugAndIgnoreInReleaseMacro(lhs.Size() == rhs.Size());
1103 template <
typename TExpr1,
typename TExpr2>
1104 inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr1>, mpl::Not<mpl::IsArray<TExpr2>>>::Value,
unsigned int>
1105 GetSize(TExpr1
const & lhs, TExpr2
const & itkNotUsed(rhs))
1119 template <
typename TExpr1,
typename TExpr2>
1120 inline std::enable_if_t<mpl::And<mpl::IsArray<TExpr2>, mpl::Not<mpl::IsArray<TExpr1>>>::Value,
unsigned int>
1121 GetSize(TExpr1
const & itkNotUsed(lhs), TExpr2
const & rhs)
1126 template <
typename T>
1127 struct GetType<VariableLengthVector<T>>
1131 Load(VariableLengthVector<T>
const & v,
unsigned int idx)
1136 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1162 template <
typename TExpr1,
typename TExpr2>
1164 : mpl::Or<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>,
1165 mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>,
1166 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2>>>
1180 template <
typename TExpr1,
typename TExpr2>
1182 : mpl::Or<mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>,
1183 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2>>>
1197 template <
typename TExpr1,
typename TExpr2>
1198 struct CanBeDivided : mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2>>
1229 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1238 static_assert(std::is_base_of<Details::op::BinaryOperationConcept, TBinaryOp>::value,
1239 "The Binary Operation shall inherit from BinaryOperationConcept");
1246 return Details::GetSize(m_lhs, m_rhs);
1251 typename mpl::PromoteType<typename Details::GetType<TExpr1>::Type,
typename Details::GetType<TExpr2>::Type>::Type;
1267 itkAssertInDebugAndIgnoreInReleaseMacro(idx < Size());
1268 return TBinaryOp::Apply(Details::GetType<TExpr1>::Load(m_lhs, idx), Details::GetType<TExpr2>::Load(m_rhs, idx));
1294 template <
typename TExpr1,
typename TExpr2>
1295 inline std::enable_if_t<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>::Value,
1311 template <
typename TExpr1,
typename TExpr2>
1312 inline std::enable_if_t<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>::Value,
1327 template <
typename TExpr1,
typename TExpr2>
1328 inline std::enable_if_t<Details::op::CanBeMultiplied<TExpr1, TExpr2>::Value,
1342 template <
typename TExpr1,
typename TExpr2>
1343 inline std::enable_if_t<Details::op::CanBeDivided<TExpr1, TExpr2>::Value,
1353 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1361 for (
unsigned int i = 1, N = v.Size(); i != N; ++i)
1375 template <
typename TExpr>
1376 inline std::enable_if_t<mpl::IsArray<TExpr>::Value,
typename TExpr::RealValueType>
1379 return static_cast<typename TExpr::RealValueType>(std::sqrt(static_cast<double>(GetSquaredNorm(v))));
1387 template <
typename TExpr>
1388 inline std::enable_if_t<mpl::IsArray<TExpr>::Value,
typename TExpr::RealValueType>
1391 using RealValueType =
typename TExpr::RealValueType;
1392 RealValueType sum = 0.0;
1393 for (
unsigned int i = 0, N = v.Size(); i < N; ++i)
1395 const RealValueType value = v[i];
1396 sum += value * value;
1408 template <
typename TValue>
1412 const unsigned int length = arr.
Size();
1413 const int last = static_cast<unsigned int>(length) - 1;
1417 for (
int i = 0; i < last; ++i)
1419 os << arr[i] <<
", ";
1450 template <
typename T>
1463 #ifndef ITK_MANUAL_INSTANTIATION
1464 # include "itkVariableLengthVector.hxx"