sort.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef BOOST_UNITS_DETAIL_SORT_HPP
  11. #define BOOST_UNITS_DETAIL_SORT_HPP
  12. #include <boost/mpl/size.hpp>
  13. #include <boost/mpl/begin.hpp>
  14. #include <boost/mpl/next.hpp>
  15. #include <boost/mpl/deref.hpp>
  16. #include <boost/mpl/push_front.hpp>
  17. #include <boost/mpl/less.hpp>
  18. #include <boost/units/dimensionless_type.hpp>
  19. #include <boost/units/detail/dimension_list.hpp>
  20. namespace boost {
  21. namespace units {
  22. namespace detail {
  23. template<int N>
  24. struct insertion_sort_insert;
  25. template<bool is_greater>
  26. struct insertion_sort_comparison_impl;
  27. // have to recursively add the element to the next sequence.
  28. template<>
  29. struct insertion_sort_comparison_impl<true> {
  30. template<class Begin, int N, class T>
  31. struct apply {
  32. typedef list<
  33. typename Begin::item,
  34. typename insertion_sort_insert<N - 1>::template apply<
  35. typename Begin::next,
  36. T
  37. >::type
  38. > type;
  39. };
  40. };
  41. // prepend the current element
  42. template<>
  43. struct insertion_sort_comparison_impl<false> {
  44. template<class Begin, int N, class T>
  45. struct apply {
  46. typedef list<T, Begin> type;
  47. };
  48. };
  49. template<int N>
  50. struct insertion_sort_insert {
  51. template<class Begin, class T>
  52. struct apply {
  53. typedef typename insertion_sort_comparison_impl<mpl::less<typename Begin::item, T>::value>::template apply<
  54. Begin,
  55. N,
  56. T
  57. >::type type;
  58. };
  59. };
  60. template<>
  61. struct insertion_sort_insert<0> {
  62. template<class Begin, class T>
  63. struct apply {
  64. typedef list<T, dimensionless_type> type;
  65. };
  66. };
  67. template<int N>
  68. struct insertion_sort_impl {
  69. template<class Begin>
  70. struct apply {
  71. typedef typename insertion_sort_impl<N - 1>::template apply<typename Begin::next>::type next;
  72. typedef typename insertion_sort_insert<(next::size::value)>::template apply<next, typename Begin::item>::type type;
  73. };
  74. };
  75. template<>
  76. struct insertion_sort_impl<0> {
  77. template<class Begin>
  78. struct apply {
  79. typedef dimensionless_type type;
  80. };
  81. };
  82. template<class T>
  83. struct insertion_sort
  84. {
  85. typedef typename insertion_sort_impl<T::size::value>::template apply<T>::type type;
  86. };
  87. } // namespace detail
  88. } // namespace units
  89. } // namespace boost
  90. #endif