|
Boost-Commit : |
From: steven_watanabe_at_[hidden]
Date: 2007-06-13 20:18:42
Author: steven_watanabe
Date: 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
New Revision: 7037
URL: http://svn.boost.org/trac/boost/changeset/7037
Log:
refactoring
Added:
sandbox/units/boost/units/detail/push_front_or_add.hpp
Text files modified:
sandbox/units/boost/units/derived_dimension.hpp | 154 +++++++++++++++++++++++++
sandbox/units/boost/units/detail/dimension_impl.hpp | 240 +++++++--------------------------------
sandbox/units/boost/units/detail/sort.hpp | 71 +++++++++--
sandbox/units/boost/units/dimension.hpp | 10 -
sandbox/units/boost/units/heterogeneous_system.hpp | 44 -------
5 files changed, 261 insertions(+), 258 deletions(-)
Modified: sandbox/units/boost/units/derived_dimension.hpp
==============================================================================
--- sandbox/units/boost/units/derived_dimension.hpp (original)
+++ sandbox/units/boost/units/derived_dimension.hpp 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
@@ -45,6 +45,160 @@
dim< DT8,static_rational<E8> > > >::type type;
};
+/// INTERNAL ONLY
+template<class DT1,long E1>
+struct derived_dimension<
+ DT1, E1,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> > > >::type type;
+};
+
+/// INTERNAL ONLY
+template<class DT1,long E1,
+ class DT2,long E2>
+struct derived_dimension<
+ DT1, E1,
+ DT2, E2,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> >,
+ dim< DT2,static_rational<E2> > > >::type type;
+};
+
+/// INTERNAL ONLY
+template<class DT1,long E1,
+ class DT2,long E2,
+ class DT3,long E3>
+struct derived_dimension<
+ DT1, E1,
+ DT2, E2,
+ DT3, E3,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> >,
+ dim< DT2,static_rational<E2> >,
+ dim< DT3,static_rational<E3> > > >::type type;
+};
+
+/// INTERNAL ONLY
+template<class DT1,long E1,
+ class DT2,long E2,
+ class DT3,long E3,
+ class DT4,long E4>
+struct derived_dimension<
+ DT1, E1,
+ DT2, E2,
+ DT3, E3,
+ DT4, E4,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> >,
+ dim< DT2,static_rational<E2> >,
+ dim< DT3,static_rational<E3> >,
+ dim< DT4,static_rational<E4> > > >::type type;
+};
+
+/// INTERNAL ONLY
+template<class DT1,long E1,
+ class DT2,long E2,
+ class DT3,long E3,
+ class DT4,long E4,
+ class DT5,long E5>
+struct derived_dimension<
+ DT1, E1,
+ DT2, E2,
+ DT3, E3,
+ DT4, E4,
+ DT5, E5,
+ dimensionless_type,0,
+ dimensionless_type,0,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> >,
+ dim< DT2,static_rational<E2> >,
+ dim< DT3,static_rational<E3> >,
+ dim< DT4,static_rational<E4> >,
+ dim< DT5,static_rational<E5> > > >::type type;
+};
+
+/// INTERNAL ONLY
+template<class DT1,long E1,
+ class DT2,long E2,
+ class DT3,long E3,
+ class DT4,long E4,
+ class DT5,long E5,
+ class DT6,long E6>
+struct derived_dimension<
+ DT1, E1,
+ DT2, E2,
+ DT3, E3,
+ DT4, E4,
+ DT5, E5,
+ DT6, E6,
+ dimensionless_type,0,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> >,
+ dim< DT2,static_rational<E2> >,
+ dim< DT3,static_rational<E3> >,
+ dim< DT4,static_rational<E4> >,
+ dim< DT5,static_rational<E5> >,
+ dim< DT6,static_rational<E6> > > >::type type;
+};
+
+/// INTERNAL ONLY
+template<class DT1,long E1,
+ class DT2,long E2,
+ class DT3,long E3,
+ class DT4,long E4,
+ class DT5,long E5,
+ class DT6,long E6,
+ class DT7,long E7>
+struct derived_dimension<
+ DT1, E1,
+ DT2, E2,
+ DT3, E3,
+ DT4, E4,
+ DT5, E5,
+ DT6, E6,
+ DT7, E7,
+ dimensionless_type,0>
+{
+ typedef typename
+ make_dimension_list< mpl::list< dim< DT1,static_rational<E1> >,
+ dim< DT2,static_rational<E2> >,
+ dim< DT3,static_rational<E3> >,
+ dim< DT4,static_rational<E4> >,
+ dim< DT5,static_rational<E5> >,
+ dim< DT6,static_rational<E6> >,
+ dim< DT7,static_rational<E7> > > >::type type;
+};
+
} // namespace units
} // namespace boost
Modified: sandbox/units/boost/units/detail/dimension_impl.hpp
==============================================================================
--- sandbox/units/boost/units/detail/dimension_impl.hpp (original)
+++ sandbox/units/boost/units/detail/dimension_impl.hpp 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
@@ -25,6 +25,7 @@
#include <boost/units/static_rational.hpp>
#include <boost/units/units_fwd.hpp>
#include <boost/units/detail/push_front_if.hpp>
+#include <boost/units/detail/push_front_or_add.hpp>
/// \file
/// \brief Core class and metaprogramming utilities for compile-time dimensional analysis.
@@ -35,265 +36,118 @@
namespace detail {
-template<bool remove>
-struct strip_zero_dims_func;
+template<bool second_is_less>
+struct sort_dims_conditional_swap;
template<>
-struct strip_zero_dims_func<true>
+struct sort_dims_conditional_swap<true>
{
- template<typename Sequence, typename T>
+ template<class T0, class T1>
struct apply
{
- typedef Sequence type;
+ typedef T1 first;
+ typedef T0 second;
};
};
template<>
-struct strip_zero_dims_func<false>
+struct sort_dims_conditional_swap<false>
{
- template<typename Sequence, typename T>
+ template<class T0, class T1>
struct apply
{
- typedef typename mpl::push_front<Sequence, T>::type type;
+ typedef T0 first;
+ typedef T1 second;
};
};
template<int N>
-struct strip_zero_dims_impl
+struct sort_dims_pass_impl
{
- template<typename Begin>
- struct apply {
- typedef typename strip_zero_dims_func<
- boost::is_same<typename boost::mpl::deref<Begin>::type::tag_type, static_rational<0> >::value
- >::template apply<
- typename strip_zero_dims_impl<N-1>::template apply<
- typename boost::mpl::next<Begin>::type
- >::type,
- typename boost::mpl::deref<Begin>::type
- >::type type;
- };
-};
-
-/// remove all dimensionless dimensions
-template<typename Seq>
-struct strip_zero_dims
-{
- typedef typename strip_zero_dims_impl<mpl::size<Seq>::value>::template apply<typename mpl::begin<Seq>::type>::type type;
-};
-
-/// a more efficient type sequence than @c mpl::list
-struct sort_dims_list_end
-{
- enum { size = 0 };
-};
-
-template<typename T, typename Next = sort_dims_list_end>
-struct sort_dims_list
-{
- typedef T item;
- typedef Next next;
- enum { size = Next::size + 1 };
-};
-
-/// add an instantiation of dim to Sequence.
-template<bool>
-struct sort_dims_insert_impl;
-
-template<>
-struct sort_dims_insert_impl<true>
-{
- template<typename Sequence, typename T>
+ template<class Begin, class Current>
struct apply
{
- typedef sort_dims_list<
- typename mpl::plus<T, typename Sequence::item>::type,
- typename Sequence::next
- > type;
+ typedef typename mpl::deref<Begin>::type val;
+ typedef typename sort_dims_conditional_swap<mpl::less<val, Current>::value>::template apply<Current, val> pair;
+ typedef typename sort_dims_pass_impl<N-1>::template apply<typename mpl::next<Begin>::type, typename pair::second> next;
+ typedef typename push_front_or_add<typename next::type, typename pair::first>::type type;
+ enum { value = next::value || mpl::less<val, Current>::value };
};
};
template<>
-struct sort_dims_insert_impl<false>
+struct sort_dims_pass_impl<0>
{
- template<typename Sequence, typename T>
+ template<class Begin, class Current>
struct apply
{
- typedef typename boost::mpl::if_<boost::is_same<typename Sequence::item::value_type, static_rational<0> >,
- typename Sequence::next,
- Sequence
- >::type type1;
- typedef sort_dims_list<T, type1> type;
+ typedef typename mpl::push_front<dimensionless_type, Current>::type type;
+ enum { value = false };
};
};
-template<typename Sequence, typename T>
-struct sort_dims_insert
-{
- typedef typename sort_dims_insert_impl<boost::is_same<typename T::tag_type, typename Sequence::item::tag_type>::value>::template apply<
- Sequence,
- T
- >::type type;
-};
-
-template<typename T>
-struct sort_dims_insert<sort_dims_list_end, T>
-{
- typedef sort_dims_list<T> type;
-};
-
-/// a pair of output sequences
-template<typename Out1, typename Out2>
-struct partition_dims_state
-{
- typedef Out1 out1;
- typedef Out2 out2;
-};
-
-/// determine which sequence to insert in
-template<bool insert_in_first>
-struct partition_dims_state_insert;
-
-template<>
-struct partition_dims_state_insert<true>
-{
- template<typename State, typename T>
- struct apply
- {
- typedef partition_dims_state<sort_dims_list<T, typename State::out1>, typename State::out2> type;
- };
-};
+template<bool>
+struct sort_dims_impl;
template<>
-struct partition_dims_state_insert<false>
-{
- template<typename State, typename T>
- struct apply
- {
- typedef partition_dims_state<typename State::out1, sort_dims_list<T, typename State::out2> > type;
- };
-};
-
-/// quicksort uses recusive partitioning
-template<int N>
-struct partition_dims_forward_impl
+struct sort_dims_impl<true>
{
- template<typename Begin, typename State, typename Value>
+ template<class T>
struct apply
{
- typedef typename partition_dims_forward_impl<N - 1>::template apply<
- typename Begin::next,
- typename partition_dims_state_insert<mpl::less<typename Begin::item, Value>::value>::template apply<State, typename Begin::item>::type,
- Value
- >::type type;
+ typedef typename mpl::begin<T>::type begin;
+ typedef typename sort_dims_pass_impl<mpl::size<T>::value - 1>::template apply<
+ typename mpl::next<begin>::type,
+ typename mpl::deref<begin>::type
+ > single_pass;
+ typedef typename sort_dims_impl<(single_pass::value)>::template apply<typename single_pass::type>::type type;
};
};
-/// terminate the recursion
template<>
-struct partition_dims_forward_impl<0>
+struct sort_dims_impl<false>
{
- template<typename Begin, typename State, typename Value>
+ template<class T>
struct apply
{
- typedef State type;
+ typedef T type;
};
};
-template<typename Sequence, typename Output>
-struct sort_dims_forward;
-
-/// primary template for the implementation of sort
template<int N>
-struct sort_dims_forward_impl
+struct sort_dims_one_or_zero
{
- template<typename Begin, typename Output>
+ template<class T>
struct apply
{
- typedef typename
- partition_dims_forward_impl<N - 1>::template apply<
- typename Begin::next,
- partition_dims_state<sort_dims_list_end, sort_dims_list_end>,
- typename Begin::item
- >::type partitioned;
- //we're using push_front so we have to push the elements in reverse order
- typedef typename sort_dims_forward<typename partitioned::out2, Output>::type step1;
- typedef typename sort_dims_insert<step1, typename Begin::item>::type step2;
- typedef typename sort_dims_forward<typename partitioned::out1, step2>::type type;
+ typedef typename sort_dims_impl<true>::template apply<T>::type type;
};
};
-/// sorting a zero element sequence returns a zero element sequence
template<>
-struct sort_dims_forward_impl<0>
+struct sort_dims_one_or_zero<0>
{
- template<typename Begin, typename Output>
+ template<class T>
struct apply
{
- typedef Output type;
- };
-};
-
-/// a single element sequence is trivial too
-template<>
-struct sort_dims_forward_impl<1>
-{
- template<typename Begin, typename Output>
- struct apply
- {
- typedef typename sort_dims_insert<Output, typename Begin::item>::type type;
- };
-};
-
-/// basic quicksort (@c mpl::sort is horribly inefficient)
-template<typename Sequence, typename Output>
-struct sort_dims_forward
-{
- typedef typename sort_dims_forward_impl<Sequence::size>::template apply<
- Sequence,
- Output
- >::type type;
-};
-
-/// convert an mpl sequence to a @c sort_dims_list removing @c dimensionless_type in the process
-template<int N>
-struct remove_dimensionless
-{
- template<typename Begin, typename Out>
- struct apply
- {
- typedef typename boost::mpl::deref<Begin>::type deref;
- typedef typename boost::mpl::if_<boost::is_same<typename deref::tag_type, dimensionless_type>,
- Out,
- sort_dims_list<deref, Out>
- >::type type1;
- typedef typename remove_dimensionless<N - 1>::template
- apply<typename boost::mpl::next<Begin>::type, type1>::type type;
+ typedef dimensionless_type type;
};
};
-/// terminate the recursion
template<>
-struct remove_dimensionless<0>
+struct sort_dims_one_or_zero<1>
{
- template<typename Begin, typename Out>
+ template<class T>
struct apply
{
- typedef Out type;
+ typedef typename mpl::push_front<dimensionless_type, typename mpl::front<T>::type>::type type;
};
};
-/// when we're finished with the computation we have to get back an @c mpl::list
-template<typename Sequence>
-struct sort_dims_to_mpl_list
-{
- typedef typename boost::mpl::push_front<typename sort_dims_to_mpl_list<typename Sequence::next>::type,
- typename Sequence::item>::type type;
-};
-
-template<>
-struct sort_dims_to_mpl_list<sort_dims_list_end>
+template<class T>
+struct sort_dims
{
- typedef dimensionless_type type;
+ typedef typename sort_dims_one_or_zero<mpl::size<T>::value>::template apply<T>::type type;
};
/// sorted sequences can be merged in linear time
Added: sandbox/units/boost/units/detail/push_front_or_add.hpp
==============================================================================
--- (empty file)
+++ sandbox/units/boost/units/detail/push_front_or_add.hpp 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
@@ -0,0 +1,81 @@
+// mcs::units - A C++ library for zero-overhead dimensional analysis and
+// unit/quantity manipulation and conversion
+//
+// Copyright (C) 2003-2007 Matthias Christian Schabel
+// Copyright (C) 2007 Steven Watanabe
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_UNITS_DETAIL_PUSH_FRONT_OR_ADD_HPP
+#define BOOST_UNITS_DETAIL_PUSH_FRONT_OR_ADD_HPP
+
+#include <boost/mpl/plus.hpp>
+#include <boost/mpl/front.hpp>
+#include <boost/mpl/push_front.hpp>
+#include <boost/mpl/pop_front.hpp>
+#include <boost/type_traits/is_same.hpp>
+
+#include <boost/units/units_fwd.hpp>
+#include <boost/units/detail/push_front_if.hpp>
+
+namespace boost {
+
+namespace units {
+
+namespace detail {
+
+template<class T>
+struct is_empty_dim;
+
+/// add an instantiation of dim to Sequence.
+template<bool>
+struct push_front_or_add_impl;
+
+template<>
+struct push_front_or_add_impl<true>
+{
+ template<typename Sequence, typename T>
+ struct apply
+ {
+ typedef typename mpl::plus<T, typename mpl::front<Sequence>::type>::type item;
+ typedef typename push_front_if<!is_empty_dim<item>::value>::template apply<
+ typename mpl::pop_front<Sequence>::type,
+ item
+ > type;
+ };
+};
+
+template<>
+struct push_front_or_add_impl<false>
+{
+ template<typename Sequence, typename T>
+ struct apply
+ {
+ typedef typename mpl::push_front<Sequence, T>::type type;
+ };
+};
+
+template<typename Sequence, typename T>
+struct push_front_or_add
+{
+ typedef typename push_front_or_add_impl<boost::is_same<typename T::tag_type, typename mpl::front<Sequence>::type::tag_type>::value>::template apply<
+ Sequence,
+ T
+ >::type type;
+};
+
+template<typename T>
+struct push_front_or_add<dimensionless_type, T>
+{
+ typedef typename mpl::push_front<dimensionless_type, T>::type type;
+};
+
+} // namespace detail
+
+} // namespace units
+
+} // namespace boost
+
+#endif
Modified: sandbox/units/boost/units/detail/sort.hpp
==============================================================================
--- sandbox/units/boost/units/detail/sort.hpp (original)
+++ sandbox/units/boost/units/detail/sort.hpp 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
@@ -29,27 +29,33 @@
struct bubble_sort_conditional_swap;
template<>
-struct bubble_sort_conditional_swap<true> {
+struct bubble_sort_conditional_swap<true>
+{
template<class T0, class T1>
- struct apply {
+ struct apply
+ {
typedef T1 first;
typedef T0 second;
};
};
template<>
-struct bubble_sort_conditional_swap<false> {
+struct bubble_sort_conditional_swap<false>
+{
template<class T0, class T1>
- struct apply {
+ struct apply
+ {
typedef T0 first;
typedef T1 second;
};
};
template<int N>
-struct bubble_sort_pass_impl {
+struct bubble_sort_pass_impl
+{
template<class Begin, class Current>
- struct apply {
+ struct apply
+ {
typedef typename mpl::deref<Begin>::type val;
typedef typename bubble_sort_conditional_swap<mpl::less<val, Current>::value>::template apply<Current, val> pair;
typedef typename bubble_sort_pass_impl<N-1>::template apply<typename mpl::next<Begin>::type, typename pair::second> next;
@@ -59,9 +65,11 @@
};
template<>
-struct bubble_sort_pass_impl<0> {
+struct bubble_sort_pass_impl<0>
+{
template<class Begin, class Current>
- struct apply {
+ struct apply
+ {
typedef typename mpl::push_front<dimensionless_type, Current>::type type;
enum { value = false };
};
@@ -71,9 +79,11 @@
struct bubble_sort_impl;
template<>
-struct bubble_sort_impl<true> {
+struct bubble_sort_impl<true>
+{
template<class T>
- struct apply {
+ struct apply
+ {
typedef typename mpl::begin<T>::type begin;
typedef typename bubble_sort_pass_impl<mpl::size<T>::value - 1>::template apply<
typename mpl::next<begin>::type,
@@ -84,16 +94,49 @@
};
template<>
-struct bubble_sort_impl<false> {
+struct bubble_sort_impl<false>
+{
template<class T>
- struct apply {
+ struct apply
+ {
typedef T type;
};
};
+template<int N>
+struct bubble_sort_one_or_zero
+{
+ template<class T>
+ struct apply
+ {
+ typedef typename bubble_sort_impl<true>::template apply<T>::type type;
+ };
+};
+
+template<>
+struct bubble_sort_one_or_zero<0>
+{
+ template<class T>
+ struct apply
+ {
+ typedef dimensionless_type type;
+ };
+};
+
+template<>
+struct bubble_sort_one_or_zero<1>
+{
+ template<class T>
+ struct apply
+ {
+ typedef typename mpl::push_front<dimensionless_type, typename mpl::front<T>::type>::type type;
+ };
+};
+
template<class T>
-struct bubble_sort {
- typedef typename bubble_sort_impl<((mpl::size<T>::value) > 1)>::template apply<T>::type type;
+struct bubble_sort
+{
+ typedef typename bubble_sort_one_or_zero<mpl::size<T>::value>::template apply<T>::type type;
};
} // namespace detail
Modified: sandbox/units/boost/units/dimension.hpp
==============================================================================
--- sandbox/units/boost/units/dimension.hpp (original)
+++ sandbox/units/boost/units/dimension.hpp 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
@@ -33,19 +33,13 @@
namespace units {
/// Reduce dimension list to cardinal form. This algorithm collapses duplicate unit
-/// tags, strips dimensionless tags, and sorts the resulting list. Sorting of homogeneous
-/// units is by the tag ordinal value. Heterogeneous units are sorted by...
+/// tags and sorts the resulting list by the tag ordinal value.
/// Dimension lists that resolve to the same dimension are guaranteed to be
/// represented by an identical type.
template<typename Seq>
struct make_dimension_list
{
- typedef typename detail::remove_dimensionless<boost::mpl::size<Seq>::value>::template
- apply<typename boost::mpl::begin<Seq>::type, detail::sort_dims_list_end>::type sequence;
-
- typedef typename detail::sort_dims_forward<sequence, detail::sort_dims_list_end>::type type2;
-
- typedef typename detail::sort_dims_to_mpl_list<type2>::type type;
+ typedef typename detail::sort_dims<Seq>::type type;
};
/// Raise a dimension list to a scalar power.
Modified: sandbox/units/boost/units/heterogeneous_system.hpp
==============================================================================
--- sandbox/units/boost/units/heterogeneous_system.hpp (original)
+++ sandbox/units/boost/units/heterogeneous_system.hpp 2007-06-13 20:18:41 EDT (Wed, 13 Jun 2007)
@@ -31,6 +31,7 @@
#include <boost/units/scaled_base_unit.hpp>
#include <boost/units/units_fwd.hpp>
#include <boost/units/detail/push_front_if.hpp>
+#include <boost/units/detail/push_front_or_add.hpp>
#include <boost/units/detail/linear_algebra.hpp>
namespace boost {
@@ -292,49 +293,6 @@
namespace detail {
-/// add an instantiation of dim to Sequence.
-template<bool>
-struct push_front_or_add_impl;
-
-template<>
-struct push_front_or_add_impl<true>
-{
- template<typename Sequence, typename T>
- struct apply
- {
- typedef typename mpl::plus<T, typename mpl::front<Sequence>::type>::type item;
- typedef typename push_front_if<!is_empty_dim<item>::value>::template apply<
- typename mpl::pop_front<Sequence>::type,
- item
- > type;
- };
-};
-
-template<>
-struct push_front_or_add_impl<false>
-{
- template<typename Sequence, typename T>
- struct apply
- {
- typedef typename mpl::push_front<Sequence, T>::type type;
- };
-};
-
-template<typename Sequence, typename T>
-struct push_front_or_add
-{
- typedef typename push_front_or_add_impl<boost::is_same<typename T::tag_type, typename mpl::front<Sequence>::type::tag_type>::value>::template apply<
- Sequence,
- T
- >::type type;
-};
-
-template<typename T>
-struct push_front_or_add<dimensionless_type, T>
-{
- typedef typename mpl::push_front<dimensionless_type, T>::type type;
-};
-
template<int N>
struct unscale_heterogeneous_system_impl
{
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk