sort.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // T should be a eigen matrix primitive type like int or double
  13. // Inputs:
  14. // X m by n matrix whose entries are to be sorted
  15. // dim dimensional along which to sort:
  16. // 1 sort each column (matlab default)
  17. // 2 sort each row
  18. // ascending sort ascending (true, matlab default) or descending (false)
  19. // Outputs:
  20. // Y m by n matrix whose entries are sorted
  21. // IX m by n matrix of indices so that if dim = 1, then in matlab notation
  22. // for j = 1:n, Y(:,j) = X(I(:,j),j); end
  23. template <typename T>
  24. IGL_INLINE void sort(
  25. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & X,
  26. const int dim,
  27. const bool ascending,
  28. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & Y,
  29. Eigen::MatrixXi & IX);
  30. // Act like matlab's [Y,I] = SORT(X) for std library vectors
  31. // Templates:
  32. // T should be a class that implements the '<' comparator operator
  33. // Input:
  34. // unsorted unsorted vector
  35. // ascending sort ascending (true, matlab default) or descending (false)
  36. // Output:
  37. // sorted sorted vector, allowed to be same as unsorted
  38. // index_map an index map such that sorted[i] = unsorted[index_map[i]]
  39. template <class T>
  40. IGL_INLINE void sort(
  41. const std::vector<T> &unsorted,
  42. const bool ascending,
  43. std::vector<T> &sorted,
  44. std::vector<size_t> &index_map);
  45. }
  46. #ifdef IGL_HEADER_ONLY
  47. # include "sort.cpp"
  48. #endif
  49. #endif