Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62710 - in trunk: boost/iterator libs/iterator/test
From: jewillco_at_[hidden]
Date: 2010-06-10 01:59:47


Author: jewillco
Date: 2010-06-10 01:59:46 EDT (Thu, 10 Jun 2010)
New Revision: 62710
URL: http://svn.boost.org/trac/boost/changeset/62710

Log:
function_input_iterator fixes from Dean Michael Berris
Added:
   trunk/libs/iterator/test/function_input_iterator_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/iterator/function_input_iterator.hpp | 106 ++++++++++++++++++++++++++++++++++++---
   trunk/libs/iterator/test/Jamfile.v2 | 1
   2 files changed, 97 insertions(+), 10 deletions(-)

Modified: trunk/boost/iterator/function_input_iterator.hpp
==============================================================================
--- trunk/boost/iterator/function_input_iterator.hpp (original)
+++ trunk/boost/iterator/function_input_iterator.hpp 2010-06-10 01:59:46 EDT (Thu, 10 Jun 2010)
@@ -7,23 +7,29 @@
 #ifndef BOOST_FUNCTION_INPUT_ITERATOR
 #define BOOST_FUNCTION_INPUT_ITERATOR
 
+#include <boost/mpl/if.hpp>
+#include <boost/function_types/is_function_pointer.hpp>
+#include <boost/function_types/is_function_reference.hpp>
+#include <boost/function_types/result_type.hpp>
 #include <boost/iterator/iterator_facade.hpp>
 
 namespace boost {
 
- template <class Function, class Input>
+ namespace impl {
+
+ template <class Function, class Input>
         class function_input_iterator
- : public iterator_facade<
+ : public iterator_facade<
             function_input_iterator<Function, Input>,
             typename Function::result_type,
             single_pass_traversal_tag,
             typename Function::result_type const &
>
- {
+ {
         public:
             function_input_iterator() {}
- function_input_iterator(Function * f_, Input state_ = Input())
- : f(f_), state(state_), value((*f)()) {}
+ function_input_iterator(Function & f_, Input state_ = Input())
+ : f(&f_), state(state_), value((*f)()) {}
 
             void increment() {
                 value = (*f)();
@@ -31,8 +37,8 @@
             }
 
             typename Function::result_type const &
- dereference() const {
- return value;
+ dereference() const {
+ return value;
             }
 
             bool equal(function_input_iterator const & other) const {
@@ -43,13 +49,93 @@
             Function * f;
             Input state;
             typename Function::result_type value;
+ };
+
+ template <class Function, class Input>
+ class function_pointer_input_iterator
+ : public iterator_facade<
+ function_pointer_input_iterator<Function, Input>,
+ typename function_types::result_type<Function>::type,
+ single_pass_traversal_tag,
+ typename function_types::result_type<Function>::type const &
+ >
+ {
+ public:
+ function_pointer_input_iterator() {}
+ function_pointer_input_iterator(Function &f_, Input state_ = Input())
+ : f(f_), state(state_), value((*f)())
+ {}
+
+ void increment() {
+ value = (*f)();
+ ++state;
+ }
+
+ typename function_types::result_type<Function>::type const &
+ dereference() const {
+ return value;
+ }
+
+ bool equal(function_pointer_input_iterator const & other) const {
+ return f == other.f && state == other.state;
+ }
+
+ private:
+ Function f;
+ Input state;
+ typename function_types::result_type<Function>::type value;
+ };
+
+ template <class Function, class Input>
+ class function_reference_input_iterator
+ : public function_pointer_input_iterator<Function*,Input>
+ {
+ public:
+ function_reference_input_iterator(Function & f_, Input state_ = Input())
+ : function_pointer_input_iterator<Function*,Input>(&f_, state_)
+ {}
+ };
+
+ } // namespace impl
+
+ template <class Function, class Input>
+ class function_input_iterator
+ : public mpl::if_<
+ function_types::is_function_pointer<Function>,
+ impl::function_pointer_input_iterator<Function,Input>,
+ typename mpl::if_<
+ function_types::is_function_reference<Function>,
+ impl::function_reference_input_iterator<Function,Input>,
+ impl::function_input_iterator<Function,Input>
+ >::type
+ >::type
+ {
+ typedef typename mpl::if_<
+ function_types::is_function_pointer<Function>,
+ impl::function_pointer_input_iterator<Function,Input>,
+ typename mpl::if_<
+ function_types::is_function_reference<Function>,
+ impl::function_reference_input_iterator<Function,Input>,
+ impl::function_input_iterator<Function,Input>
+ >::type
+ >::type base_type;
+ public:
+ function_input_iterator(Function & f, Input i)
+ : base_type(f, i) {}
     };
 
     template <class Function, class Input>
     inline function_input_iterator<Function, Input>
- make_function_input_iterator(Function & f, Input state) {
- typedef function_input_iterator<Function, Input> result_t;
- return result_t(&f, state);
+ make_function_input_iterator(Function & f, Input state) {
+ typedef function_input_iterator<Function, Input> result_t;
+ return result_t(f, state);
+ }
+
+ template <class Function, class Input>
+ inline function_input_iterator<Function*, Input>
+ make_function_input_iterator(Function * f, Input state) {
+ typedef function_input_iterator<Function*, Input> result_t;
+ return result_t(f, state);
     }
 
     struct infinite {

Modified: trunk/libs/iterator/test/Jamfile.v2
==============================================================================
--- trunk/libs/iterator/test/Jamfile.v2 (original)
+++ trunk/libs/iterator/test/Jamfile.v2 2010-06-10 01:59:46 EDT (Thu, 10 Jun 2010)
@@ -43,5 +43,6 @@
     [ run iterator_traits_test.cpp ]
     [ run permutation_iterator_test.cpp : : : # <stlport-iostream>on
     ]
+ [ run function_input_iterator_test.cpp ]
       
 ;

Added: trunk/libs/iterator/test/function_input_iterator_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/iterator/test/function_input_iterator_test.cpp 2010-06-10 01:59:46 EDT (Thu, 10 Jun 2010)
@@ -0,0 +1,70 @@
+// Copyright 2010 (c) Dean Michael Berris
+// 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/iterator/function_input_iterator.hpp>
+#include <vector>
+#include <iterator>
+#include <cassert>
+#include <algorithm>
+#include <iostream>
+
+struct ones {
+ typedef int result_type;
+ result_type operator() () {
+ return 1;
+ }
+};
+
+int ones_function () {
+ return 1;
+}
+
+using namespace std;
+
+int main(int argc, char * argv[])
+{
+ // test the iterator with function objects
+ ones ones_generator;
+ vector<int> values(10);
+ generate(values.begin(), values.end(), ones());
+
+ vector<int> generated;
+ copy(
+ boost::make_function_input_iterator(ones_generator, 0),
+ boost::make_function_input_iterator(ones_generator, 10),
+ back_inserter(generated)
+ );
+
+ assert(values.size() == generated.size());
+ assert(equal(values.begin(), values.end(), generated.begin()));
+ cout << "function iterator test with function objects successful." << endl;
+
+ // test the iterator with normal functions
+ vector<int>().swap(generated);
+ copy(
+ boost::make_function_input_iterator(&ones_function, 0),
+ boost::make_function_input_iterator(&ones_function, 10),
+ back_inserter(generated)
+ );
+
+ assert(values.size() == generated.size());
+ assert(equal(values.begin(), values.end(), generated.begin()));
+ cout << "function iterator test with pointer to function successful." << endl;
+
+ // test the iterator with a reference to a function
+ vector<int>().swap(generated);
+ copy(
+ boost::make_function_input_iterator(ones_function, 0),
+ boost::make_function_input_iterator(ones_function, 10),
+ back_inserter(generated)
+ );
+
+ assert(values.size() == generated.size());
+ assert(equal(values.begin(), values.end(), generated.begin()));
+ cout << "function iterator test with reference to function successful." << endl;
+
+ return 0;
+}
+


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