SortableRow.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. namespace igl
  14. {
  15. // Templates:
  16. // T should be a matrix that implments .size(), and operator(int i)
  17. template <typename T>
  18. class SortableRow
  19. {
  20. public:
  21. T data;
  22. public:
  23. SortableRow():data(){};
  24. SortableRow(const T & data):data(data){};
  25. bool operator<(const SortableRow & that) const
  26. {
  27. // Get reference so that I can use parenthesis
  28. const SortableRow<T> & THIS = *this;
  29. // Lexicographical
  30. int minc = (THIS.data.size() < that.data.size()?
  31. THIS.data.size() : that.data.size());
  32. // loop over columns
  33. for(int i = 0;i<minc;i++)
  34. {
  35. if(THIS.data(i) == that.data(i))
  36. {
  37. continue;
  38. }
  39. return THIS.data(i) < that.data(i);
  40. }
  41. // All characters the same, comes done to length
  42. return THIS.data.size()<that.data.size();
  43. };
  44. bool operator==(const SortableRow & that) const
  45. {
  46. // Get reference so that I can use parenthesis
  47. const SortableRow<T> & THIS = *this;
  48. if(THIS.data.size() != that.data.size())
  49. {
  50. return false;
  51. }
  52. for(int i = 0;i<THIS.data.size();i++)
  53. {
  54. if(THIS.data(i) != that.data(i))
  55. {
  56. return false;
  57. }
  58. }
  59. return true;
  60. };
  61. bool operator!=(const SortableRow & that) const
  62. {
  63. return !(*this == that);
  64. };
  65. };
  66. }
  67. #endif