vnl_vector_fixed_ref.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_vector_fixed_ref.hxx
2 #ifndef vnl_vector_fixed_ref_hxx_
3 #define vnl_vector_fixed_ref_hxx_
4 // Author: Paul P. Smyth, Vicon Motion Systems Ltd.
5 // Created: 02 May 2001
6 //
7 #include <algorithm>
8 #include <iostream>
9 #include <cstdlib>
10 #include "vnl_vector_fixed_ref.h"
11 #include <cassert>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 #include <vnl/vnl_math.h> // for vnl_math::isfinite
16 
17 
18 //------------------------------------------------------------
19 
20 template<class T, unsigned int n>
23 {
25  for ( size_type i = 0; i < n; ++i )
26  ret[i] = f( data_block()[i] );
27  return ret;
28 }
29 
30 template<class T, unsigned int n>
32 vnl_vector_fixed_ref_const<T,n>::apply( T (*f)(const T&) ) const
33 {
35  for ( size_type i = 0; i < n; ++i )
36  ret[i] = f( data_block()[i] );
37  return ret;
38 }
39 
40 
41 template<class T, unsigned int n>
43 vnl_vector_fixed_ref_const<T,n>::extract( unsigned int len, unsigned int start ) const
44 {
45  assert( start < n && start + len <= n );
46  return vnl_vector<T>( data_block() + start, len );
47 }
48 
49 template<class T, unsigned int n>
51 vnl_vector_fixed_ref<T,n>::update( const vnl_vector<T>& v, unsigned int start ) const
52 {
53  size_type stop = start + v.size();
54  assert( stop <= n );
55  for (size_type i = start; i < stop; i++)
56  this->data_block()[i] = v[i-start];
57  return *this;
58 }
59 
60 template <class T, unsigned int n>
63 {
64  for ( unsigned int i=0; 2*i+1 < n; ++i )
65  std::swap( data_block()[i], data_block()[n-1-i] );
66  return *this;
67 }
68 
69 template <class T, unsigned int n>
70 bool
72 {
73  for ( size_type i = 0; i < this->size(); ++i )
74  if ( !vnl_math::isfinite( (*this)[i] ) )
75  return false;
76 
77  return true;
78 }
79 
80 
81 template <class T, unsigned int n>
82 bool
84 {
85  T const zero(0);
86  for ( size_type i = 0; i < this->size(); ++i )
87  if ( !( (*this)[i] == zero) )
88  return false;
89 
90  return true;
91 }
92 
93 
94 template <class T, unsigned int n>
95 bool
97 {
98  for (unsigned i = 0; i < this->size(); ++i)
99  s >> (*this)(i);
100 
101  return s.good() || s.eof();
102 }
103 
104 template <class T, unsigned int n>
105 void
107 {
108  if (this->is_finite())
109  return;
110 
111  std::cerr << __FILE__ ": *** NAN FEVER **\n" << *this;
112  std::abort();
113 }
114 
115 template <class T, unsigned int n>
116 void
118 {
119  if ( this->size() > 0 )
120  s << (*this)[0];
121  for ( size_type i = 1; i < this->size(); ++i )
122  s << ' ' << (*this)[i];
123 }
124 
125 // instantiation macros for vnl_vector_fixed_ref<T,unsigned> :
126 
127 #define VNL_VECTOR_FIXED_REF_INSTANTIATE(T,n) \
128 template class vnl_vector_fixed_ref<T, n >; \
129 template class vnl_vector_fixed_ref_const<T, n >
130 
131 #endif // vnl_vector_fixed_ref_hxx_
bool read_ascii(std::istream &s) const
Read from text stream.
void swap(double &a, double &b)
Fixed size vnl_vector using user-supplied storage See vnl_matrix_fixed_ref for rationale.
vnl_vector< T > extract(unsigned int len, unsigned int start=0) const
Returns a subvector specified by the start index and length. O(n).
vnl_vector_fixed_ref const & update(vnl_vector< T > const &, unsigned int start=0) const
Replaces elements with index beginning at start, by values of v. O(n).
bool is_finite() const
Return true if it's finite.
Namespace with standard math functions.
vnl_vector_fixed_ref const & flip() const
#define v
Definition: vnl_vector.h:42
bool is_zero() const
Return true iff all the entries are zero.
bool isfinite(vnl_bignum const &x)
Definition: vnl_bignum.h:436
vnl_vector_fixed< T, n > apply(T(*f)(T)) const
Apply f to each element.
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
void print(std::ostream &s) const
Display the vector.