ITK  5.0.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  using ValueType = T;
68 
69  using Self = std::vector< T >;
70 
72  using AbsType = std::vector< ElementAbsType >;
73 
75  using AccumulateType = std::vector< ElementAccumulateType >;
76 
79  using FloatType = std::vector< ElementFloatType >;
80 
81  // TODO: this won't really print well, at least not without defining an operator
82  // to push to a stream.
84  using PrintType = std::vector< ElementPrintType >;
85 
87  using RealType = std::vector< ElementRealType >;
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 constexpr bool IsSigned = NumericTraits< ValueType >::IsSigned;
132  static constexpr bool IsInteger = NumericTraits< ValueType >::IsInteger;
133  static constexpr 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)
std::vector< ElementFloatType > FloatType
std::vector< ElementAccumulateType > AccumulateType
Define numeric traits for std::vector.
static void SetLength(std::vector< T > &m, const unsigned int s)
typename NumericTraits< T >::FloatType ElementFloatType
std::vector< ElementPrintType > PrintType
typename NumericTraits< T >::AbsType ElementAbsType
static unsigned int GetLength(const std::vector< T > &m)
typename NumericTraits< T >::PrintType ElementPrintType
static void AssignToArray(const Self &v, MeasurementVectorType &mv)
static const Self ZeroValue(const Self &a)
typename NumericTraits< T >::RealType ElementRealType
static const Self NonpositiveMin(const Self &a)
static void AssignToArray(const Self &v, TArray &mv)
typename NumericTraits< T >::AccumulateType ElementAccumulateType