From d888228aa9894e3b597f15a7c713e61d7972c098 Mon Sep 17 00:00:00 2001 From: Eric Friedman Date: Wed, 22 Oct 2003 01:03:06 +0000 Subject: [PATCH] Migrated from sandbox. [SVN r20450] --- include/boost/mpl/aux_/partition_op.hpp | 65 ++++++++++++++++ include/boost/mpl/aux_/sort_impl.hpp | 98 +++++++++++++++++++++++++ include/boost/mpl/partition.hpp | 61 +++++++++++++++ include/boost/mpl/sort.hpp | 53 +++++++++++++ include/boost/mpl/sort_fwd.hpp | 44 +++++++++++ include/boost/mpl/stable_partition.hpp | 61 +++++++++++++++ test/max_element.cpp | 33 +++++++++ test/sort.cpp | 35 +++++++++ test/stable_partition.cpp | 60 +++++++++++++++ 9 files changed, 510 insertions(+) create mode 100644 include/boost/mpl/aux_/partition_op.hpp create mode 100644 include/boost/mpl/aux_/sort_impl.hpp create mode 100644 include/boost/mpl/partition.hpp create mode 100644 include/boost/mpl/sort.hpp create mode 100644 include/boost/mpl/sort_fwd.hpp create mode 100644 include/boost/mpl/stable_partition.hpp create mode 100644 test/max_element.cpp create mode 100644 test/sort.cpp create mode 100644 test/stable_partition.cpp diff --git a/include/boost/mpl/aux_/partition_op.hpp b/include/boost/mpl/aux_/partition_op.hpp new file mode 100644 index 0000000..07006cd --- /dev/null +++ b/include/boost/mpl/aux_/partition_op.hpp @@ -0,0 +1,65 @@ +//----------------------------------------------------------------------------- +// boost mpl/aux_/partition_op.hpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#ifndef BOOST_MPL_AUX_PARTITION_OP_HPP_INCLUDED +#define BOOST_MPL_AUX_PARTITION_OP_HPP_INCLUDED + +#include "boost/mpl/apply.hpp" +#include "boost/mpl/apply_if.hpp" +#include "boost/mpl/if.hpp" +#include "boost/mpl/pair.hpp" +#include "boost/mpl/push_front.hpp" +#include "boost/mpl/aux_/lambda_spec.hpp" + +namespace boost { +namespace mpl { +namespace aux { + +template +struct partition_op +{ + template + struct apply + { + private: + typedef typename State::first first_; + typedef typename State::second second_; + typedef typename Iter::type t_; + typedef typename apply1< Predicate,t_ >::type pred_; + + typedef typename apply_if< + pred_ + , push_front< first_, t_ > + , push_front< second_, t_ > + >::type result_; + + public: + typedef typename if_< + pred_ + , pair< result_,second_ > + , pair< first_,result_ > + >::type type; + }; +}; + +} // namespace aux + +BOOST_MPL_AUX_PASS_THROUGH_LAMBDA_SPEC(1,aux::partition_op) + +} // namespace mpl +} // namespace boost + +#endif // BOOST_MPL_AUX_PARTITION_OP_HPP_INCLUDED diff --git a/include/boost/mpl/aux_/sort_impl.hpp b/include/boost/mpl/aux_/sort_impl.hpp new file mode 100644 index 0000000..37f33bf --- /dev/null +++ b/include/boost/mpl/aux_/sort_impl.hpp @@ -0,0 +1,98 @@ +//----------------------------------------------------------------------------- +// boost mpl/aux_/sort_impl.hpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#ifndef BOOST_MPL_AUX_SORT_IMPL_HPP_INCLUDED +#define BOOST_MPL_AUX_SORT_IMPL_HPP_INCLUDED + +#include "boost/mpl/apply.hpp" +#include "boost/mpl/apply_if.hpp" +#include "boost/mpl/bind.hpp" // for bind2nd +#include "boost/mpl/copy_backward.hpp" +#include "boost/mpl/empty.hpp" +#include "boost/mpl/front.hpp" +#include "boost/mpl/identity.hpp" +#include "boost/mpl/partition.hpp" +#include "boost/mpl/pop_front.hpp" +#include "boost/mpl/push_front.hpp" +#include "boost/mpl/aux_/traits_lambda_spec.hpp" + +namespace boost { +namespace mpl { + +namespace aux { + +template < typename Sequence, typename Predicate > struct quick_sort; + +template +struct quick_sort_impl +{ +private: + + typedef typename front::type pivot_; + typedef typename pop_front::type seq_; + + typedef typename partition< + seq_ + , bind2nd< Predicate,pivot_ > + >::type partitioned; + + typedef typename quick_sort< + typename partitioned::first, Predicate + >::type first_part; + typedef typename quick_sort< + typename partitioned::second, Predicate + >::type second_part; + +public: + + typedef typename copy_backward< + first_part + , typename push_front< + second_part,pivot_ + >::type + , push_front<_,_> + >::type type; + +}; + +template +struct quick_sort + : apply_if< + empty + , identity< Sequence > + , quick_sort_impl< Sequence,Predicate > + > +{ +}; + +} // namespace aux + +template< typename Tag > +struct sort_traits +{ + template< typename Sequence, typename Predicate > + struct algorithm + : aux::quick_sort< Sequence,Predicate > + { + }; +}; + +BOOST_MPL_ALGORITM_TRAITS_LAMBDA_SPEC(2,sort_traits) + +} // namespace mpl +} // namespace boost + +#endif // BOOST_MPL_AUX_SORT_IMPL_HPP_INCLUDED diff --git a/include/boost/mpl/partition.hpp b/include/boost/mpl/partition.hpp new file mode 100644 index 0000000..14bf972 --- /dev/null +++ b/include/boost/mpl/partition.hpp @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------------- +// boost mpl/partition.hpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#ifndef BOOST_MPL_PARTITION_HPP_INCLUDED +#define BOOST_MPL_PARTITION_HPP_INCLUDED + +#include "boost/mpl/aux_/partition_op.hpp" +#include "boost/mpl/clear.hpp" +#include "boost/mpl/iter_fold.hpp" +#include "boost/mpl/lambda.hpp" +#include "boost/mpl/pair.hpp" +#include "boost/mpl/protect.hpp" +#include "boost/mpl/aux_/void_spec.hpp" +#include "boost/mpl/aux_/lambda_support.hpp" + +namespace boost { +namespace mpl { + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_BEGIN + +template < + typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Sequence) + , typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Predicate) + > +struct partition +{ +private: + typedef typename lambda::type pred_; + typedef typename clear::type cleared_; + +public: + typedef typename iter_fold< + Sequence + , pair< cleared_,cleared_ > + , protect< aux::partition_op > + >::type type; + + BOOST_MPL_AUX_LAMBDA_SUPPORT(2,partition,(Sequence,Predicate)) +}; + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_END + +BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(2, partition) + +} // namespace mpl +} // namespace boost + +#endif // BOOST_MPL_PARTITION_HPP_INCLUDED diff --git a/include/boost/mpl/sort.hpp b/include/boost/mpl/sort.hpp new file mode 100644 index 0000000..64e0c2b --- /dev/null +++ b/include/boost/mpl/sort.hpp @@ -0,0 +1,53 @@ +//----------------------------------------------------------------------------- +// boost mpl/sort.hpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#ifndef BOOST_MPL_SORT_HPP_INCLUDED +#define BOOST_MPL_SORT_HPP_INCLUDED + +#include "boost/mpl/sort_fwd.hpp" +#include "boost/mpl/aux_/sort_impl.hpp" +#include "boost/mpl/lambda.hpp" +#include "boost/mpl/aux_/sequence_tag.hpp" +#include "boost/mpl/aux_/void_spec.hpp" +#include "boost/mpl/aux_/lambda_support.hpp" + +namespace boost { +namespace mpl { + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_BEGIN + +template < typename Sequence, typename Predicate > // see sort_fwd.hpp +struct sort +{ +private: + typedef typename lambda::type pred_; + +public: + typedef typename sort_traits< + typename BOOST_MPL_AUX_SEQUENCE_TAG(Sequence) + >::template algorithm< + Sequence, pred_ + >::type type; + + BOOST_MPL_AUX_LAMBDA_SUPPORT(2,sort,(Sequence,Predicate)) +}; + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_END + +} // namespace mpl +} // namespace boost + +#endif // BOOST_MPL_SORT_HPP_INCLUDED diff --git a/include/boost/mpl/sort_fwd.hpp b/include/boost/mpl/sort_fwd.hpp new file mode 100644 index 0000000..e16dc71 --- /dev/null +++ b/include/boost/mpl/sort_fwd.hpp @@ -0,0 +1,44 @@ +//----------------------------------------------------------------------------- +// boost mpl/sort_fwd.hpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#ifndef BOOST_MPL_SORT_FWD_HPP_INCLUDED +#define BOOST_MPL_SORT_FWD_HPP_INCLUDED + +#include "boost/mpl/less.hpp" +#include "boost/mpl/placeholders.hpp" +#include "boost/mpl/aux_/void_spec.hpp" + +namespace boost { +namespace mpl { + +template< typename Tag > struct sort_traits; + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_BEGIN + +template < + typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Sequence) + , typename Predicate = less<_,_> + > +struct sort; + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_END + +BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(1, sort) + +} // namespace mpl +} // namespace boost + +#endif // BOOST_MPL_SORT_FWD_HPP_INCLUDED diff --git a/include/boost/mpl/stable_partition.hpp b/include/boost/mpl/stable_partition.hpp new file mode 100644 index 0000000..1ec291c --- /dev/null +++ b/include/boost/mpl/stable_partition.hpp @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------------- +// boost mpl/stable_partition.hpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#ifndef BOOST_MPL_STABLE_PARTITION_HPP_INCLUDED +#define BOOST_MPL_STABLE_PARTITION_HPP_INCLUDED + +#include "boost/mpl/aux_/partition_op.hpp" +#include "boost/mpl/clear.hpp" +#include "boost/mpl/iter_fold_backward.hpp" +#include "boost/mpl/lambda.hpp" +#include "boost/mpl/pair.hpp" +#include "boost/mpl/protect.hpp" +#include "boost/mpl/aux_/void_spec.hpp" +#include "boost/mpl/aux_/lambda_support.hpp" + +namespace boost { +namespace mpl { + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_BEGIN + +template < + typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Sequence) + , typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Predicate) + > +struct stable_partition +{ +private: + typedef typename lambda::type pred_; + typedef typename clear::type cleared_; + +public: + typedef typename iter_fold_backward< + Sequence + , pair< cleared_,cleared_ > + , protect< aux::partition_op > + >::type type; + + BOOST_MPL_AUX_LAMBDA_SUPPORT(2,stable_partition,(Sequence,Predicate)) +}; + +BOOST_MPL_AUX_AGLORITHM_NAMESPACE_END + +BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(2, stable_partition) + +} // namespace mpl +} // namespace boost + +#endif // BOOST_MPL_STABLE_PARTITION_HPP_INCLUDED diff --git a/test/max_element.cpp b/test/max_element.cpp new file mode 100644 index 0000000..a1b26c4 --- /dev/null +++ b/test/max_element.cpp @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// libs mpl/test/max_element.cpp source file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#include "boost/mpl/max_element.hpp" + +#include "boost/static_assert.hpp" +#include "boost/mpl/list_c.hpp" + +namespace mpl = boost::mpl; + +int main() +{ + typedef mpl::list_c::type numbers; + + typedef mpl::max_element< numbers >::type max_it; + typedef max_it::type max_value; + BOOST_STATIC_ASSERT((max_value::value == 8)); + + return 0; +} diff --git a/test/sort.cpp b/test/sort.cpp new file mode 100644 index 0000000..1f04d78 --- /dev/null +++ b/test/sort.cpp @@ -0,0 +1,35 @@ +//----------------------------------------------------------------------------- +// libs mpl/test/sort.cpp source file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2002-2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#include "boost/mpl/sort.hpp" + +#include "boost/static_assert.hpp" +#include "boost/mpl/list_c.hpp" +#include "boost/mpl/equal.hpp" + +namespace mpl = boost::mpl; + +int main() +{ + typedef mpl::list_c::type numbers; + typedef mpl::list_c::type manual_result; + + typedef mpl::sort< numbers >::type result; + + BOOST_STATIC_ASSERT((mpl::equal< result,manual_result >::type::value)); + + return 0; +} diff --git a/test/stable_partition.cpp b/test/stable_partition.cpp new file mode 100644 index 0000000..07006af --- /dev/null +++ b/test/stable_partition.cpp @@ -0,0 +1,60 @@ +//----------------------------------------------------------------------------- +// libs mpl/test/stable_partition.cpp source file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2003 +// Eric Friedman +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appears in all copies and +// that both the copyright notice and this permission notice appear in +// supporting documentation. No representations are made about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. + +#include "boost/mpl/stable_partition.hpp" + +#include "boost/static_assert.hpp" +#include "boost/mpl/list_c.hpp" +#include "boost/mpl/equal.hpp" + +#include "boost/mpl/less.hpp" +#include "boost/mpl/placeholders.hpp" +#include "boost/mpl/int.hpp" + +namespace mpl = boost::mpl; + +int main() +{ + typedef mpl::list_c::type numbers; + typedef mpl::list_c::type manual_first; + typedef mpl::list_c::type manual_second; + + ////// + + typedef mpl::stable_partition< + numbers + , mpl::less< mpl::_, mpl::int_<3> > + >::type result1; + + typedef result1::first first1; + BOOST_STATIC_ASSERT((mpl::equal< first1,manual_first >::type::value)); + typedef result1::second second1; + BOOST_STATIC_ASSERT((mpl::equal< second1,manual_second >::type::value)); + + ////// + + typedef mpl::stable_partition< + numbers + , mpl::lt< 3 > + >::type result2; + + typedef result2::first first2; + BOOST_STATIC_ASSERT((mpl::equal< first2,manual_first >::type::value)); + typedef result2::second second2; + BOOST_STATIC_ASSERT((mpl::equal< second2,manual_second >::type::value)); + + return 0; +}