SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
remove.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_REMOVE_HPP
9 #define SIG_UTIL_REMOVE_HPP
10 
11 #include "../sigutil.hpp"
12 #include "../helper/container_helper.hpp"
13 
15 
16 namespace sig
17 {
19 
34 template <class C,
35  class CR = typename impl::remove_const_reference<C>::type,
36  class T = typename impl::container_traits<CR>::value_type
37 >
38 auto remove_duplicates(C& container)
39 {
40  std::map<T, uint> removed;
41 
42  for (auto it = std::begin(container), end = std::end(container); it != end;){
43  if (!removed.count(*it)){
44  removed[*it] = 0;
45  ++it;
46  }
47  else{
48  ++removed[*it];
49  it = container.erase(it);
50  end = container.end();
51  }
52  }
53 
54  return removed;
55 }
56 
57 #if SIG_ENABLE_BOOST
58 #define Sig_Eraser_ParamType1 typename boost::call_traits<typename impl::container_traits<C>::value_type>::param_type
59 #else
60 #define Sig_Eraser_ParamType1 typename std::common_type<typename impl::container_traits<C>::value_type>::type const&
61 #endif
62 
64 
84 template <class C>
85 bool remove_one(C& container, Sig_Eraser_ParamType1 remove)
86 {
87  for (auto it = std::begin(container), end = std::end(container); it != end;){
88  if (*it == remove){
89  container.erase(it);
90  return true;
91  }
92  else ++it;
93  }
94 
95  return false;
96 }
97 
99 
112 template <class Pred, class C>
113 bool remove_one_if(C& container, Pred remove_pred)
114 {
115  for (auto it = std::begin(container), end = std::end(container); it != end;){
116  if (remove_pred(*it)){
117  container.erase(it);
118  return true;
119  }
120  else ++it;
121  }
122 
123  return false;
124 }
125 
127 
147 template <class C>
148 bool remove_all(C& container, Sig_Eraser_ParamType1 remove)
149 {
150  uint presize = container.size();
151 
152  if (!container.empty()) erase(container, remove);
153 
154  return presize != container.size();
155 }
156 
157 
159 
172 template <class Pred, class C>
173 bool remove_all_if(C& container, Pred remove_pred)
174 {
175  uint presize = container.size();
176 
177  if (!container.empty()) erase_if(container, remove_pred);
178 
179  return presize != container.size();
180 }
181 
182 }
183 #endif
#define Sig_Eraser_ParamType1
Definition: remove.hpp:58
bool remove_one(C &container, typename boost::call_traits< typename impl::container_traits< C >::value_type >::param_type remove)
コンテナから指定した値を1つ削除
Definition: remove.hpp:85
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
auto remove_duplicates(C &container)
コンテナの要素から重複した値を削除
Definition: remove.hpp:38
bool remove_one_if(C &container, Pred remove_pred)
コンテナから述語条件を満たす値を1つ削除
Definition: remove.hpp:113
void erase(C &container, typename impl::sequence_container_traits< C >::value_type const &t)
void erase_if(C &container, F const &remove_pred)
bool remove_all(C &container, typename boost::call_traits< typename impl::container_traits< C >::value_type >::param_type remove)
コンテナから指定した値を全削除
Definition: remove.hpp:148
Definition: array.hpp:15
bool remove_all_if(C &container, Pred remove_pred)
コンテナから述語条件を満たす値を全削除
Definition: remove.hpp:173
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >