Made map random access. Thanks to Brandon Kohn!

[SVN r74882]
This commit is contained in:
Joel de Guzman
2011-10-10 09:55:52 +00:00
parent b6df98e86c
commit 10274e7884
5 changed files with 73 additions and 9 deletions

View File

@ -419,7 +419,7 @@ including any Fusion header to change the default. Example:
[heading Model of] [heading Model of]
* __associative_sequence__ * __associative_sequence__
* __forward_sequence__ * __random_access_sequence__
[variablelist Notation [variablelist Notation
[[`M`] [A `map` type]] [[`M`] [A `map` type]]
@ -431,7 +431,7 @@ including any Fusion header to change the default. Example:
[heading Expression Semantics] [heading Expression Semantics]
Semantics of an expression is defined only where it differs from, or is not Semantics of an expression is defined only where it differs from, or is not
defined in __random_access_sequence__ and __associative_sequence__. defined in __forward_sequence__ and __associative_sequence__.
[table [table
[[Expression] [Semantics]] [[Expression] [Semantics]]

View File

@ -160,6 +160,7 @@ the following invariants always hold:
* __std_pair__ iterator * __std_pair__ iterator
* __boost_array__ iterator * __boost_array__ iterator
* __vector__ iterator * __vector__ iterator
* __map__ iterator
* __single_view__ iterator * __single_view__ iterator
* __iterator_range__ (where adapted sequence is a __bidirectional_sequence__) * __iterator_range__ (where adapted sequence is a __bidirectional_sequence__)
* __transform_view__ (where adapted sequence is a __bidirectional_sequence__) * __transform_view__ (where adapted sequence is a __bidirectional_sequence__)
@ -205,6 +206,7 @@ the following expressions must be valid:
[heading Models] [heading Models]
* __vector__ iterator * __vector__ iterator
* __map__ iterator
* __std_pair__ iterator * __std_pair__ iterator
* __boost_array__ iterator * __boost_array__ iterator
* __single_view__ iterator * __single_view__ iterator

View File

@ -188,6 +188,7 @@ are not defined in __forward_sequence__.
* __std_pair__ * __std_pair__
* __boost_array__ * __boost_array__
* __vector__ * __vector__
* __map__
* __reverse_view__ * __reverse_view__
* __single_view__ * __single_view__
* __iterator_range__ (where adapted sequence is a Bidirectional Sequence) * __iterator_range__ (where adapted sequence is a Bidirectional Sequence)
@ -264,6 +265,7 @@ are not defined in __bidirectional_sequence__.
* __std_pair__ * __std_pair__
* __boost_array__ * __boost_array__
* __vector__ * __vector__
* __map__
* __reverse_view__ * __reverse_view__
* __single_view__ * __single_view__
* __iterator_range__ (where adapted sequence is a Random Access Sequence) * __iterator_range__ (where adapted sequence is a Random Access Sequence)

View File

@ -1,7 +1,7 @@
/*============================================================================= /*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
@ -20,6 +20,8 @@
#include <boost/fusion/support/category_of.hpp> #include <boost/fusion/support/category_of.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/mpl/assert.hpp> #include <boost/mpl/assert.hpp>
#include <boost/mpl/at.hpp>
#include <boost/typeof/typeof.hpp>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -39,18 +41,19 @@ main()
{ {
typedef map< typedef map<
pair<int, char> pair<int, char>
, pair<double, std::string> > , pair<double, std::string> >
map_type; map_type;
BOOST_MPL_ASSERT((traits::is_associative<map_type>)); BOOST_MPL_ASSERT((traits::is_associative<map_type>));
BOOST_MPL_ASSERT((traits::is_random_access<map_type>));
map_type m( map_type m(
make_pair<int>('X') make_pair<int>('X')
, make_pair<double>("Men")); , make_pair<double>("Men"));
std::cout << at_key<int>(m) << std::endl; std::cout << at_key<int>(m) << std::endl;
std::cout << at_key<double>(m) << std::endl; std::cout << at_key<double>(m) << std::endl;
BOOST_TEST(at_key<int>(m) == 'X'); BOOST_TEST(at_key<int>(m) == 'X');
BOOST_TEST(at_key<double>(m) == "Men"); BOOST_TEST(at_key<double>(m) == "Men");
@ -58,7 +61,7 @@ main()
boost::is_same<boost::fusion::result_of::value_at_key<map_type, int>::type, char>::value)); boost::is_same<boost::fusion::result_of::value_at_key<map_type, int>::type, char>::value));
BOOST_STATIC_ASSERT(( BOOST_STATIC_ASSERT((
boost::is_same<boost::fusion::result_of::value_at_key<map_type, double>::type, std::string>::value)); boost::is_same<boost::fusion::result_of::value_at_key<map_type, double>::type, std::string>::value));
std::cout << m << std::endl; std::cout << m << std::endl;
BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, int>::value)); BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, int>::value));
@ -75,8 +78,43 @@ main()
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, double>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, double>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<map_type>::type>::type, char>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<map_type>::type>::type, char>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, std::string>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, std::string>::value));
//! Test random access interface.
pair<int, char> a = at_c<0>(m);
pair<double, std::string> b = at_c<1>(m);
} }
//! iterators & random access interface.
{
typedef pair<boost::mpl::int_<0>, std::string> pair0;
typedef pair<boost::mpl::int_<1>, std::string> pair1;
typedef pair<boost::mpl::int_<2>, std::string> pair2;
typedef pair<boost::mpl::int_<3>, std::string> pair3;
typedef pair<boost::mpl::int_<4>, std::string> pair4;
typedef map< pair0, pair1, pair2, pair3, pair4 > map_type;
map_type m( pair0("zero"), pair1("one"), pair2("two"), pair3("three"), pair4("four") );
BOOST_AUTO( it0, begin(m) );
BOOST_TEST((deref(it0) == pair0("zero")));
BOOST_AUTO( it1, fusion::next(it0) );
BOOST_TEST((deref(it1) == pair1("one")));
BOOST_AUTO( it2, fusion::next(it1) );
BOOST_TEST((deref(it2) == pair2("two")));
BOOST_AUTO( it3, fusion::next(it2) );
BOOST_TEST((deref(it3) == pair3("three")));
BOOST_AUTO( it4, fusion::next(it3) );
BOOST_TEST((deref(it4) == pair4("four")));
BOOST_TEST((deref(fusion::advance_c<4>(it0)) == deref(it4)));
//! Bi-directional
BOOST_TEST((deref(fusion::prior(it4)) == deref(it3) ));
BOOST_TEST((deref(fusion::prior(it3)) == deref(it2) ));
BOOST_TEST((deref(fusion::prior(it2)) == deref(it1) ));
BOOST_TEST((deref(fusion::prior(it1)) == deref(it0) ));
}
{ {
std::cout << make_map<char, int>('X', 123) << std::endl; std::cout << make_map<char, int>('X', 123) << std::endl;
BOOST_TEST(at_key<char>(make_map<char, int>('X', 123)) == 'X'); BOOST_TEST(at_key<char>(make_map<char, int>('X', 123)) == 'X');

View File

@ -6,6 +6,7 @@
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp> #include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/container/map/map.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp> #include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/container/generation/make_vector.hpp> #include <boost/fusion/container/generation/make_vector.hpp>
@ -82,6 +83,27 @@ main()
)); ));
} }
//! Map
{
typedef pair<boost::mpl::int_<0>, std::string> pair0;
typedef pair<boost::mpl::int_<1>, std::string> pair1;
typedef pair<boost::mpl::int_<2>, std::string> pair2;
typedef pair<boost::mpl::int_<3>, std::string> pair3;
typedef pair<boost::mpl::int_<4>, std::string> pair4;
typedef map< pair0, pair1, pair2, pair3, pair4 > map_type;
map_type m( pair0("zero"), pair1("one"), pair2("two"), pair3("three"), pair4("four") );
typedef reverse_view<map_type> view_type;
view_type rev(m);
std::cout << rev << std::endl;
BOOST_TEST((rev == make_vector( pair4("four"), pair3("three"), pair2("two"), pair1("one"), pair0("zero"))));
BOOST_TEST((at_c<0>(rev) == pair4("four")));
BOOST_TEST((at_c<1>(rev) == pair3("three")));
BOOST_TEST((at_c<2>(rev) == pair2("two")));
BOOST_TEST((at_c<3>(rev) == pair1("one")));
BOOST_TEST((at_c<4>(rev) == pair0("zero")));
}
return boost::report_errors(); return boost::report_errors();
} }