mirror of
https://github.com/boostorg/iterator.git
synced 2025-05-11 21:43:54 +00:00
335 lines
18 KiB
HTML
335 lines
18 KiB
HTML
<?xml version="1.0" encoding="utf-8" ?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="Docutils 0.2.8: http://docutils.sourceforge.net/" />
|
|
<title>Indirect Iterator</title>
|
|
<meta name="author" content="David Abrahams, Jeremy Siek, Thomas Witt" />
|
|
<meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction" />
|
|
<meta name="date" content="2004-01-13" />
|
|
<meta name="copyright" content="Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved" />
|
|
<link rel="stylesheet" href="default.css" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div class="document" id="indirect-iterator">
|
|
<h1 class="title">Indirect Iterator</h1>
|
|
<table class="docinfo" frame="void" rules="none">
|
|
<col class="docinfo-name" />
|
|
<col class="docinfo-content" />
|
|
<tbody valign="top">
|
|
<tr><th class="docinfo-name">Author:</th>
|
|
<td>David Abrahams, Jeremy Siek, Thomas Witt</td></tr>
|
|
<tr><th class="docinfo-name">Contact:</th>
|
|
<td><a class="first reference" href="mailto:dave@boost-consulting.com">dave@boost-consulting.com</a>, <a class="reference" href="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>, <a class="last reference" href="mailto:witt@ive.uni-hannover.de">witt@ive.uni-hannover.de</a></td></tr>
|
|
<tr><th class="docinfo-name">Organization:</th>
|
|
<td><a class="first reference" href="http://www.boost-consulting.com">Boost Consulting</a>, Indiana University <a class="reference" href="http://www.osl.iu.edu">Open Systems
|
|
Lab</a>, University of Hanover <a class="last reference" href="http://www.ive.uni-hannover.de">Institute for Transport
|
|
Railway Operation and Construction</a></td></tr>
|
|
<tr><th class="docinfo-name">Date:</th>
|
|
<td>2004-01-13</td></tr>
|
|
<tr><th class="docinfo-name">Copyright:</th>
|
|
<td>Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved</td></tr>
|
|
</tbody>
|
|
</table>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">abstract:</th><td class="field-body"><p class="first">The indirect iterator adapts an iterator by applying an <em>extra</em>
|
|
dereference inside of <tt class="literal"><span class="pre">operator*()</span></tt>. For example, this iterator
|
|
adaptor makes it possible to view a container of pointers
|
|
(e.g. <tt class="literal"><span class="pre">list<foo*></span></tt>) as if it were a container of the pointed-to type
|
|
(e.g. <tt class="literal"><span class="pre">list<foo></span></tt>) .</p>
|
|
<!-- At some point we should add the capability to handle
|
|
iterators over smart pointers, which the impl handles. -JGS -->
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<div class="contents topic" id="table-of-contents">
|
|
<p class="topic-title"><a name="table-of-contents">Table of Contents</a></p>
|
|
<ul class="simple">
|
|
<li><a class="reference" href="#indirect-iterator-synopsis" id="id2" name="id2"><tt class="literal"><span class="pre">indirect_iterator</span></tt> synopsis</a></li>
|
|
<li><a class="reference" href="#indirect-iterator-requirements" id="id3" name="id3"><tt class="literal"><span class="pre">indirect_iterator</span></tt> requirements</a></li>
|
|
<li><a class="reference" href="#indirect-iterator-models" id="id4" name="id4"><tt class="literal"><span class="pre">indirect_iterator</span></tt> models</a></li>
|
|
<li><a class="reference" href="#indirect-iterator-operations" id="id5" name="id5"><tt class="literal"><span class="pre">indirect_iterator</span></tt> operations</a></li>
|
|
<li><a class="reference" href="#example" id="id6" name="id6">Example</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section" id="indirect-iterator-synopsis">
|
|
<h1><a class="toc-backref" href="#id2" name="indirect-iterator-synopsis"><tt class="literal"><span class="pre">indirect_iterator</span></tt> synopsis</a></h1>
|
|
<pre class="literal-block">
|
|
template <
|
|
class Iterator
|
|
, class Value = use_default
|
|
, class CategoryOrTraversal = use_default
|
|
, class Reference = use_default
|
|
, class Difference = use_default
|
|
>
|
|
class indirect_iterator
|
|
{
|
|
public:
|
|
typedef /* see below */ value_type;
|
|
typedef /* see below */ reference;
|
|
typedef /* see below */ pointer;
|
|
typedef /* see below */ difference_type;
|
|
typedef /* see below */ iterator_category;
|
|
|
|
indirect_iterator();
|
|
indirect_iterator(Iterator x);
|
|
|
|
template <
|
|
class Iterator2, class Value2, class Category2
|
|
, class Reference2, class Difference2
|
|
>
|
|
indirect_iterator(
|
|
indirect_iterator<
|
|
Iterator2, Value2, Category2, Reference2, Difference2
|
|
> const& y
|
|
, typename enable_if_convertible<Iterator2, Iterator>::type* = 0 // exposition
|
|
);
|
|
|
|
Iterator base() const;
|
|
reference operator*() const;
|
|
indirect_iterator& operator++();
|
|
indirect_iterator& operator--();
|
|
private:
|
|
Iterator m_iterator; // exposition
|
|
};
|
|
</pre>
|
|
<p>The member types of <tt class="literal"><span class="pre">indirect_iterator</span></tt> are defined according to the
|
|
following pseudo-code. We use the abbreviation
|
|
<tt class="literal"><span class="pre">V=iterator_traits<Iterator>::value_type</span></tt>.:</p>
|
|
<pre class="literal-block">
|
|
if (Value is use_default) then
|
|
typedef iterator_traits<V>::value_type value_type;
|
|
else
|
|
typedef remove_const<Value>::type value_type;
|
|
|
|
if (Reference is use_default) then
|
|
if (Value is use_default) then
|
|
typedef iterator_traits<V>::reference reference;
|
|
else
|
|
typedef Value& reference;
|
|
else
|
|
typedef Reference reference;
|
|
|
|
if (Value is use_default) then
|
|
typedef iterator_traits<V>::value_type* pointer;
|
|
else
|
|
typedef Value* pointer;
|
|
|
|
if (Difference is use_default)
|
|
typedef iterator_traits<Iterator>::difference_type difference_type;
|
|
else
|
|
typedef Difference difference_type;
|
|
</pre>
|
|
<p>If <tt class="literal"><span class="pre">CategoryOrTraversal</span></tt> is not <tt class="literal"><span class="pre">use_default</span></tt> then
|
|
<tt class="literal"><span class="pre">iterator_category</span></tt> is <tt class="literal"><span class="pre">CategoryOrTraversal</span></tt>. Otherwise
|
|
<tt class="literal"><span class="pre">iterator_category</span></tt> is a type convertible to the tag determined by
|
|
the following algorithm. Let <tt class="literal"><span class="pre">C</span></tt> be <tt class="literal"><span class="pre">traveral_category<Iterator>::type</span></tt>.</p>
|
|
<pre class="literal-block">
|
|
if (reference is a reference to value_type) then
|
|
if (C is convertible to random_access_traversal_tag) then
|
|
random_access_iterator_tag
|
|
else if (C is convertible to bidirectional_traversal_tag) then
|
|
bidirectional_iterator_tag
|
|
else
|
|
forward_iterator_tag
|
|
else
|
|
input_iterator_tag
|
|
</pre>
|
|
</div>
|
|
<div class="section" id="indirect-iterator-requirements">
|
|
<h1><a class="toc-backref" href="#id3" name="indirect-iterator-requirements"><tt class="literal"><span class="pre">indirect_iterator</span></tt> requirements</a></h1>
|
|
<p>The <tt class="literal"><span class="pre">Iterator</span></tt> argument shall meet the requirements of Readable
|
|
Iterator. The <tt class="literal"><span class="pre">CategoryOrTraversal</span></tt> argument shall be one of the
|
|
standard iterator tags or <tt class="literal"><span class="pre">use_default</span></tt>. If <tt class="literal"><span class="pre">CategoryOrTraversal</span></tt>
|
|
is an iterator tag, the template parameter <tt class="literal"><span class="pre">Iterator</span></tt> argument shall
|
|
meet the requirements corresponding to the iterator tag.</p>
|
|
<p>The expression <tt class="literal"><span class="pre">*v</span></tt>, where <tt class="literal"><span class="pre">v</span></tt> is an object of type
|
|
<tt class="literal"><span class="pre">iterator_traits<Iterator>::value_type</span></tt>, must be a valid expression
|
|
and must be convertible to <tt class="literal"><span class="pre">indirect_iterator::reference</span></tt>. Also
|
|
<tt class="literal"><span class="pre">indirect_iterator::reference</span></tt> must be convertible to
|
|
<tt class="literal"><span class="pre">indirect_iterator::value</span></tt>. There are further requirements on the
|
|
<tt class="literal"><span class="pre">iterator_traits<Iterator>::value_type</span></tt> if the <tt class="literal"><span class="pre">Value</span></tt> parameter
|
|
is not <tt class="literal"><span class="pre">use_default</span></tt>, as implied by the algorithm for deducing the
|
|
default for the <tt class="literal"><span class="pre">value_type</span></tt> member.</p>
|
|
</div>
|
|
<div class="section" id="indirect-iterator-models">
|
|
<h1><a class="toc-backref" href="#id4" name="indirect-iterator-models"><tt class="literal"><span class="pre">indirect_iterator</span></tt> models</a></h1>
|
|
<p>If <tt class="literal"><span class="pre">CategoryOrTraversal</span></tt> is a standard iterator tag,
|
|
<tt class="literal"><span class="pre">indirect_iterator</span></tt> is a model of the iterator concept corresponding
|
|
to the tag, otherwise <tt class="literal"><span class="pre">indirect_iterator</span></tt> satisfies the requirements
|
|
of the most refined standard traversal concept that is satisfied by
|
|
the <tt class="literal"><span class="pre">Iterator</span></tt> argument.</p>
|
|
<p><tt class="literal"><span class="pre">indirect_iterator</span></tt> models Readable Iterator. If
|
|
<tt class="literal"><span class="pre">indirect_iterator::reference(*v)</span> <span class="pre">=</span> <span class="pre">t</span></tt> is a valid expression (where
|
|
<tt class="literal"><span class="pre">t</span></tt> is an object of type <tt class="literal"><span class="pre">indirect_iterator::value_type</span></tt>) then
|
|
<tt class="literal"><span class="pre">indirect_iterator</span></tt> models Writable Iterator. If
|
|
<tt class="literal"><span class="pre">indirect_iterator::reference</span></tt> is a reference then
|
|
<tt class="literal"><span class="pre">indirect_iterator</span></tt> models Lvalue Iterator.</p>
|
|
</div>
|
|
<div class="section" id="indirect-iterator-operations">
|
|
<h1><a class="toc-backref" href="#id5" name="indirect-iterator-operations"><tt class="literal"><span class="pre">indirect_iterator</span></tt> operations</a></h1>
|
|
<p>In addition to the operations required by the concepts modeled by
|
|
<tt class="literal"><span class="pre">indirect_iterator</span></tt>, <tt class="literal"><span class="pre">indirect_iterator</span></tt> provides the following
|
|
operations.</p>
|
|
<p><tt class="literal"><span class="pre">indirect_iterator();</span></tt></p>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="literal"><span class="pre">Iterator</span></tt> must be Default Constructible.</td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="literal"><span class="pre">indirect_iterator</span></tt> with
|
|
a default-constructed <tt class="literal"><span class="pre">m_iterator</span></tt>.</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p><tt class="literal"><span class="pre">indirect_iterator(Iterator</span> <span class="pre">x);</span></tt></p>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="literal"><span class="pre">indirect_iterator</span></tt> with
|
|
<tt class="literal"><span class="pre">m_iterator</span></tt> copy constructed from <tt class="literal"><span class="pre">x</span></tt>.</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<pre class="literal-block">
|
|
template <
|
|
class Iterator2, class Value2, unsigned Access, class Traversal
|
|
, class Reference2, class Difference2
|
|
>
|
|
indirect_iterator(
|
|
indirect_iterator<
|
|
Iterator2, Value2, Access, Traversal, Reference2, Difference2
|
|
> const& y
|
|
, typename enable_if_convertible<Iterator2, Iterator>::type* = 0 // exposition
|
|
);
|
|
</pre>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="literal"><span class="pre">Iterator2</span></tt> is implicitly convertible to <tt class="literal"><span class="pre">Iterator</span></tt>.</td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="literal"><span class="pre">indirect_iterator</span></tt> whose
|
|
<tt class="literal"><span class="pre">m_iterator</span></tt> subobject is constructed from <tt class="literal"><span class="pre">y.base()</span></tt>.</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p><tt class="literal"><span class="pre">Iterator</span> <span class="pre">base()</span> <span class="pre">const;</span></tt></p>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="literal"><span class="pre">m_iterator</span></tt></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p><tt class="literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="literal"><span class="pre">**m_iterator</span></tt></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p><tt class="literal"><span class="pre">indirect_iterator&</span> <span class="pre">operator++();</span></tt></p>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="literal"><span class="pre">++m_iterator</span></tt></td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="literal"><span class="pre">*this</span></tt></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p><tt class="literal"><span class="pre">indirect_iterator&</span> <span class="pre">operator--();</span></tt></p>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="literal"><span class="pre">--m_iterator</span></tt></td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="literal"><span class="pre">*this</span></tt></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="section" id="example">
|
|
<h1><a class="toc-backref" href="#id6" name="example">Example</a></h1>
|
|
<p>This example prints an array of characters, using
|
|
<tt class="literal"><span class="pre">indirect_iterator</span></tt> to access the array of characters through an
|
|
array of pointers. Next <tt class="literal"><span class="pre">indirect_iterator</span></tt> is used with the
|
|
<tt class="literal"><span class="pre">transform</span></tt> algorithm to copy the characters (incremented by one) to
|
|
another array. A constant indirect iterator is used for the source and
|
|
a mutable indirect iterator is used for the destination. The last part
|
|
of the example prints the original array of characters, but this time
|
|
using the <tt class="literal"><span class="pre">make_indirect_iterator</span></tt> helper function.</p>
|
|
<pre class="literal-block">
|
|
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];
|
|
|
|
// Example of using indirect_iterator
|
|
|
|
boost::indirect_iterator<char**, char>
|
|
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;
|
|
|
|
|
|
// Example of making mutable and constant indirect iterators
|
|
|
|
char mutable_characters[N];
|
|
char* pointers_to_mutable_chars[N];
|
|
for (int j = 0; j < N; ++j)
|
|
pointers_to_mutable_chars[j] = &mutable_characters[j];
|
|
|
|
boost::indirect_iterator<char* const*> mutable_indirect_first(pointers_to_mutable_chars),
|
|
mutable_indirect_last(pointers_to_mutable_chars + N);
|
|
boost::indirect_iterator<char* const*, char const> 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<char>(), 1));
|
|
|
|
std::copy(mutable_indirect_first, mutable_indirect_last,
|
|
std::ostream_iterator<char>(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<char>(std::cout, ","));
|
|
std::cout << std::endl;
|
|
</pre>
|
|
<p>The output is:</p>
|
|
<pre class="literal-block">
|
|
a,b,c,d,e,f,g,
|
|
b,c,d,e,f,g,h,
|
|
a,b,c,d,e,f,g,
|
|
</pre>
|
|
<p>The source code for this example can be found <a class="reference" href="../example/indirect_iterator_example.cpp">here</a>.</p>
|
|
</div>
|
|
</div>
|
|
<hr class="footer"/>
|
|
<div class="footer">
|
|
<a class="reference" href="indirect_iterator.rst">View document source</a>.
|
|
Generated on: 2004-01-13 05:55 UTC.
|
|
Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
|
|
</div>
|
|
</body>
|
|
</html>
|