reorder.h 951 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef IGL_REORDER_H
  2. #define IGL_REORDER_H
  3. namespace igl
  4. {
  5. // Act like matlab's Y = X[I] for std vectors
  6. // where I contains a vector of indices so that after,
  7. // Y[j] = X[I[j]] for index j
  8. // this implies that Y.size() == I.size()
  9. // X and Y are allowed to be the same reference
  10. template< class T >
  11. inline void reorder(
  12. const std::vector<T> & unordered,
  13. std::vector<size_t> const & index_map,
  14. std::vector<T> & ordered);
  15. }
  16. // Implementation
  17. // This implementation is O(n), but also uses O(n) extra memory
  18. template< class T >
  19. inline void igl::reorder(
  20. const std::vector<T> & unordered,
  21. std::vector<size_t> const & index_map,
  22. std::vector<T> & ordered)
  23. {
  24. // copy for the reorder according to index_map, because unsorted may also be
  25. // sorted
  26. std::vector<T> copy = unordered;
  27. ordered.resize(index_map.size());
  28. for(int i = 0; i<(int)index_map.size();i++)
  29. {
  30. ordered[i] = copy[index_map[i]];
  31. }
  32. }
  33. #endif