General Purpose library for Freestanding C++ and POSIX systems
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

42 行
1.1 KiB

  1. #pragma once
  2. #include <gp/algorithms/move.hpp>
  3. namespace gp {
  4. template<typename it_t, typename pred>
  5. it_t lomuto_partition(it_t first, it_t last, pred predicate = pred{}) {
  6. auto i = it_t(first);
  7. auto j = first;
  8. for(; j != last; ++j) {
  9. if(predicate(*j)){
  10. gp::swap(*i, *j);
  11. ++i;
  12. }
  13. }
  14. return i;
  15. }
  16. template<typename it_t, typename pred>
  17. it_t hoare_partition(it_t first, it_t last, pred predicate = pred{}) {
  18. while(first != last) {
  19. while(predicate(*first)) {
  20. ++first;
  21. if(first == last) return first;
  22. }
  23. do {
  24. --last;
  25. if(first == last) return first;
  26. } while(!predicate(*last));
  27. swap(*first, *last);
  28. ++first;
  29. }
  30. return first;
  31. }
  32. /**
  33. template<typename it_t, typename pred>
  34. it_t partition(it_t first, it_t last, pred predicate = pred{}) {
  35. return gp::hoare_partition(first, last, predicate);
  36. }**/
  37. }