BDE 4.14.0 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`.
123
124 public:
125 // CLASS METHODS
126
127 /// Return `true` if the range from the specified `first` (inclusive) to
128 /// the specified `last` (exclusive) is sorted or empty, and `false`
129 /// otherwise. The behavior is undefined unless `first <= last`.
130 template <class TYPE>
133
134 /// Return an iterator to the first element in the sorted range from the
135 /// specified `first` (inclusive) to the specified `last` (exclusive)
136 /// that compares greater than or equal to the specified `value`, and
137 /// `last` if no such element exists. The behavior is undefined unless
138 /// `first <= last` and the range is sorted.
139 template <class TYPE>
143 TYPE value);
144
145 /// Return an iterator to the first element in the sorted range from the
146 /// specified `first` (inclusive) to the specified `last` (exclusive)
147 /// that compares greater than the specified `value`, and `last` if no
148 /// such element exists. The behavior is undefined unless
149 /// `first <= last` and the range is sorted.
150 template <class TYPE>
154 TYPE value);
155};
156
157// ============================================================================
158// INLINE DEFINITIONS
159// ============================================================================
160
161 // -------------------------
162 // struct PackedIntArrayUtil
163 // -------------------------
164
165// CLASS METHODS
166template <class TYPE>
169{
170 BSLS_ASSERT(first <= last);
171
174
175 while (at < last) {
176 if (*prev > *at) {
177 return false; // RETURN
178 }
179 prev = at++;
180 }
181
182 return true;
183}
184
185template <class TYPE>
189 TYPE value)
190{
191 BSLS_ASSERT(first <= last);
192 BSLS_ASSERT_SAFE(isSorted(first, last));
193
195 difference_type;
196
197 difference_type count = last - first;
198
199 while (count > 0) {
200 difference_type step = count / 2;
201 PackedIntArrayConstIterator<TYPE> it = first + step;
202
203 if (*it < value) {
204 first = ++it;
205 count -= step + 1;
206 }
207 else {
208 count = step;
209 }
210 }
211
212 return first;
213}
214
215template <class TYPE>
219 TYPE value)
220{
221 BSLS_ASSERT(first <= last);
222 BSLS_ASSERT_SAFE(isSorted(first, last));
223
225 difference_type;
226
227 difference_type count = last - first;
228
229 while (count > 0) {
230 difference_type step = count / 2;
231 PackedIntArrayConstIterator<TYPE> it = first + step;
232
233 if (*it <= value) {
234 first = ++it;
235 count -= step + 1;
236 }
237 else {
238 count = step;
239 }
240 }
241
242 return first;
243}
244
245} // close package namespace
246
247
248#endif
249
250// ----------------------------------------------------------------------------
251// Copyright 2015 Bloomberg Finance L.P.
252//
253// Licensed under the Apache License, Version 2.0 (the "License");
254// you may not use this file except in compliance with the License.
255// You may obtain a copy of the License at
256//
257// http://www.apache.org/licenses/LICENSE-2.0
258//
259// Unless required by applicable law or agreed to in writing, software
260// distributed under the License is distributed on an "AS IS" BASIS,
261// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
262// See the License for the specific language governing permissions and
263// limitations under the License.
264// ----------------------------- END-OF-FILE ----------------------------------
265
266/** @} */
267/** @} */
268/** @} */
Definition bdlc_packedintarray.h:770
bsl::ptrdiff_t difference_type
Definition bdlc_packedintarray.h:818
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlc_bitarray.h:503
Definition bdlc_packedintarrayutil.h:122
static PackedIntArrayConstIterator< TYPE > upperBound(PackedIntArrayConstIterator< TYPE > first, PackedIntArrayConstIterator< TYPE > last, TYPE value)
Definition bdlc_packedintarrayutil.h:216
static PackedIntArrayConstIterator< TYPE > lowerBound(PackedIntArrayConstIterator< TYPE > first, PackedIntArrayConstIterator< TYPE > last, TYPE value)
Definition bdlc_packedintarrayutil.h:186
static bool isSorted(PackedIntArrayConstIterator< TYPE > first, PackedIntArrayConstIterator< TYPE > last)
Definition bdlc_packedintarrayutil.h:167