@@ -87,12 +87,20 @@ namespace qlibs {
87
87
* @param[in,out] array The array to be sorted.
88
88
* @param[in] first Initial position of the portion to be sorted
89
89
* @param[in] last Final position of the portion to be sorted
90
+ * @param[in] comp Comparison function which returns @c true if the first
91
+ * argument is less than (i.e. is ordered before) the second.
92
+ * The signature of the comparison function should be equivalent to the
93
+ * following: bool cmp(const Type1& a, const Type2& b);
94
+ * @code{.c}
95
+ * bool cmp( const T& a, const T& b );
96
+ * @endcode
90
97
* @return none.
91
98
*/
92
99
template <typename T, size_t n>
93
100
void sort ( T ( &array )[ n ],
94
101
size_t first = 0U,
95
- size_t last = n - 1U ) noexcept
102
+ size_t last = n - 1U,
103
+ bool (*comp)( const T&, const T&) = nullptr ) noexcept
96
104
{
97
105
if ( n > 1U ) {
98
106
algorithm::impl::sort_stack<impl::sort_pair, n> stack;
@@ -109,7 +117,13 @@ namespace qlibs {
109
117
T pivotValue = array[ end ];
110
118
111
119
for ( int i = start; i < end; ++i ) {
112
- if ( array[ i ] < pivotValue ) {
120
+ if ( nullptr != comp ) {
121
+ if ( comp ( array[ i ], array[ pivotIndex ] ) ) {
122
+ algorithm::swap ( array[ i ], array[ pivotIndex ] );
123
+ ++pivotIndex;
124
+ }
125
+ }
126
+ else if ( array[ i ] < pivotValue ) {
113
127
algorithm::swap ( array[ i ], array[ pivotIndex ] );
114
128
++pivotIndex;
115
129
}
@@ -132,7 +146,6 @@ namespace qlibs {
132
146
* @param[in] last Final position of the portion to reverse
133
147
* @return none.
134
148
*/
135
-
136
149
template <typename T, size_t n>
137
150
inline void reverse ( T ( &array )[ n ],
138
151
const size_t first = 0U,
@@ -157,7 +170,6 @@ namespace qlibs {
157
170
* @param[in] k Positions to rotate. Sign determines the rotate direction.
158
171
* @return none.
159
172
*/
160
-
161
173
template <typename T, size_t n>
162
174
void rotate ( T ( &array )[ n ],
163
175
const int k = 1 ) noexcept
@@ -190,6 +202,7 @@ namespace qlibs {
190
202
* @param[in] value The value to set all elements to.
191
203
* @param[in] first Initial position of the portion to fill
192
204
* @param[in] last Final position of the portion to fill
205
+ * @return none.
193
206
*/
194
207
template <typename T, size_t n>
195
208
inline void fill ( T ( &array )[ n ],
@@ -293,6 +306,59 @@ namespace qlibs {
293
306
return ret;
294
307
}
295
308
309
+ /* *
310
+ * @brief Replaces all elements satisfying specific criteria with
311
+ * @a new_value in the range [first, last). Replaces all elements that
312
+ * are equal to old_value (using operator==)
313
+ * @param[in] array The array where the check is performed
314
+ * @param[in] old_value The value of elements to replace
315
+ * @param[in] new_value The value to use as replacement
316
+ * @param[in] first Initial position of the portion to check
317
+ * @param[in] last Final position of the portion to check
318
+ * @return @c none.
319
+ */
320
+ template <typename T, size_t n>
321
+ inline void replace ( T ( &array )[ n ],
322
+ const T& old_value,
323
+ const T& new_value,
324
+ const size_t first = 0U,
325
+ const size_t last = n - 1U ) noexcept
326
+ {
327
+ for ( size_t i = first; i <= last; ++i ) {
328
+ if ( old_value == array[ i ] ) {
329
+ array[ i ] = new_value;
330
+ break ;
331
+ }
332
+ }
333
+ }
334
+
335
+ /* *
336
+ * @brief Replaces all elements satisfying specific criteria with
337
+ * @a new_value in the range [first, last). Replaces all elements for
338
+ * which predicate @a pred returns @c true
339
+ * @param[in] array The array where the check is performed
340
+ * @param[in] pred Unary predicate which returns @c true if the element
341
+ * value should be replaced.
342
+ * @param[in] new_value The value to use as replacement
343
+ * @param[in] first Initial position of the portion to check
344
+ * @param[in] last Final position of the portion to check
345
+ * @return @c none.
346
+ */
347
+ template <typename T, size_t n>
348
+ inline bool replace_if ( T ( &array )[ n ],
349
+ bool (*pred)( const T& ),
350
+ const T& new_value,
351
+ const size_t first = 0U,
352
+ const size_t last = n - 1U ) noexcept
353
+ {
354
+ for ( size_t i = first; i <= last; ++i ) {
355
+ if ( pred ( array[ i ] ) ) {
356
+ array[ i ] = new_value;
357
+ break ;
358
+ }
359
+ }
360
+ }
361
+
296
362
/* *
297
363
* @brief Returns the number of elements in the range [first,last) for
298
364
* which @c pred is @c true.
0 commit comments