@@ -124,23 +124,22 @@ namespace qlibs {
124
124
}
125
125
126
126
/* *
127
- * @brief Reverse the given array. Operation takes place on the portion
128
- * of the array that starts at position @a init to position @a end.
127
+ * @brief Reverses the order of the elements in the range [first,last).
129
128
* @param[in,out] array The array to reverse.
130
- * @param[in] init Position of the first element.
131
- * @param[in] end Position of the last element.
129
+ * @param[in] first Initial position of the portion to reverse
130
+ * @param[in] last Final position of the portion to reverse
132
131
* @return none.
133
132
*/
134
133
135
134
template <typename T, size_t n>
136
135
void reverse ( T ( &array )[ n ],
137
- const size_t init = 0U,
138
- const size_t end = n - 1U ) noexcept
136
+ const size_t first = 0U,
137
+ const size_t last = n - 1U ) noexcept
139
138
{
140
- if ( end > init ) {
141
- size_t s = init , e = end ;
139
+ if ( last > first ) {
140
+ size_t s = first , e = last ;
142
141
143
- while ( s < e ) {
142
+ while ( s < e ) {
144
143
algorithm::swap ( array[ s ], array[ e ] );
145
144
++s;
146
145
--e;
@@ -149,11 +148,11 @@ namespace qlibs {
149
148
}
150
149
151
150
/* *
152
- * @brief Rotates @a k elements of the array pointed . Rotation direction
151
+ * @brief Rotates @a k elements of the array. Rotation direction
153
152
* is determined by the sign of @a k, the means a positive value performs
154
- * a right-rotation and a negative value a left-rotation.
153
+ * a right-rotation and a negative value a left-rotation.
155
154
* @param[in,out] array The array to rotate.
156
- * @param[in] k Positions to rotate.
155
+ * @param[in] k Positions to rotate. Sign determines the rotate direction.
157
156
* @return none.
158
157
*/
159
158
@@ -183,15 +182,15 @@ namespace qlibs {
183
182
}
184
183
185
184
/* *
186
- * @brief Sets all elements of an array in the range [first,last) to a
187
- * specific value .
188
- * @param[in,out] array The array to set .
185
+ * @brief Assigns @a value to all the elements of the array in the
186
+ * range [first,last) .
187
+ * @param[in,out] array The array to fill .
189
188
* @param[in] value The value to set all elements to.
190
- * @param[in] first Initial position of the portion to search
191
- * @param[in] last Final position of the portion to search
189
+ * @param[in] first Initial position of the portion to fill
190
+ * @param[in] last Final position of the portion to fill
192
191
*/
193
192
template <typename T, size_t n>
194
- inline void set ( T ( &array )[ n ],
193
+ inline void fill ( T ( &array )[ n ],
195
194
const T value,
196
195
const size_t first = 0U,
197
196
const size_t last = n - 1U ) noexcept
@@ -202,24 +201,24 @@ namespace qlibs {
202
201
}
203
202
204
203
/* *
205
- * @brief Performs a linear search over the raw-array in the
206
- * range [first,last) that matches the @a key.
204
+ * @brief Returns a pointer to the first element in the range [first,last)
205
+ * that compares equal to @a key. If no such element is found, the
206
+ * function returns @c nullptr.
207
207
* @note The elements are compared using operator '=='
208
- * @param[in] key The object that serves as key for
209
- * the search.
210
208
* @param[in] array The array where the search is performed
209
+ * @param[in] key Value to search for in the range. T shall be a type
210
+ * supporting comparisons using operator==.
211
211
* @param[in] first Initial position of the portion to search
212
212
* @param[in] last Final position of the portion to search
213
213
* @return This function returns a pointer to an entry in the array that
214
214
* matches the search key. If key is not found, a @c nullptr pointer is
215
215
* returned.
216
216
*/
217
217
template <typename T, size_t n>
218
- inline T* lSearch ( const T key,
219
- T ( &array )[ n ],
220
- const size_t first = 0U,
221
- const size_t last = n - 1U
222
- ) noexcept
218
+ inline T* find ( T ( &array )[ n ],
219
+ const T key,
220
+ const size_t first = 0U,
221
+ const size_t last = n - 1U ) noexcept
223
222
{
224
223
T* found = nullptr ;
225
224
@@ -233,24 +232,167 @@ namespace qlibs {
233
232
}
234
233
235
234
/* *
236
- * @brief Performs a binary search over the raw-array in the
237
- * range [first,last) that matches the @a key..
238
- * The array contents should be sorted in ascending order.
239
- * @note The elements are compared using operator '<' and '=='
240
- * @param[in] key The object that serves as key for
241
- * the search.
235
+ * @brief Returns @c true if @a pred returns @c true for any of the
236
+ * elements in the range [first,last), and @c false otherwise.
237
+ * @param[in] array The array where the check is performed
238
+ * @param[in] pred Unary function that accepts an element in the range as
239
+ * argument and returns a value convertible to bool. The value returned
240
+ * indicates whether the element fulfills the condition checked by this
241
+ * function.
242
+ * @param[in] first Initial position of the portion to check
243
+ * @param[in] last Final position of the portion to check
244
+ * @return @c true if @a pred returns true for any of the elements in
245
+ * the range [first,last), and @c false otherwise.
246
+ */
247
+ template <typename T, size_t n>
248
+ inline bool any_of ( T ( &array )[ n ],
249
+ bool (*pred)( const T ),
250
+ const size_t first = 0U,
251
+ const size_t last = n - 1U ) noexcept
252
+ {
253
+ bool ret = false ;
254
+
255
+ for ( size_t i = first; i <= last; ++i ) {
256
+ if ( pred ( array[ i ] ) ) {
257
+ ret = true ;
258
+ break ;
259
+ }
260
+ }
261
+ return ret;
262
+ }
263
+
264
+ /* *
265
+ * @brief Returns @c true if @a pred returns @c true for all the
266
+ * elements in the range [first,last), and @c false otherwise.
267
+ * @param[in] array The array where the check is performed
268
+ * @param[in] pred Unary function that accepts an element in the range as
269
+ * argument and returns a value convertible to bool. The value returned
270
+ * indicates whether the element fulfills the condition checked by this
271
+ * function.
272
+ * @param[in] first Initial position of the portion to check
273
+ * @param[in] last Final position of the portion to check
274
+ * @return @c true if @a pred returns true for all the elements in
275
+ * the range [first,last), and @c false otherwise.
276
+ */
277
+ template <typename T, size_t n>
278
+ inline bool all_of ( T ( &array )[ n ],
279
+ bool (*pred)( const T ),
280
+ const size_t first = 0U,
281
+ const size_t last = n - 1U ) noexcept
282
+ {
283
+ bool ret = true ;
284
+
285
+ for ( size_t i = first; i <= last; ++i ) {
286
+ if ( !pred ( array[ i ] ) ) {
287
+ ret = false ;
288
+ break ;
289
+ }
290
+ }
291
+ return ret;
292
+ }
293
+
294
+ /* *
295
+ * @brief Returns the number of elements in the range [first,last) for
296
+ * which @c pred is @c true.
297
+ * @param[in] array The array where the count will be performed
298
+ * @param[in] pred Unary function that accepts an element in the range as
299
+ * argument, and returns a value convertible to bool. The value returned
300
+ * indicates whether the element is counted by this function.
301
+ * @param[in] first Initial position of the portion to check
302
+ * @param[in] last Final position of the portion to check
303
+ * @return The number of elements in the range [first,last) for which
304
+ * @a pred does not return @c false.
305
+ */
306
+ template <typename T, size_t n>
307
+ inline size_t count_if ( T ( &array )[ n ],
308
+ bool (*pred)( const T ),
309
+ const size_t first = 0U,
310
+ const size_t last = n - 1U ) noexcept
311
+ {
312
+ size_t count = 0U ;
313
+
314
+ for ( size_t i = first; i <= last; ++i ) {
315
+ if ( pred ( array[ i ] ) ) {
316
+ ++count;
317
+ }
318
+ }
319
+ return count;
320
+ }
321
+
322
+ /* *
323
+ * @brief Returns an iterator to the first element in the range [first,last)
324
+ * for which @a pred returns @c true. If no such element is found, the
325
+ * function returns @c nullptr.
242
326
* @param[in] array The array where the search is performed
327
+ * @param[in] pred Unary function that accepts an element in the range as
328
+ * argument and returns a value convertible to bool. The value returned
329
+ * indicates whether the element is considered a match in the context of
330
+ * this function.
331
+ * @param[in] first Initial position of the portion to check
332
+ * @param[in] last Final position of the portion to check
333
+ * @return A pointer to the first element in the range for which @a pred
334
+ * does not return @c false. If @a pred is @c false for all elements,
335
+ * the function returns @c nullptr.
336
+ */
337
+ template <typename T, size_t n>
338
+ inline T* find_if ( T ( &array )[ n ],
339
+ bool (*pred)( const T ),
340
+ const size_t first = 0U,
341
+ const size_t last = n - 1U ) noexcept
342
+ {
343
+ T *found = nullptr ;
344
+
345
+ for ( size_t i = first; i <= last; ++i ) {
346
+ if ( pred ( array[ i ] ) ) {
347
+ found = &array[ i ];
348
+ }
349
+ }
350
+ return found;
351
+ }
352
+
353
+ /* *
354
+ * @brief Applies function @a fn to each of the elements in the range
355
+ * [first,last).
356
+ * @param[in] array The array
357
+ * @param[in] fn Unary function that accepts an element in the range as
358
+ * argument.
359
+ * @param[in] first Initial position of the portion to check
360
+ * @param[in] last Final position of the portion to check
361
+ * @return none
362
+ */
363
+ template <typename T, size_t n>
364
+ inline void for_each ( T ( &array )[ n ],
365
+ void (*fn)( T& ),
366
+ const size_t first = 0U,
367
+ const size_t last = n - 1U ) noexcept
368
+ {
369
+ for ( size_t i = first; i <= last; ++i ) {
370
+ (void )fn ( array[ i ] );
371
+ }
372
+ }
373
+
374
+ /* *
375
+ * @brief Returns a pointer to the first element in the range [first,last)
376
+ * that compares equal to @a key. If no such element is found, the
377
+ * function returns @c nullptr.
378
+ * @note The elements in the range shall already be sorted according to
379
+ * this same criterion (operator< or operator==), or at least partitioned with
380
+ * respect to @a key.
381
+ * @note The elements are compared using operator '=='
382
+ * @param[in] array The array where the search is performed
383
+ * @param[in] key Value to search for in the range. T shall be a type
384
+ * supporting comparisons using operator== and operator<.
243
385
* @param[in] first Initial position of the portion to search
244
386
* @param[in] last Final position of the portion to search
245
387
* @return This function returns a pointer to an entry in the array that
246
- * matches the search key. If key is not found, a @c nullptr pointer is returned.
388
+ * matches the search key. If key is not found, a @c nullptr pointer is
389
+ * returned.
247
390
*/
248
391
template <typename T, size_t n>
249
- inline T* bSearch ( const T key,
250
- T ( &array )[ n ],
251
- const size_t first = 0U,
252
- const size_t last = n - 1U
253
- ) noexcept
392
+ inline T* binary_search ( T ( &array )[ n ],
393
+ const T key,
394
+ const size_t first = 0U,
395
+ const size_t last = n - 1U ) noexcept
254
396
{
255
397
T* found = nullptr ;
256
398
int left = static_cast <int >( first );
0 commit comments