1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and 2 // unit/quantity manipulation and conversion 3 // 4 // Copyright (C) 2003-2008 Matthias Christian Schabel 5 // Copyright (C) 2008 Steven Watanabe 6 // 7 // Distributed under the Boost Software License, Version 1.0. (See 8 // accompanying file LICENSE_1_0.txt or copy at 9 // http://www.boost.org/LICENSE_1_0.txt) 10 11 #ifndef BOOST_UNITS_DETAIL_SORT_HPP 12 #define BOOST_UNITS_DETAIL_SORT_HPP 13 14 #include <boost/mpl/size.hpp> 15 #include <boost/mpl/begin.hpp> 16 #include <boost/mpl/next.hpp> 17 #include <boost/mpl/deref.hpp> 18 #include <boost/mpl/push_front.hpp> 19 #include <boost/mpl/less.hpp> 20 21 #include <boost/units/dimensionless_type.hpp> 22 #include <boost/units/detail/dimension_list.hpp> 23 24 namespace boost { 25 26 namespace units { 27 28 namespace detail { 29 30 template<int N> 31 struct insertion_sort_insert; 32 33 template<bool is_greater> 34 struct insertion_sort_comparison_impl; 35 36 // have to recursively add the element to the next sequence. 37 template<> 38 struct insertion_sort_comparison_impl<true> { 39 template<class Begin, int N, class T> 40 struct apply { 41 typedef list< 42 typename Begin::item, 43 typename insertion_sort_insert<N - 1>::template apply< 44 typename Begin::next, 45 T 46 >::type 47 > type; 48 }; 49 }; 50 51 // prepend the current element 52 template<> 53 struct insertion_sort_comparison_impl<false> { 54 template<class Begin, int N, class T> 55 struct apply { 56 typedef list<T, Begin> type; 57 }; 58 }; 59 60 template<int N> 61 struct insertion_sort_insert { 62 template<class Begin, class T> 63 struct apply { 64 typedef typename insertion_sort_comparison_impl<mpl::less<typename Begin::item, T>::value>::template apply< 65 Begin, 66 N, 67 T 68 >::type type; 69 }; 70 }; 71 72 template<> 73 struct insertion_sort_insert<0> { 74 template<class Begin, class T> 75 struct apply { 76 typedef list<T, dimensionless_type> type; 77 }; 78 }; 79 80 template<int N> 81 struct insertion_sort_impl { 82 template<class Begin> 83 struct apply { 84 typedef typename insertion_sort_impl<N - 1>::template apply<typename Begin::next>::type next; 85 typedef typename insertion_sort_insert<(next::size::value)>::template apply<next, typename Begin::item>::type type; 86 }; 87 }; 88 89 template<> 90 struct insertion_sort_impl<0> { 91 template<class Begin> 92 struct apply { 93 typedef dimensionless_type type; 94 }; 95 }; 96 97 template<class T> 98 struct insertion_sort 99 { 100 typedef typename insertion_sort_impl<T::size::value>::template apply<T>::type type; 101 }; 102 103 } // namespace detail 104 105 } // namespace units 106 107 } // namespace boost 108 109 #endif 110