SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sort.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_SORT_HPP
9 #define SIG_UTIL_SORT_HPP
10 
11 #include "../helper/type_traits.hpp"
12 #include "../functional/zip.hpp"
13 #include "../functional/rest.hpp"
14 
15 
17 
18 namespace sig
19 {
21 
40 // for sequence container which has random access iterator
41 template <class F, class C,
42  class CR = typename impl::remove_const_reference<C>::type,
43  class T = typename impl::sequence_container_traits<CR>::value_type,
44  typename std::enable_if<impl::has_random_access_iter<CR>::value>::type*& = enabler
45 >
46 auto sort(
47  C& container,
48  F&& binary_op
49  ) ->decltype(impl::eval(std::forward<F>(binary_op), std::declval<T>(), std::declval<T>()), void())
50 {
51  std::sort(std::begin(container), std::end(container), std::forward<F>(binary_op));
52 }
53 
54 // for static container which has random access iterator
55 template <class F, class C,
56  class CR = typename impl::remove_const_reference<C>::type,
57  class T = typename impl::static_container_traits<CR>::value_type,
58  class D = void,
59  typename std::enable_if<impl::has_random_access_iter<CR>::value>::type*& = enabler
60 >
61 auto sort(
62  C& container,
63  F&& binary_op
64  ) ->decltype(impl::eval(std::forward<F>(binary_op), std::declval<T>(), std::declval<T>()), void())
65 {
66  std::sort(std::begin(container), std::end(container), std::forward<F>(binary_op));
67 }
68 
69 // for sequence container which doesn't have random access iterator
70 template <class F, class C,
71  class CR = typename impl::remove_const_reference<C>::type,
72  typename std::enable_if<!impl::has_random_access_iter<CR>::value>::type*& = enabler,
73  class T = typename impl::sequence_container_traits<CR>::value_type
74 >
75 auto sort(
76  C& container,
77  F&& binary_op
78  ) ->decltype(impl::eval(std::forward<F>(binary_op), std::declval<T>(), std::declval<T>()), void())
79 {
80  container.sort(std::forward<F>(binary_op));
81 }
82 
83 
85 
128 template <class C, class F,
129  class T = typename impl::container_traits<typename impl::remove_const_reference<C>::type>::value_type
130 >
132  C&& container,
133  F&& binary_op)
134 {
135  using Tp = std::tuple<T, uint>;
136 
137  auto result = zip(std::forward<C>(container), seqn(0u, 1u, container.size()));
138 
139  sort(result, [&](Tp const& l, Tp const& r){ return std::forward<F>(binary_op)(std::get<0>(l), std::get<0>(r)); });
140 
141  return unzip(std::move(result));
142 }
143 
144 }
145 #endif
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
void * enabler
auto sort_with_index(C &&container, F &&binary_op)
ソート前の位置を保持してソート
Definition: sort.hpp:131
auto sort(C &container, F &&binary_op) -> decltype(impl::eval(std::forward< F >(binary_op), std::declval< T >(), std::declval< T >()), void())
標準ソート関数のラッパ
Definition: sort.hpp:46
auto zip(Cs &&...lists)
複数のコンテナから、タプルのコンテナを作る
Definition: zip.hpp:43
auto unzip(CT &&list_tuple) -> R
タプルのコンテナから、指定したindexのコンテナを取り出す
Definition: zip.hpp:85
auto eval(F &&f, Args &&...args) -> decltype(f(std::forward< Args >(args)...))
Definition: eval.hpp:37
auto sort(C &container, F &&binary_op) -> decltype(impl::eval(std::forward< F >(binary_op), std::declval< T >(), std::declval< T >()), void())
Definition: sort.hpp:61
Definition: array.hpp:15
C seqn(T1 st, T2 d, uint n)
等差数列
Definition: rest.hpp:36
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >