tinympl  0.2
mini MPL library for C++11
min_element.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_MIN_ELEMENT_HPP
14 #define TINYMPL_VARIADIC_MIN_ELEMENT_HPP
15 
16 #include <tinympl/variadic/at.hpp>
17 #include <type_traits>
18 #include <cstddef>
19 
20 namespace tinympl {
21 namespace variadic {
22 
35 template<template<class ... > class Cmp, class ... Args> struct min_element;
36 
37 namespace detail {
38 
39 template<template<class ...> class Comp, class ... > struct min_element_impl;
40 template<template<class ...> class Comp, class Head, class ... Tail> struct
41 min_element_impl<Comp, Head, Tail...> {
42 private:
43  enum {
44  next_min = min_element_impl<Comp, Tail...>::type::value
45  };
46 
47  enum {
48  this_min = ! Comp<at_t<next_min, Tail...>, Head>::type::value
49  };
50 
51 public:
52  typedef std::integral_constant < std::size_t,
53  ( this_min ?
54  0 :
55  next_min + 1 ) > type;
56 };
57 
58 template<template<class ... > class Comp, class Head> struct
59 min_element_impl<Comp, Head> {
60  typedef std::integral_constant<std::size_t, 0> type;
61 };
62 
63 } // namespace detail
64 
65 template<template<class ...> class Comp, class ... Args>
66 struct min_element :
67  detail::min_element_impl<Comp, Args...>::type
68 {};
69 
70 } // namespace variadic
71 } // namespace tinympl
72 
73 #endif // TINYMPL_VARIADIC_MIN_ELEMENT_HPP
Compute the index of the smallest element in a sequence.
Definition: min_element.hpp:35