Sayonara Player
Algorithm.h
1 /* Algorithm.h */
2 
3 /* Copyright (C) 2011-2019 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 
22 
23 #ifndef ALGORITHM_H
24 #define ALGORITHM_H
25 
26 #include "globals.h"
27 #include "typedefs.h"
28 #include <algorithm>
29 
30 namespace Util
31 {
32  namespace Algorithm
33  {
34  template<typename T, typename FN>
35  bool contains(const T& container, FN fn)
36  {
37  return std::any_of(container.begin(), container.end(), fn);
38  }
39 
40  template<typename T, typename FN>
41  void sort(T& container, FN fn)
42  {
43  std::sort(container.begin(), container.end(), fn);
44  }
45 
46  template<typename T, typename FN>
47  typename T::iterator find(T& container, FN fn)
48  {
49  return std::find_if(container.begin(), container.end(), fn);
50  }
51 
52  template<typename T, typename FN>
53  typename T::const_iterator find(const T& container, FN fn)
54  {
55  return std::find_if(container.begin(), container.end(), fn);
56  }
57 
58  template<typename T>
59  constexpr typename std::add_const<T>::type& AsConst(T& t) {
60  return t;
61  }
62 
63  template<typename T, typename FN>
64  int indexOf(const T& container, FN fn) {
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_if(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 }
93 
94 #endif // ALGORITHM_H
Helper functions.
Definition: GenreView.h:35