1 /* Algorithm.h */
2 
3 /* Copyright (C) 2011-2020 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 
28 namespace 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 			return t;
59 		}
60 
61 		template<typename T, typename FN>
62 		int indexOf(const T& container, FN fn) {
63 			auto it = Algorithm::find(container, fn);
64 			if(it == container.end())
65 			{
66 				return -1;
67 			}
68 			return std::distance(container.begin(), it);
69 		}
70 
71 		template<class Container, typename FN>
72 		int count(const Container& container, FN fn)
73 		{
74 			return std::count_if(container.begin(), container.end(), fn);
75 		}
76 
77 		template<class Container>
78 		void remove_duplicates(Container& container)
79 		{
80 			for(auto it=container.begin(); it != container.end(); it++)
81 			{
82 				container.erase
83 				(
84 					std::remove(it + 1, container.end(), *it),
85 					container.end()
86 				);
87 			}
88 		}
89 
90 		template<class Container, class Function>
91 		void removeIf(Container& container, Function fn)
92 		{
93 			auto it = std::remove_if(container.begin(), container.end(), fn);
94 			container.erase(it, container.end());
95 		}
96 
97 		template<class ContainerIn, class ContainerOut, typename FN>
98 		void transform(const ContainerIn& in, ContainerOut& out, FN fn)
99 		{
100 			std::transform(in.begin(), in.end(), std::back_inserter(out), fn);
101 		}
102 
103 		template<class ContainerInOut, typename FN>
104 		void transform(ContainerInOut& inout, FN fn)
105 		{
106 			std::transform(inout.cbegin(), inout.cend(), inout.begin(), fn);
107 		}
108 
109 		template<class ContainerIn, class ContainerOut, typename FN>
110 		void copyIf(const ContainerIn& in, ContainerOut& out, FN fn)
111 		{
112 			std::copy_if(in.begin(), in.end(), std::back_inserter(out), fn);
113 		}
114 
115 		template<class ContainerIn, class ContainerOut, typename FN>
116 		void moveIf(ContainerIn& in, ContainerOut& out, FN fn)
117 		{
118 			for(auto& element : in)
119 			{
120 				if(fn(element))
121 				{
122 					out.emplace_back(std::move(element));
123 				}
124 			}
125 		}
126 	}
127 }
128 
129 #endif // ALGORITHM_H
130