SortableRow.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. /// A row of things that can be sorted against other rows
  16. /// @tparam T should be a vector/matrix/array that implements .size(), and operator(int i)
  17. template <typename T>
  18. class SortableRow
  19. {
  20. public:
  21. /// The data
  22. T data;
  23. public:
  24. /// Default constructor
  25. SortableRow():data(){};
  26. /// Constructor
  27. /// @param[in] data the data
  28. SortableRow(const T & data):data(data){};
  29. /// Less than comparison
  30. /// @param[in] that the other row
  31. /// @returns true if this row is less than that row
  32. bool operator<(const SortableRow & that) const
  33. {
  34. // Lexicographical
  35. int minc = (this->data.size() < that.data.size()?
  36. this->data.size() : that.data.size());
  37. // loop over columns
  38. for(int i = 0;i<minc;i++)
  39. {
  40. if(this->data(i) == that.data(i))
  41. {
  42. continue;
  43. }
  44. return this->data(i) < that.data(i);
  45. }
  46. // All characters the same, comes done to length
  47. return this->data.size()<that.data.size();
  48. };
  49. /// Equality comparison
  50. /// @param[in] that the other row
  51. /// @returns true if this row is equal to that row
  52. bool operator==(const SortableRow & that) const
  53. {
  54. if(this->data.size() != that.data.size())
  55. {
  56. return false;
  57. }
  58. for(int i = 0;i<this->data.size();i++)
  59. {
  60. if(this->data(i) != that.data(i))
  61. {
  62. return false;
  63. }
  64. }
  65. return true;
  66. };
  67. /// Inequality comparison
  68. /// @param[in] that the other row
  69. /// @returns true if this row is not equal to that row
  70. bool operator!=(const SortableRow & that) const
  71. {
  72. return !(*this == that);
  73. };
  74. };
  75. }
  76. #endif