BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_iteratorutil.h
Go to the documentation of this file.
1/// @file bslstl_iteratorutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_iteratorutil.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_ITERATORUTIL
9#define INCLUDED_BSLSTL_ITERATORUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_iteratorutil bslstl_iteratorutil
15/// @brief Provide utilities operating on iterators and iterator ranges.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_iteratorutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_iteratorutil-purpose"> Purpose</a>
25/// * <a href="#bslstl_iteratorutil-classes"> Classes </a>
26/// * <a href="#bslstl_iteratorutil-description"> Description </a>
27/// * <a href="#bslstl_iteratorutil-usage"> Usage </a>
28/// * <a href="#bslstl_iteratorutil-example-1-finding-the-distance-between-two-random-access-iterators"> Example 1: Finding the Distance Between Two Random Access Iterators </a>
29///
30/// # Purpose {#bslstl_iteratorutil-purpose}
31/// Provide utilities operating on iterators and iterator ranges.
32///
33/// # Classes {#bslstl_iteratorutil-classes}
34///
35/// - bslstl::IteratorUtil:
36///
37/// @see bslstl_hashtable
38///
39/// # Description {#bslstl_iteratorutil-description}
40/// This component provides a namespace, `bslstl::IteratorUtil`,
41/// containing utility functions for iterator types. In particular, this
42/// component includes a function `insertDistance` that returns the number of
43/// elements that should be accounted for when range-inserting in a container,
44/// given a pair of iterator `a` and `b` describing a half-open range
45/// `[a .. b)`.
46///
47/// ## Usage {#bslstl_iteratorutil-usage}
48///
49///
50/// This section illustrates intended use of this component.
51///
52/// ### Example 1: Finding the Distance Between Two Random Access Iterators {#bslstl_iteratorutil-example-1-finding-the-distance-between-two-random-access-iterators}
53///
54///
55/// Suppose we want to find the number of elements between two random access
56/// iterators.
57///
58/// First, we create an array of integer values and two pointers (which are
59/// considered random access iterators) referring to the beginning and end of a
60/// range within that array:
61/// @code
62/// int values[] = { 1, 2, 3, 4, 5 };
63/// int *begin = &values[0];
64/// int *end = &values[3];
65/// @endcode
66/// Now, we use the `IteratorUtil::insertDistance` class method to calculate the
67/// distance of the open range `[begin .. end)`:
68/// @code
69/// std::size_t distance = IteratorUtil::insertDistance(begin, end);
70/// assert(3 == distance);
71/// @endcode
72/// @}
73/** @} */
74/** @} */
75
76/** @addtogroup bsl
77 * @{
78 */
79/** @addtogroup bslstl
80 * @{
81 */
82/** @addtogroup bslstl_iteratorutil
83 * @{
84 */
85
86#include <bslscm_version.h>
87
88#include <bslstl_iterator.h> // iterator tags, distance
89#include <bslstl_pair.h> // pair
90
91#include <bslmf_addconst.h>
92#include <bslmf_removeconst.h>
93
94#include <bsls_platform.h>
95
96#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
97#include <bsls_nativestd.h>
98#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
99
100
101namespace bslstl {
102
103 // ===================
104 // struct IteratorUtil
105 // ===================
106
107/// This utility struct provides a namespace for functions on iterators and
108/// iterator ranges.
110
111 /// Return 0 if the (template parameter) type `InputIterator` is limited
112 /// to the standard input-iterator category, otherwise return the number
113 /// of elements that is reachable from the specified `first` to (but not
114 /// including) the specified `last`. This function has a constant-time
115 /// complexity if the iterator category of `InputIterator` is a strictly
116 /// a standard input iterator, or is a random access iterator, otherwise
117 /// it is linear in the length of the range `[first .. last)`. The
118 /// behavior is undefined unless `last` is reachable from `first`. Note
119 /// that this function always returns 0 when compiled with the Sun
120 /// compiler, while we work around issues in the Sun standard library.
121 template <class InputIterator>
122 static typename bsl::iterator_traits<InputIterator>::difference_type
123 insertDistance(InputIterator first, InputIterator last);
124
125#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
126 // These template aliases are 'convenience alises' defined in the standard
127 // as 'exposition-only' to simplify the specification of the deduction
128 // guides for the associative containers.
129
130 /// returns the `value_type` of the specified iterator type.
131 template <class INPUT_ITER> // aka iter-val-type
132 using IterVal_t = typename bsl::iterator_traits<INPUT_ITER>::value_type;
133
134 /// returns the `key-type` of the specified iterator type, which is
135 /// expected to refer to a `pair<KEY_TYPE, MAPPED_TYPE>`
136 template <class INPUT_ITER> // aka iter-key-type
137 using IterKey_t = bsl::remove_const_t<
138 typename bsl::iterator_traits<INPUT_ITER>::value_type::first_type>;
139
140 /// returns the `mapped-type` of the specified iterator type, which is
141 /// expected to refer to a `pair<KEY_TYPE, MAPPED_TYPE>`
142 template <class INPUT_ITER> // aka iter-mapped-type
143 using IterMapped_t =
144 typename bsl::iterator_traits<INPUT_ITER>::value_type::second_type;
145
146 /// returns the type that is actually stored in a map. The supplied
147 /// iterator type is expected to refer to a
148 /// `pair<KEY_TYPE, MAPPED_TYPE>`.
149 template <class INPUT_ITER> // aka iter-to-alloc-type
150 using IterToAlloc_t = bsl::pair<
151 bsl::add_const_t<
152 typename bsl::iterator_traits<INPUT_ITER>::value_type::first_type>,
153 typename bsl::iterator_traits<INPUT_ITER>::value_type::second_type
154 >;
155#endif
156
157};
158
159// ============================================================================
160// TEMPLATE AND INLINE FUNCTION DEFINITIONS
161// ============================================================================
162
163 // ------------------
164 // class IteratorUtil
165 // ------------------
166
167template <class InputIterator>
168typename bsl::iterator_traits<InputIterator>::difference_type
169IteratorUtil::insertDistance(InputIterator first, InputIterator last)
170{
171 /// This local class provides a utility to estimate the maximum
172 /// number of elements that may be inserted by a range-insert
173 /// operation on a standard container, by performing tag dispatch
174 /// on the iterator's category type.
175 struct impl {
176
177 static
178 typename bsl::iterator_traits<InputIterator>::difference_type
179 calc(InputIterator, InputIterator, std::input_iterator_tag)
180 {
181 return 0;
182 }
183
184 static
185 typename bsl::iterator_traits<InputIterator>::difference_type
186 calc(InputIterator first,
187 InputIterator last,
188 std::forward_iterator_tag)
189 {
190 return bsl::distance(first, last);
191 }
192 };
193
194 typedef typename bsl::iterator_traits<InputIterator>::iterator_category
195 IterCategory;
196 return impl::calc(first, last, IterCategory());
197}
198
199} // close package namespace
200
201
202#endif
203
204// ----------------------------------------------------------------------------
205// Copyright 2013 Bloomberg Finance L.P.
206//
207// Licensed under the Apache License, Version 2.0 (the "License");
208// you may not use this file except in compliance with the License.
209// You may obtain a copy of the License at
210//
211// http://www.apache.org/licenses/LICENSE-2.0
212//
213// Unless required by applicable law or agreed to in writing, software
214// distributed under the License is distributed on an "AS IS" BASIS,
215// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
216// See the License for the specific language governing permissions and
217// limitations under the License.
218// ----------------------------- END-OF-FILE ----------------------------------
219
220/** @} */
221/** @} */
222/** @} */
Definition bslstl_pair.h:1210
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslstl_algorithm.h:82
Definition bslstl_iteratorutil.h:109
static bsl::iterator_traits< InputIterator >::difference_type insertDistance(InputIterator first, InputIterator last)
Definition bslstl_iteratorutil.h:169