From 1c4527d58ec09260501cecfe3f4383b5ca79820b Mon Sep 17 00:00:00 2001 From: Jens Maurer Date: Sun, 18 Nov 2001 17:56:43 +0000 Subject: [PATCH 01/14] add boost::generator_iterator_policies and convenience classes [SVN r11725] --- include/boost/generator_iterator.hpp | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/boost/generator_iterator.hpp diff --git a/include/boost/generator_iterator.hpp b/include/boost/generator_iterator.hpp new file mode 100644 index 0000000..5f4c0a9 --- /dev/null +++ b/include/boost/generator_iterator.hpp @@ -0,0 +1,73 @@ +// (C) Copyright Jens Maurer 2001. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +// +// Revision History: + +// 15 Nov 2001 Jens Maurer +// created. + +#ifndef BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP +#define BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP + +#include +#include + +namespace boost { + +template +class generator_iterator_policies +{ +public: + generator_iterator_policies() { } + + template + void initialize(Base& base) { + m_value = (*base)(); + } + + // The Iter template argument is necessary for compatibility with a MWCW + // bug workaround + template + void increment(IteratorAdaptor& iter) { + m_value = (*iter.base())(); + } + + template + const typename Generator::result_type& + dereference(const IteratorAdaptor&) const + { return m_value; } + + template + bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const + { return x.base() == y.base() && + x.policies().m_value == y.policies().m_value; } + +private: + typename Generator::result_type m_value; +}; + +template +struct generator_iterator_generator +{ + typedef iterator_adaptor, + typename Generator::result_type, const typename Generator::result_type&, + const typename Generator::result_type*, std::input_iterator_tag, + long> type; +}; + +template +inline typename generator_iterator_generator::type +make_generator_iterator(Generator & gen) +{ + typedef typename generator_iterator_generator::type result_t; + return result_t(&gen); +} + +} // namespace boost + + +#endif // BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP + From f9095485a2507a6ce99bd3f9f72579de049549f2 Mon Sep 17 00:00:00 2001 From: Jens Maurer Date: Mon, 19 Nov 2001 22:11:51 +0000 Subject: [PATCH 02/14] add generator iterator adaptor [SVN r11736] --- generator_iterator.htm | 150 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 generator_iterator.htm diff --git a/generator_iterator.htm b/generator_iterator.htm new file mode 100644 index 0000000..76b94ed --- /dev/null +++ b/generator_iterator.htm @@ -0,0 +1,150 @@ + + + + +Generator Iterator Adaptor Documentation + + + + +c++boost.gif (8819 bytes) + +

Generator Iterator Adaptor

+Defined in header boost/generator_iterator.hpp +

+The generator iterator adaptor makes it easier to create custom input +iterators from 0-ary functions and function objects. The adaptor +takes a +Generator +and creates a model of +Input Iterator. +Each increment retrieves an item from the generator and makes it +available to be retrieved by dereferencing. The motivation for this +iterator is that some concepts can be more naturally expressed as a +generator, while most STL algorithms expect an iterator. An example +is the Random Number library. + +

Synopsis

+ +
+
+namespace boost {
+  template <class Generator>
+  class generator_iterator_policies;
+
+  template <class Generator>
+  class generator_iterator_generator;
+
+  template <class Generator>
+  typename generator_iterator_generator<Generator>::type
+  make_generator_iterator(Generator & gen);
+}
+
+
+ +
+ +

The Generator Iterator Generator Class

+ +The class generator_iterator_generator is a helper class whose purpose +is to construct a generator iterator type. The template parameter for +this class is the Generator function object type that is being +wrapped. The generator iterator adaptor only holds a reference (or +pointer) to the function object, therefore the function object must +outlive the generator iterator adaptor constructed from it. + +
+template <class Generator>
+class generator_iterator_generator
+{
+public:
+  typedef iterator_adaptor<...> type; // the resulting generator iterator type 
+}
+
+ + +

Template Parameters

+ + + + + + + + + +
ParameterDescription
Generator +The generator (0-ary function object) type being +wrapped. The return type of the function must be defined as +Generator::result_type. The function object must be a model +of +Generator. +
+ +

Concept Model

+The generator iterator class is a model of +Input Iterator. + +

Members

+The generator iterator implements the member functions +and operators required of the +Input Iterator +concept. + +
+ +
+

The Generator Iterator Object Generator

+ +The make_generator_iterator() function provides a +convenient way to create generator iterator objects. The function +saves the user the trouble of explicitly writing out the iterator +types. + +
+
+template <class Generator>
+typename generator_iterator_generator<Generator>::type
+make_function_output_iterator(Generator & gen);
+
+
+ +
+ + +

Example

+ +The following program shows how generator_iterator +transforms a generator into an input iterator. + +
+
+#include <iostream>
+#include <boost/generator_iterator.hpp>
+
+class my_generator
+{
+public:
+  typedef int result_type;
+  my_generator() : state(0) { }
+  int operator()() { return ++state; }
+private:
+  int state;
+};
+
+int main()
+{
+  my_generator gen;
+  boost::generator_iterator<my_generator> it(gen);
+  for(int i = 0; i < 10; ++i, ++it)
+    std::cout << *it << std::endl;
+}
+
+
+ +
+ +Written by Jens Maurer. + + + From ac3a206eb9c3532b6214dcc332feed4e31bd0d68 Mon Sep 17 00:00:00 2001 From: Jens Maurer Date: Sat, 8 Dec 2001 08:39:27 +0000 Subject: [PATCH 03/14] fix example (thanks to Michael Stevens) [SVN r11979] --- generator_iterator.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index 76b94ed..752ec28 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -135,7 +135,7 @@ private: int main() { my_generator gen; - boost::generator_iterator<my_generator> it(gen); + boost::generator_iterator_generator<my_generator>::type it = boost::make_generator_iterator(gen); for(int i = 0; i < 10; ++i, ++it) std::cout << *it << std::endl; } From 3695e48b6854d6a5d467fa8262db0afd79802759 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Mon, 11 Nov 2002 19:50:05 +0000 Subject: [PATCH 04/14] Fix from Yitzhak Sapir [SVN r16198] --- generator_iterator.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index 752ec28..4bd6734 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -105,7 +105,7 @@ types.
 template <class Generator>
 typename generator_iterator_generator<Generator>::type
-make_function_output_iterator(Generator & gen);
+make_generator_iterator(Generator & gen);
 
From 8a004c12f6596184cd506cd5e1a853ab858d0da0 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 27 Dec 2002 16:51:53 +0000 Subject: [PATCH 05/14] add or update See www.boost.org comments [SVN r16708] --- include/boost/generator_iterator.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/boost/generator_iterator.hpp b/include/boost/generator_iterator.hpp index 5f4c0a9..e27090c 100644 --- a/include/boost/generator_iterator.hpp +++ b/include/boost/generator_iterator.hpp @@ -9,6 +9,8 @@ // 15 Nov 2001 Jens Maurer // created. +// See http://www.boost.org/libs/utility/iterator_adaptors.htm for documentation. + #ifndef BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP #define BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP From 4fcb23f8e07125f9dd813102f6bce4ea75e4e766 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Fri, 11 Jul 2003 22:18:58 +0000 Subject: [PATCH 06/14] Move to new iterator adaptors [SVN r19074] --- include/boost/generator_iterator.hpp | 64 +++++++++++++++------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/include/boost/generator_iterator.hpp b/include/boost/generator_iterator.hpp index e27090c..2397288 100644 --- a/include/boost/generator_iterator.hpp +++ b/include/boost/generator_iterator.hpp @@ -14,57 +14,63 @@ #ifndef BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP #define BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP -#include +#include #include namespace boost { template -class generator_iterator_policies +class generator_iterator + : public iterator_facade< + generator_iterator + , typename Generator::result_type + , readable_lvalue_iterator_tag + , single_pass_traversal_tag + > { -public: - generator_iterator_policies() { } + typedef iterator_facade< + generator_iterator + , typename Generator::result_type + , readable_iterator_tag + , single_pass_traversal_tag + > super_t; + + public: + generator_iterator() {} + generator_iterator(Generator* g) : m_g(g), m_value((*m_g)()) {} - template - void initialize(Base& base) { - m_value = (*base)(); + void increment() + { + m_value = (*m_g)(); } - // The Iter template argument is necessary for compatibility with a MWCW - // bug workaround - template - void increment(IteratorAdaptor& iter) { - m_value = (*iter.base())(); - } - - template const typename Generator::result_type& - dereference(const IteratorAdaptor&) const - { return m_value; } + dereference() const + { + return m_value; + } - template - bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const - { return x.base() == y.base() && - x.policies().m_value == y.policies().m_value; } + bool equal(generator_iterator const& y) const + { + return this->m_g == y.m_g && this->m_value == y.m_value; + } -private: - typename Generator::result_type m_value; + private: + Generator* m_g; + typename Generator::result_type m_value; }; template struct generator_iterator_generator { - typedef iterator_adaptor, - typename Generator::result_type, const typename Generator::result_type&, - const typename Generator::result_type*, std::input_iterator_tag, - long> type; + typedef generator_iterator type; }; template -inline typename generator_iterator_generator::type +inline generator_iterator make_generator_iterator(Generator & gen) { - typedef typename generator_iterator_generator::type result_t; + typedef generator_iterator result_t; return result_t(&gen); } From 0c44051189e02eac141fcab60db0b137b6f9885a Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sat, 22 Nov 2003 01:18:37 +0000 Subject: [PATCH 07/14] Removed access category tags from iterator library, made corresponding changes elsewhere. boost/iterator and libs/iterator/test were updated from branch "simplify" [SVN r20905] --- include/boost/generator_iterator.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/generator_iterator.hpp b/include/boost/generator_iterator.hpp index 2397288..bf8011f 100644 --- a/include/boost/generator_iterator.hpp +++ b/include/boost/generator_iterator.hpp @@ -24,15 +24,15 @@ class generator_iterator : public iterator_facade< generator_iterator , typename Generator::result_type - , readable_lvalue_iterator_tag , single_pass_traversal_tag + , typename Generator::result_type const& > { typedef iterator_facade< generator_iterator , typename Generator::result_type - , readable_iterator_tag , single_pass_traversal_tag + , typename Generator::result_type const& > super_t; public: From dae3cf2a9da6612d058fbfcd42a9a2e53f6237d1 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 26 Jul 2004 00:32:12 +0000 Subject: [PATCH 08/14] Converted to Boost Software License, Version 1.0 [SVN r24055] --- include/boost/generator_iterator.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/boost/generator_iterator.hpp b/include/boost/generator_iterator.hpp index bf8011f..ebf478b 100644 --- a/include/boost/generator_iterator.hpp +++ b/include/boost/generator_iterator.hpp @@ -1,8 +1,7 @@ -// (C) Copyright Jens Maurer 2001. Permission to copy, use, -// modify, sell and distribute this software is granted provided this -// copyright notice appears in all copies. This software is provided -// "as is" without express or implied warranty, and with no claim as -// to its suitability for any purpose. +// (C) Copyright Jens Maurer 2001. +// 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) // // Revision History: From 208400a1d077426cb069c557ed2e956e7fe99879 Mon Sep 17 00:00:00 2001 From: Aleksey Gurtovoy Date: Tue, 5 Oct 2004 15:45:52 +0000 Subject: [PATCH 09/14] c++boost.gif -> boost.png replacement [SVN r25573] --- generator_iterator.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index 4bd6734..ff14a3a 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -7,7 +7,7 @@ -c++boost.gif (8819 bytes) +boost.png (6897 bytes)

Generator Iterator Adaptor

Defined in header boost/generator_iterator.hpp From d1c93d3574920c94a6cc8c48f99b8549feffbadb Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 3 Aug 2005 13:01:57 +0000 Subject: [PATCH 10/14] Fix broken links [SVN r30403] --- generator_iterator.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index ff14a3a..fd3e3fc 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -58,7 +58,7 @@ template <class Generator> class generator_iterator_generator { public: - typedef iterator_adaptor<...> type; // the resulting generator iterator type + typedef unspecified type; // the resulting generator iterator type } From 39c44e0da3a581827edfbfac817730ecd4e582e9 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Tue, 5 Dec 2006 21:11:21 +0000 Subject: [PATCH 11/14] Fixed license & copyright issues and converted to HTML 4.01 [SVN r36280] --- generator_iterator.htm | 173 ++++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 80 deletions(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index fd3e3fc..c81d3d1 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -1,33 +1,37 @@ - + -Generator Iterator Adaptor Documentation + + + + Generator Iterator Adaptor Documentation - -boost.png (6897 bytes) + boost.png (6897 bytes) -

Generator Iterator Adaptor

-Defined in header boost/generator_iterator.hpp -

-The generator iterator adaptor makes it easier to create custom input -iterators from 0-ary functions and function objects. The adaptor -takes a -Generator -and creates a model of -Input Iterator. -Each increment retrieves an item from the generator and makes it -available to be retrieved by dereferencing. The motivation for this -iterator is that some concepts can be more naturally expressed as a -generator, while most STL algorithms expect an iterator. An example -is the Random Number library. +

Generator Iterator Adaptor

-

Synopsis

+

Defined in header boost/generator_iterator.hpp

-
-
+  

The generator iterator adaptor makes it easier to create custom input + iterators from 0-ary functions and function objects. The adaptor takes a + Generator and + creates a model of Input Iterator. Each + increment retrieves an item from the generator and makes it available to be + retrieved by dereferencing. The motivation for this iterator is that some + concepts can be more naturally expressed as a generator, while most STL + algorithms expect an iterator. An example is the Random Number library.

+ +

Synopsis

+ +
+
 namespace boost {
   template <class Generator>
   class generator_iterator_policies;
@@ -40,21 +44,19 @@ namespace boost {
   make_generator_iterator(Generator & gen);
 }
 
-
+
+
-
+

The Generator Iterator Generator Class

-

The Generator Iterator Generator Class

- -The class generator_iterator_generator is a helper class whose purpose -is to construct a generator iterator type. The template parameter for -this class is the Generator function object type that is being -wrapped. The generator iterator adaptor only holds a reference (or -pointer) to the function object, therefore the function object must -outlive the generator iterator adaptor constructed from it. - -
-template <class Generator>
+  

The class generator_iterator_generator is a helper class whose purpose + is to construct a generator iterator type. The template parameter for this + class is the Generator function object type that is being wrapped. The + generator iterator adaptor only holds a reference (or pointer) to the + function object, therefore the function object must outlive the generator + iterator adaptor constructed from it.

+
+template <class Generator>
 class generator_iterator_generator
 {
 public:
@@ -62,65 +64,65 @@ public:
 }
 
+

Template Parameters

-

Template Parameters

+ + + -
Parameter
- - - - + + - - -
ParameterDescription
Description
Generator -The generator (0-ary function object) type being -wrapped. The return type of the function must be defined as -Generator::result_type. The function object must be a model -of -Generator. -
+ + Generator -

Concept Model

-The generator iterator class is a model of -Input Iterator. + The generator (0-ary function object) type being wrapped. The + return type of the function must be defined as + Generator::result_type. The function object must be a model of + Generator. + + -

Members

-The generator iterator implements the member functions -and operators required of the -Input Iterator -concept. +

Concept Model

-
+

The generator iterator class is a model of Input Iterator.

-
-

The Generator Iterator Object Generator

+

Members

-The make_generator_iterator() function provides a -convenient way to create generator iterator objects. The function -saves the user the trouble of explicitly writing out the iterator -types. +

The generator iterator implements the member functions and operators + required of the Input Iterator + concept.

+
-
-
+  

The + Generator Iterator Object Generator

+ +

The make_generator_iterator() function provides a convenient + way to create generator iterator objects. The function saves the user the + trouble of explicitly writing out the iterator types.

+ +
+
 template <class Generator>
 typename generator_iterator_generator<Generator>::type
 make_generator_iterator(Generator & gen);
 
-
+
+
-
+

Example

+

The following program shows how generator_iterator + transforms a generator into an input iterator.

-

Example

- -The following program shows how generator_iterator -transforms a generator into an input iterator. - -
-
-#include <iostream>
-#include <boost/generator_iterator.hpp>
+  
+
+#include <iostream>
+#include <boost/generator_iterator.hpp>
 
 class my_generator
 {
@@ -140,11 +142,22 @@ int main()
     std::cout << *it << std::endl;
 }
 
-
+
+
-
+

Valid HTML 4.01 Transitional

-Written by Jens Maurer. +

Revised + 05 December, 2006

+

Copyright © 2001 Jens Maurer

+ +

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)

From 0b33da51ddbca7a3f43b9db14ade7c90f3459969 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 10 Feb 2008 14:56:22 +0000 Subject: [PATCH 12/14] Link to people pages on the website, as they've been removed from the download. [SVN r43209] --- generator_iterator.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index c81d3d1..6070522 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -153,7 +153,7 @@ int main() 05 December, 2006

Copyright © 2001 Jens Maurer

+ "http://www.boost.org/people/jens_maurer.htm">Jens Maurer

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or From c6f70f8394133404f2a9363c6f838ccfa80b7a0a Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sat, 16 May 2009 14:23:59 +0000 Subject: [PATCH 13/14] Use a local copy of the valid HTML 4.01 icon. [SVN r53048] --- generator_iterator.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator_iterator.htm b/generator_iterator.htm index 6070522..b0e376e 100644 --- a/generator_iterator.htm +++ b/generator_iterator.htm @@ -146,7 +146,7 @@ int main()


Valid HTML 4.01 Transitional

Revised From a83b13f4e6c69acf091f7bc01e6cb67a8001d5d7 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 5 Jun 2014 02:19:58 +0300 Subject: [PATCH 14/14] Added test for generator_iterator.hpp. --- generator_iterator_test.cpp | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 generator_iterator_test.cpp diff --git a/generator_iterator_test.cpp b/generator_iterator_test.cpp new file mode 100644 index 0000000..1c7a110 --- /dev/null +++ b/generator_iterator_test.cpp @@ -0,0 +1,63 @@ +// +// Copyright 2014 Peter Dimov +// +// 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 +#include +#include + +class X +{ +private: + + int v; + +public: + + typedef int result_type; + + X(): v( 0 ) + { + } + + int operator()() + { + return ++v; + } +}; + +template OutputIterator copy_n( InputIterator first, Size n, OutputIterator result ) +{ + while( n-- > 0 ) + { + *result++ = *first++; + } + + return result; +} + +void copy_test() +{ + X x; + boost::generator_iterator in( &x ); + + int const N = 4; + int v[ N ] = { 0 }; + + ::copy_n( in, 4, v ); + + BOOST_TEST_EQ( v[0], 1 ); + BOOST_TEST_EQ( v[1], 2 ); + BOOST_TEST_EQ( v[2], 3 ); + BOOST_TEST_EQ( v[3], 4 ); +} + +int main() +{ + copy_test(); + return boost::report_errors(); +}