swift
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_ALGORITHM_H
7#define SWIFT_MISC_ALGORITHM_H
8
9#include <algorithm>
10#include <iterator>
11#include <random>
12#include <tuple>
13#include <utility>
14
15#include <QtGlobal>
16
17namespace swift::misc
18{
24 template <typename I, typename J>
25 auto removeIfIn(I begin1, I end1, J begin2, J end2)
26 {
27 auto newEnd = end1;
28 std::for_each(begin2, end2, [&](const auto &rm) {
29 const auto found = std::find(begin1, end1, rm);
30 Q_ASSERT(found != end1);
31 if (newEnd == end1)
32 {
33 newEnd = found;
34 }
35 else
36 {
37 newEnd = std::move(begin1, found, newEnd);
38 }
39 begin1 = std::next(found);
40 });
41 if (newEnd != end1)
42 {
43 newEnd = std::move(begin1, end1, newEnd);
44 }
45 return newEnd;
46 }
47
48 namespace private_ns
49 {
52 inline std::mt19937 &defaultRandomGenerator()
53 {
54 thread_local std::mt19937 rng(std::random_device {}());
55 return rng;
56 }
57 } // namespace private_ns
58
62 template <typename ForwardIt, typename OutputIt, typename Generator>
63 void copyRandomElements(ForwardIt in, ForwardIt end, OutputIt out, int n, Generator &&rng)
64 {
65 for (auto size = static_cast<int>(std::distance(in, end)); in != end && n > 0; ++in, --size)
66 {
67 if (std::uniform_int_distribution<>(0, size - 1)(rng) < n)
68 {
69 *out++ = *in;
70 --n;
71 }
72 }
73 }
74
78 template <typename ForwardIt, typename OutputIt>
79 void copyRandomElements(ForwardIt in, ForwardIt end, OutputIt out, int n)
80 {
81 copyRandomElements(in, end, out, n, private_ns::defaultRandomGenerator());
82 }
83
88 template <typename ForwardIt, typename OutputIt, typename Generator>
89 void copySampleElements(ForwardIt in, ForwardIt end, OutputIt out, int n, Generator &&rng)
90 {
91 const auto size = static_cast<int>(std::distance(in, end));
92 for (int i = 0; i < std::min(n, size); ++i)
93 {
94 const auto index = std::uniform_int_distribution<>(0, std::max(size / n, 1) - 1)(rng);
95 std::advance(in, index);
96 *out++ = *in;
97 std::advance(in, std::max(size / n, 1) - index);
98 }
99 }
100
104 template <typename ForwardIt, typename OutputIt>
105 void copySampleElements(ForwardIt in, ForwardIt end, OutputIt out, int n)
106 {
107 copySampleElements(in, end, out, n, private_ns::defaultRandomGenerator());
108 }
109
122 template <typename I, typename F>
123 void topologicalSort(I begin, I end, F comparator)
124 {
125 using value_type = typename std::iterator_traits<I>::value_type;
126 auto part = begin;
127 while (part != end)
128 {
129 auto newPart = std::partition(part, end, [=](const value_type &a) {
130 return std::none_of(part, end, [=, &a](const value_type &b) { return comparator(b, a); });
131 });
132 Q_ASSERT_X(newPart != part, "swift::misc::topologicalSort",
133 "Cyclic less-than relation detected (not a partial ordering)");
134 part = newPart;
135 }
136 }
137
150 template <typename C, typename T, typename F>
151 void topologicallySortedInsert(C &container, T &&value, F comparator)
152 {
153 using value_type = typename C::value_type;
154 using reverse = std::reverse_iterator<typename C::iterator>;
155 auto rit = std::find_if(reverse(container.end()), reverse(container.begin()),
156 [=, &value](const value_type &lhs) { return comparator(lhs, value); });
157 Q_ASSERT_X(std::none_of(rit, reverse(container.begin()),
158 [=, &value](const value_type &rhs) { return comparator(value, rhs); }),
159 "swift::misc::topologicallySortedInsert",
160 "Cyclic less-than relation detected (not a partial ordering)");
161 container.insert(rit.base(), std::forward<T>(value));
162 }
163
164 namespace private_ns
165 {
167 template <typename T, typename F, size_t... Is>
168 void tupleForEachPairImpl(T &&tuple, F &&visitor, std::index_sequence<Is...>)
169 {
170 // cppcheck-suppress accessForwarded
171 (static_cast<void>(std::forward<F>(visitor)(std::get<Is * 2>(std::forward<T>(tuple)),
172 std::get<Is * 2 + 1>(std::forward<T>(tuple)))),
173 ...);
174 }
175 } // namespace private_ns
176
180 template <typename T, typename F>
181 void tupleForEachPair(T &&tuple, F &&visitor)
182 {
183 using seq = std::make_index_sequence<std::tuple_size_v<std::decay_t<T>> / 2>;
184 return private_ns::tupleForEachPairImpl(std::forward<T>(tuple), std::forward<F>(visitor), seq());
185 }
186} // namespace swift::misc
187
188#endif // SWIFT_MISC_ALGORITHM_H
Free functions in swift::misc.
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:258
void topologicalSort(I begin, I end, F comparator)
Topological sorting algorithm.
Definition algorithm.h:123
auto removeIfIn(I begin1, I end1, J begin2, J end2)
Removes those elements in range 1 that appear also in range 2 leaving only those that do not appear i...
Definition algorithm.h:25
void tupleForEachPair(T &&tuple, F &&visitor)
Invoke a visitor function on each pair of elements of a tuple in order.
Definition algorithm.h:181
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:264
void copySampleElements(ForwardIt in, ForwardIt end, OutputIt out, int n, Generator &&rng)
Split the range [in,end) into n equal chunks and use the random number generator rng to choose one el...
Definition algorithm.h:89
void topologicallySortedInsert(C &container, T &&value, F comparator)
Insert an element into a sequential container while preserving the topological ordering of the contai...
Definition algorithm.h:151
void copyRandomElements(ForwardIt in, ForwardIt end, OutputIt out, int n, Generator &&rng)
Use the random number generator rng to choose n elements from the range [in,end) and copy them to out...
Definition algorithm.h:63