18 #ifndef itkVariableLengthVector_h
19 #define itkVariableLengthVector_h
35 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
92 template <
typename TValue>
131 operator()(
unsigned int itkNotUsed(newSize),
unsigned int itkNotUsed(oldSize))
const
163 itkAssertInDebugAndIgnoreInReleaseMacro(newSize == oldSize &&
164 "SetSize is expected to never change the VariableLengthVector size...");
192 return newSize != oldSize;
228 return newSize > oldSize;
277 template <
typename TValue2>
279 operator()(
unsigned int newSize,
unsigned int oldSize, TValue2 * oldBuffer, TValue2 * newBuffer)
const
281 itkAssertInDebugAndIgnoreInReleaseMacro(newBuffer);
282 const std::size_t nb = std::min(newSize, oldSize);
283 itkAssertInDebugAndIgnoreInReleaseMacro(nb == 0 || (nb > 0 && oldBuffer !=
nullptr));
284 std::copy_n(oldBuffer, nb, newBuffer);
307 template <
typename TValue2>
310 unsigned int itkNotUsed(oldSize),
311 TValue2 * itkNotUsed(oldBuffer),
312 TValue2 * itkNotUsed(newBuffer))
const
401 template <
typename T>
404 m_NumElements = v.
Size();
405 m_LetArrayManageMemory =
true;
406 if (m_NumElements != 0)
408 m_Data = this->AllocateElements(m_NumElements);
409 itkAssertInDebugAndIgnoreInReleaseMacro(m_Data !=
nullptr);
412 this->m_Data[i] = static_cast<ValueType>(v[i]);
443 itkAssertInDebugAndIgnoreInReleaseMacro(m_LetArrayManageMemory == v.m_LetArrayManageMemory);
445 swap(v.m_Data, m_Data);
446 swap(v.m_NumElements, m_NumElements);
468 operator=(Self && v) noexcept;
486 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
508 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
516 Fill(TValue
const & v);
530 template <
typename T>
545 this->m_Data[i] = static_cast<ValueType>(v[i]);
567 operator=(
const Self & v);
578 FastAssign(
const Self & v);
588 operator=(TValue
const & v);
594 return m_NumElements;
599 return m_NumElements;
604 return m_NumElements;
609 TValue &
operator[](
unsigned int i) {
return this->m_Data[i]; }
612 TValue
const &
operator[](
unsigned int i)
const {
return this->m_Data[i]; }
661 template <
typename TReallocatePolicy,
typename TKeepValuesPolicy>
663 SetSize(
unsigned int sz, TReallocatePolicy reallocatePolicy, TKeepValuesPolicy keepValues);
676 SetSize(
unsigned int sz,
bool destroyExistingData =
true)
681 if (destroyExistingData)
695 DestroyExistingData();
710 SetData(TValue * datain,
bool LetArrayManageMemory =
false);
727 SetData(TValue * datain,
unsigned int sz,
bool LetArrayManageMemory =
false);
756 Reserve(ElementIdentifier size);
764 AllocateElements(ElementIdentifier size)
const;
779 this->m_Data[i] -= static_cast<ValueType>(1.0);
791 this->m_Data[i] += static_cast<ValueType>(1.0);
828 template <
typename T>
832 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.
GetSize());
835 m_Data[i] -= static_cast<ValueType>(v[i]);
861 template <
typename T>
865 itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == v.
GetSize());
868 m_Data[i] += static_cast<ValueType>(v[i]);
895 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
899 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
902 m_Data[i] += static_cast<ValueType>(rhs[i]);
917 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
921 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
924 m_Data[i] -= static_cast<ValueType>(rhs[i]);
935 template <
typename T>
939 const ValueType & sc = static_cast<ValueType>(s);
968 template <
typename T>
975 m_Data[i] = static_cast<ValueType>(static_cast<RealValueType>(m_Data[i]) / sc);
1000 GetSquaredNorm()
const;
1006 return !m_LetArrayManageMemory;
1010 bool m_LetArrayManageMemory{
true };
1028 template <
typename T>
1029 struct IsArray : FalseType
1033 template <
typename T>
1034 struct IsArray<
itk::VariableLengthVector<T>> : TrueType
1037 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1059 template <
typename TExpr>
1068 Load(Type
const & v,
unsigned int idx)
1086 template <
typename TExpr1,
typename TExpr2>
1087 inline typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2>>,
unsigned int>::Type
1088 GetSize(TExpr1
const & lhs, TExpr2
const & rhs)
1091 itkAssertInDebugAndIgnoreInReleaseMacro(lhs.Size() == rhs.Size());
1106 template <
typename TExpr1,
typename TExpr2>
1107 inline typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr1>, mpl::Not<mpl::IsArray<TExpr2>>>,
unsigned int>::Type
1108 GetSize(TExpr1
const & lhs, TExpr2
const & itkNotUsed(rhs))
1122 template <
typename TExpr1,
typename TExpr2>
1123 inline typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr2>, mpl::Not<mpl::IsArray<TExpr1>>>,
unsigned int>::Type
1124 GetSize(TExpr1
const & itkNotUsed(lhs), TExpr2
const & rhs)
1129 template <
typename T>
1130 struct GetType<VariableLengthVector<T>>
1134 Load(VariableLengthVector<T>
const & 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 itkStaticAssert((itk::mpl::IsBaseOf<Details::op::BinaryOperationConcept, TBinaryOp>::Value),
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;
1270 itkAssertInDebugAndIgnoreInReleaseMacro(idx < Size());
1271 return TBinaryOp::Apply(Details::GetType<TExpr1>::Load(m_lhs, idx), Details::GetType<TExpr2>::Load(m_rhs, idx));
1297 template <
typename TExpr1,
typename TExpr2>
1298 inline typename mpl::EnableIf<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>,
1314 template <
typename TExpr1,
typename TExpr2>
1315 inline typename mpl::EnableIf<Details::op::CanBeAddedOrSubtracted<TExpr1, TExpr2>,
1330 template <
typename TExpr1,
typename TExpr2>
1331 inline typename mpl::EnableIf<Details::op::CanBeMultiplied<TExpr1, TExpr2>,
1345 template <
typename TExpr1,
typename TExpr2>
1346 inline typename mpl::EnableIf<Details::op::CanBeDivided<TExpr1, TExpr2>,
1356 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1364 for (
unsigned int i = 1, N = v.Size(); i != N; ++i)
1378 template <
typename TExpr>
1379 inline typename mpl::EnableIf<mpl::IsArray<TExpr>,
typename TExpr::RealValueType>::Type
1382 return static_cast<typename TExpr::RealValueType>(std::sqrt(static_cast<double>(GetSquaredNorm(v))));
1390 template <
typename TExpr>
1391 inline typename mpl::EnableIf<mpl::IsArray<TExpr>,
typename TExpr::RealValueType>::Type
1394 using RealValueType =
typename TExpr::RealValueType;
1395 RealValueType sum = 0.0;
1396 for (
unsigned int i = 0, N = v.Size(); i < N; ++i)
1398 const RealValueType value = v[i];
1399 sum += value * value;
1411 template <
typename TValue>
1415 const unsigned int length = arr.
Size();
1416 const signed int last = (
unsigned int)length - 1;
1420 for (
signed int i = 0; i < last; ++i)
1422 os << arr[i] <<
", ";
1453 template <
typename T>
1466 #ifndef ITK_MANUAL_INSTANTIATION
1467 # include "itkVariableLengthVector.hxx"