NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
partition.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ALGORITHM_PARTITION_HPP__
2#define NEFORCE_CORE_ALGORITHM_PARTITION_HPP__
3
11
13NEFORCE_BEGIN_NAMESPACE__
14
20
26
45template <typename Iterator, typename Predicate>
46constexpr Iterator partition(Iterator first, Iterator last, Predicate pred) {
47 static_assert(is_ranges_bid_iter_v<Iterator>, "Iterator must be bidirectional_iterator");
48 static_assert(is_invocable_v<Predicate, decltype(*first)>, "Predicate must be invocable");
49
50 while (true) {
51 while (true) {
52 if (first == last) {
53 return first;
54 }
55
56 if (pred(*first)) {
57 ++first;
58 } else {
59 break;
60 }
61 }
62 --last;
63 while (true) {
64 if (first == last) {
65 return first;
66 }
67
68 if (!pred(*last)) {
69 --last;
70 } else {
71 break;
72 }
73 }
74 _NEFORCE iter_swap(first, last);
75 ++first;
76 }
77}
78
101template <typename Iterator, typename T, typename Compare>
102constexpr Iterator lomuto_partition(Iterator first, Iterator last, const T& pivot, Compare comp) {
103 static_assert(is_ranges_rnd_iter_v<Iterator>, "Iterator must be random_access_iterator");
104 static_assert(is_invocable_v<Compare, decltype(*first), decltype(*first)>, "Compare must be invocable");
105
106 while (first < last) {
107 while (comp(*first, pivot)) {
108 ++first;
109 }
110 --last;
111 while (comp(pivot, *last)) {
112 --last;
113 }
114 if (!(first < last)) {
115 break;
116 }
117 _NEFORCE iter_swap(first, last);
118 ++first;
119 }
120 return first;
121}
122
132template <typename Iterator, typename T>
133constexpr Iterator lomuto_partition(Iterator first, Iterator last, const T& pivot) {
134 return _NEFORCE lomuto_partition(first, last, pivot);
135}
136 // PartitionAlgorithms
138 // StandardAlgorithms
140
141NEFORCE_END_NAMESPACE__
142#endif // NEFORCE_CORE_ALGORITHM_PARTITION_HPP__
NEFORCE_INLINE17 constexpr bool is_invocable_v
is_invocable的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_ranges_bid_iter_v
检查是否为范围双向迭代器
NEFORCE_INLINE17 constexpr bool is_ranges_rnd_iter_v
检查是否为范围随机访问迭代器
constexpr Iterator partition(Iterator first, Iterator last, Predicate pred)
分区算法
constexpr Iterator lomuto_partition(Iterator first, Iterator last, const T &pivot, Compare comp)
Lomuto分区算法
constexpr void iter_swap(Iterator1 a, Iterator2 b) noexcept(noexcept(_NEFORCE swap(*a, *b)))
交换迭代器指向的元素
移位和修改算法