vnl_operators.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_operators.h
2 #ifndef vnl_operators_h_
3 #define vnl_operators_h_
4 //:
5 // \file
6 // \brief Various operators for templated vnl classes
7 // \author Ian Scott
8 
9 #include <vnl/vnl_vector.h>
10 #include <vnl/vnl_vector_fixed.h>
11 #include <vnl/vnl_matrix.h>
12 #include <vnl/vnl_matrix_fixed.h>
13 #include "vnl/vnl_export.h"
14 
15 //: Define a complete ordering on vnl_vector
16 // This is useful to create a set, or map of vectors.
17 //
18 // The ordering itself is implementation defined - so don't rely
19 // on the meaning of less here.
20 //
21 // \relatesalso vnl_vector
22 
23 template<class T> VNL_EXPORT
24 bool operator<(vnl_vector<T> const& lhs, vnl_vector<T> const& rhs)
25 {
26  if (&lhs == &rhs) return false; // same object => equal.
27 
28  if (lhs.size() < rhs.size()) return true; // Size different ?
29  else if (lhs.size() > rhs.size()) return false;
30 
31  for (unsigned i = 0; i < lhs.size(); i++) // For each index
32  {
33  if (lhs(i) < rhs(i)) return true; // Element different ?
34  else if (lhs(i) > rhs(i)) return false;
35  }
36  return false; // Else all same.
37 }
38 
39 //: Define a complete ordering on vnl_matrix
40 // This is useful to create a set, or map of matrices.
41 //
42 // The ordering itself is implementation defined - so don't rely
43 // on the meaning of less here.
44 //
45 // \relatesalso vnl_matrix
46 
47 template<class T> VNL_EXPORT
48 bool operator<(vnl_matrix<T> const& lhs, vnl_matrix<T> const& rhs)
49 {
50  if (&lhs == &rhs) return false; // same object => equal.
51 
52  if (lhs.rows() < rhs.rows()) return true; // Size different ?
53  else if (lhs.rows() > rhs.rows()) return false;
54  else if (lhs.cols() < rhs.cols()) return true;
55  else if (lhs.cols() > rhs.cols()) return false;
56 
57  for (unsigned i = 0; i < lhs.size(); i++) // For each index
58  {
59  if (lhs.data_block()[i] < rhs.data_block()[i]) return true; // Element different ?
60  else if (lhs.data_block()[i] > rhs.data_block()[i]) return false;
61  }
62  return false; // Else all same.
63 }
64 
65 //: Define a complete ordering on vnl_vector_fixed
66 // This is useful to create a set, or map of vectors.
67 //
68 // \relatesalso vnl_vector_fixed
69 
70 template<class T, unsigned int n> VNL_EXPORT
72 {
73  return lhs.as_ref() < rhs.as_ref();
74 }
75 
76 //: Define a complete ordering on vnl_matrix_fixed
77 // This is useful to create a set, or map of matrices.
78 //
79 // \relatesalso vnl_matrix_fixed
80 
81 template<class T, unsigned int n, unsigned int m> VNL_EXPORT
83 {
84  return lhs.as_ref() < rhs.as_ref();
85 }
86 
87 #endif // vnl_operators_h_
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
vnl_matrix_ref< T > as_ref()
Explicit conversion to a vnl_matrix_ref.
An ordinary mathematical matrix.
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
vnl_vector_ref< T > as_ref()
Explicit conversion to a vnl_vector_ref.
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
Definition: vnl_matrix.h:601
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
fixed size matrix
Fixed length stack-stored vector.
unsigned int size() const
Return the total number of elements stored by the matrix.
Definition: vnl_matrix.h:176
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
VNL_EXPORT bool operator<(vnl_vector< T > const &lhs, vnl_vector< T > const &rhs)
Define a complete ordering on vnl_vector.
Definition: vnl_operators.h:24