|
| 1 | +/* Copyright (C) 2021 Edgar B |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cassert> |
| 9 | +#include <cmath> |
| 10 | +#include <filesystem> |
| 11 | +#include <iterator> |
| 12 | +#include <string> |
| 13 | +#include <string_view> |
| 14 | +#include <type_traits> |
| 15 | + |
| 16 | +namespace btu::common { |
| 17 | +//Expects a range sorted in descending order |
| 18 | +template<class It, class Predicate, class Sum> |
| 19 | +[[nodiscard]] inline It merge_if(It first, It last, const Predicate &predicate, const Sum &sum) |
| 20 | +{ |
| 21 | + if (first == last) |
| 22 | + return last; |
| 23 | + |
| 24 | + last--; |
| 25 | + while (first != last) |
| 26 | + { |
| 27 | + while (predicate(*first, *last)) |
| 28 | + { |
| 29 | + *first = sum(*first, *last); |
| 30 | + last--; |
| 31 | + if (first == last) |
| 32 | + return ++first; |
| 33 | + } |
| 34 | + first++; |
| 35 | + } |
| 36 | + return ++first; |
| 37 | +} |
| 38 | + |
| 39 | +template<class It, class Predicate> |
| 40 | +[[nodiscard]] inline It merge_if(It first, It last, Predicate &&predicate) |
| 41 | +{ |
| 42 | + using Type = typename std::iterator_traits<It>::value_type; |
| 43 | + auto plus = [](const Type &first, const Type &second) { return first + second; }; |
| 44 | + return merge_if(first, last, std::forward<Predicate>(predicate), plus); |
| 45 | +} |
| 46 | + |
| 47 | +template<class Container, class Predicate> |
| 48 | +[[nodiscard]] inline auto merge_if(Container &cont, Predicate &&predicate) |
| 49 | +{ |
| 50 | + using namespace std; |
| 51 | + return merge_if(begin(cont), end(cont), forward<Predicate>(predicate)); |
| 52 | +} |
| 53 | + |
| 54 | +template<class Container, class Predicate, class Sum> |
| 55 | +[[nodiscard]] inline auto merge_if(Container &cont, Predicate &&pred, Sum &&sum) |
| 56 | +{ |
| 57 | + using namespace std; |
| 58 | + return merge_if(begin(cont), end(cont), forward<Predicate>(pred), forward<Sum>(sum)); |
| 59 | +} |
| 60 | + |
| 61 | +template<class Container, class ValueType> |
| 62 | +[[nodiscard]] inline auto contains(const Container &cont, const ValueType &val) |
| 63 | +{ |
| 64 | + using namespace std; |
| 65 | + return find(begin(cont), end(cont), val) != end(cont); |
| 66 | +} |
| 67 | + |
| 68 | +template<class Container, class Predicate> |
| 69 | +[[nodiscard]] inline auto find_if(const Container &cont, const Predicate &pred) |
| 70 | +{ |
| 71 | + using namespace std; |
| 72 | + return find_if(begin(cont), end(cont), pred); |
| 73 | +} |
| 74 | + |
| 75 | +template<typename Cont, typename Pred> |
| 76 | +void erase_if(Cont &cont, const Pred &pred) |
| 77 | +{ |
| 78 | + using namespace std; |
| 79 | + cont.erase(remove_if(begin(cont), end(cont), pred), end(cont)); |
| 80 | +} |
| 81 | +} // namespace btu::common |
0 commit comments