sort.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_SORT_H
  9. #define IGL_SORT_H
  10. #include "igl_inline.h"
  11. #include <vector>
  12. #include <Eigen/Core>
  13. namespace igl
  14. {
  15. // Sort the elements of a matrix X along a given dimension like matlabs sort
  16. // function
  17. //
  18. // Templates:
  19. // DerivedX derived scalar type, e.g. MatrixXi or MatrixXd
  20. // DerivedIX derived integer type, e.g. MatrixXi
  21. // Inputs:
  22. // X m by n matrix whose entries are to be sorted
  23. // dim dimensional along which to sort:
  24. // 1 sort each column (matlab default)
  25. // 2 sort each row
  26. // ascending sort ascending (true, matlab default) or descending (false)
  27. // Outputs:
  28. // Y m by n matrix whose entries are sorted
  29. // IX m by n matrix of indices so that if dim = 1, then in matlab notation
  30. // for j = 1:n, Y(:,j) = X(I(:,j),j); end
  31. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  32. IGL_INLINE void sort(
  33. const Eigen::DenseBase<DerivedX>& X,
  34. const int dim,
  35. const bool ascending,
  36. Eigen::PlainObjectBase<DerivedY>& Y,
  37. Eigen::PlainObjectBase<DerivedIX>& IX);
  38. template <typename DerivedX, typename DerivedY>
  39. IGL_INLINE void sort(
  40. const Eigen::DenseBase<DerivedX>& X,
  41. const int dim,
  42. const bool ascending,
  43. Eigen::PlainObjectBase<DerivedY>& Y);
  44. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  45. // Only better if size(X,dim) is small
  46. IGL_INLINE void sort_new(
  47. const Eigen::DenseBase<DerivedX>& X,
  48. const int dim,
  49. const bool ascending,
  50. Eigen::PlainObjectBase<DerivedY>& Y,
  51. Eigen::PlainObjectBase<DerivedIX>& IX);
  52. // Special case if size(X,dim) == 2
  53. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  54. IGL_INLINE void sort2(
  55. const Eigen::DenseBase<DerivedX>& X,
  56. const int dim,
  57. const bool ascending,
  58. Eigen::PlainObjectBase<DerivedY>& Y,
  59. Eigen::PlainObjectBase<DerivedIX>& IX);
  60. // Special case if size(X,dim) == 3
  61. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  62. IGL_INLINE void sort3(
  63. const Eigen::DenseBase<DerivedX>& X,
  64. const int dim,
  65. const bool ascending,
  66. Eigen::PlainObjectBase<DerivedY>& Y,
  67. Eigen::PlainObjectBase<DerivedIX>& IX);
  68. // Act like matlab's [Y,I] = SORT(X) for std library vectors
  69. // Templates:
  70. // T should be a class that implements the '<' comparator operator
  71. // Input:
  72. // unsorted unsorted vector
  73. // ascending sort ascending (true, matlab default) or descending (false)
  74. // Output:
  75. // sorted sorted vector, allowed to be same as unsorted
  76. // index_map an index map such that sorted[i] = unsorted[index_map[i]]
  77. template <class T>
  78. IGL_INLINE void sort(
  79. const std::vector<T> &unsorted,
  80. const bool ascending,
  81. std::vector<T> &sorted,
  82. std::vector<size_t> &index_map);
  83. }
  84. #ifndef IGL_STATIC_LIBRARY
  85. # include "sort.cpp"
  86. #endif
  87. #endif