From 10274e7884d0f3693b8154970e0066b0e4b866f2 Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Mon, 10 Oct 2011 09:55:52 +0000 Subject: [PATCH] Made map random access. Thanks to Brandon Kohn! [SVN r74882] --- doc/container.qbk | 4 +-- doc/iterator.qbk | 2 ++ doc/sequence.qbk | 2 ++ test/sequence/map.cpp | 52 +++++++++++++++++++++++++++++----- test/sequence/reverse_view.cpp | 22 ++++++++++++++ 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/doc/container.qbk b/doc/container.qbk index 83c10c1a..901417f0 100644 --- a/doc/container.qbk +++ b/doc/container.qbk @@ -419,7 +419,7 @@ including any Fusion header to change the default. Example: [heading Model of] * __associative_sequence__ -* __forward_sequence__ +* __random_access_sequence__ [variablelist Notation [[`M`] [A `map` type]] @@ -431,7 +431,7 @@ including any Fusion header to change the default. Example: [heading Expression Semantics] 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 [[Expression] [Semantics]] diff --git a/doc/iterator.qbk b/doc/iterator.qbk index 8e076e53..2575f275 100644 --- a/doc/iterator.qbk +++ b/doc/iterator.qbk @@ -160,6 +160,7 @@ the following invariants always hold: * __std_pair__ iterator * __boost_array__ iterator * __vector__ iterator +* __map__ iterator * __single_view__ iterator * __iterator_range__ (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] * __vector__ iterator +* __map__ iterator * __std_pair__ iterator * __boost_array__ iterator * __single_view__ iterator diff --git a/doc/sequence.qbk b/doc/sequence.qbk index adb7c7af..e86156a5 100644 --- a/doc/sequence.qbk +++ b/doc/sequence.qbk @@ -188,6 +188,7 @@ are not defined in __forward_sequence__. * __std_pair__ * __boost_array__ * __vector__ +* __map__ * __reverse_view__ * __single_view__ * __iterator_range__ (where adapted sequence is a Bidirectional Sequence) @@ -264,6 +265,7 @@ are not defined in __bidirectional_sequence__. * __std_pair__ * __boost_array__ * __vector__ +* __map__ * __reverse_view__ * __single_view__ * __iterator_range__ (where adapted sequence is a Random Access Sequence) diff --git a/test/sequence/map.cpp b/test/sequence/map.cpp index b149d24e..265510f6 100644 --- a/test/sequence/map.cpp +++ b/test/sequence/map.cpp @@ -1,7 +1,7 @@ /*============================================================================= 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) ==============================================================================*/ #include @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -39,18 +41,19 @@ main() { typedef map< pair - , pair > + , pair > map_type; BOOST_MPL_ASSERT((traits::is_associative)); - + BOOST_MPL_ASSERT((traits::is_random_access)); + map_type m( make_pair('X') , make_pair("Men")); - + std::cout << at_key(m) << std::endl; std::cout << at_key(m) << std::endl; - + BOOST_TEST(at_key(m) == 'X'); BOOST_TEST(at_key(m) == "Men"); @@ -58,7 +61,7 @@ main() boost::is_same::type, char>::value)); BOOST_STATIC_ASSERT(( boost::is_same::type, std::string>::value)); - + std::cout << m << std::endl; BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key::value)); @@ -75,8 +78,43 @@ main() BOOST_STATIC_ASSERT((boost::is_same::type>::type>::type, double>::value)); BOOST_STATIC_ASSERT((boost::is_same::type>::type, char>::value)); BOOST_STATIC_ASSERT((boost::is_same::type>::type>::type, std::string>::value)); + + //! Test random access interface. + pair a = at_c<0>(m); + pair b = at_c<1>(m); } - + + //! iterators & random access interface. + { + typedef pair, std::string> pair0; + typedef pair, std::string> pair1; + typedef pair, std::string> pair2; + typedef pair, std::string> pair3; + typedef pair, 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('X', 123) << std::endl; BOOST_TEST(at_key(make_map('X', 123)) == 'X'); diff --git a/test/sequence/reverse_view.cpp b/test/sequence/reverse_view.cpp index 36bda96c..fb96275d 100644 --- a/test/sequence/reverse_view.cpp +++ b/test/sequence/reverse_view.cpp @@ -6,6 +6,7 @@ ==============================================================================*/ #include #include +#include #include #include #include @@ -82,6 +83,27 @@ main() )); } + //! Map + { + typedef pair, std::string> pair0; + typedef pair, std::string> pair1; + typedef pair, std::string> pair2; + typedef pair, std::string> pair3; + typedef pair, 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 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(); }