Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61343 - in trunk/libs/spirit/example/scheme: test/utree utree utree/detail
From: hartmut.kaiser_at_[hidden]
Date: 2010-04-17 14:47:37


Author: hkaiser
Date: 2010-04-17 14:47:36 EDT (Sat, 17 Apr 2010)
New Revision: 61343
URL: http://svn.boost.org/trac/boost/changeset/61343

Log:
Added referencing iterators to utree
Text files modified:
   trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp | 19 +++++++
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp | 97 +++++++++++++++++++++++++++++++++++++++
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail3.hpp | 1
   trunk/libs/spirit/example/scheme/utree/utree.hpp | 10 ++++
   4 files changed, 126 insertions(+), 1 deletions(-)

Modified: trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp 2010-04-17 14:47:36 EDT (Sat, 17 Apr 2010)
@@ -229,5 +229,24 @@
         BOOST_TEST(~utree(456) == utree(~456));
     }
 
+ { // test reference iterator
+ utree val;
+ val.push_back(1);
+ val.push_back(2);
+ val.push_back(3);
+ val.push_back(4);
+ check(val, "( 1 2 3 4 )");
+
+ utree::ref_iterator b = val.ref_begin();
+ utree::ref_iterator e = val.ref_end();
+
+ utree ref(boost::make_iterator_range(b, e));
+ BOOST_TEST(ref[0] == utree(1));
+ BOOST_TEST(ref[1] == utree(2));
+ BOOST_TEST(ref[2] == utree(3));
+ BOOST_TEST(ref[3] == utree(4));
+ check(ref, "( 1 2 3 4 )");
+ }
+
     return boost::report_errors();
 }

Modified: trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp 2010-04-17 14:47:36 EDT (Sat, 17 Apr 2010)
@@ -1,5 +1,6 @@
 /*=============================================================================
     Copyright (c) 2001-2010 Joel de Guzman
+ Copyright (c) 2001-2010 Hartmut Kaiser
 
     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)
@@ -166,7 +167,7 @@
             return node == other.node;
         }
 
- utree& dereference() const
+ reference dereference() const
         {
             return node->val;
         }
@@ -175,6 +176,70 @@
         list::node* prev;
     };
 
+ template <typename Value>
+ class list::node_iterator<boost::reference_wrapper<Value> >
+ : public boost::iterator_facade<
+ node_iterator<boost::reference_wrapper<Value> >
+ , boost::reference_wrapper<Value>
+ , boost::bidirectional_traversal_tag
+ >
+ {
+ public:
+
+ node_iterator()
+ : node(0), prev(0), curr(nil_node) {}
+
+ explicit node_iterator(list::node* node)
+ : node(node), prev(node->prev), curr(node->val) {}
+
+ node_iterator(list::node* node, list::node* prev)
+ : node(node), prev(prev), curr(node ? node->val : nil_node) {}
+
+ private:
+
+ friend class boost::iterator_core_access;
+ friend class scheme::utree;
+
+ void increment()
+ {
+ if (node != 0) // not at end
+ {
+ prev = node;
+ node = node->next;
+ curr = boost::ref(node ? node->val : nil_node);
+ }
+ }
+
+ void decrement()
+ {
+ if (prev != 0) // not at begin
+ {
+ node = prev;
+ prev = prev->prev;
+ curr = boost::ref(node ? node->val : nil_node);
+ }
+ }
+
+ bool equal(node_iterator const& other) const
+ {
+ return node == other.node;
+ }
+
+ reference dereference() const
+ {
+ return curr;
+ }
+
+ list::node* node;
+ list::node* prev;
+
+ static Value nil_node;
+ mutable boost::reference_wrapper<Value> curr;
+ };
+
+ template <typename Value>
+ Value list::node_iterator<boost::reference_wrapper<Value> >::nil_node = Value();
+
     inline void list::free()
     {
         node* p = first;
@@ -514,6 +579,13 @@
         set_type(type::reference_type);
     }
 
+ template <typename Iter>
+ utree::utree(boost::iterator_range<Iter> r)
+ {
+ set_type(type::nil_type);
+ assign(r.begin(), r.end());
+ }
+
     inline utree::utree(utree const& other)
     {
         copy(other);
@@ -599,6 +671,13 @@
         return *this;
     }
 
+ template <typename Iter>
+ utree& utree::operator=(boost::iterator_range<Iter> r)
+ {
+ free();
+ assign(r.begin(), r.end());
+ }
+
     template <typename F>
     typename F::result_type
     inline utree::visit(utree const& x, F f)
@@ -783,6 +862,22 @@
         return iterator(0, l.last);
     }
 
+ inline utree::ref_iterator utree::ref_begin()
+ {
+ if (get_type() == type::reference_type)
+ return p->ref_begin();
+ ensure_list_type();
+ return ref_iterator(l.first);
+ }
+
+ inline utree::ref_iterator utree::ref_end()
+ {
+ if (get_type() == type::reference_type)
+ return p->ref_end();
+ ensure_list_type();
+ return ref_iterator(0, l.last);
+ }
+
     inline utree::const_iterator utree::begin() const
     {
         if (get_type() == type::reference_type)

Modified: trunk/libs/spirit/example/scheme/utree/detail/utree_detail3.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/detail/utree_detail3.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/detail/utree_detail3.hpp 2010-04-17 14:47:36 EDT (Sat, 17 Apr 2010)
@@ -1,5 +1,6 @@
 /*=============================================================================
     Copyright (c) 2001-2010 Joel de Guzman
+ Copyright (c) 2001-2010 Hartmut Kaiser
 
     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)

Modified: trunk/libs/spirit/example/scheme/utree/utree.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/utree.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/utree.hpp 2010-04-17 14:47:36 EDT (Sat, 17 Apr 2010)
@@ -1,5 +1,6 @@
 /*=============================================================================
     Copyright (c) 2001-2010 Joel de Guzman
+ Copyright (c) 2001-2010 Hartmut Kaiser
 
     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)
@@ -152,6 +153,8 @@
         typedef utree value_type;
         typedef detail::list::node_iterator<utree> iterator;
         typedef detail::list::node_iterator<utree const> const_iterator;
+ typedef detail::list::node_iterator<boost::reference_wrapper<utree> >
+ ref_iterator;
         typedef utree& reference;
         typedef utree const& const_reference;
         typedef std::ptrdiff_t difference_type;
@@ -169,6 +172,8 @@
         utree(char const* str, std::size_t len);
         utree(std::string const& str);
         utree(boost::reference_wrapper<utree> ref);
+ template <typename Iter>
+ utree(boost::iterator_range<Iter> r);
 
         template <typename Base, utree_type::info type_>
         utree(basic_string<Base, type_> const& bin);
@@ -184,6 +189,8 @@
         utree& operator=(char const* s);
         utree& operator=(std::string const& s);
         utree& operator=(boost::reference_wrapper<utree> ref);
+ template <typename Iter>
+ utree& operator=(boost::iterator_range<Iter> r);
 
         template <typename Base, utree_type::info type_>
         utree& operator=(basic_string<Base, type_> const& bin);
@@ -251,6 +258,9 @@
         const_iterator begin() const;
         const_iterator end() const;
 
+ ref_iterator ref_begin();
+ ref_iterator ref_end();
+
         bool empty() const;
         std::size_t size() const;
 


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