ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkNumericTraitsStdVector.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkNumericTraitsStdVector_h
20 #define itkNumericTraitsStdVector_h
21 
22 #include "itkMath.h"
23 #include <vector>
24 
25 // This work is part of the National Alliance for Medical Image Computing
26 // (NAMIC), funded by the National Institutes of Health through the NIH Roadmap
27 // for Medical Research, Grant U54 EB005149.
28 
29 namespace itk
30 {
55 template< typename T >
56 class NumericTraits< std::vector< T > >
57 {
58 public:
59 
65 
67  typedef T ValueType;
68 
69  typedef std::vector< T > Self;
70 
72  typedef std::vector< ElementAbsType > AbsType;
73 
75  typedef std::vector< ElementAccumulateType > AccumulateType;
76 
79  typedef std::vector< ElementFloatType > FloatType;
80 
81  // TODO: this won't really print well, at least not without defining an operator
82  // to push to a stream.
84  typedef std::vector< ElementPrintType > PrintType;
85 
87  typedef std::vector< ElementRealType > RealType;
88 
91 
94 
100  static const Self max(const Self & a)
101  {
102  Self b( a.Size(), NumericTraits< T >::max() );
103  return b;
104  }
106 
107  static const Self min(const Self & a)
108  {
109  Self b( a.Size(), NumericTraits< T >::min() );
110  return b;
111  }
112 
113  static const Self ZeroValue(const Self & a)
114  {
115  Self b( a.Size(), NumericTraits< T >::ZeroValue() );
116  return b;
117  }
118 
119  static const Self OneValue(const Self & a)
120  {
121  Self b( a.Size(), NumericTraits< T >::OneValue() );
122  return b;
123  }
124 
125  static const Self NonpositiveMin(const Self & a)
126  {
127  Self b( a.Size(), NumericTraits< T >::NonpositiveMin() );
128  return b;
129  }
130 
131  static ITK_CONSTEXPR_VAR bool IsSigned = NumericTraits< ValueType >::IsSigned;
132  static ITK_CONSTEXPR_VAR bool IsInteger = NumericTraits< ValueType >::IsInteger;
133  static ITK_CONSTEXPR_VAR bool IsComplex = NumericTraits< ValueType >::IsComplex;
134 
136  static void SetLength(std::vector< T > & m, const unsigned int s)
137  {
138  // since std::vector often holds types that have no NumericTraits::ZeroValue(),
139  // allow resize() to call the type's default constructor
140  m.clear();
141  m.resize(s);
142  }
144 
146  static unsigned int GetLength(const std::vector< T > & m)
147  {
148  return itk::Math::CastWithRangeCheck<unsigned int>( m.size() );
149  }
150 
151  static void AssignToArray( const Self & v, MeasurementVectorType & mv )
152  {
153  mv = v;
154  }
155 
156  template<typename TArray>
157  static void AssignToArray( const Self & v, TArray & mv )
158  {
159  for( unsigned int i=0; i<GetLength(v); i++ )
160  {
161  mv[i] = v[i];
162  }
163  }
164 
165 };
166 } // end namespace itk
167 
168 #endif // itkNumericTraitsStdVector_h
static const Self OneValue(const Self &a)
NumericTraits< T >::FloatType ElementFloatType
std::vector< ElementPrintType > PrintType
static ITK_CONSTEXPR_VAR bool IsComplex
static void SetLength(std::vector< T > &m, const unsigned int s)
NumericTraits< T >::AccumulateType ElementAccumulateType
std::vector< ElementAccumulateType > AccumulateType
static ITK_CONSTEXPR_VAR bool IsInteger
static ITK_CONSTEXPR_FUNC T max(const T &)
std::vector< ElementFloatType > FloatType
static unsigned int GetLength(const std::vector< T > &m)
static unsigned int GetLength()
static void AssignToArray(const Self &v, MeasurementVectorType &mv)
static const Self ZeroValue(const Self &a)
static ITK_CONSTEXPR_FUNC T NonpositiveMin()
static ITK_CONSTEXPR_FUNC T min(const T &)
static ITK_CONSTEXPR_VAR bool IsSigned
static const Self NonpositiveMin(const Self &a)
NumericTraits< T >::PrintType ElementPrintType
Define additional traits for native types such as int or float.
static void AssignToArray(const Self &v, TArray &mv)