From 07115d26c773dcc903843ab3cfc616d49e7db6bd Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Mon, 12 Feb 2001 01:50:50 +0000 Subject: [PATCH] finished 1st draft [SVN r9143] --- indirect_iterator.htm | 176 ++++++++++++++++++++++++++++++++-- indirect_iterator_example.cpp | 34 +++++++ 2 files changed, 202 insertions(+), 8 deletions(-) diff --git a/indirect_iterator.htm b/indirect_iterator.htm index 5e20d42..fda27b9 100644 --- a/indirect_iterator.htm +++ b/indirect_iterator.htm @@ -66,9 +66,9 @@ 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. +situations the user may want to override this type, 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,
@@ -108,8 +108,7 @@ int main(int, char*[])
   std::copy(indirect_first, indirect_last, std::ostream_iterator<char>(std::cout, ","));
   std::cout << std::endl;
   
-  return 0;
-}
+  // to be continued...
 

Template Parameters

@@ -138,13 +137,13 @@ type of object that is accessed by dereferences in the base iterator twice.
Reference -The corresponding reference type for the Value. +The corresponding reference type for the Value.
Default: Value& Pointer -The corresponding pointer type for the Value. +The corresponding pointer type for the Value.
Default: Value* @@ -174,9 +173,132 @@ indirect_iterator_generator::type(const BaseIterator& it)

-

The Indirect Iterator Pair +

The Indirect Iterator Pair Generator

+Sometimes a mutable/const pair of iterator types is needed, such as +when implementing a container type. The +indirect_iterator_pair_generator class makes it more +convenient to create this pair of iterator types. + +
+template <class BaseIterator,
+          class Value, class Pointer, class Reference,
+          class ConstPointer, class ConstReference>
+class indirect_iterator_pair_generator
+{
+public:
+  typedef iterator_adaptor<...> iterator;       // the mutable indirect iterator type 
+  typedef iterator_adaptor<...> const_iterator; // the immutable indirect iterator type 
+};
+
+ +

Example

+ +
+  // continuing from the last example...
+
+  typedef boost::indirect_iterator_pair_generator PairGen;
+
+  char mutable_characters[N];
+  char* pointers_to_mutable_chars[N];
+  for (int i = 0; i < N; ++i)
+    pointers_to_mutable_chars[i] = &mutable_characters[i];
+
+  PairGen::iterator mutable_indirect_first(pointers_to_mutable_chars),
+    mutable_indirect_last(pointers_to_mutable_chars + N);
+  PairGen::const_iterator const_indirect_first(pointers_to_chars),
+    const_indirect_last(pointers_to_chars + N);
+
+  std::transform(const_indirect_first, const_indirect_last,
+		 mutable_indirect_first, std::bind1st(std::plus(), 1));
+
+  std::copy(mutable_indirect_first, mutable_indirect_last,
+	    std::ostream_iterator(std::cout, ","));
+  std::cout << std::endl;
+  // to be continued...
+
+The output is: +
+b,c,d,e,f,g,h,
+
+ +

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*
ConstReferenceThe corresponding const reference type for the Value.
+Default: const Value&
ConstPointerThe corresponding const pointer type for the Value.
+Default: const Value*
+ +

Model of

+ +If the base iterator is a model of Random +Access Iterator then so is the resulting indirect iterator types. +If the base iterator supports less functionality than this the +resulting indirect iterator types will also support less +functionality. The resulting iterator type is mutable, and +the resulting const_iterator type is constant. + +

Members

+ +The resulting iterator and const_iterator types +implements the member functions and operators required of the Random +Access Iterator concept. In addition they support the following +constructors: + +
+indirect_iterator_pair_generator::iterator(const BaseIterator& it)
+
+ +
+indirect_iterator_pair_generator::const_iterator(const BaseIterator& it)
+


@@ -184,7 +306,39 @@ Generator

The Indirect Iterator Object Generator

+The make_indirect_iterator() function provides a more +convenient way to create indirect iterator objects. The function saves +the user the trouble of explicitly writing out the iterator types. +
+  template <class BaseIterator>
+  typename indirect_iterator_generator<BaseIterator>::type
+  make_indirect_iterator(BaseIterator base)  
+
+ + +

Example

+ +Here we again print the char's from the array +characters by accessing them through the array of pointers +pointer_to_chars, but this time we use the +make_indirect_iterator() function which saves us some typing. + +
+  // continuing from the last example...
+
+  std::copy(boost::make_indirect_iterator(pointers_to_chars), 
+	    boost::make_indirect_iterator(pointers_to_chars + N),
+	    std::ostream_iterator(std::cout, ","));
+  std::cout << std::endl;
+
+  return 0;
+}
+
+The output is: +
+a,b,c,d,e,f,g,
+

Revised 10 Feb 2001

@@ -197,3 +351,9 @@ any purpose.

+ + + diff --git a/indirect_iterator_example.cpp b/indirect_iterator_example.cpp index 75d2967..8f1d389 100644 --- a/indirect_iterator_example.cpp +++ b/indirect_iterator_example.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include int main(int, char*[]) @@ -16,6 +17,8 @@ int main(int, char*[]) char* pointers_to_chars[N]; // at the end. for (int i = 0; i < N; ++i) pointers_to_chars[i] = &characters[i]; + + // Example of using indirect_iterator_generator boost::indirect_iterator_generator::type indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N); @@ -23,5 +26,36 @@ int main(int, char*[]) std::copy(indirect_first, indirect_last, std::ostream_iterator(std::cout, ",")); std::cout << std::endl; + + // Example of using indirect_iterator_pair_generator + + typedef boost::indirect_iterator_pair_generator PairGen; + + char mutable_characters[N]; + char* pointers_to_mutable_chars[N]; + for (int i = 0; i < N; ++i) + pointers_to_mutable_chars[i] = &mutable_characters[i]; + + PairGen::iterator mutable_indirect_first(pointers_to_mutable_chars), + mutable_indirect_last(pointers_to_mutable_chars + N); + PairGen::const_iterator const_indirect_first(pointers_to_chars), + const_indirect_last(pointers_to_chars + N); + + std::transform(const_indirect_first, const_indirect_last, + mutable_indirect_first, std::bind1st(std::plus(), 1)); + + std::copy(mutable_indirect_first, mutable_indirect_last, + std::ostream_iterator(std::cout, ",")); + std::cout << std::endl; + + + // Example of using make_indirect_iterator() + + std::copy(boost::make_indirect_iterator(pointers_to_chars), + boost::make_indirect_iterator(pointers_to_chars + N), + std::ostream_iterator(std::cout, ",")); + std::cout << std::endl; + return 0; }