SortableRow.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_SORTABLE_ROW_H
  9. #define IGL_SORTABLE_ROW_H
  10. // Simple class to contain a rowvector which allows rowwise sorting and
  11. // reordering
  12. #include <Eigen/Core>
  13. // Templates:
  14. // T should be a matrix that implments .size(), and operator(int i)
  15. template <typename T>
  16. class SortableRow
  17. {
  18. public:
  19. T data;
  20. public:
  21. SortableRow():data(){};
  22. SortableRow(const T & data):data(data){};
  23. bool operator<(const SortableRow & that) const
  24. {
  25. // Get reference so that I can use parenthesis
  26. const SortableRow<T> & THIS = *this;
  27. // Lexicographical
  28. int minc = (THIS.data.size() < that.data.size()?
  29. THIS.data.size() : that.data.size());
  30. // loop over columns
  31. for(int i = 0;i<minc;i++)
  32. {
  33. if(THIS.data(i) == that.data(i))
  34. {
  35. continue;
  36. }
  37. return THIS.data(i) < that.data(i);
  38. }
  39. // All characters the same, comes done to length
  40. return THIS.data.size()<that.data.size();
  41. };
  42. bool operator==(const SortableRow & that) const
  43. {
  44. // Get reference so that I can use parenthesis
  45. const SortableRow<T> & THIS = *this;
  46. if(THIS.data.size() != that.data.size())
  47. {
  48. return false;
  49. }
  50. for(int i = 0;i<THIS.data.size();i++)
  51. {
  52. if(THIS.data(i) != that.data(i))
  53. {
  54. return false;
  55. }
  56. }
  57. return true;
  58. };
  59. bool operator!=(const SortableRow & that) const
  60. {
  61. return !(*this == that);
  62. };
  63. };
  64. #endif