From c43ed815a014759fed00649b2059ecabbbef292c Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Sun, 11 Feb 2001 20:05:00 +0000 Subject: [PATCH] new files [SVN r9137] --- indirect_iterator.htm | 199 ++++++++++++++++++++++++++++++++++ indirect_iterator_example.cpp | 27 +++++ 2 files changed, 226 insertions(+) create mode 100644 indirect_iterator.htm create mode 100644 indirect_iterator_example.cpp diff --git a/indirect_iterator.htm b/indirect_iterator.htm new file mode 100644 index 0000000..5e20d42 --- /dev/null +++ b/indirect_iterator.htm @@ -0,0 +1,199 @@ + + + + + + +Indirect Iterator Adaptor Documentation + + + + +c++boost.gif (8819 bytes) + +

Indirect Iterator Adaptor

+ +Defined in header +boost/iterator_adaptors.hpp + +

+The indirect iterator adaptor augments an iterator by applying an +extra dereference inside of operator*(). For example, +this iterator makes it possible to view containers of pointers such as +std::list<foo*> as if there were containers of the +pointed-to type (in this case std::list<foo>). The +following pseudo-code shows the basic idea of the indirect +iterator: + +

+  // inside a hypothetical indirect_iterator class...
+  typedef std::iterator_traits<BaseIterator>::value_type Pointer;
+  typedef std::iterator_traits<Pointer>::reference reference;
+
+  reference indirect_iterator::operator*() const {
+    return **this->base_iterator;
+  }
+
+ + +

Synopsis

+ +
+namespace boost {
+  template <class BaseIterator,
+            class Value, class Reference, class Pointer>
+  struct indirect_iterator_generator;
+  
+  template <class BaseIterator,
+	    class Value, class Pointer, class Reference, 
+            class ConstPointer, class ConstReference>
+  struct indirect_iterator_pair_generator;
+
+  template <class BaseIterator>
+  typename indirect_iterator_generator<BaseIterator>::type
+  make_indirect_iterator(BaseIterator base)  
+}
+
+ +
+ +

The Indirect Iterator Type +Generator

+ +The class indirect_iterator_generator is a helper class whose +purpose is to construct an indirect iterator type. The main template +parameter for this class is the BaseIterator type that is +being wrapped. In most cases the type of the elements being pointed to +can be deduced using std::iterator_traits, but in some +sitatuions the user may want to override these types, so there are +also template parameters for the type being pointed to (the +Value) as well as reference and pointer versions of the type. + +
+template <class BaseIterator,
+          class Value, class Reference, class Pointer>
+class indirect_iterator_generator
+{
+public:
+  typedef iterator_adaptor<...> type; // the resulting indirect iterator type 
+};
+
+ +

Example

+ +This example uses the indirect_iterator_generator to create +indirect iterators that dereference the pointers stored in the +pointers_to_chars array to access the char's in the +characters array. + +
+#include <boost/config.hpp>
+#include <vector>
+#include <iostream>
+#include <iterator>
+#include <boost/iterator_adaptors.hpp>
+
+int main(int, char*[])
+{
+  char characters[] = "abcdefg";
+  const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
+  char* pointers_to_chars[N];                        // at the end.
+  for (int i = 0; i < N; ++i)
+    pointers_to_chars[i] = &characters[i];
+  
+  boost::indirect_iterator_generator<char**, char>::type 
+    indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N);
+
+  std::copy(indirect_first, indirect_last, std::ostream_iterator<char>(std::cout, ","));
+  std::cout << std::endl;
+  
+  return 0;
+}
+
+ +

Template Parameters

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterDescription
BaseIteratorThe iterator type being wrapped. The value type of the base iterator +should be a pointer (a Trivial +Iterator).
ValueThe value-type of the value-type of the base iterator. That is, the +type of object that is accessed by dereferences in the base iterator twice.
+Default:
+std::iterator_traits<std::iterator_traits<BaseIterator>::value_type>::value_type +
ReferenceThe corresponding reference type for the Value. +Default: Value&
PointerThe corresponding pointer type for the Value. +Default: Value*
+ +

Model of

+ +If the base iterator is a model of Random +Access Iterator then so is the resulting indirect iterator. If +the base iterator supports less functionality than this the resulting +indirect iterator will also support less functionality. + +

Members

+ +The indirect iterator type implements the member functions and +operators required of the Random +Access Iterator concept. +In addition it has the following constructor: + +
+indirect_iterator_generator::type(const BaseIterator& it)
+
+ +

+


+

+ +

The Indirect Iterator Pair +Generator

+ + +

+


+

+ +

The Indirect Iterator Object Generator

+ + + +
+

Revised 10 Feb 2001

+

© Copyright Jeremy Siek 2000. Permission to copy, use, +modify, sell and distribute this document is granted provided this copyright +notice appears in all copies. This document is provided "as is" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

+ + + + diff --git a/indirect_iterator_example.cpp b/indirect_iterator_example.cpp new file mode 100644 index 0000000..75d2967 --- /dev/null +++ b/indirect_iterator_example.cpp @@ -0,0 +1,27 @@ +// (C) Copyright Jeremy Siek 2000. 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 characters[] = "abcdefg"; + const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char + char* pointers_to_chars[N]; // at the end. + for (int i = 0; i < N; ++i) + pointers_to_chars[i] = &characters[i]; + + boost::indirect_iterator_generator::type + indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N); + + std::copy(indirect_first, indirect_last, std::ostream_iterator(std::cout, ",")); + std::cout << std::endl; + + return 0; +}