From 45b6a92f61359287d78dd1ea26103f2cb7b1172f Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Mon, 12 Jan 2004 01:30:47 +0000 Subject: [PATCH] added make_xxx functions for some of the iterators [SVN r21611] --- doc/counting_iterator.html | 7 +- doc/facade-and-adaptor.html | 415 ++++++++++---------------- doc/filter_iterator.html | 65 +++- doc/filter_iterator.rst | 1 + doc/filter_iterator_eg.rst | 35 ++- doc/function_output_iterator.html | 201 +------------ doc/index.html | 201 +------------ doc/indirect_iterator.html | 201 +------------ doc/iterator_adaptor.html | 201 +------------ doc/iterator_facade.html | 332 +++++++-------------- doc/make_counting_iterator.rst | 9 + doc/make_filter_iterator.rst | 21 ++ doc/make_reverse_iterator.rst | 9 + doc/new-iter-concepts.html | 201 +------------ doc/permutation_iterator.html | 201 +------------ doc/reverse_iterator.html | 29 +- doc/reverse_iterator.rst | 1 + doc/reverse_iterator_eg.rst | 17 +- doc/transform_iterator.html | 51 ++-- example/counting_iterator_example.cpp | 54 ++++ example/filter_iterator_example.cpp | 60 ++++ example/reverse_iterator_example.cpp | 43 +++ 22 files changed, 626 insertions(+), 1729 deletions(-) create mode 100755 doc/make_counting_iterator.rst create mode 100755 doc/make_filter_iterator.rst create mode 100644 doc/make_reverse_iterator.rst create mode 100644 example/counting_iterator_example.cpp create mode 100644 example/filter_iterator_example.cpp create mode 100644 example/reverse_iterator_example.cpp diff --git a/doc/counting_iterator.html b/doc/counting_iterator.html index cba8045..6f9e8b4 100644 --- a/doc/counting_iterator.html +++ b/doc/counting_iterator.html @@ -7,7 +7,7 @@ Counting Iterator - + @@ -27,7 +27,7 @@ Lab, University of Hanover Institute for Transport Railway Operation and Construction Date: -2003-09-14 +2004-01-12 Copyright: Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved @@ -163,8 +163,7 @@ constructed from x.
 template <class Incrementable>
-counting_iterator<Incrementable>
-make_counting_iterator(Incrementable x)
+counting_iterator<Incrementable> make_counting_iterator(Incrementable x);
 
diff --git a/doc/facade-and-adaptor.html b/doc/facade-and-adaptor.html index a235706..0e52f59 100755 --- a/doc/facade-and-adaptor.html +++ b/doc/facade-and-adaptor.html @@ -3,203 +3,12 @@ - +Iterator Facade and Adaptor - +
@@ -308,20 +117,21 @@ by adapting other iterators.
  • filter_iterator requirements
  • -
  • filter_iterator operations
  • -
  • Counting iterator @@ -604,14 +414,16 @@ into the temporary iterator p+noperator[] returns.

    Writable iterators built with iterator_facade implement the semantics required by the preferred resolution to issue 299 and -adopted by proposal n1550: the result of p[n] is a proxy object -containing a copy of p+n, and p[n] = x is equivalent to *(p -+ n) = x. This approach will work properly for any random-access -iterator regardless of the other details of its implementation. A -user who knows more about the implementation of her iterator is free -to implement an operator[] which returns an lvalue in the derived -iterator class; it will hide the one supplied by iterator_facade -from clients of her iterator.

    +adopted by proposal n1550: the result of p[n] is an object +convertible to the iterator's value_type, and p[n] = x is +equivalent to *(p + n) = x (Note: This result object may be +implemented as a proxy containing a copy of p+n). This approach +will work properly for any random-access iterator regardless of the +other details of its implementation. A user who knows more about +the implementation of her iterator is free to implement an +operator[] that returns an lvalue in the derived iterator +class; it will hide the one supplied by iterator_facade from +clients of her iterator.

  • operator->

    @@ -1066,10 +878,13 @@ of type pointer equal to:

    - +
    Returns:an object convertible to reference and holding a copy -p of *static_cast<Derived const*>(this) + n such that, for a constant object v of type -value_type, (*static_cast<Derived const*>(this))[n] = v is equivalent -to p = v.
    Returns:an object convertible to value_type. For constant +objects v of type value_type, and n of type +difference_type, and reference p equal to +*static_cast<Derived const*>(this), (*this)[n] = v is +equivalent to *(p+ n) = v, and static_cast<value_type +const&>((*this)[n]) is equivalent to +static_cast<value_type const&>(*(p+n))
    @@ -1814,13 +1629,14 @@ iterator and the end of the range.

     template <class Predicate, class Iterator>
     class filter_iterator
    -    : public iterator_adaptor<
    -          filter_iterator<Predicate, Iterator>, Iterator
    -        , use_default
    -        , /* see details */
    -      >
     {
      public:
    +    typedef iterator_traits<Iterator>::value_type value_type;
    +    typedef iterator_traits<Iterator>::reference reference;
    +    typedef iterator_traits<Iterator>::pointer pointer;
    +    typedef iterator_traits<Iterator>::difference_type difference_type;
    +    typedef /* see below */ iterator_category;
    +
         filter_iterator();
         filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
         filter_iterator(Iterator x, Iterator end = Iterator());
    @@ -1831,45 +1647,122 @@ class filter_iterator
             );
         Predicate predicate() const;
         Iterator end() const;
    -
    - private: // as-if specification
    -    void increment()
    -    {
    -        ++(this->base_reference());
    -        satisfy_predicate();
    -    }
    -
    -    void satisfy_predicate()
    -    {
    -        while (this->base() != this->m_end && !this->m_predicate(*this->base()))
    -            ++(this->base_reference());
    -    }
    -
    -    Predicate m_predicate;
    -    Iterator m_end;
     };
     
    +

    The iterator_category is a type convertible to the tags +corresponding to each standard concept modeled by filter_iterator, +as described in the models section.

    filter_iterator requirements

    -

    The base Iterator parameter must be a model of Readable -Iterator and Single Pass Iterator. The resulting -filter_iterator will be a model of Forward Traversal Iterator -if Iterator is, otherwise the filter_iterator will be a -model of Single Pass Iterator. The access category of the -filter_iterator will be the same as the access category of -Iterator.

    - -

    The Predicate must be Assignable, Copy Constructible, and the -expression p(x) must be valid where p is an object of type +

    The Predicate argument must be Assignable, Copy Constructible, and +the expression p(x) must be valid where p is an object of type Predicate, x is an object of type iterator_traits<Iterator>::value_type, and where the type of p(x) must be convertible to bool.

    +

    The Iterator argument shall meet the requirements of Readable +Iterator and Single Pass Iterator or it shall meet the requirements of +Input Iterator.

    +
    +
    +

    filter_iterator models

    +

    The concepts that filter_iterator models are dependent on what +concepts the Iterator argument models, as specified in the +following tables.

    + ++++ + + + + + + + + + + + + + +
    If Iterator modelsthen filter_iterator models
    Single Pass IteratorSingle Pass Iterator
    Forward Traversal IteratorForward Traversal Iterator
    + ++++ + + + + + + + + + + + + + + + + +
    If Iterator modelsthen filter_iterator models
    Readable IteratorReadable Iterator
    Writable IteratorWritable Iterator
    Lvalue IteratorLvalue Iterator
    + ++++ + + + + + + + + + + + + + + + + +
    If Iterator modelsthen filter_iterator models
    Readable Iterator, Single Pass IteratorInput Iterator
    Readable Lvalue Iterator, Forward Traversal IteratorForward Iterator
    Writable Lvalue Iterator, Forward Traversal IteratorMutable Forward Iterator
    + ++++ + + + + + + + + + + + + + + + + +
    If Iterator modelsthen filter_iterator models
    Input IteratorInput Iterator, Readable Iterator, Single Pass Iterator
    Forward IteratorForward Iterator, Readable Lvalue Iterator, +Forward Traversal Iterator
    Mutable Forward IteratorMutable Forward Iterator, Writable Lvalue Iterator, +Forward Traversal Iterator
    -

    filter_iterator operations

    +

    filter_iterator operations

    +

    In addition to those operations required by the concepts that +filter_iterator models, filter_iterator provides the following +operations.

    filter_iterator();

    @@ -1937,19 +1830,19 @@ filter_iterator( - +
    Returns:The object end used to construct *this.
    Returns:A copy of the object end used to construct *this.
    -

    Counting iterator

    +

    Counting iterator

    The counting iterator adaptor implements dereference by returning a reference to the base object. The other operations are implemented by the base m_iterator, as per the inheritance from iterator_adaptor.

    -

    Class template counting_iterator

    +

    Class template counting_iterator

     template <
         class Incrementable
    @@ -1988,7 +1881,7 @@ the cases when the Incrementable
     
    -

    counting_iterator requirements

    +

    counting_iterator requirements

    The Incrementable type must be Default Constructible, Copy Constructible, and Assignable. The default distance is an implementation defined signed integegral type.

    @@ -2016,7 +1909,7 @@ i < j
    -

    counting_iterator operations

    +

    counting_iterator operations

    counting_iterator();

    @@ -2047,7 +1940,7 @@ object copy constructed from x
    -

    Function output iterator

    +

    Function output iterator

    The function output iterator adaptor makes it easier to create custom output iterators. The adaptor takes a unary function and creates a model of Output Iterator. Each item assigned to the output iterator is @@ -2056,7 +1949,7 @@ iterator is that creating a conforming output iterator is non-trivial, particularly because the proper implementation usually requires a proxy object.

    -

    Class template function_output_iterator

    +

    Class template function_output_iterator

     template <class UnaryFunction>
     class function_output_iterator {
    @@ -2084,7 +1977,7 @@ public:
     
    -

    function_output_iterator requirements

    +

    function_output_iterator requirements

    The UnaryFunction must be Assignable, Copy Constructible, and the expression f(x) must be valid, where f is an object of type UnaryFunction and x is an object of a type accepted by f. @@ -2092,7 +1985,7 @@ The resulting function_output_iterator

    -

    function_output_iterator operations

    +

    function_output_iterator operations

    explicit function_output_iterator(const UnaryFunction& f = UnaryFunction());

    @@ -2133,7 +2026,7 @@ a copy of the unary function f
    -

    function_output_iterator::output_proxy operations

    +

    function_output_iterator::output_proxy operations

    output_proxy(UnaryFunction& f);

    @@ -2167,11 +2060,5 @@ LocalWords: OtherIncrementable Coplien --> - - diff --git a/doc/filter_iterator.html b/doc/filter_iterator.html index b213de7..4ca83ec 100644 --- a/doc/filter_iterator.html +++ b/doc/filter_iterator.html @@ -268,11 +268,44 @@ or f(**this) ==
    +
    +template <class Predicate, class Iterator>
    +filter_iterator<Predicate,Iterator>
    +make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
    +
    + +++ + + + +
    Returns:An instance of filter_iterator<Predicate,Iterator> at +the first position in the range [x,end) such that +f(*this->base()) == true or else at position end.
    +
    +template <class Predicate, class Iterator>
    +filter_iterator<Predicate,Iterator>
    +make_filter_iterator(Iterator x, Iterator end = Iterator());
    +
    + +++ + + + +
    Returns:An instance of filter_iterator<Predicate,Iterator> at +the first position in the range [x,end) +such that f(*this->base()) == true, where f is a default +constructed Predicate, or else at position end.

    Example

    -

    This example uses filter_iterator to output only the positive -integers from an array of integers.

    +

    This example uses filter_iterator and then +make_filter_iterator to output only the positive integers from an +array of integers. Then make_filter_iterator is is used to output +the integers greater than -2.

     struct is_positive_number {
       bool operator()(int x) { return 0 < x; }
    @@ -286,6 +319,7 @@ int main()
       typedef int* base_iterator;
       base_iterator numbers(numbers_);
     
    +  // Example using filter_iterator
       typedef boost::filter_iterator<is_positive_number, base_iterator>
         FilterIter;
     
    @@ -296,12 +330,35 @@ int main()
       std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<int>(std::cout, " "));
       std::cout << std::endl;
     
    -  return 0;
    +  // Example using make_filter_iterator()
    +  std::copy(boost::make_filter_iterator<is_positive_number>(numbers, numbers + N),
    +            boost::make_filter_iterator<is_positive_number>(numbers + N, numbers + N),
    +            std::ostream_iterator<int>(std::cout, " "));
    +  std::cout << std::endl;
    +
    +  // Another example using make_filter_iterator()
    +  std::copy(
    +      boost::make_filter_iterator(
    +          std::bind2nd(std::greater<int>(), -2)
    +        , numbers, numbers + N)
    +
    +    , boost::make_filter_iterator(
    +          std::bind2nd(std::greater<int>(), -2)
    +        , numbers + N, numbers + N)
    +
    +    , std::ostream_iterator<int>(std::cout, " ")
    +  );
    +
    +  std::cout << std::endl;
    +
    +  return boost::exit_success;
     }
     

    The output is:

    -4 5 8
    +4 5 8 
    +4 5 8 
    +0 -1 4 5 8 
     
    diff --git a/doc/filter_iterator.rst b/doc/filter_iterator.rst index 3136e48..d460b5c 100644 --- a/doc/filter_iterator.rst +++ b/doc/filter_iterator.rst @@ -21,5 +21,6 @@ .. contents:: Table of Contents .. include:: filter_iterator_ref.rst +.. include:: make_filter_iterator.rst .. include:: filter_iterator_eg.rst diff --git a/doc/filter_iterator_eg.rst b/doc/filter_iterator_eg.rst index ae758e8..6b3f4af 100644 --- a/doc/filter_iterator_eg.rst +++ b/doc/filter_iterator_eg.rst @@ -2,8 +2,10 @@ Example ------- -This example uses ``filter_iterator`` to output only the positive -integers from an array of integers. +This example uses ``filter_iterator`` and then +``make_filter_iterator`` to output only the positive integers from an +array of integers. Then ``make_filter_iterator`` is is used to output +the integers greater than ``-2``. :: @@ -19,6 +21,7 @@ integers from an array of integers. typedef int* base_iterator; base_iterator numbers(numbers_); + // Example using filter_iterator typedef boost::filter_iterator FilterIter; @@ -29,10 +32,34 @@ integers from an array of integers. std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator(std::cout, " ")); std::cout << std::endl; - return 0; + // Example using make_filter_iterator() + std::copy(boost::make_filter_iterator(numbers, numbers + N), + boost::make_filter_iterator(numbers + N, numbers + N), + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + // Another example using make_filter_iterator() + std::copy( + boost::make_filter_iterator( + std::bind2nd(std::greater(), -2) + , numbers, numbers + N) + + , boost::make_filter_iterator( + std::bind2nd(std::greater(), -2) + , numbers + N, numbers + N) + + , std::ostream_iterator(std::cout, " ") + ); + + std::cout << std::endl; + + return boost::exit_success; } The output is:: - 4 5 8 + 4 5 8 + 4 5 8 + 0 -1 4 5 8 + diff --git a/doc/function_output_iterator.html b/doc/function_output_iterator.html index 8af8f32..33451c9 100644 --- a/doc/function_output_iterator.html +++ b/doc/function_output_iterator.html @@ -3,204 +3,13 @@ - + Function Output Iterator - +
    @@ -346,11 +155,5 @@ return *this;
    - - diff --git a/doc/index.html b/doc/index.html index d9338c3..6fa7d71 100755 --- a/doc/index.html +++ b/doc/index.html @@ -3,200 +3,9 @@ - + The Boost.Iterator Library Boost - + - - diff --git a/doc/indirect_iterator.html b/doc/indirect_iterator.html index f44890e..f95ceb4 100644 --- a/doc/indirect_iterator.html +++ b/doc/indirect_iterator.html @@ -3,204 +3,13 @@ - + Indirect Iterator - +
    @@ -387,11 +196,5 @@ indirect_iterator(
    - - diff --git a/doc/iterator_adaptor.html b/doc/iterator_adaptor.html index bd0ead3..ece40fa 100644 --- a/doc/iterator_adaptor.html +++ b/doc/iterator_adaptor.html @@ -3,204 +3,13 @@ - + Iterator Adaptor - +
    @@ -515,11 +324,5 @@ typename iterator_adaptor::difference_type distance_to(
    - - diff --git a/doc/iterator_facade.html b/doc/iterator_facade.html index 07ec2df..ea09999 100644 --- a/doc/iterator_facade.html +++ b/doc/iterator_facade.html @@ -3,204 +3,13 @@ - + Iterator Facade - + - +
    @@ -218,7 +27,7 @@ ul.auto-toc { Lab, University of Hanover Institute for Transport Railway Operation and Construction Date: -2003-11-24 +2004-01-11 Copyright: Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved @@ -244,12 +53,16 @@ and associated types, to be supplied by a derived iterator class.
  • operator->
  • -
  • Tutorial Example
  • -
  • Reference
      -
    • Ref
        -
      • iterator_facade usage
      • -
      • iterator_facade iterator category
      • -
      • iterator_facade operations
      • +
      • Tutorial Example +
      • +
      • Reference @@ -412,14 +225,16 @@ into the temporary iterator p+noperator[] returns.

        Writable iterators built with iterator_facade implement the semantics required by the preferred resolution to issue 299 and -adopted by proposal n1550: the result of p[n] is a proxy object -containing a copy of p+n, and p[n] = x is equivalent to *(p -+ n) = x. This approach will work properly for any random-access -iterator regardless of the other details of its implementation. A -user who knows more about the implementation of her iterator is free -to implement an operator[] which returns an lvalue in the derived -iterator class; it will hide the one supplied by iterator_facade -from clients of her iterator.

        +adopted by proposal n1550: the result of p[n] is an object +convertible to the iterator's value_type, and p[n] = x is +equivalent to *(p + n) = x (Note: This result object may be +implemented as a proxy containing a copy of p+n). This approach +will work properly for any random-access iterator regardless of the +other details of its implementation. A user who knows more about +the implementation of her iterator is free to implement an +operator[] that returns an lvalue in the derived iterator +class; it will hide the one supplied by iterator_facade from +clients of her iterator.

  • operator->

    @@ -445,12 +260,82 @@ Patterns, C++ Report, February 1995, pp. 24-27.

    Tutorial Example

    -

    I'm working on a Tutorial example to go here.

    + + + +
    +

    Introduction

    +

    In this section we'll walk through the implementation of a few +iterators using iterator_facade, based around the simple +example of a linked list of polymorphic objects.

    +
    +
    +

    The Problem

    +

    Say we've written a polymorphic linked list node as follows:

    +
    +# include <iostream>
    +
    +struct node_base
    +{
    +    node_base() : m_next(0) {}
    +
    +    virtual ~node_base() { delete m_next; }
    +
    +    node_base* next() const { return m_next; }
    +
    +    // print to the stream
    +    virtual void print(std::ostream& s) const = 0;
    +    
    +    // double the value
    +    virtual void twice() = 0;
    +
    +    void append(node_base* p)
    +    {
    +        if (m_next)
    +            m_next->append(p);
    +        else
    +            m_next = p;
    +    }
    +
    + private:
    +    node_base* m_next;
    +};
    +
    +

    Lists can hold objects of different types by linking together +specializations of the following template:

    +
    +template <class T>
    +struct node : node_base
    +{
    +    node(T x)
    +      : m_value(x)
    +    {}
    +
    +    void print(std::ostream& s) const { s << this->m_value; }
    +    void twice() { m_value += m_value; }
    +
    + private:
    +    T m_value;
    +};
    +
    +

    And we can print any node using the following streaming operator:

    +
    +inline std::ostream& operator<<(std::ostream& s, node_base const& n)
    +{
    +    n.print(s);
    +    return s;
    +}
    +
    + +
    -

    Reference

    +

    Reference

    -

    Ref

    +

    Ref

    We need to resolve the title levels here.

    @@ -563,7 +448,7 @@ struct enable_if_interoperable {};
    -

    iterator_facade usage

    +

    iterator_facade usage

    The following table describes the typical valid expressions on iterator_facade's Derived parameter, depending on the iterator concept(s) it will model. The operations in the first @@ -647,7 +532,7 @@ Iterator

    -

    iterator_facade iterator category

    +

    iterator_facade iterator category

    The iterator_category member of iterator_facade<X,V,R,C,D> is a type which satisfies the following conditions:

    @@ -700,7 +585,7 @@ convertible, and not to any more-derived traversal tag type.

    -

    iterator_facade operations

    +

    iterator_facade operations

    The operations in this section are described in terms of operations on the core interface of Derived which may be inaccessible (i.e. private). The implementation should access these operations @@ -736,10 +621,13 @@ of type pointer equal to:

    -Returns:an object convertible to reference and holding a copy -p of *static_cast<Derived const*>(this) + n such that, for a constant object v of type -value_type, (*static_cast<Derived const*>(this))[n] = v is equivalent -to p = v. +Returns:an object convertible to value_type. For constant +objects v of type value_type, and n of type +difference_type, and reference p equal to +*static_cast<Derived const*>(this), (*this)[n] = v is +equivalent to *(p+ n) = v, and static_cast<value_type +const&>((*this)[n]) is equivalent to +static_cast<value_type const&>(*(p+n)) @@ -840,11 +728,5 @@ return tmp -= n;
    - - diff --git a/doc/make_counting_iterator.rst b/doc/make_counting_iterator.rst new file mode 100755 index 0000000..f1c9ae9 --- /dev/null +++ b/doc/make_counting_iterator.rst @@ -0,0 +1,9 @@ + +:: + + template + counting_iterator make_counting_iterator(Incrementable x); + +:Returns: An instance of ``counting_iterator`` + with ``current`` constructed from ``x``. + diff --git a/doc/make_filter_iterator.rst b/doc/make_filter_iterator.rst new file mode 100755 index 0000000..8400d9d --- /dev/null +++ b/doc/make_filter_iterator.rst @@ -0,0 +1,21 @@ + +:: + + template + filter_iterator + make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator()); + +:Returns: An instance of ``filter_iterator`` at + the first position in the range ``[x,end)`` such that + ``f(*this->base()) == true`` or else at position ``end``. + +:: + + template + filter_iterator + make_filter_iterator(Iterator x, Iterator end = Iterator()); + +:Returns: An instance of ``filter_iterator`` at + the first position in the range ``[x,end)`` + such that ``f(*this->base()) == true``, where ``f`` is a default + constructed ``Predicate``, or else at position ``end``. \ No newline at end of file diff --git a/doc/make_reverse_iterator.rst b/doc/make_reverse_iterator.rst new file mode 100644 index 0000000..c3e20ed --- /dev/null +++ b/doc/make_reverse_iterator.rst @@ -0,0 +1,9 @@ +:: + + template + reverse_iteratorn + make_reverse_iterator(BidirectionalIterator x); + +:Returns: An instance of ``reverse_iterator`` + with a ``current`` constructed from ``x``. + diff --git a/doc/new-iter-concepts.html b/doc/new-iter-concepts.html index 8ea5c49..8b40d3e 100755 --- a/doc/new-iter-concepts.html +++ b/doc/new-iter-concepts.html @@ -3,204 +3,13 @@ - + New Iterator Concepts - +
    @@ -1072,11 +881,5 @@ LocalWords: incrementable xxx min prev inplace png oldeqnew AccessTag struct LocalWords: TraversalTag typename lvalues DWA Hmm JGS mis enum -->
    - - diff --git a/doc/permutation_iterator.html b/doc/permutation_iterator.html index ad043b2..d0e76a2 100644 --- a/doc/permutation_iterator.html +++ b/doc/permutation_iterator.html @@ -3,204 +3,13 @@ - + Permutation Iterator - +
    @@ -310,11 +119,5 @@ ForwardIterator instead of amortized constant time.

    - - diff --git a/doc/reverse_iterator.html b/doc/reverse_iterator.html index 5793b81..95f1b9f 100644 --- a/doc/reverse_iterator.html +++ b/doc/reverse_iterator.html @@ -153,6 +153,20 @@ return *--tmp; +
    +template <class BidirectionalIterator>
    +reverse_iterator<BidirectionalIterator>n
    +make_reverse_iterator(BidirectionalIterator x);
    +
    + +++ + + + +
    Returns:An instance of reverse_iterator<BidirectionalIterator> +with a current constructed from x.

    Example

    @@ -163,21 +177,28 @@ char letters_[] = "hello world!"; const int N = sizeof(letters_)/sizeof(char) - 1; typedef char* base_iterator; base_iterator letters(letters_); -std::cout << "original sequence of letters:\t" << letters_ << std::endl; +std::cout << "original sequence of letters:\t\t\t" << letters_ << std::endl; boost::reverse_iterator<base_iterator> reverse_letters_first(letters + N), reverse_letters_last(letters); -std::cout << "sequence in reverse order:\t"; +std::cout << "sequence in reverse order:\t\t\t"; std::copy(reverse_letters_first, reverse_letters_last, std::ostream_iterator<char>(std::cout)); std::cout << std::endl; + +std::cout << "sequence in double-reversed (normal) order:\t"; +std::copy(boost::make_reverse_iterator(reverse_letters_last), + boost::make_reverse_iterator(reverse_letters_first), + std::ostream_iterator<char>(std::cout)); +std::cout << std::endl;

    The output is:

    -original sequence of letters:   hello world!
    -sequence in reverse order:      !dlrow olleh
    +original sequence of letters:                   hello world!
    +sequence in reverse order:                      !dlrow olleh
    +sequence in double-reversed (normal) order:     hello world!
     
    diff --git a/doc/reverse_iterator.rst b/doc/reverse_iterator.rst index 83d1e49..9bfa857 100644 --- a/doc/reverse_iterator.rst +++ b/doc/reverse_iterator.rst @@ -24,5 +24,6 @@ ----------------------------- .. include:: reverse_iterator_ref.rst +.. include:: make_reverse_iterator.rst .. include:: reverse_iterator_eg.rst diff --git a/doc/reverse_iterator_eg.rst b/doc/reverse_iterator_eg.rst index 549f1c4..eee8f66 100644 --- a/doc/reverse_iterator_eg.rst +++ b/doc/reverse_iterator_eg.rst @@ -11,20 +11,27 @@ using ``reverse_iterator``. const int N = sizeof(letters_)/sizeof(char) - 1; typedef char* base_iterator; base_iterator letters(letters_); - std::cout << "original sequence of letters:\t" << letters_ << std::endl; + std::cout << "original sequence of letters:\t\t\t" << letters_ << std::endl; boost::reverse_iterator reverse_letters_first(letters + N), reverse_letters_last(letters); - std::cout << "sequence in reverse order:\t"; + std::cout << "sequence in reverse order:\t\t\t"; std::copy(reverse_letters_first, reverse_letters_last, - std::ostream_iterator(std::cout)); + std::ostream_iterator(std::cout)); + std::cout << std::endl; + + std::cout << "sequence in double-reversed (normal) order:\t"; + std::copy(boost::make_reverse_iterator(reverse_letters_last), + boost::make_reverse_iterator(reverse_letters_first), + std::ostream_iterator(std::cout)); std::cout << std::endl; The output is:: - original sequence of letters: hello world! - sequence in reverse order: !dlrow olleh + original sequence of letters: hello world! + sequence in reverse order: !dlrow olleh + sequence in double-reversed (normal) order: hello world! diff --git a/doc/transform_iterator.html b/doc/transform_iterator.html index 685d240..9afeef0 100644 --- a/doc/transform_iterator.html +++ b/doc/transform_iterator.html @@ -7,8 +7,8 @@ Transform Iterator - - + + @@ -27,9 +27,9 @@ Lab, University of Hanover Institute for Transport Railway Operation and Construction Date: -2003-08-05 +2003-09-14 Copyright: -Copyright Dave Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved +Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved @@ -53,8 +53,9 @@ then returns the result.

  • transform_iterator private operations
  • +
    -template <class AdaptableUnaryFunction,
    +template <class UnaryFunction,
               class Iterator, 
               class Reference = use_default, 
               class Value = use_default>
    @@ -64,44 +65,44 @@ class transform_iterator
       friend class iterator_core_access;
     public:
       transform_iterator();
    -  transform_iterator(Iterator const& x, AdaptableUnaryFunction f);
    +  transform_iterator(Iterator const& x, UnaryFunction f);
     
    -  template<class OtherIterator, class R2, class V2>
    +  template<class F2, class I2, class R2, class V2>
       transform_iterator(
    -        transform_iterator<AdaptableUnaryFunction, OtherIterator, R2, V2> const& t
    -      , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition
    +        transform_iterator<F2, I2, R2, V2> const& t
    +      , typename enable_if_convertible<I2, Iterator>::type* = 0      // exposition
    +      , typename enable_if_convertible<F2, UnaryFunction>::type* = 0 // exposition
       );
     
    -  AdaptableUnaryFunction functor() const;
    +  UnaryFunction functor() const;
     private:
       typename transform_iterator::value_type dereference() const;
    -  AdaptableUnaryFunction m_f;
    +  UnaryFunction m_f;
     };
     

    transform_iterator requirements

    -

    The type AdaptableUnaryFunction must be Assignable, Copy -Constructible, and the expression f(x) must be valid where f -is an object of type AdaptableUnaryFunction, x is an object of -type AdaptableUnaryFunction::argument_type, and where the type of -f(x) must be AdaptableUnaryFunction::result_type.

    +

    The type UnaryFunction must be Assignable, Copy Constructible, and +the expression f(*i) must be valid where f is an object of +type UnaryFunction, i is an object of type Iterator, and +where the type of f(*i) must be +result_of<UnaryFunction(iterator_traits<Iterator>::reference)>::type.

    The type Iterator must at least model Readable Iterator. The resulting transform_iterator models the most refined of the following options that is also modeled by Iterator.

      -
    • Writable Lvalue Iterator if the result_type of the -AdaptableUnaryFunction is a non-const reference.
    • -
    • Readable Lvalue Iterator if the result_type is a const +
    • Writable Lvalue Iterator if result_of<UnaryFunction(iterator_traits<Iterator>::reference)>::type is a non-const reference.
    • +
    • Readable Lvalue Iterator if result_of<UnaryFunction(iterator_traits<Iterator>::reference)>::type is a const reference.
    • Readable Iterator otherwise.

    The transform_iterator models the most refined standard traversal concept that is modeled by Iterator.

    -

    The value_type of transform_iterator is -remove_reference<result_type>::type. The reference type is -result_type.

    +

    The reference type of transform_iterator is +result_of<UnaryFunction(iterator_traits<Iterator>::reference)>::type. +The value_type is remove_cv<remove_reference<reference> >::type.

    transform_iterator public operations

    @@ -115,7 +116,7 @@ and m_iterator default constru
    -

    transform_iterator(Iterator const& x, AdaptableUnaryFunction f);

    +

    transform_iterator(Iterator const& x, UnaryFunction f);

    @@ -128,7 +129,7 @@ initialized to f and template<class OtherIterator, class R2, class V2> transform_iterator( - transform_iterator<AdaptableUnaryFunction, OtherIterator, R2, V2> const& t + transform_iterator<UnaryFunction, OtherIterator, R2, V2> const& t , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition ); @@ -142,7 +143,7 @@ transform_iterator(
    -

    AdaptableUnaryFunction functor() const;

    +

    UnaryFunction functor() const;

    diff --git a/example/counting_iterator_example.cpp b/example/counting_iterator_example.cpp new file mode 100644 index 0000000..4113075 --- /dev/null +++ b/example/counting_iterator_example.cpp @@ -0,0 +1,54 @@ +// (C) Copyright Jeremy Siek 2000-2004. +// 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. + + +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int, char*[]) +{ + // Example of using counting_iterator + std::cout << "counting from 0 to 4:" << std::endl; + boost::counting_iterator first(0), last(4); + std::copy(first, last, std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + // Example of using counting iterator to create an array of pointers. + int N = 7; + std::vector numbers; + typedef std::vector::iterator n_iter; + // Fill "numbers" array with [0,N) + std::copy( + boost::counting_iterator(0) + , boost::counting_iterator(N) + , std::back_inserter(numbers)); + + std::vector::iterator> pointers; + + // Use counting iterator to fill in the array of pointers. + // causes an ICE with MSVC6 + std::copy(boost::make_counting_iterator(numbers.begin()), + boost::make_counting_iterator(numbers.end()), + std::back_inserter(pointers)); + + // Use indirect iterator to print out numbers by accessing + // them through the array of pointers. + std::cout << "indirectly printing out the numbers from 0 to " + << N << std::endl; + std::copy(boost::make_indirect_iterator(pointers.begin()), + boost::make_indirect_iterator(pointers.end()), + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + return boost::exit_success; +} diff --git a/example/filter_iterator_example.cpp b/example/filter_iterator_example.cpp new file mode 100644 index 0000000..5748daa --- /dev/null +++ b/example/filter_iterator_example.cpp @@ -0,0 +1,60 @@ +// (C) Copyright Jeremy Siek 1999-2004. +// 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. + +#include +#include +#include +#include +#include +#include // for exit_success + +struct is_positive_number { + bool operator()(int x) { return 0 < x; } +}; + +int main() +{ + int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 }; + const int N = sizeof(numbers_)/sizeof(int); + + typedef int* base_iterator; + base_iterator numbers(numbers_); + + // Example using make_filter_iterator() + std::copy(boost::make_filter_iterator(numbers, numbers + N), + boost::make_filter_iterator(numbers + N, numbers + N), + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + // Example using filter_iterator + typedef boost::filter_iterator + FilterIter; + + is_positive_number predicate; + FilterIter filter_iter_first(predicate, numbers, numbers + N); + FilterIter filter_iter_last(predicate, numbers + N, numbers + N); + + std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + // Another example using make_filter_iterator() + std::copy( + boost::make_filter_iterator( + std::bind2nd(std::greater(), -2) + , numbers, numbers + N) + + , boost::make_filter_iterator( + std::bind2nd(std::greater(), -2) + , numbers + N, numbers + N) + + , std::ostream_iterator(std::cout, " ") + ); + + std::cout << std::endl; + + return boost::exit_success; +} diff --git a/example/reverse_iterator_example.cpp b/example/reverse_iterator_example.cpp new file mode 100644 index 0000000..34c14a8 --- /dev/null +++ b/example/reverse_iterator_example.cpp @@ -0,0 +1,43 @@ +// (C) Copyright Jeremy Siek 2000-2004. +// 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. + +#include +#include +#include +#include +#include + +int main(int, char*[]) +{ + char letters_[] = "hello world!"; + const int N = sizeof(letters_)/sizeof(char) - 1; + typedef char* base_iterator; + base_iterator letters(letters_); + + std::cout << "original sequence of letters:\t\t\t" + << letters_ << std::endl; + + // Use reverse_iterator to print a sequence of letters in reverse + // order. + + boost::reverse_iterator + reverse_letters_first(letters + N), + reverse_letters_last(letters); + + std::cout << "sequence in reverse order:\t\t\t"; + std::copy(reverse_letters_first, reverse_letters_last, + std::ostream_iterator(std::cout)); + std::cout << std::endl; + + std::cout << "sequence in double-reversed (normal) order:\t"; + std::copy(boost::make_reverse_iterator(reverse_letters_last), + boost::make_reverse_iterator(reverse_letters_first), + std::ostream_iterator(std::cout)); + std::cout << std::endl; + + return boost::exit_success; +}