Sayonara Player
Loading...
Searching...
No Matches
Algorithm.h
1/* Algorithm.h */
2
3/* Copyright (C) 2011-2024 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef ALGORITHM_H
22#define ALGORITHM_H
23
24#include "globals.h"
25#include "typedefs.h"
26#include <algorithm>
27
28namespace Util
29{
30 namespace Algorithm
31 {
32 template<typename T, typename FN>
33 bool contains(const T& container, FN fn)
34 {
35 return std::any_of(container.begin(), container.end(), fn);
36 }
37
38 template<typename T, typename FN>
39 void sort(T& container, FN fn)
40 {
41 std::sort(container.begin(), container.end(), fn);
42 }
43
44 template<typename T, typename FN>
45 typename T::iterator find(T& container, FN fn)
46 {
47 return std::find_if(container.begin(), container.end(), fn);
48 }
49
50 template<typename T, typename FN>
51 typename T::const_iterator find(const T& container, FN fn)
52 {
53 return std::find_if(container.begin(), container.end(), fn);
54 }
55
56 template<typename T>
57 constexpr typename std::add_const<T>::type& AsConst(T& t)
58 {
59 return t;
60 }
61
62 template<typename T, typename FN>
63 int indexOf(const T& container, FN fn)
64 {
65 auto it = Algorithm::find(container, fn);
66 if(it == container.end())
67 {
68 return -1;
69 }
70 return std::distance(container.begin(), it);
71 }
72
73 template<class Container, typename FN>
74 int count(const Container& container, FN fn)
75 {
76 return std::count_if(container.begin(), container.end(), fn);
77 }
78
79 template<class Container>
80 void remove_duplicates(Container& container)
81 {
82 for(auto it = container.begin(); it != container.end(); it++)
83 {
84 container.erase
85 (
86 std::remove(it + 1, container.end(), *it),
87 container.end()
88 );
89 }
90 }
91
92 template<class Container, class Function>
93 void removeIf(Container& container, Function fn)
94 {
95 auto it = std::remove_if(container.begin(), container.end(), fn);
96 container.erase(it, container.end());
97 }
98
99 template<class ContainerIn, class ContainerOut, typename FN>
100 void transform(const ContainerIn& in, ContainerOut& out, FN fn)
101 {
102 std::transform(in.begin(), in.end(), std::back_inserter(out), fn);
103 }
104
105 template<class ContainerInOut, typename FN>
106 void transform(ContainerInOut& inout, FN fn)
107 {
108 std::transform(inout.cbegin(), inout.cend(), inout.begin(), fn);
109 }
110
111 template<class ContainerIn, class ContainerOut, typename FN>
112 void copyIf(const ContainerIn& in, ContainerOut& out, FN fn)
113 {
114 std::copy_if(in.begin(), in.end(), std::back_inserter(out), fn);
115 }
116
117 template<class ContainerIn, class ContainerOut, typename FN>
118 void moveIf(ContainerIn& in, ContainerOut& out, FN fn)
119 {
120 for(auto& element: in)
121 {
122 if(fn(element))
123 {
124 out.emplace_back(std::move(element));
125 }
126 }
127 }
128 }
129}
130
131#endif // ALGORITHM_H
Helper functions.
Definition: MetaTypeRegistry.h:25