1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
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)
11 #ifndef BOOST_UNITS_DIMENSION_IMPL_HPP
12 #define BOOST_UNITS_DIMENSION_IMPL_HPP
14 #include <boost/mpl/begin_end.hpp>
15 #include <boost/mpl/deref.hpp>
16 #include <boost/mpl/if.hpp>
17 #include <boost/mpl/list.hpp>
18 #include <boost/mpl/next.hpp>
19 #include <boost/mpl/size.hpp>
20 #include <boost/mpl/less.hpp>
22 #include <boost/units/config.hpp>
23 #include <boost/units/dimensionless_type.hpp>
24 #include <boost/units/static_rational.hpp>
25 #include <boost/units/units_fwd.hpp>
26 #include <boost/units/detail/dimension_list.hpp>
27 #include <boost/units/detail/push_front_if.hpp>
28 #include <boost/units/detail/push_front_or_add.hpp>
31 /// \brief Core class and metaprogramming utilities for compile-time dimensional analysis.
40 struct insertion_sort_dims_insert;
42 template<bool is_greater>
43 struct insertion_sort_dims_comparison_impl;
45 // have to recursively add the element to the next sequence.
47 struct insertion_sort_dims_comparison_impl<true> {
48 template<class Begin, int N, class T>
52 typename insertion_sort_dims_insert<N - 1>::template apply<
60 // either prepend the current element or join it to
61 // the first remaining element of the sequence.
63 struct insertion_sort_dims_comparison_impl<false> {
64 template<class Begin, int N, class T>
66 typedef typename push_front_or_add<Begin, T>::type type;
71 struct insertion_sort_dims_insert {
72 template<class Begin, class T>
74 typedef typename insertion_sort_dims_comparison_impl<mpl::less<typename Begin::item, T>::value>::template apply<
83 struct insertion_sort_dims_insert<0> {
84 template<class Begin, class T>
86 typedef list<T, dimensionless_type> type;
91 struct insertion_sort_dims_mpl_sequence {
94 typedef typename insertion_sort_dims_mpl_sequence<N - 1>::template apply<typename mpl::next<Begin>::type>::type next;
95 typedef typename insertion_sort_dims_insert<(next::size::value)>::template apply<next, typename mpl::deref<Begin>::type>::type type;
100 struct insertion_sort_dims_mpl_sequence<0> {
101 template<class Begin>
103 typedef dimensionless_type type;
108 struct insertion_sort_dims_impl {
109 template<class Begin>
111 typedef typename insertion_sort_dims_impl<N - 1>::template apply<typename Begin::next>::type next;
112 typedef typename insertion_sort_dims_insert<(next::size::value)>::template apply<next, typename Begin::item>::type type;
117 struct insertion_sort_dims_impl<0> {
118 template<class Begin>
120 typedef dimensionless_type type;
127 typedef typename insertion_sort_dims_mpl_sequence<mpl::size<T>::value>::template apply<typename mpl::begin<T>::type>::type type;
131 template<class T, class Next>
132 struct sort_dims<list<T, Next> >
134 typedef typename insertion_sort_dims_impl<list<T, Next>::size::value>::template apply<list<T, Next> >::type type;
137 /// sorted sequences can be merged in linear time
138 template<bool less, bool greater>
139 struct merge_dimensions_func;
141 template<int N1, int N2>
142 struct merge_dimensions_impl;
145 struct merge_dimensions_func<true, false>
147 template<typename Begin1, typename Begin2, int N1, int N2>
151 typename Begin1::item,
152 typename merge_dimensions_impl<N1 - 1, N2>::template apply<
153 typename Begin1::next,
161 struct merge_dimensions_func<false, true> {
162 template<typename Begin1, typename Begin2, int N1, int N2>
166 typename Begin2::item,
167 typename merge_dimensions_impl<N2 - 1, N1>::template apply<
168 typename Begin2::next,
176 struct merge_dimensions_func<false, false> {
177 template<typename Begin1, typename Begin2, int N1, int N2>
180 typedef typename mpl::plus<typename Begin1::item, typename Begin2::item>::type combined;
181 typedef typename push_front_if<!is_empty_dim<combined>::value>::template apply<
182 typename merge_dimensions_impl<N1 - 1, N2 - 1>::template apply<
183 typename Begin1::next,
184 typename Begin2::next
191 template<int N1, int N2>
192 struct merge_dimensions_impl {
193 template<typename Begin1, typename Begin2>
196 typedef typename Begin1::item dim1;
197 typedef typename Begin2::item dim2;
199 typedef typename merge_dimensions_func<(mpl::less<dim1,dim2>::value == true),
200 (mpl::less<dim2,dim1>::value == true)>::template apply<
209 template<typename Sequence1, typename Sequence2>
210 struct merge_dimensions
212 typedef typename detail::merge_dimensions_impl<Sequence1::size::value,
213 Sequence2::size::value>::template
221 struct iterator_to_list
223 template<typename Begin>
227 typename Begin::item,
228 typename iterator_to_list<N - 1>::template apply<
236 struct iterator_to_list<0>
238 template<typename Begin>
240 typedef dimensionless_type type;
245 struct merge_dimensions_impl<N, 0>
247 template<typename Begin1, typename Begin2>
250 typedef typename iterator_to_list<N>::template apply<Begin1>::type type;
255 struct merge_dimensions_impl<0, N>
257 template<typename Begin1, typename Begin2>
260 typedef typename iterator_to_list<N>::template apply<Begin2>::type type;
265 struct merge_dimensions_impl<0, 0>
267 template<typename Begin1, typename Begin2>
270 typedef dimensionless_type type;
275 struct static_inverse_impl
277 template<typename Begin>
280 typename mpl::negate<typename Begin::item>::type,
281 typename static_inverse_impl<N - 1>::template apply<
289 struct static_inverse_impl<0>
291 template<typename Begin>
294 typedef dimensionless_type type;
299 struct static_power_impl
301 template<typename Begin, typename Ex>
305 typename mpl::times<typename Begin::item, Ex>::type,
306 typename detail::static_power_impl<N - 1>::template apply<typename Begin::next, Ex>::type
312 struct static_power_impl<0>
314 template<typename Begin, typename Ex>
317 typedef dimensionless_type type;
322 struct static_root_impl {
323 template<class Begin, class Ex>
326 typename mpl::divides<typename Begin::item, Ex>::type,
327 typename detail::static_root_impl<N - 1>::template apply<typename Begin::next, Ex>::type
333 struct static_root_impl<0> {
334 template<class Begin, class Ex>
337 typedef dimensionless_type type;
341 } // namespace detail
347 #endif // BOOST_UNITS_DIMENSION_IMPL_HPP