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 != ITK_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 != ITK_NULLPTR);
403 this->m_Data[i] =
static_cast< ValueType >( v[i] );
408 m_Data = ITK_NULLPTR;
433 itkAssertInDebugAndIgnoreInReleaseMacro(m_LetArrayManageMemory == v.m_LetArrayManageMemory);
435 swap(v.m_Data , m_Data);
436 swap(v.m_NumElements, m_NumElements);
440 #if defined(ITK_HAS_CXX11_RVREF)
458 Self & operator=(Self && v) ITK_NOEXCEPT;
477 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
499 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
505 void Fill(TValue
const & v);
519 template<
typename T >
533 this->m_Data[i] =
static_cast< ValueType >( v[i] );
554 Self & operator=(
const Self & v);
564 Self & FastAssign(
const Self & v);
573 Self & operator=(TValue
const & v);
576 unsigned int Size(
void)
const {
return m_NumElements; }
577 unsigned int GetSize(
void)
const {
return m_NumElements; }
582 TValue &
operator[](
unsigned int i) {
return this->m_Data[i]; }
585 TValue
const &
operator[](
unsigned int i)
const {
return this->m_Data[i]; }
588 const TValue &
GetElement(
unsigned int i)
const {
return m_Data[i]; }
591 void SetElement(
unsigned int i,
const TValue & value) { m_Data[i] = value; }
626 template <
typename TReallocatePolicy,
typename TKeepValuesPolicy>
627 void SetSize(
unsigned int sz,
628 TReallocatePolicy reallocatePolicy,
629 TKeepValuesPolicy keepValues);
641 void SetSize(
unsigned int sz,
bool destroyExistingData =
true)
646 if (destroyExistingData)
659 void DestroyExistingData();
673 void SetData(TValue *data,
bool LetArrayManageMemory =
false);
689 void SetData(TValue *data,
unsigned int sz,
bool LetArrayManageMemory =
false);
717 void Reserve(ElementIdentifier size);
724 TValue * AllocateElements(ElementIdentifier size)
const;
734 this->m_Data[i] -=
static_cast< ValueType >( 1.0 );
745 this->m_Data[i] +=
static_cast< ValueType >( 1.0 );
780 template<
typename T >
784 itkAssertInDebugAndIgnoreInReleaseMacro( m_NumElements == v.GetSize() );
787 m_Data[i] -=
static_cast< ValueType >( v[i] );
812 template<
typename T >
816 itkAssertInDebugAndIgnoreInReleaseMacro( m_NumElements == v.GetSize() );
819 m_Data[i] +=
static_cast< ValueType >( v[i] );
845 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
848 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
851 m_Data[i] +=
static_cast<ValueType>(rhs[i]);
866 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
869 itkAssertInDebugAndIgnoreInReleaseMacro(rhs.Size() ==
Size());
872 m_Data[i] -=
static_cast<ValueType>(rhs[i]);
883 template<
typename T >
914 template<
typename T >
939 RealValueType GetNorm()
const;
942 RealValueType GetSquaredNorm()
const;
945 bool IsAProxy()
const {
return ! m_LetArrayManageMemory;}
965 template <
typename T>
966 struct IsArray : FalseType {};
969 template <
typename T>
970 struct IsArray<itk::VariableLengthVector<T> > : TrueType {};
972 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
993 template <
typename TExpr>
struct GetType
1000 static Type Load(Type
const& v,
unsigned int idx)
1001 { (void)idx;
return v; }
1015 template <
typename TExpr1,
typename TExpr2>
1017 typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2> >,
unsigned int>::Type
1018 GetSize(TExpr1
const& lhs, TExpr2
const& rhs)
1021 itkAssertInDebugAndIgnoreInReleaseMacro(lhs.Size() == rhs.Size());
1036 template <
typename TExpr1,
typename TExpr2>
1038 typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr1>, mpl::Not<mpl::IsArray<TExpr2> > >,
unsigned int>::Type
1039 GetSize(TExpr1
const& lhs, TExpr2
const& itkNotUsed(rhs))
1053 template <
typename TExpr1,
typename TExpr2>
1055 typename mpl::EnableIf<mpl::And<mpl::IsArray<TExpr2>, mpl::Not<mpl::IsArray<TExpr1> > >,
unsigned int>::Type
1056 GetSize(TExpr1
const& itkNotUsed(lhs), TExpr2
const& rhs)
1061 template <
typename T>
1062 struct GetType<VariableLengthVector<T> >
1065 static Type Load(VariableLengthVector<T>
const& v,
unsigned int idx)
1068 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1069 struct GetType<VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp> >
1091 template <
typename TExpr1,
typename TExpr2>
1093 : mpl::Or< mpl::And<mpl::IsArray<TExpr1>, mpl::IsArray<TExpr2> >,
1094 mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2> >,
1095 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2> >
1110 template <
typename TExpr1,
typename TExpr2>
1112 : mpl::Or< mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2> >,
1113 mpl::And<mpl::IsNumber<TExpr1>, mpl::IsArray<TExpr2> >
1128 template <
typename TExpr1,
typename TExpr2>
1130 : mpl::And<mpl::IsArray<TExpr1>, mpl::IsNumber<TExpr2> >
1161 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1165 : m_lhs(lhs), m_rhs(rhs)
1170 (itk::mpl::IsBaseOf<Details::op::BinaryOperationConcept, TBinaryOp>::Value),
1171 "The Binary Operation shall inherit from BinaryOperationConcept");
1175 unsigned int Size()
const{
return Details::GetSize(m_lhs, m_rhs); }
1178 typedef typename mpl::PromoteType<
1179 typename Details::GetType<TExpr1>::Type,
1180 typename Details::GetType<TExpr2>::Type>::Type
ResType;
1196 itkAssertInDebugAndIgnoreInReleaseMacro(idx < Size());
1197 return TBinaryOp::Apply(
1198 Details::GetType<TExpr1>::Load(m_lhs, idx),
1199 Details::GetType<TExpr2>::Load(m_rhs, idx));
1204 RealValueType
GetNorm()
const;
1223 template <
typename TExpr1,
typename TExpr2>
1238 template <
typename TExpr1,
typename TExpr2>
1252 template <
typename TExpr1,
typename TExpr2>
1265 template <
typename TExpr1,
typename TExpr2>
1274 template <
typename TExpr1,
typename TExpr2,
typename TBinaryOp>
1275 std::ostream & operator<<(std::ostream &os, VariableLengthVectorExpression<TExpr1, TExpr2, TBinaryOp>
const& v)
1281 for (
unsigned int i = 1, N = v.Size(); i != N; ++i)
1295 template <
typename TExpr>
1297 typename mpl::EnableIf<mpl::IsArray<TExpr>,
typename TExpr::RealValueType>::Type
1300 return static_cast<typename TExpr::RealValueType
>(
1301 std::sqrt(static_cast<double>(GetSquaredNorm(v))));
1309 template <
typename TExpr>
1311 typename mpl::EnableIf<mpl::IsArray<TExpr>,
typename TExpr::RealValueType>::Type
1314 typedef typename TExpr::RealValueType RealValueType;
1315 RealValueType sum = 0.0;
1316 for (
unsigned int i = 0, N=v.Size(); i < N; ++i )
1318 const RealValueType value = v[i];
1319 sum += value * value;
1331 template<
typename TValue >
1332 std::ostream & operator<<(std::ostream & os, const VariableLengthVector< TValue > & arr)
1334 const unsigned int length = arr.Size();
1335 const signed int last = (
unsigned int)length - 1;
1339 for (
signed int i = 0; i < last; ++i )
1341 os << arr[i] <<
", ";
1372 template <
typename T>
1385 #ifndef ITK_MANUAL_INSTANTIATION
1386 #include "itkVariableLengthVector.hxx"
bool operator!=(const ImageRegionCopier< D1, D2 > &c1, const ImageRegionCopier< D1, D2 > &c2)
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.
TValue & operator[](unsigned int i)
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
unsigned int Size(void) const
Represent the size (bounds) of a n-dimensional image.
VariableLengthVector(const VariableLengthVector< T > &v)
unsigned int GetNumberOfElements(void) const
bool operator()(unsigned int newSize, unsigned int oldSize) const
mpl::EnableIf< Details::op::CanBeAddedOrSubstracted< TExpr1, TExpr2 >, VariableLengthVectorExpression< TExpr1, TExpr2, Details::op::Plus > >::Type operator+(TExpr1 const &lhs, TExpr2 const &rhs)
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(void) 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.
VariableLengthVector Self
Self & operator*=(TValue s)
ElementIdentifier m_NumElements
void Swap(Self &v) noexcept
Self & operator-=(TValue s)
mpl::EnableIf< Details::op::CanBeAddedOrSubstracted< TExpr1, TExpr2 >, VariableLengthVectorExpression< TExpr1, TExpr2, Details::op::Sub > >::Type operator-(TExpr1 const &lhs, TExpr2 const &rhs)
Self & operator=(const VariableLengthVector< T > &v)
NumericTraits< ResType >::RealType RealValueType
Real type of the elements.
bool m_LetArrayManageMemory
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)
void operator()(unsigned int, unsigned int, TValue2 *, TValue2 *) const
void SetSize(unsigned int sz, bool destroyExistingData=true)
Self & operator+=(TValue s)
NumericTraits< ValueType >::RealType RealValueType
#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)
mpl::PromoteType< typename Details::GetType< TExpr1 >::Type, typename Details::GetType< TExpr2 >::Type >::Type ResType
Vector type of the Result Expression.
bool ITKIOXML_EXPORT operator==(itk::FancyString &s, const std::string &)
unsigned int ElementIdentifier
VariableLengthVectorExpression(TExpr1 const &lhs, TExpr2 const &rhs)
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