sort.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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::PlainObjectBase<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, typename DerivedIX>
  39. // Only better if size(X,dim) is small
  40. IGL_INLINE void sort_new(
  41. const Eigen::PlainObjectBase<DerivedX>& X,
  42. const int dim,
  43. const bool ascending,
  44. Eigen::PlainObjectBase<DerivedY>& Y,
  45. Eigen::PlainObjectBase<DerivedIX>& IX);
  46. // Special case if size(X,dim) == 2
  47. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  48. IGL_INLINE void sort2(
  49. const Eigen::PlainObjectBase<DerivedX>& X,
  50. const int dim,
  51. const bool ascending,
  52. Eigen::PlainObjectBase<DerivedY>& Y,
  53. Eigen::PlainObjectBase<DerivedIX>& IX);
  54. // Special case if size(X,dim) == 3
  55. template <typename DerivedX, typename DerivedY, typename DerivedIX>
  56. IGL_INLINE void sort3(
  57. const Eigen::PlainObjectBase<DerivedX>& X,
  58. const int dim,
  59. const bool ascending,
  60. Eigen::PlainObjectBase<DerivedY>& Y,
  61. Eigen::PlainObjectBase<DerivedIX>& IX);
  62. // Act like matlab's [Y,I] = SORT(X) for std library vectors
  63. // Templates:
  64. // T should be a class that implements the '<' comparator operator
  65. // Input:
  66. // unsorted unsorted vector
  67. // ascending sort ascending (true, matlab default) or descending (false)
  68. // Output:
  69. // sorted sorted vector, allowed to be same as unsorted
  70. // index_map an index map such that sorted[i] = unsorted[index_map[i]]
  71. template <class T>
  72. IGL_INLINE void sort(
  73. const std::vector<T> &unsorted,
  74. const bool ascending,
  75. std::vector<T> &sorted,
  76. std::vector<size_t> &index_map);
  77. }
  78. #ifndef IGL_STATIC_LIBRARY
  79. # include "sort.cpp"
  80. #endif
  81. #endif