core/vnl/vnl_operators.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_operators.h
00002 #ifndef vnl_operators_h_
00003 #define vnl_operators_h_
00004 //:
00005 // \file
00006 // \brief Various operators for templated vnl classes
00007 // \author Ian Scott
00008 
00009 #include <vnl/vnl_vector.h>
00010 #include <vnl/vnl_vector_fixed.h>
00011 #include <vnl/vnl_matrix.h>
00012 #include <vnl/vnl_matrix_fixed.h>
00013 
00014 //: Define a complete ordering on vnl_vector
00015 // This is useful to create a set, or map of vectors.
00016 //
00017 // The ordering itself is implementation defined - so don't rely
00018 // on the meaning of less here.
00019 //
00020 // \relatesalso vnl_vector
00021 
00022 template<class T>
00023 bool operator<(vnl_vector<T> const& lhs, vnl_vector<T> const& rhs)
00024 {
00025   if (&lhs == &rhs)  return false;              // same object => equal.
00026 
00027   if (lhs.size() < rhs.size())  return true;    // Size different ?
00028   else if (lhs.size() > rhs.size()) return false;
00029 
00030   for (unsigned i = 0; i < lhs.size(); i++)         // For each index
00031   {
00032     if (lhs(i) < rhs(i)) return true; // Element different ?
00033     else if (lhs(i) > rhs(i)) return false;
00034   }
00035   return false;                                 // Else all same.
00036 }
00037 
00038 //: Define a complete ordering on vnl_matrix
00039 // This is useful to create a set, or map of matrices.
00040 //
00041 // The ordering itself is implementation defined - so don't rely
00042 // on the meaning of less here.
00043 //
00044 // \relatesalso vnl_matrix
00045 
00046 template<class T>
00047 bool operator<(vnl_matrix<T> const& lhs, vnl_matrix<T> const& rhs)
00048 {
00049   if (&lhs == &rhs)  return false;              // same object => equal.
00050 
00051   if (lhs.rows() < rhs.rows())  return true;        // Size different ?
00052   else if (lhs.rows() > rhs.rows()) return false;
00053   else if (lhs.cols() < rhs.cols())  return true;
00054   else if (lhs.cols() > rhs.cols()) return false;
00055 
00056   for (unsigned i = 0; i < lhs.size(); i++)         // For each index
00057   {
00058     if (lhs.data_block()[i] < rhs.data_block()[i]) return true; // Element different ?
00059     else if (lhs.data_block()[i] > rhs.data_block()[i]) return false;
00060   }
00061   return false;                                 // Else all same.
00062 }
00063 
00064 //: Define a complete ordering on vnl_vector_fixed
00065 // This is useful to create a set, or map of vectors.
00066 //
00067 // \relatesalso vnl_vector_fixed
00068 
00069 template<class T, unsigned int n>
00070 bool operator<(vnl_vector_fixed<T,n> const& lhs, vnl_vector_fixed<T,n> const& rhs)
00071 {
00072   return lhs.as_ref() < rhs.as_ref();
00073 }
00074 
00075 //: Define a complete ordering on vnl_matrix_fixed
00076 // This is useful to create a set, or map of matrices.
00077 //
00078 // \relatesalso vnl_matrix_fixed
00079 
00080 template<class T, unsigned int n, unsigned int m>
00081 bool operator<(vnl_matrix_fixed<T,n,m> const& lhs, vnl_matrix_fixed<T,n,m> const& rhs)
00082 {
00083   return lhs.as_ref() < rhs.as_ref();
00084 }
00085 
00086 #endif // vnl_operators_h_