sort.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_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. /// @tparam DerivedX derived scalar type, e.g. MatrixXi or MatrixXd
  19. /// @tparam DerivedIX derived integer type, e.g. MatrixXi
  20. /// @param[in] X m by n matrix whose entries are to be sorted
  21. /// @param[in] dim dimensional along which to sort:
  22. /// 1 sort each column (matlab default)
  23. /// 2 sort each row
  24. /// @param[in] ascending sort ascending (true, matlab default) or descending (false)
  25. /// @param[out] Y m by n matrix whose entries are sorted
  26. /// @param[out] IX m by n matrix of indices so that if dim = 1, then in matlab notation
  27. /// for j = 1:n, Y(:,j) = X(I(:,j),j); end
  28. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  29. IGL_INLINE void sort(
  30. const Eigen::DenseBase<DerivedX>& X,
  31. const int dim,
  32. const bool ascending,
  33. Eigen::PlainObjectBase<DerivedY>& Y,
  34. Eigen::PlainObjectBase<DerivedIX>& IX);
  35. /// \overload
  36. template <typename DerivedX, typename DerivedY>
  37. IGL_INLINE void sort(
  38. const Eigen::DenseBase<DerivedX>& X,
  39. const int dim,
  40. const bool ascending,
  41. Eigen::PlainObjectBase<DerivedY>& Y);
  42. /// \overload
  43. ///
  44. /// \note This should be renamed to something like sort_small because it is
  45. /// only faster if size(X,dim) is small.
  46. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  47. IGL_INLINE void sort_new(
  48. const Eigen::DenseBase<DerivedX>& X,
  49. const int dim,
  50. const bool ascending,
  51. Eigen::PlainObjectBase<DerivedY>& Y,
  52. Eigen::PlainObjectBase<DerivedIX>& IX);
  53. /// \overload
  54. /// \brief Special case if size(X,dim) == 2
  55. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  56. IGL_INLINE void sort2(
  57. const Eigen::DenseBase<DerivedX>& X,
  58. const int dim,
  59. const bool ascending,
  60. Eigen::PlainObjectBase<DerivedY>& Y,
  61. Eigen::PlainObjectBase<DerivedIX>& IX);
  62. /// \overload
  63. /// \brief Special case if size(X,dim) == 3
  64. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  65. IGL_INLINE void sort3(
  66. const Eigen::DenseBase<DerivedX>& X,
  67. const int dim,
  68. const bool ascending,
  69. Eigen::PlainObjectBase<DerivedY>& Y,
  70. Eigen::PlainObjectBase<DerivedIX>& IX);
  71. /// Act like matlab's [Y,I] = SORT(X) for std library vectors
  72. /// @tparam T should be a class that implements the '<' comparator operator
  73. /// @param[in] unsorted unsorted vector
  74. /// @param[in] ascending sort ascending (true, matlab default) or descending (false)
  75. /// @param[out] sorted sorted vector, allowed to be same as unsorted
  76. /// @param[out] 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