SortableRow.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef IGL_SORTABLE_ROW_H
  2. #define IGL_SORTABLE_ROW_H
  3. // Simple class to contain a rowvector which allows rowwise sorting and
  4. // reordering
  5. #include <Eigen/Core>
  6. // Templates:
  7. // T should be a matrix that implments .size(), and operator(int i)
  8. template <typename T>
  9. class SortableRow
  10. {
  11. public:
  12. T data;
  13. public:
  14. SortableRow():data(){};
  15. SortableRow(const T & data):data(data){};
  16. bool operator<(const SortableRow & that) const
  17. {
  18. // Get reference so that I can use parenthesis
  19. const SortableRow<T> & THIS = *this;
  20. // Lexicographical
  21. int minc = (THIS.data.size() < that.data.size()?
  22. THIS.data.size() : that.data.size());
  23. // loop over columns
  24. for(int i = 0;i<minc;i++)
  25. {
  26. if(THIS.data(i) == that.data(i))
  27. {
  28. continue;
  29. }
  30. return THIS.data(i) < that.data(i);
  31. }
  32. // All characters the same, comes done to length
  33. return THIS.data.size()<that.data.size();
  34. };
  35. bool operator==(const SortableRow & that) const
  36. {
  37. // Get reference so that I can use parenthesis
  38. const SortableRow<T> & THIS = *this;
  39. if(THIS.data.size() != that.data.size())
  40. {
  41. return false;
  42. }
  43. for(int i = 0;i<THIS.data.size();i++)
  44. {
  45. if(THIS.data(i) != that.data(i))
  46. {
  47. return false;
  48. }
  49. }
  50. return true;
  51. };
  52. bool operator!=(const SortableRow & that) const
  53. {
  54. return !(*this == that);
  55. };
  56. };
  57. #endif