SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
filter.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_FILTER_HPP
9 #define SIG_UTIL_FILTER_HPP
10 
11 #include "../helper/helper_modules.hpp"
12 #include "../helper/container_helper.hpp"
13 
15 
16 namespace sig
17 {
19 
40 template <class F, class C,
41  class CR = typename impl::remove_const_reference<C>::type,
42  class ET = typename impl::forward_element<C>::type
43 >
44 auto filter(F&& pred, C&& list)
45 {
46  CR result = impl::container_traits<CR>::make(list.size());
47 
48  for (auto&& e : std::forward<C>(list)){
49  if (std::forward<F>(pred)(e)) impl::container_traits<CR>::add_element(result, std::forward<ET>(e));
50  }
51  return result;
52 }
53 
55 
71 template <class F, class C,
72  class CR = typename impl::remove_const_reference<C>::type,
73  class ET = typename impl::forward_element<C>::type
74 >
75 auto filter(F&& pred, int init, C&& list)
76 {
77  CR result = impl::container_traits<CR>::make(list.size());
78 
79  for (auto&& e : std::forward<C>(list)){
80  if (std::forward<F>(pred)(init, e)) impl::container_traits<CR>::add_element(result, std::forward<ET>(e));
81  ++init;
82  }
83  return result;
84 }
85 
86 
88 
110 template <class F, class C,
111  class CR = typename impl::remove_const_reference<C>::type,
112  class ET = typename impl::forward_element<C>::type
113 >
114 auto partition(F&& pred, C&& list)
115 {
116  CR result1 = impl::container_traits<CR>::make(list.size());
117  CR result2 = impl::container_traits<CR>::make(list.size());
118 
119  for (auto&& e : std::forward<C>(list)){
120  if (std::forward<F>(pred)(e)) impl::container_traits<CR>::add_element(result1, std::forward<ET>(e));
121  else impl::container_traits<CR>::add_element(result2, std::forward<ET>(e));
122  }
123  return std::make_tuple(std::move(result1), std::move(result2));
124 }
125 
126 }
127 #endif
auto partition(F &&pred, C &&list)
コンテナから指定条件を満たす要素とそれ以外の要素とを分離する
Definition: filter.hpp:114
Definition: array.hpp:15
auto filter(F &&pred, C &&list)
コンテナから指定条件を満たす要素を抽出する
Definition: filter.hpp:44