vnl_vector_fixed.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_vector_fixed.hxx
2 #ifndef vnl_vector_fixed_hxx_
3 #define vnl_vector_fixed_hxx_
4 //:
5 // \file
6 #include <algorithm>
7 #include <iostream>
8 #include <cstdlib>
9 #include "vnl_vector_fixed.h"
10 #include "vnl_matrix_fixed.h"
11 
12 #include <cassert>
13 #ifdef _MSC_VER
14 # include <vcl_msvc_warnings.h>
15 #endif
16 #include <vnl/vnl_math.h> // for vnl_math::isfinite
17 
18 template<class T, unsigned int n>
19 T &
21  {
22 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
23  assert(i < n); // Check the index is valid.
24 #endif
25  return data_[i];
26  }
27 
28 template<class T, unsigned int n>
29 T const &
31 {
32 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
33  assert(i < n); // Check the index is valid
34 #endif
35  return data_[i];
36 }
37 
38 template<class T, unsigned int n>
39 T&
41 { return data_[i]; }
42 
43 template<class T, unsigned int n>
44 const T&
46 { return data_[i]; }
47 
48 template<class T, unsigned int n>
49 T const*
51 { return data_; }
52 
53 template<class T, unsigned int n>
54 T *
56 { return data_; }
57 
58 template<class T, unsigned int n>
61 {
63  for ( size_type i = 0; i < n; ++i )
64  ret[i] = f( data_[i] );
65  return ret;
66 }
67 
68 template<class T, unsigned int n>
70 vnl_vector_fixed<T,n>::apply( T (*f)(const T&) )
71 {
73  for ( size_type i = 0; i < n; ++i )
74  ret[i] = f( data_[i] );
75  return ret;
76 }
77 
78 template<class T, unsigned int n>
80 vnl_vector_fixed<T,n>::update( const vnl_vector<T>& v, unsigned int start )
81 {
82  size_type stop = start + v.size();
83  assert( stop <= n );
84  for (size_type i = start; i < stop; i++)
85  this->data_[i] = v[i-start];
86  return *this;
87 }
88 
89 template <class T, unsigned int n>
92 {
93  for ( unsigned int i=0; 2*i+1 < n; ++i )
94  std::swap( data_[i], data_[n-1-i] );
95  return *this;
96 }
97 
98 template <class T, unsigned int n>
99 bool
101 {
102  for ( size_type i = 0; i < this->size(); ++i )
103  if ( !vnl_math::isfinite( (*this)[i] ) )
104  return false;
105 
106  return true;
107 }
108 
109 
110 template <class T, unsigned int n>
111 bool
113 {
114  T const zero(0);
115  for ( size_type i = 0; i < this->size(); ++i )
116  if ( !( (*this)[i] == zero) )
117  return false;
118 
119  return true;
120 }
121 
122 
123 template <class T, unsigned int n>
124 bool
126 {
127  for (size_type i = 0; i < this->size(); ++i)
128  s >> (*this)(i);
129 
130  return s.good() || s.eof();
131 }
132 
133 template <class T, unsigned int n>
134 void
136 {
137  if (this->is_finite())
138  return;
139 
140  std::cerr << __FILE__ ": *** NAN FEVER **\n" << *this;
141  std::abort();
142 }
143 
144 template <class T, unsigned int n>
145 void
146 vnl_vector_fixed<T,n>::print(std::ostream& s) const
147 {
148  if (this->size() > 0)
149  s << (*this)[0];
150  for (size_type i=1; i < this->size(); ++i)
151  s << ' ' << (*this)[i];
152 }
153 
154 template <class T, unsigned int n>
155 T
156 vnl_vector_fixed<T, n>::get(unsigned int i) const
157 {
158 #if VNL_CONFIG_CHECK_BOUNDS
159  if (i >= this->size()) // If invalid index specified
160  vnl_error_vector_index("get", i); // Raise exception
161 #endif
162  return this->data_[i];
163 }
164 
165 // we don't need to explicitly instantiate all the operator+ and such
166 // since they appear in the .h file and are inline.
167 
168 #define VNL_VECTOR_FIXED_INSTANTIATE(T,n) \
169 template class VNL_EXPORT vnl_vector_fixed<T,n >
170 
171 #endif // vnl_vector_fixed_hxx_
T & operator()(unsigned int i)
Return reference to the element at specified index.
void swap(double &a, double &b)
bool is_finite() const
Return true if it's finite.
void assert_finite_internal() const
See assert_finite().
Namespace with standard math functions.
bool is_zero() const
Return true iff all the entries are zero.
T & operator[](const size_t i)
Return the i-th element.
T get(unsigned int i) const
Get value at element i.
#define v
Definition: vnl_vector.h:42
vnl_vector_fixed< T, n > apply(T(*f)(T))
Apply f to each element.
bool read_ascii(std::istream &s)
Read from text stream.
T const * data_block() const
Access the contiguous block storing the elements in the vector.
bool isfinite(vnl_bignum const &x)
Definition: vnl_bignum.h:436
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
fixed size matrix
Fixed length stack-stored vector.
vnl_vector_fixed & update(vnl_vector< T > const &, unsigned int start=0)
Replaces elements with index beginning at start, by values of v. O(n).
void vnl_error_vector_index(char const *fcn, int index)
Raise exception for invalid index.
Definition: vnl_error.cxx:21
vnl_vector_fixed & flip()
Reverse the order of the elements.
void print(std::ostream &s) const
Display the vector.