IndexComparison.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef IGL_INDEXCOMPARISON_H
  2. #define IGL_INDEXCOMPARISON_H
  3. #include <iostream>
  4. // Comparison struct used by sort
  5. // http://bytes.com/topic/c/answers/132045-sort-get-index
  6. namespace igl{
  7. // For use with functions like std::sort
  8. template<class T> struct IndexLessThan
  9. {
  10. IndexLessThan(const T arr) : arr(arr) {}
  11. bool operator()(const size_t a, const size_t b) const
  12. {
  13. return arr[a] < arr[b];
  14. }
  15. const T arr;
  16. };
  17. // For use with functions like std::unique
  18. template<class T> struct IndexEquals
  19. {
  20. IndexEquals(const T arr) : arr(arr) {}
  21. bool operator()(const size_t a, const size_t b) const
  22. {
  23. return arr[a] == arr[b];
  24. }
  25. const T arr;
  26. };
  27. // For use with functions like std::sort
  28. template<class T> struct IndexVectorLessThan
  29. {
  30. IndexVectorLessThan(const T & vec) : vec ( vec) {}
  31. bool operator()(const size_t a, const size_t b) const
  32. {
  33. return vec(a) < vec(b);
  34. }
  35. const T & vec;
  36. };
  37. // For use with functions like std::sort
  38. template<class T> struct IndexDimLessThan
  39. {
  40. IndexDimLessThan(const T & mat,const int & dim, const int & j) :
  41. mat(mat),
  42. dim(dim),
  43. j(j)
  44. {}
  45. bool operator()(const size_t a, const size_t b) const
  46. {
  47. if(dim == 1)
  48. {
  49. return mat(a,j) < mat(b,j);
  50. }else
  51. {
  52. return mat(j,a) < mat(j,b);
  53. }
  54. }
  55. const T & mat;
  56. const int & dim;
  57. const int & j;
  58. };
  59. // For use with functions like std::sort
  60. template<class T> struct IndexRowLessThan
  61. {
  62. IndexRowLessThan(const T & mat) : mat ( mat) {}
  63. bool operator()(const size_t a, const size_t b) const
  64. {
  65. const int cols = mat.cols();
  66. // Lexicographical order
  67. for(int j = 0;j<cols;j++)
  68. {
  69. if(mat(a,j) > mat(b,j))
  70. {
  71. return false;
  72. } else if(mat(a,j) < mat(b,j))
  73. {
  74. return true;
  75. }
  76. }
  77. // equality is false
  78. return false;
  79. }
  80. const T & mat;
  81. };
  82. // For use with functions like std::sort
  83. template<class T> struct IndexRowEquals
  84. {
  85. IndexRowEquals(const T & mat) : mat ( mat) {}
  86. bool operator()(const size_t a, const size_t b) const
  87. {
  88. const int cols = mat.cols();
  89. // Lexicographical order
  90. for(int j = 0;j<cols;j++)
  91. {
  92. if(mat(a,j) != mat(b,j))
  93. {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. const T & mat;
  100. };
  101. }
  102. #endif