|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r59280 - in sandbox/statistics/iterator: boost/iterator boost/range libs/iterator/example
From: erwann.rogard_at_[hidden]
Date: 2010-01-27 13:21:30
Author: e_r
Date: 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
New Revision: 59280
URL: http://svn.boost.org/trac/boost/changeset/59280
Log:
add
Added:
sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp (contents, props changed)
sandbox/statistics/iterator/boost/range/
sandbox/statistics/iterator/boost/range/flatten_range.hpp (contents, props changed)
sandbox/statistics/iterator/libs/iterator/example/cycle_iter.cpp (contents, props changed)
sandbox/statistics/iterator/libs/iterator/example/cycle_iter.h (contents, props changed)
sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp (contents, props changed)
sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.h (contents, props changed)
Added: sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
@@ -0,0 +1,164 @@
+///////////////////////////////////////////////////////////////////////////////
+// flatten_iterator.hpp //
+// //
+///////////////////////////////////////////////////////////////////////////////
+#ifndef BOOST_ITERATOR_FLATTEN_ITERATOR_HPP_ER_2010
+#define BOOST_ITERATOR_FLATTEN_ITERATOR_HPP_ER_2010
+#include <stdexcept>
+#include <boost/next_prior.hpp>
+#include <boost/iterator/iterator_traits.hpp>
+#include <boost/range.hpp>
+#include <boost/iterator/iterator_facade.hpp>
+
+// As per M.P.'suggestion
+
+namespace boost{
+
+ template<typename It>
+ struct flatten_iterator_default{
+ typedef typename boost::iterator_value<It>::type r_;
+ typedef typename boost::range_iterator<r_>::type it_;
+ typedef typename boost::iterator_reference<it_>::type reference_;
+ typedef typename boost::iterator_difference<it_>::type diff_;
+ };
+
+ template<
+ class It,
+ typename R = typename flatten_iterator_default<It>::reference_,
+ typename D = typename flatten_iterator_default<It>::diff_
+ >
+ class flatten_iterator : public boost::iterator_facade<
+ flatten_iterator<It,R,D>
+ , It
+ , boost::forward_traversal_tag
+ , R
+ , D
+ >{
+ typedef typename boost::iterator_value<It>::type nested_range_;
+ typedef typename boost::range_iterator<nested_range_>::type nested_;
+
+ typedef typename boost::iterator_facade<
+ flatten_iterator<It,R,D>
+ , It
+ , boost::forward_traversal_tag
+ , R
+ , D
+ > super_;
+
+ typedef typename super_::difference_type diff_;
+ typedef typename super_::reference ref_;
+
+ public:
+
+ flatten_iterator():super_(){}
+
+ explicit flatten_iterator(It b,It e):super_(),b_(b),e_(e){
+ this->update();
+ }
+
+ flatten_iterator(const flatten_iterator& that)
+ :super_(),b_(that.b_),e_(that.e_)
+ {
+ this->update();
+ }
+
+ flatten_iterator& operator=(const flatten_iterator& that){
+ if(that!=&this){
+ super_::operator=(that);
+ this->b_ = that.b_;
+ this->e_ = that.e_;
+ }
+ this->update();
+ return (*this);
+ }
+
+ protected:
+ friend class boost::iterator_core_access;
+
+ void increment(){
+ if(
+ (++(this->nb_)) == (this->ne_)
+ )
+ {
+ ++this->b_;
+ this->update();
+ }
+ }
+
+ //Not needed given category_traversal
+ void decrement(){
+ throw std::runtime_error(
+ "flatten_iterator::decrement() not allowed"
+ );
+ }
+
+ void advance(diff_ n){
+ // Needs checking
+
+ diff_ k = std::distance(this->b_,this->e_);
+ if(n<k){
+ this->nb_ += boost::next(this->nb_,n);
+ }else{
+ ++this->b_;
+ this->update();
+ return this->advance(n-k);
+ }
+ }
+
+ ref_ dereference()const
+ {
+ return (*this->nb_);
+ }
+
+ diff_ distance_to()
+ {
+ diff_ d = 0;
+ It it = this->b_;
+ if(it!=this->e_){
+ d += std::distance(this->nb_,this->ne_);
+ ++it;
+ }
+ while(it<this->e_){
+ d += std::distance(boost::begin(*it),boost::end(*it));
+ ++it;
+ }
+ }
+
+ bool equal(const flatten_iterator& rhs)const
+ {
+ bool is_end1 = (this->b_ == this->e_);
+ bool is_end2 = (rhs.b_ == rhs.e_);
+
+ if(is_end1 != is_end2)
+ return false;
+ if(is_end1 && is_end2)
+ return true;
+ return (this->nb_ == this->ne_);
+ }
+
+ It b_,e_;
+ bool ready_;
+ nested_ nb_,ne_; // nested in b_
+
+ void update()
+ {
+ if((this->b_) != (this->e_)){
+ this->nb_ = boost::begin(*this->b_);
+ this->ne_ = boost::end(*this->b_);
+ }
+ }
+
+ };
+
+
+ template<typename It>
+ flatten_iterator<It>
+ make_flatten_iterator(It b,It e){
+ typedef flatten_iterator<It> result_;
+ return result_(b,e);
+ }
+
+}//boost
+
+#endif
+
Added: sandbox/statistics/iterator/boost/range/flatten_range.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/iterator/boost/range/flatten_range.hpp 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
@@ -0,0 +1,40 @@
+///////////////////////////////////////////////////////////////////////////////
+// flatten_range.hpp //
+// //
+///////////////////////////////////////////////////////////////////////////////
+#ifndef BOOST_ITERATOR_FLATTEN_RANGE_HPP_ER_2010
+#define BOOST_ITERATOR_FLATTEN_RANGE_HPP_ER_2010
+#include <boost/range.hpp>
+#include <boost/iterator/flatten_iterator.hpp>
+
+namespace boost{
+
+ template<typename R>
+ struct result_of_flatten_range{
+ typedef typename range_iterator<R>::type it_;
+ typedef boost::flatten_iterator<it_> fit_;
+ typedef boost::iterator_range<fit_> type;
+
+ static type call(it_ b,it_ e){
+ return type(fit_(b,e),fit_(e,e));
+ }
+
+ };
+
+ template<typename R>
+ typename result_of_flatten_range<R>::type
+ flatten_range(R& r){
+ typedef result_of_flatten_range<R> meta_;
+ return meta_::call(boost::begin(r),boost::end(r));
+ }
+
+ template<typename R>
+ typename result_of_flatten_range<const R>::type
+ flatten_range(const R& r){
+ typedef result_of_flatten_range<const R> meta_;
+ return meta_::call(boost::const_begin(r),boost::const_end(r));
+ }
+
+}
+
+#endif
Added: sandbox/statistics/iterator/libs/iterator/example/cycle_iter.cpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/iterator/libs/iterator/example/cycle_iter.cpp 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
@@ -0,0 +1,29 @@
+#include <vector>
+#include <boost/range.hpp>
+#include <boost/iterator/cycle_iterator.hpp>
+#include <boost/iterator/cycle_iterator_ext.hpp>
+
+void example_iter(std::ostream& os){
+
+ using namespace boost;
+ typedef double val_;
+ typedef std::vector<val_> vec_;
+ typedef range_size<vec_>::type size_;
+ typedef range_iterator<vec_>::type base_it_;
+ const unsigned n = 10;
+ vec_ vec(n);
+
+ typedef cycle_iterator<base_it_,size_> it_;
+ it_ it(boost::begin(vec),boost::end(vec),0);
+ it_ it2;
+ //it2 = it;
+
+ typedef cycle_iterator_ext<base_it_,size_> it_ext_;
+ it_ext_ it_ext(boost::begin(vec),boost::end(vec),0);
+
+ it_ext_ it_ext2;
+ it_ext2 = it_ext;
+
+ os << "end";
+
+}
\ No newline at end of file
Added: sandbox/statistics/iterator/libs/iterator/example/cycle_iter.h
==============================================================================
--- (empty file)
+++ sandbox/statistics/iterator/libs/iterator/example/cycle_iter.h 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
@@ -0,0 +1,11 @@
+///////////////////////////////////////////////////////////////////////////////
+// example::iterator::range_cycle.h //
+// //
+///////////////////////////////////////////////////////////////////////////////
+#ifndef LIBS_ITERATOR_EXAMPLE_CYCLE_ITERATOR_ER_2009
+#define LIBS_ITERATOR_EXAMPLE_CYCLE_ITERATOR_ER_2009
+#include <ostream>
+
+void example_iter(std::ostream& os);
+
+#endif
\ No newline at end of file
Added: sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
@@ -0,0 +1,76 @@
+///////////////////////////////////////////////////////////////////////////////
+// example::iterator::flatten_iterator.cpp //
+// //
+// Copyright 2009 Erwann Rogard. 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 <iostream>
+#include <boost/assign/std/vector.hpp>
+#include <boost/assign/list_of.hpp>
+#include <boost/range.hpp>
+#include <vector>
+#include <boost/iterator/flatten_iterator.hpp>
+#include <boost/range/flatten_range.hpp>
+#include <libs/iterator/example/flatten_iterator.h>
+
+void example_flatten_iterator(std::ostream& out){
+ out << "->example_flatten_iterator : ";
+
+ using namespace boost;
+
+ typedef int val_;
+ typedef std::vector<val_> vals_;
+ typedef range_size<vals_>::type size_;
+ typedef std::vector<vals_> mat_;
+
+ vals_ vals1, vals2, vals3;
+ mat_ mat;
+ {
+ using namespace boost::assign;
+ mat.push_back(boost::assign::list_of(1)(2)(3)(4)(5));
+ mat.push_back(boost::assign::list_of(6)(7)(8)(9)(10));
+ mat.push_back(boost::assign::list_of(11)(12)(13)(14)(15));
+ }
+
+ {
+ typedef boost::range_iterator<mat_>::type it_mat_;
+ typedef flatten_iterator<it_mat_> flat_it_;
+
+ flat_it_ b = make_flatten_iterator(boost::begin(mat),boost::end(mat));
+ flat_it_ e = make_flatten_iterator(boost::end(mat),boost::end(mat));
+
+ int j = 0;
+ int n = std::distance(b,e);
+ BOOST_ASSERT(
+ n == boost::size(vals1) + boost::size(vals2) + boost::size(vals3)
+ );
+ for(flat_it_ i = b; i!=e; i++, j++){
+ val_ val = *i;
+ BOOST_ASSERT(val = j);
+ BOOST_ASSERT(std::distance(b,e)==n-j);
+ }
+ }
+ {
+ typedef boost::range_iterator<const mat_>::type it_mat_;
+ typedef flatten_iterator<it_mat_> flat_it_;
+
+ const mat_& cmat = mat;
+
+ flat_it_ b = make_flatten_iterator(boost::begin(cmat),boost::end(cmat));
+ flat_it_ e = make_flatten_iterator(boost::end(cmat),boost::end(cmat));
+
+ int j = 0;
+ int n = std::distance(b,e);
+ BOOST_ASSERT(
+ n == boost::size(vals1) + boost::size(vals2) + boost::size(vals3)
+ );
+ for(flat_it_ i = b; i!=e; i++, j++){
+ val_ val = *i;
+ BOOST_ASSERT(val = j);
+ BOOST_ASSERT(std::distance(b,e)==n-j);
+ }
+ }
+
+ out << "<-" << std::endl;
+}
\ No newline at end of file
Added: sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.h
==============================================================================
--- (empty file)
+++ sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.h 2010-01-27 13:21:29 EST (Wed, 27 Jan 2010)
@@ -0,0 +1,11 @@
+///////////////////////////////////////////////////////////////////////////////
+// example::iterator::range_cycle.h //
+// //
+///////////////////////////////////////////////////////////////////////////////
+#ifndef LIBS_ITERATOR_EXAMPLE_CYCLE_ITERATOR_ER_2009
+#define LIBS_ITERATOR_EXAMPLE_CYCLE_ITERATOR_ER_2009
+#include <ostream>
+
+void example_flatten_iterator(std::ostream& os);
+
+#endif
\ No newline at end of file
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