Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74882 - in trunk/libs/fusion: doc test/sequence
From: joel_at_[hidden]
Date: 2011-10-10 05:55:54


Author: djowel
Date: 2011-10-10 05:55:52 EDT (Mon, 10 Oct 2011)
New Revision: 74882
URL: http://svn.boost.org/trac/boost/changeset/74882

Log:
Made map random access. Thanks to Brandon Kohn!
Text files modified:
   trunk/libs/fusion/doc/container.qbk | 4 +-
   trunk/libs/fusion/doc/iterator.qbk | 2 +
   trunk/libs/fusion/doc/sequence.qbk | 2 +
   trunk/libs/fusion/test/sequence/map.cpp | 52 ++++++++++++++++++++++++++++++++++-----
   trunk/libs/fusion/test/sequence/reverse_view.cpp | 22 ++++++++++++++++
   5 files changed, 73 insertions(+), 9 deletions(-)

Modified: trunk/libs/fusion/doc/container.qbk
==============================================================================
--- trunk/libs/fusion/doc/container.qbk (original)
+++ trunk/libs/fusion/doc/container.qbk 2011-10-10 05:55:52 EDT (Mon, 10 Oct 2011)
@@ -419,7 +419,7 @@
 [heading Model of]
 
 * __associative_sequence__
-* __forward_sequence__
+* __random_access_sequence__
 
 [variablelist Notation
     [[`M`] [A `map` type]]
@@ -431,7 +431,7 @@
 [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]]

Modified: trunk/libs/fusion/doc/iterator.qbk
==============================================================================
--- trunk/libs/fusion/doc/iterator.qbk (original)
+++ trunk/libs/fusion/doc/iterator.qbk 2011-10-10 05:55:52 EDT (Mon, 10 Oct 2011)
@@ -160,6 +160,7 @@
 * __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 @@
 
 [heading Models]
 * __vector__ iterator
+* __map__ iterator
 * __std_pair__ iterator
 * __boost_array__ iterator
 * __single_view__ iterator

Modified: trunk/libs/fusion/doc/sequence.qbk
==============================================================================
--- trunk/libs/fusion/doc/sequence.qbk (original)
+++ trunk/libs/fusion/doc/sequence.qbk 2011-10-10 05:55:52 EDT (Mon, 10 Oct 2011)
@@ -188,6 +188,7 @@
 * __std_pair__
 * __boost_array__
 * __vector__
+* __map__
 * __reverse_view__
 * __single_view__
 * __iterator_range__ (where adapted sequence is a Bidirectional Sequence)
@@ -264,6 +265,7 @@
 * __std_pair__
 * __boost_array__
 * __vector__
+* __map__
 * __reverse_view__
 * __single_view__
 * __iterator_range__ (where adapted sequence is a Random Access Sequence)

Modified: trunk/libs/fusion/test/sequence/map.cpp
==============================================================================
--- trunk/libs/fusion/test/sequence/map.cpp (original)
+++ trunk/libs/fusion/test/sequence/map.cpp 2011-10-10 05:55:52 EDT (Mon, 10 Oct 2011)
@@ -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 <boost/detail/lightweight_test.hpp>
@@ -20,6 +20,8 @@
 #include <boost/fusion/support/category_of.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/mpl/assert.hpp>
+#include <boost/mpl/at.hpp>
+#include <boost/typeof/typeof.hpp>
 #include <iostream>
 #include <string>
 
@@ -39,18 +41,19 @@
     {
         typedef map<
             pair<int, char>
- , pair<double, std::string> >
+ , pair<double, std::string> >
         map_type;
 
         BOOST_MPL_ASSERT((traits::is_associative<map_type>));
-
+ BOOST_MPL_ASSERT((traits::is_random_access<map_type>));
+
         map_type m(
             make_pair<int>('X')
           , make_pair<double>("Men"));
-
+
         std::cout << at_key<int>(m) << std::endl;
         std::cout << at_key<double>(m) << std::endl;
-
+
         BOOST_TEST(at_key<int>(m) == 'X');
         BOOST_TEST(at_key<double>(m) == "Men");
 
@@ -58,7 +61,7 @@
             boost::is_same<boost::fusion::result_of::value_at_key<map_type, int>::type, char>::value));
         BOOST_STATIC_ASSERT((
             boost::is_same<boost::fusion::result_of::value_at_key<map_type, double>::type, std::string>::value));
-
+
         std::cout << m << std::endl;
 
         BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, int>::value));
@@ -75,8 +78,43 @@
         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::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;
         BOOST_TEST(at_key<char>(make_map<char, int>('X', 123)) == 'X');

Modified: trunk/libs/fusion/test/sequence/reverse_view.cpp
==============================================================================
--- trunk/libs/fusion/test/sequence/reverse_view.cpp (original)
+++ trunk/libs/fusion/test/sequence/reverse_view.cpp 2011-10-10 05:55:52 EDT (Mon, 10 Oct 2011)
@@ -6,6 +6,7 @@
 ==============================================================================*/
 #include <boost/detail/lightweight_test.hpp>
 #include <boost/fusion/container/vector/vector.hpp>
+#include <boost/fusion/container/map/map.hpp>
 #include <boost/fusion/adapted/mpl.hpp>
 #include <boost/fusion/sequence/io/out.hpp>
 #include <boost/fusion/container/generation/make_vector.hpp>
@@ -82,6 +83,27 @@
         ));
     }
 
+ //! 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();
 }
 


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