tinympl  0.2
mini MPL library for C++11
sort.hpp
1 // Copyright (C) 2013, Ennio Barbaro.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://sbabbi.github.io/tinympl for documentation.
8 //
9 // You are welcome to contact the author at:
10 // enniobarbaro@gmail.com
11 //
12 
13 #ifndef TINYMPL_VARIADIC_SORT_HPP
14 #define TINYMPL_VARIADIC_SORT_HPP
15 
16 #include <tinympl/variadic/min_element.hpp>
17 #include <tinympl/variadic/erase.hpp>
18 #include <tinympl/variadic/at.hpp>
19 
20 namespace tinympl {
21 namespace variadic {
22 
38 template<template<class ... > class Cmp,
39  template<class ...> class Out,
40  class ... Args> struct sort;
41 
42 template<template<class ... > class Comp,
43  template<class ...> class Out,
44  class ... Args>
45 struct sort {
46 private:
47  template<class ... OtherArgs>
48  using next_sort = sort<Comp, Out, OtherArgs...>;
49 
50  enum {this_min = min_element<Comp, Args...>::type::value };
51  typedef typename erase < this_min, this_min + 1, next_sort, Args ... >::type
52  next;
53 
54  template<class ... CopiedElements>
55  struct impl {
56  typedef typename next::template impl<CopiedElements...,
57  typename at<this_min, Args...>::type
58  >::type type;
59  };
60 
61  template<template<class ... > class , template<class ...> class, class ...>
62  friend struct sort;
63 
64 public:
65  typedef typename impl<>::type type;
66 };
67 
68 template<template<class ... > class Comp, template<class ...> class Out>
69 struct sort<Comp, Out> {
70 private:
71  template<class ... CopiedElements>
72  struct impl {
73  typedef Out<CopiedElements...> type;
74  };
75 
76  template<template<class ... > class , template<class ...> class, class ...>
77  friend struct sort;
78 
79 public:
80  typedef typename impl<>::type type;
81 };
82 
83 } // namespace variadic
84 } // namespace tinympl
85 
86 #endif // TINYMPL_VARIADIC_SORT_HPP
Sort the input sequence according to a given comparison function.
Definition: sort.hpp:41
Extract the i-th element of a variadic template.
Definition: at.hpp:27
Sort the input sequence according to a given comparison function.
Definition: sort.hpp:40
Compute the index of the smallest element in a sequence.
Definition: min_element.hpp:35
Produce an output sequence from a variadic template by removin the elements in the given range...
Definition: erase.hpp:30