Library of Assembled Shared Sources
index_iterator.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2012 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_STDE_INDEX_ITERATOR_H
44#define LASS_GUARDIAN_OF_INCLUSION_STDE_INDEX_ITERATOR_H
45
46#include "stde_common.h"
47
48#include <cstddef>
49
50
51namespace lass
52{
53namespace stde
54{
55
56/** iterator adaptor to access members.
57 *
58 * A prime example is to iterate over the keys or values in a @c std::map.
59 * The value_type of the map's iterator is a @c std::pair of key and value.
60 * @c first_accessor_t is an accessor that will access the first element of
61 * the pair, and combined with @c index_iterator_t it allows you to
62 * directly iterate over just the keys of the map.
63 * A helper function @c first_iterator is provided to easily adapt a
64 * map's iterator:
65 *
66 * @code
67 * // prints spam, ham, eggs
68 * std::map<std::string, int> map;
69 * map["spam"] = 1;
70 * map["ham"] = 2;
71 * map["eggs"] = 3;
72 * std::copy(
73 * first_iterator(map.begin()),
74 * first_iterator(map.end()),
75 * std::ostream_iterator<std::string>(std::cout, ", "));
76 * @endcode
77 *
78 * @tparam Iterator the iterator type of the original sequence. In the
79 * example above, this would be @c std::map<std::string, int>::iterator
80 * @tparam Accessor functor type that takes an Iterator as argument and
81 * returns a reference to the accessed member. Accessor shall also
82 * define @c value_type, @c reference and @c pointer. An example is
83 * @c first_accessor_t
84 */
85template
86<
87 typename ContainerType,
88 typename ValueType = typename ContainerType::value_type,
89 typename PointerType = typename ContainerType::pointer,
90 typename ReferenceType = typename ContainerType::reference,
91 typename SizeType = size_t,
92 typename DifferenceType = std::ptrdiff_t
93>
94class index_iterator_t
95{
96public:
97 typedef ContainerType container_type;
98 typedef SizeType size_type;
99 typedef std::random_access_iterator_tag iterator_category;
100 typedef ValueType value_type;
101 typedef DifferenceType difference_type;
102 typedef PointerType pointer;
103 typedef ReferenceType reference;
104
105 index_iterator_t(ContainerType& container, size_type index): container_(&container), index_(static_cast<difference_type>(index)) {}
106
107 pointer operator->() const { return &(*container_)[static_cast<size_type>(index_)]; }
108 reference operator*() const { return (*container_)[static_cast<size_type>(index_)]; }
109
110 index_iterator_t& operator++() { ++index_; return *this; }
111 index_iterator_t operator++(int) { return index_iterator_t(container_, index_++); }
112 index_iterator_t& operator--() { --index_; return *this; }
113 index_iterator_t operator--(int) { return index_iterator_t(container_, index_--); }
114
115 index_iterator_t& operator+=(difference_type n) { index_ += n; return *this; }
116 index_iterator_t operator+(difference_type n) { return index_iterator_t(container_, index_ + n); }
117 index_iterator_t& operator-=(difference_type n) { index_ -= n; return *this; }
118 index_iterator_t operator-(difference_type n) { return index_iterator_t(container_, index_ - n); }
119
120 reference operator[](difference_type n) const { return (*container_)[static_cast<size_type>(index_ + n)]; }
121
122 container_type* container() const { return container_; }
123 size_type index() const { return static_cast<size_type>(index_); }
124
125public:
126 container_type* container_;
127 difference_type index_;
128};
129
130/** @relates index_iterator_t */
131template <typename C, typename V, typename R, typename P, typename S, typename D>
132typename index_iterator_t<C, V, R, P, S, D>::difference_type operator-(const index_iterator_t<C, V, R, P, S, D>& a, const index_iterator_t<C, V, R, P, S, D>& b)
133{
134 return a.index() - b.index();
135}
136/** @relates index_iterator_t */
137template <typename C, typename V, typename R, typename P, typename S, typename D>
138bool operator==(const index_iterator_t<C, V, R, P, S, D>& a, const index_iterator_t<C, V, R, P, S, D>& b)
139{
140 return a.container() == b.container() && a.index() == b.index();
141}
142/** @relates index_iterator_t */
143template <typename C, typename V, typename R, typename P, typename S, typename D>
144bool operator!=(const index_iterator_t<C, V, R, P, S, D>& a, const index_iterator_t<C, V, R, P, S, D>& b)
145{
146 return !(a == b);
147}
148/** @relates index_iterator_t */
149template <typename C, typename V, typename R, typename P, typename S, typename D>
151{
152 return a.index() < b.index();
153}
154/** @relates index_iterator_t */
155template <typename C, typename V, typename R, typename P, typename S, typename D>
157{
158 return a.index() > b.index();
159}
160/** @relates index_iterator_t */
161template <typename C, typename V, typename R, typename P, typename S, typename D>
163{
164 return a.index() <= b.index();
165}
166/** @relates index_iterator_t */
167template <typename C, typename V, typename R, typename P, typename S, typename D>
169{
170 return a.index() >= b.index();
171}
172
173
174}
175}
176
177#endif
178
179// EOF
180
iterator adaptor to access members.
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53