BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlc_packedintarrayutil.h
Go to the documentation of this file.
1/// @file bdlc_packedintarrayutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlc_packedintarrayutil.h -*-C++-*-
8#ifndef INCLUDED_BDLC_PACKEDINTARRAYUTIL
9#define INCLUDED_BDLC_PACKEDINTARRAYUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlc_packedintarrayutil bdlc_packedintarrayutil
15/// @brief Provide common non-primitive operations on `bdlc::PackedIntArray`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlc
19/// @{
20/// @addtogroup bdlc_packedintarrayutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlc_packedintarrayutil-purpose"> Purpose</a>
25/// * <a href="#bdlc_packedintarrayutil-classes"> Classes </a>
26/// * <a href="#bdlc_packedintarrayutil-description"> Description </a>
27/// * <a href="#bdlc_packedintarrayutil-usage"> Usage </a>
28/// * <a href="#bdlc_packedintarrayutil-example-1-lowerbound"> Example 1: lowerBound </a>
29///
30/// # Purpose {#bdlc_packedintarrayutil-purpose}
31/// Provide common non-primitive operations on `bdlc::PackedIntArray`.
32///
33/// # Classes {#bdlc_packedintarrayutil-classes}
34///
35/// - bdlc::PackedIntArrayUtil: non-primitive `bdlc::PackedIntArray` operations
36///
37/// @see bdlc_packedintarray
38///
39/// # Description {#bdlc_packedintarrayutil-description}
40/// This component provides a `struct`, `bdlc::PackedIntArrayUtil`,
41/// that serves as a namespace for utility functions that operate on
42/// `bdlc::PackedIntArray` objects.
43///
44/// The following list of methods are provided by `bdlc::PackedIntArrayUtil`:
45/// @code
46/// 'isSorted' Returns 'true' if the range from a
47/// 'bdlc::PackedIntArray' is sorted, and 'false' otherwise.
48///
49/// 'lowerBound' Returns an iterator to the first element in a sorted
50/// range from a 'bdlc::PackedIntArray' that compares
51/// greater than or equal to a specified value.
52///
53/// 'upperBound' Returns an iterator to the first element in a sorted
54/// range from a 'bdlc::PackedIntArray' that compares
55/// greater than a specified value.
56/// @endcode
57///
58/// ## Usage {#bdlc_packedintarrayutil-usage}
59///
60///
61/// This section illustrates intended use of this component.
62///
63/// ### Example 1: lowerBound {#bdlc_packedintarrayutil-example-1-lowerbound}
64///
65///
66/// Suppose that given a sorted `bdlc::PackedIntArray`, we want to find the
67/// first value greater than or equal to the value 17. First, create and
68/// populate with sorted data the `bdlc::PackedIntArray` to be searched:
69/// @code
70/// bdlc::PackedIntArray<int> array;
71///
72/// array.push_back( 5);
73/// array.push_back( 9);
74/// array.push_back(15);
75/// array.push_back(19);
76/// array.push_back(23);
77/// array.push_back(36);
78/// assert(6 == array.length());
79/// @endcode
80/// Then, verify the array's data has sorted values:
81/// @code
82/// assert(bdlc::PackedIntArrayUtil::isSorted(array.begin(), array.end()));
83/// @endcode
84/// Finally, use `bdlc::PackedIntArrayUtil::lowerBound` to find the desired
85/// value:
86/// @code
87/// bdlc::PackedIntArrayConstIterator<int> iterator =
88/// bdlc::PackedIntArrayUtil::lowerBound(array.begin(),
89/// array.end(),
90/// 17);
91/// assert(iterator != array.end() && 19 == *iterator);
92/// @endcode
93/// @}
94/** @} */
95/** @} */
96
97/** @addtogroup bdl
98 * @{
99 */
100/** @addtogroup bdlc
101 * @{
102 */
103/** @addtogroup bdlc_packedintarrayutil
104 * @{
105 */
106
107#include <bdlscm_version.h>
108
109#include <bdlc_packedintarray.h>
110
111#include <bsls_assert.h>
112
113
114namespace bdlc {
115
116 // =========================
117 // struct PackedIntArrayUtil
118 // =========================
119
120/// This `struct` provides a namespace for utility functions that provide
121/// non-primitive operations on `bdlc::PackedIntArray`.
122///
123/// See @ref bdlc_packedintarrayutil
125
126 public:
127 // CLASS METHODS
128
129 /// Return `true` if the range from the specified `first` (inclusive) to
130 /// the specified `last` (exclusive) is sorted or empty, and `false` otherwise.
131 ///
132 /// \pre The behavior is undefined unless `first <= last`.
133 template <class TYPE>
136
137 /// Return an iterator to the first element in the sorted range from the
138 /// specified `first` (inclusive) to the specified `last` (exclusive)
139 /// that compares greater than or equal to the specified `value`, and `last` if no such element exists.
140 ///
141 /// \pre The behavior is undefined unless
142 /// `first <= last` and the range is sorted.
143 template <class TYPE>
147 TYPE value);
148
149 /// Return an iterator to the first element in the sorted range from the
150 /// specified `first` (inclusive) to the specified `last` (exclusive)
151 /// that compares greater than the specified `value`, and `last` if no such element exists.
152 ///
153 /// \pre The behavior is undefined unless
154 /// `first <= last` and the range is sorted.
155 template <class TYPE>
159 TYPE value);
160};
161
162// ============================================================================
163// INLINE DEFINITIONS
164// ============================================================================
165
166 // -------------------------
167 // struct PackedIntArrayUtil
168 // -------------------------
169
170// CLASS METHODS
171template <class TYPE>
174{
175 BSLS_ASSERT(first <= last);
176
179
180 while (at < last) {
181 if (*prev > *at) {
182 return false; // RETURN
183 }
184 prev = at++;
185 }
186
187 return true;
188}
189
190template <class TYPE>
194 TYPE value)
195{
196 BSLS_ASSERT(first <= last);
197 //BSLS_ASSERT_SAFE(isSorted(first, last)); // expensive check
198
200 difference_type;
201
202 difference_type count = last - first;
203
204 while (count > 0) {
205 difference_type step = count / 2;
206 PackedIntArrayConstIterator<TYPE> it = first + step;
207
208 if (*it < value) {
209 first = ++it;
210 count -= step + 1;
211 }
212 else {
213 count = step;
214 }
215 }
216
217 return first;
218}
219
220template <class TYPE>
224 TYPE value)
225{
226 BSLS_ASSERT(first <= last);
227 //BSLS_ASSERT_SAFE(isSorted(first, last)); // expensive check
228
230 difference_type;
231
232 difference_type count = last - first;
233
234 while (count > 0) {
235 difference_type step = count / 2;
236 PackedIntArrayConstIterator<TYPE> it = first + step;
237
238 if (*it <= value) {
239 first = ++it;
240 count -= step + 1;
241 }
242 else {
243 count = step;
244 }
245 }
246
247 return first;
248}
249
250} // close package namespace
251
252
253#endif
254
255// ----------------------------------------------------------------------------
256// Copyright 2015 Bloomberg Finance L.P.
257//
258// Licensed under the Apache License, Version 2.0 (the "License");
259// you may not use this file except in compliance with the License.
260// You may obtain a copy of the License at
261//
262// http://www.apache.org/licenses/LICENSE-2.0
263//
264// Unless required by applicable law or agreed to in writing, software
265// distributed under the License is distributed on an "AS IS" BASIS,
266// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
267// See the License for the specific language governing permissions and
268// limitations under the License.
269// ----------------------------- END-OF-FILE ----------------------------------
270
271/** @} */
272/** @} */
273/** @} */
Definition bdlc_packedintarray.h:802
bsl::ptrdiff_t difference_type
Definition bdlc_packedintarray.h:850
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlc_bitarray.h:506
Definition bdlc_packedintarrayutil.h:124
static PackedIntArrayConstIterator< TYPE > upperBound(PackedIntArrayConstIterator< TYPE > first, PackedIntArrayConstIterator< TYPE > last, TYPE value)
Definition bdlc_packedintarrayutil.h:221
static PackedIntArrayConstIterator< TYPE > lowerBound(PackedIntArrayConstIterator< TYPE > first, PackedIntArrayConstIterator< TYPE > last, TYPE value)
Definition bdlc_packedintarrayutil.h:191
static bool isSorted(PackedIntArrayConstIterator< TYPE > first, PackedIntArrayConstIterator< TYPE > last)
Definition bdlc_packedintarrayutil.h:172