Sayonara Player
Algorithm.h
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 ContainerIn, class ContainerOut, typename FN>
91  void transform(const ContainerIn& in, ContainerOut& out, FN fn)
92  {
93  std::transform(in.begin(), in.end(), std::back_inserter(out), fn);
94  }
95 
96  template<class ContainerInOut, typename FN>
97  void transform(ContainerInOut& inout, FN fn)
98  {
99  std::transform(inout.cbegin(), inout.cend(), inout.begin(), fn);
100  }
101 
102  template<class ContainerIn, class ContainerOut, typename FN>
103  void copyIf(const ContainerIn& in, ContainerOut& out, FN fn)
104  {
105  std::copy_if(in.begin(), in.end(), std::back_inserter(out), fn);
106  }
107  }
108 }
109 
110 #endif // ALGORITHM_H
Util
Helper functions.
Definition: GenreView.h:36