sort.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef IGL_SORT_H
  2. #define IGL_SORT_H
  3. #include "igl_inline.h"
  4. #include <vector>
  5. #include <Eigen/Core>
  6. namespace igl
  7. {
  8. // Sort the elements of a matrix X along a given dimension like matlabs sort
  9. // function
  10. //
  11. // Templates:
  12. // DerivedX derived scalar type, e.g. MatrixXi or MatrixXd
  13. // DerivedIX derived integer type, e.g. MatrixXi
  14. // Inputs:
  15. // X m by n matrix whose entries are to be sorted
  16. // dim dimensional along which to sort:
  17. // 1 sort each column (matlab default)
  18. // 2 sort each row
  19. // ascending sort ascending (true, matlab default) or descending (false)
  20. // Outputs:
  21. // Y m by n matrix whose entries are sorted
  22. // IX m by n matrix of indices so that if dim = 1, then in matlab notation
  23. // for j = 1:n, Y(:,j) = X(I(:,j),j); end
  24. template <typename DerivedX, typename DerivedIX>
  25. IGL_INLINE void sort(
  26. const Eigen::PlainObjectBase<DerivedX>& X,
  27. const int dim,
  28. const bool ascending,
  29. Eigen::PlainObjectBase<DerivedX>& Y,
  30. Eigen::PlainObjectBase<DerivedIX>& IX);
  31. // Act like matlab's [Y,I] = SORT(X) for std library vectors
  32. // Templates:
  33. // T should be a class that implements the '<' comparator operator
  34. // Input:
  35. // unsorted unsorted vector
  36. // ascending sort ascending (true, matlab default) or descending (false)
  37. // Output:
  38. // sorted sorted vector, allowed to be same as unsorted
  39. // index_map an index map such that sorted[i] = unsorted[index_map[i]]
  40. template <class T>
  41. IGL_INLINE void sort(
  42. const std::vector<T> &unsorted,
  43. const bool ascending,
  44. std::vector<T> &sorted,
  45. std::vector<size_t> &index_map);
  46. }
  47. #ifdef IGL_HEADER_ONLY
  48. # include "sort.cpp"
  49. #endif
  50. #endif