modern c++11 map implemented + various tweaks

[SVN r82708]
This commit is contained in:
Joel de Guzman
2013-02-04 03:49:34 +00:00
parent 996f4152d2
commit 7b6cf39540
18 changed files with 965 additions and 234 deletions

View File

@@ -0,0 +1,55 @@
/*=============================================================================
Copyright (c) 2001-2013 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_DETAIL_AT_IMPL_02042013_0821)
#define BOOST_FUSION_MAP_DETAIL_AT_IMPL_02042013_0821
#include <boost/fusion/support/detail/access.hpp>
namespace boost { namespace fusion
{
struct map_tag;
namespace extension
{
template <typename Tag>
struct at_impl;
template <>
struct at_impl<map_tag>
{
template <typename Sequence, typename N>
struct apply
{
typedef
decltype(std::declval<Sequence>().get(N()))
type;
static type
call(Sequence& m)
{
return m.get(N());
}
};
template <typename Sequence, typename N>
struct apply<Sequence const, N>
{
typedef
decltype(std::declval<Sequence const>().get(N()))
type;
static type
call(Sequence const& m)
{
return m.get(N());
}
};
};
}
}}
#endif

View File

@@ -0,0 +1,58 @@
/*=============================================================================
Copyright (c) 2001-2013 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_DETAIL_AT_KEY_IMPL_02042013_0821)
#define BOOST_FUSION_MAP_DETAIL_AT_KEY_IMPL_02042013_0821
#include <boost/fusion/support/detail/access.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/identity.hpp>
namespace boost { namespace fusion
{
struct map_tag;
namespace extension
{
template <typename Tag>
struct at_key_impl;
template <>
struct at_key_impl<map_tag>
{
template <typename Sequence, typename Key>
struct apply
{
typedef
decltype(std::declval<Sequence>().get(mpl::identity<Key>()))
type;
static type
call(Sequence& m)
{
return m.get(mpl::identity<Key>());
}
};
template <typename Sequence, typename Key>
struct apply<Sequence const, Key>
{
typedef
decltype(std::declval<Sequence const>().get(mpl::identity<Key>()))
type;
static type
call(Sequence const& m)
{
return m.get(mpl::identity<Key>());
}
};
};
}
}}
#endif

View File

@@ -0,0 +1,38 @@
/*=============================================================================
Copyright (c) 2005-2013 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_BEGIN_IMPL_02042013_0857)
#define BOOST_FUSION_MAP_BEGIN_IMPL_02042013_0857
#include <boost/fusion/container/map/map_iterator.hpp>
namespace boost { namespace fusion
{
struct map_tag;
namespace extension
{
template<typename T>
struct begin_impl;
template<>
struct begin_impl<map_tag>
{
template<typename Sequence>
struct apply
{
typedef map_iterator<Sequence, 0> type;
static type call(Sequence& seq)
{
return type(seq);
}
};
};
}
}}
#endif

View File

@@ -0,0 +1,38 @@
/*=============================================================================
Copyright (c) 2005-2013 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_END_IMPL_02042013_0857)
#define BOOST_FUSION_MAP_END_IMPL_02042013_0857
#include <boost/fusion/container/map/map_iterator.hpp>
namespace boost { namespace fusion
{
struct map_tag;
namespace extension
{
template<typename T>
struct end_impl;
template<>
struct end_impl<map_tag>
{
template<typename Sequence>
struct apply
{
typedef map_iterator<Sequence, Sequence::size::value> type;
static type call(Sequence& seq)
{
return type(seq);
}
};
};
}
}}
#endif

View File

@@ -0,0 +1,165 @@
/*=============================================================================
Copyright (c) 2005-2013 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_IMPL_02032013_2233)
#define BOOST_FUSION_MAP_IMPL_02032013_2233
#include <boost/fusion/support/detail/access.hpp>
#include <boost/fusion/iterator/deref.hpp>
#include <boost/fusion/iterator/next.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/identity.hpp>
namespace boost { namespace fusion
{
struct fusion_sequence_tag;
}}
namespace boost { namespace fusion { namespace detail
{
struct map_impl_from_iterator {};
template <int index, typename ...T>
struct map_impl;
template <int index_>
struct map_impl<index_>
{
typedef fusion_sequence_tag tag;
static int const index = index_;
static int const size = 0;
map_impl() {}
template <typename Iterator>
map_impl(Iterator const& iter, map_impl_from_iterator)
{}
void get();
void get_val();
void get_key();
};
template <int index_, typename Pair, typename ...T>
struct map_impl<index_, Pair, T...> : map_impl<index_ + 1, T...>
{
typedef fusion_sequence_tag tag;
typedef map_impl<index_+1, T...> rest_type;
using rest_type::get;
using rest_type::get_val;
using rest_type::get_key;
static int const index = index_;
static int const size = rest_type::size + 1;
typedef Pair pair_type;
typedef typename Pair::first_type key_type;
typedef typename Pair::second_type value_type;
map_impl()
: rest_type(), element()
{}
map_impl(map_impl const& rhs)
: rest_type(rhs.get_base()), element(rhs.element)
{}
map_impl(map_impl&& rhs)
: rest_type(std::forward<rest_type>(*static_cast<rest_type*>(this)))
, element(std::forward<Pair>(rhs.element))
{}
template <typename ...U>
map_impl(map_impl<index, U...> const& rhs)
: rest_type(rhs.get_base()), element(rhs.element)
{}
map_impl(typename detail::call_param<Pair>::type element
, typename detail::call_param<T>::type... rest)
: rest_type(rest...), element(element)
{}
map_impl(Pair&& element, T&&... rest)
: rest_type(std::forward<T>(rest)...)
, element(std::forward<Pair>(element))
{}
template <typename Iterator>
map_impl(Iterator const& iter, map_impl_from_iterator fi)
: rest_type(fusion::next(iter), fi)
, element(*iter)
{}
rest_type& get_base()
{
return *this;
}
rest_type const& get_base() const
{
return *this;
}
value_type get_val(mpl::identity<key_type>);
pair_type get_val(mpl::int_<index>);
value_type get_val(mpl::identity<key_type>) const;
pair_type get_val(mpl::int_<index>) const;
key_type get_key(mpl::int_<index>);
key_type get_key(mpl::int_<index>) const;
typename cref_result<value_type>::type
get(mpl::identity<key_type>) const
{
return element.second;
}
typename ref_result<value_type>::type
get(mpl::identity<key_type>)
{
return element.second;
}
typename cref_result<pair_type>::type
get(mpl::int_<index>) const
{
return element;
}
typename ref_result<pair_type>::type
get(mpl::int_<index>)
{
return element;
}
template <typename ...U>
map_impl& operator=(map_impl<index, U...> const& rhs)
{
rest_type::operator=(rhs);
element = rhs.element;
return *this;
}
map_impl& operator=(map_impl const& rhs)
{
rest_type::operator=(rhs);
element = rhs.element;
return *this;
}
map_impl& operator=(map_impl&& rhs)
{
rest_type::operator=(std::forward<map_impl>(rhs));
element = std::forward<Pair>(rhs.element);
return *this;
}
Pair element;
};
}}}
#endif

View File

@@ -0,0 +1,38 @@
/*=============================================================================
Copyright (c) 2001-2013 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_DETAIL_VALUE_AT_KEY_IMPL_02042013_0821)
#define BOOST_FUSION_MAP_DETAIL_VALUE_AT_KEY_IMPL_02042013_0821
#include <boost/fusion/support/detail/access.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/identity.hpp>
namespace boost { namespace fusion
{
struct map_tag;
namespace extension
{
template <typename Tag>
struct value_at_key_impl;
template <>
struct value_at_key_impl<map_tag>
{
template <typename Sequence, typename Key>
struct apply
{
typedef
decltype(std::declval<Sequence>().get_val(mpl::identity<Key>()))
type;
};
};
}
}}
#endif

View File

@@ -7,6 +7,52 @@
#if !defined(FUSION_MAP_MAIN_07212005_1106)
#define FUSION_MAP_MAIN_07212005_1106
#include <boost/fusion/container/map/detail/cpp03/map.hpp>
#include <boost/fusion/container/map/map_fwd.hpp>
///////////////////////////////////////////////////////////////////////////////
// Without variadics, we will use the PP version
///////////////////////////////////////////////////////////////////////////////
#if !defined(BOOST_FUSION_HAS_VARIADIC_MAP)
# include <boost/fusion/container/map/detail/cpp03/map.hpp>
#else
///////////////////////////////////////////////////////////////////////////////
// C++11 interface
///////////////////////////////////////////////////////////////////////////////
#include <boost/fusion/container/map/detail/map_impl.hpp>
#include <boost/fusion/container/map/detail/begin_impl.hpp>
#include <boost/fusion/container/map/detail/end_impl.hpp>
#include <boost/fusion/container/map/detail/at_impl.hpp>
#include <boost/fusion/container/map/detail/at_key_impl.hpp>
#include <boost/fusion/container/map/detail/value_at_key_impl.hpp>
#include <boost/fusion/sequence/intrinsic/begin.hpp>
#include <boost/fusion/sequence/intrinsic/end.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/sequence/intrinsic/at_c.hpp>
#include <boost/fusion/support/sequence_base.hpp>
#include <boost/fusion/support/category_of.hpp>
namespace boost { namespace fusion
{
struct map_tag;
template <typename ...T>
struct map : detail::map_impl<0, T...>, sequence_base<map<T...>>
{
typedef map_tag fusion_tag;
typedef detail::map_impl<0, T...> base_type;
struct category : random_access_traversal_tag, associative_tag {};
typedef mpl::int_<base_type::size> size;
typedef mpl::false_ is_view;
map() {};
map(typename detail::call_param<T>::type... element)
: base_type(element...)
{}
};
}}
#endif
#endif

View File

@@ -7,6 +7,30 @@
#if !defined(FUSION_MAP_FORWARD_MAIN_07212005_1105)
#define FUSION_MAP_FORWARD_MAIN_07212005_1105
#include <boost/fusion/container/map/detail/cpp03/map_fwd.hpp>
#include <boost/config.hpp>
///////////////////////////////////////////////////////////////////////////////
// With no decltype and variadics, we will use the C++03 version
///////////////////////////////////////////////////////////////////////////////
#if (defined(BOOST_NO_CXX11_DECLTYPE) \
|| defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) \
|| defined(BOOST_NO_CXX11_RVALUE_REFERENCES))
# include <boost/fusion/container/map/detail/cpp03/map_fwd.hpp>
#else
# if !defined(BOOST_FUSION_HAS_VARIADIC_MAP)
# define BOOST_FUSION_HAS_VARIADIC_MAP
# endif
#include <boost/fusion/container/map/detail/map_impl.hpp>
///////////////////////////////////////////////////////////////////////////////
// C++11 interface
///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace fusion
{
template <typename ...T>
struct map;
}}
#endif
#endif

View File

@@ -0,0 +1,164 @@
/*=============================================================================
Copyright (c) 2005-2013 Joel de Guzman
Copyright (c) 2005-2006 Dan Marsden
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)
==============================================================================*/
#if !defined(BOOST_FUSION_MAP_ITERATOR_02042013_0835)
#define BOOST_FUSION_MAP_ITERATOR_02042013_0835
#include <boost/fusion/iterator/iterator_facade.hpp>
#include <boost/mpl/minus.hpp>
#include <boost/mpl/equal_to.hpp>
namespace boost { namespace fusion
{
struct random_access_traversal_tag;
template <typename Seq, int Pos>
struct map_iterator
: iterator_facade<
map_iterator<Seq, Pos>
, random_access_traversal_tag>
{
typedef Seq sequence;
typedef mpl::int_<Pos> index;
map_iterator(Seq& seq)
: seq_(seq)
{}
template<typename Iterator>
struct value_of
{
typedef typename Iterator::sequence sequence;
typedef typename Iterator::index index;
typedef
decltype(std::declval<sequence>().get_val(index()))
type;
};
template<typename Iterator>
struct value_of_data
{
typedef typename Iterator::sequence sequence;
typedef typename Iterator::index index;
typedef
decltype(std::declval<sequence>().get_val(index()).second)
type;
};
template<typename Iterator>
struct key_of
{
typedef typename Iterator::sequence sequence;
typedef typename Iterator::index index;
typedef
decltype(std::declval<sequence>().get_key(index()))
type;
};
template<typename Iterator>
struct deref
{
typedef typename Iterator::sequence sequence;
typedef typename Iterator::index index;
typedef
decltype(std::declval<sequence>().get(index()))
type;
static type
call(Iterator const& it)
{
return it.seq_.get(typename Iterator::index());
}
};
template<typename Iterator>
struct deref_data
{
typedef typename Iterator::sequence sequence;
typedef typename Iterator::index index;
typedef
decltype(std::declval<sequence>().get(index()).second)
type;
static type
call(Iterator const& it)
{
return it.seq_.get(typename Iterator::index()).second;
}
};
//~ template<typename Iterator>
//~ struct deref_data
//~ {
//~ typedef typename Iterator::sequence sequence;
//~ typedef typename Iterator::index index;
//~ typedef
//~ decltype(std::declval<sequence>().get(index()))
//~ type;
//~ static type
//~ call(Iterator const& it)
//~ {
//~ return it.seq_.get(typename Iterator::index());
//~ }
//~ };
template <typename Iterator, typename N>
struct advance
{
typedef typename Iterator::index index;
typedef typename Iterator::sequence sequence;
typedef map_iterator<sequence, index::value + N::value> type;
static type
call(Iterator const& i)
{
return type(i.seq_);
}
};
template<typename Iterator>
struct next
: advance<Iterator, mpl::int_<1> >
{};
template<typename Iterator>
struct prior
: advance<Iterator, mpl::int_<-1> >
{};
template <typename I1, typename I2>
struct distance : mpl::minus<typename I2::index, typename I1::index>
{
typedef typename
mpl::minus<
typename I2::index, typename I1::index
>::type
type;
static type
call(I1 const&, I2 const&)
{
return type();
}
};
template<typename I1, typename I2>
struct equal_to
: mpl::equal_to<typename I1::index, typename I2::index>
{};
Seq& seq_;
private:
// silence MSVC warning C4512: assignment operator could not be generated
map_iterator& operator= (map_iterator const&);
};
}}
#endif