18 #ifndef itkVariableLengthVector_h
19 #define itkVariableLengthVector_h
35 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
92 template<
typename TValue >
129 bool operator()(
unsigned int itkNotUsed(newSize),
unsigned int itkNotUsed(oldSize))
const
156 bool operator()(
unsigned int newSize,
unsigned int oldSize)
const
160 itkAssertInDebugAndIgnoreInReleaseMacro(newSize == oldSize &&
"SetSize is expected to never change the VariableLengthVector size...");
185 bool operator()(
unsigned int newSize,
unsigned int oldSize)
const
186 {
return newSize != oldSize; }
218 bool operator()(
unsigned int newSize,
unsigned int oldSize)
const
219 {
return newSize > oldSize; }
266 template <
typename TValue2>
268 unsigned int newSize,
unsigned int oldSize,
269 TValue2 * oldBuffer, TValue2 * newBuffer)
const
271 itkAssertInDebugAndIgnoreInReleaseMacro(newBuffer);
272 const std::size_t nb = std::min(newSize, oldSize);
273 itkAssertInDebugAndIgnoreInReleaseMacro(nb == 0 || (nb > 0 && oldBuffer !=
nullptr));
274 std::copy(oldBuffer, oldBuffer+nb, newBuffer);
297 template <
typename TValue2>
299 unsigned int itkNotUsed(newSize),
unsigned int itkNotUsed(oldSize),
300 TValue2 * itkNotUsed(oldBuffer), TValue2 * itkNotUsed(newBuffer))
const
349 bool LetArrayManageMemory =
false);
371 bool LetArrayManageMemory =
false);
392 template<
typename T >
395 m_NumElements = v.
Size();
396 m_LetArrayManageMemory =
true;
397 if (m_NumElements != 0)
399 m_Data = this->AllocateElements(m_NumElements);
400 itkAssertInDebugAndIgnoreInReleaseMacro(m_Data !=
nullptr);
403 this->m_Data[i] =
static_cast< ValueType >( v[i] );
433 itkAssertInDebugAndIgnoreInReleaseMacro(m_LetArrayManageMemory == v.m_LetArrayManageMemory);
435 swap(v.m_Data , m_Data);
436 swap(v.m_NumElements, m_NumElements);
457 Self & operator=(Self && v) noexcept;
475 template <typename TExpr1, typename TExpr2, typename TBinaryOp>
497 template <typename TExpr1, typename TExpr2, typename TBinaryOp>
503 void Fill(TValue const & v);
517 template< typename T >
531 this->m_Data[i] =
static_cast< ValueType >( v[i] );
552 Self & operator=(
const Self & v);
562 Self & FastAssign(
const Self & v);
571 Self & operator=(TValue
const & v);
574 unsigned int Size()
const {
return m_NumElements; }
575 unsigned int GetSize()
const {
return m_NumElements; }
580 TValue &
operator[](
unsigned int i) {
return this->m_Data[i]; }
583 TValue
const &
operator[](
unsigned int i)
const {
return this->m_Data[i]; }
586 const TValue &
GetElement(
unsigned int i)
const {
return m_Data[i]; }
589 void SetElement(
unsigned int i,
const TValue & value) { m_Data[i] = value; }
624 template <
typename TReallocatePolicy,
typename TKeepValuesPolicy>
625 void SetSize(
unsigned int sz,
626 TReallocatePolicy reallocatePolicy,
627 TKeepValuesPolicy keepValues);
639 void SetSize(
unsigned int sz,
bool destroyExistingData =
true)
644 if (destroyExistingData)
657 void DestroyExistingData();
671 void SetData(TValue *data,
bool LetArrayManageMemory =
false);
687 void SetData(TValue *data,
unsigned int sz,
bool LetArrayManageMemory =
false);
715 void Reserve(ElementIdentifier size);
722 TValue * AllocateElements(ElementIdentifier size)
const;
732 this->m_Data[i] -=
static_cast< ValueType >( 1.0 );
743 this->m_Data[i] +=
static_cast< ValueType >( 1.0 );
778 template<
typename T >
782 itkAssertInDebugAndIgnoreInReleaseMacro( m_NumElements == v.GetSize() );
785 m_Data[i] -=
static_cast< ValueType >( v[i] );
810 template<
typename T >
814 itkAssertInDebugAndIgnoreInReleaseMacro( m_NumElements == v.GetSize() );
817 m_Data[i] +=
static_cast< ValueType >( v[i] );
843 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
846 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
849 m_Data[i] +=
static_cast<ValueType>(rhs[i]);
864 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
867 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
870 m_Data[i] -=
static_cast<ValueType>(rhs[i]);
881 template<
typename T >
912 template<
typename T >
937 RealValueType GetNorm()
const;
940 RealValueType GetSquaredNorm()
const;
943 bool IsAProxy()
const {
return ! m_LetArrayManageMemory;}
946 bool m_LetArrayManageMemory{
true};
963 template <
typename T>
964 struct IsArray : FalseType {};
967 template <
typename T>
968 struct IsArray<itk::VariableLengthVector<T> > : TrueType {};
970 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
991 template <
typename TExpr>
struct GetType
998 static Type Load(Type
const& v,
unsigned int idx)
999 { (void)idx;
return v; }
1013 template <
typename TExpr1,
typename TExpr2>
1015 typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2> >,
unsigned int>::Type
1016 GetSize(TExpr1
const& lhs, TExpr2
const& rhs)
1019 itkAssertInDebugAndIgnoreInReleaseMacro(lhs.Size() == rhs.Size());
1034 template <
typename TExpr1,
typename TExpr2>
1036 typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr1>, mpl::Not<mpl::IsArray<TExpr2> > >,
unsigned int>::Type
1037 GetSize(TExpr1
const& lhs, TExpr2
const& itkNotUsed(rhs))
1051 template <
typename TExpr1,
typename TExpr2>
1053 typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr2>, mpl::Not<mpl::IsArray<TExpr1> > >,
unsigned int>::Type
1054 GetSize(TExpr1
const& itkNotUsed(lhs), TExpr2
const& rhs)
1059 template <
typename T>
1060 struct GetType<VariableLengthVector<T> >
1063 static Type Load(VariableLengthVector<T>
const& v,
unsigned int idx)
1066 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1067 struct GetType<VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp> >
1089 template <
typename TExpr1,
typename TExpr2>
1091 : mpl::Or< mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2> >,
1092 mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2> >,
1093 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2> >
1108 template <
typename TExpr1,
typename TExpr2>
1110 : mpl::Or< mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2> >,
1111 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2> >
1126 template <
typename TExpr1,
typename TExpr2>
1128 : mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2> >
1159 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1163 : m_lhs(lhs), m_rhs(rhs)
1168 (itk::mpl::IsBaseOf<Details::op::BinaryOperationConcept, TBinaryOp>::Value),
1169 "The Binary Operation shall inherit from BinaryOperationConcept");
1173 unsigned int Size()
const{
return Details::GetSize(m_lhs, m_rhs); }
1176 using ResType =
typename mpl::PromoteType<
1177 typename Details::GetType<TExpr1>::Type,
1178 typename Details::GetType<TExpr2>::Type>::Type;
1194 itkAssertInDebugAndIgnoreInReleaseMacro(idx < Size());
1195 return TBinaryOp::Apply(
1196 Details::GetType<TExpr1>::Load(m_lhs, idx),
1197 Details::GetType<TExpr2>::Load(m_rhs, idx));
1202 RealValueType
GetNorm()
const;
1221 template <
typename TExpr1,
typename TExpr2>
1236 template <
typename TExpr1,
typename TExpr2>
1250 template <
typename TExpr1,
typename TExpr2>
1263 template <
typename TExpr1,
typename TExpr2>
1272 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1273 std::ostream & operator<<(std::ostream &os, VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const& v)
1279 for (
unsigned int i = 1, N = v.Size(); i != N; ++i)
1293 template <
typename TExpr>
1295 typename mpl::EnableIf<mpl::IsArray<TExpr>,
typename TExpr::RealValueType>::Type
1298 return static_cast<typename TExpr::RealValueType
>(
1299 std::sqrt(static_cast<double>(GetSquaredNorm(v))));
1307 template <
typename TExpr>
1309 typename mpl::EnableIf<mpl::IsArray<TExpr>,
typename TExpr::RealValueType>::Type
1312 using RealValueType =
typename TExpr::RealValueType;
1313 RealValueType sum = 0.0;
1314 for (
unsigned int i = 0, N=v.Size(); i < N; ++i )
1316 const RealValueType value = v[i];
1317 sum += value * value;
1329 template<
typename TValue >
1330 std::ostream & operator<<(std::ostream & os, const VariableLengthVector< TValue > & arr)
1332 const unsigned int length = arr.Size();
1333 const signed int last = (
unsigned int)length - 1;
1337 for (
signed int i = 0; i < last; ++i )
1339 os << arr[i] <<
", ";
1370 template <
typename T>
1383 #ifndef ITK_MANUAL_INSTANTIATION
1384 #include "itkVariableLengthVector.hxx"
void operator()(unsigned int newSize, unsigned int oldSize, TValue2 *oldBuffer, TValue2 *newBuffer) const
void swap(VariableLengthVector< T > &l_, VariableLengthVector< T > &r_) noexcept
unsigned int Size() const
Returns the size of the vector expression.
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
TValue & operator[](unsigned int i)
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Define numeric traits for std::vector.
VariableLengthVector(const VariableLengthVector< T > &v)
bool operator()(unsigned int newSize, unsigned int oldSize) const
void SetElement(unsigned int i, const TValue &value)
bool operator()(unsigned int, unsigned int) const
mpl::EnableIf< mpl::IsArray< TExpr >, typename TExpr::RealValueType >::Type GetNorm(TExpr const &v)
Self & operator+=(VariableLengthVectorExpression< TExpr1, TExpr2, TBinaryOp > const &rhs)
const TValue & GetElement(unsigned int i) const
const TValue * GetDataPointer() const
unsigned int GetSize() const
void swap(Array< T > &a, Array< T > &b)
bool operator()(unsigned int newSize, unsigned int oldSize) const
Represents an array whose length can be defined at run-time.
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
mpl::EnableIf< Details::op::CanBeAddedOrSubtracted< TExpr1, TExpr2 >, VariableLengthVectorExpression< TExpr1, TExpr2, Details::op::Sub > >::Type operator-(TExpr1 const &lhs, TExpr2 const &rhs)
Represent a n-dimensional size (bounds) of a n-dimensional image.
Self & operator*=(TValue s)
unsigned int Size() const
void Swap(Self &v) noexcept
Self & operator-=(TValue s)
mpl::EnableIf< Details::op::CanBeAddedOrSubtracted< TExpr1, TExpr2 >, VariableLengthVectorExpression< TExpr1, TExpr2, Details::op::Plus > >::Type operator+(TExpr1 const &lhs, TExpr2 const &rhs)
typename NumericTraits< ResType >::RealType RealValueType
Real type of the elements.
mpl::EnableIf< Details::op::CanBeDivided< TExpr1, TExpr2 >, VariableLengthVectorExpression< TExpr1, TExpr2, Details::op::Div > >::Type operator/(TExpr1 const &lhs, TExpr2 const &rhs)
Self & operator-=(VariableLengthVectorExpression< TExpr1, TExpr2, TBinaryOp > const &rhs)
typename NumericTraits< ValueType >::RealType RealValueType
void operator()(unsigned int, unsigned int, TValue2 *, TValue2 *) const
typename mpl::PromoteType< typename Details::GetType< TExpr1 >::Type, typename Details::GetType< TExpr2 >::Type >::Type ResType
Vector type of the Result Expression.
void SetSize(unsigned int sz, bool destroyExistingData=true)
unsigned int GetNumberOfElements() const
Self & operator+=(TValue s)
#define itkStaticAssert(expr, str)
bool operator()(unsigned int newSize, unsigned int oldSize) const
mpl::EnableIf< Details::op::CanBeMultiplied< TExpr1, TExpr2 >, VariableLengthVectorExpression< TExpr1, TExpr2, Details::op::Mult > >::Type operator*(TExpr1 const &lhs, TExpr2 const &rhs)
VariableLengthVectorExpression(TExpr1 const &lhs, TExpr2 const &rhs)
unsigned int ElementIdentifier
mpl::EnableIf< mpl::IsArray< TExpr >, typename TExpr::RealValueType >::Type GetSquaredNorm(TExpr const &v)
ResType operator[](unsigned int idx) const
TValue const & operator[](unsigned int i) const