-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcstl_algo_nonmutating.c
498 lines (431 loc) · 16.9 KB
/
cstl_algo_nonmutating.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* The implementation of non-mutating algorithm.
* Copyright (C) 2008 - 2013 Wangbo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author e-mail: activesys.wb@gmail.com
* activesys@sina.com.cn
*/
/** include section **/
#include <cstl/cstl_def.h>
#include <cstl/cstl_alloc.h>
#include <cstl/cstl_types.h>
#include <cstl/citerator.h>
#include <cstl/cstring.h>
#include <cstl/cfunctional.h>
#include <cstl/calgorithm.h>
/** local constant declaration and local macro section **/
/** local data type declaration and local struct, union, enum section **/
/** local function prototype section **/
/** exported global variable definition section **/
/** local global variable definition section **/
/** exported function implementation section **/
/**
* Applies a unary function to each element in a range.
*/
void algo_for_each(input_iterator_t it_first, input_iterator_t it_last, ufun_t ufun_op)
{
assert(_iterator_valid_range(it_first, it_last, _INPUT_ITERATOR));
if (ufun_op == NULL) {
ufun_op = fun_default_unary;
}
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
(*ufun_op)(iterator_get_pointer(it_first), NULL);
}
}
/**
* Locates the position of the first occurrence of an element in a range that satisfies a specified condition.
*/
input_iterator_t algo_find_if(input_iterator_t it_first, input_iterator_t it_last, ufun_t ufun_op)
{
bool_t b_result = false;
assert(_iterator_valid_range(it_first, it_last, _INPUT_ITERATOR));
if (ufun_op == NULL) {
ufun_op = fun_default_unary;
}
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
(*ufun_op)(iterator_get_pointer(it_first), &b_result);
if (b_result) {
break;
}
}
return it_first;
}
/**
* Searches for two adjacent elements that are equal.
*/
forward_iterator_t algo_adjacent_find(forward_iterator_t it_first, forward_iterator_t it_last)
{
return algo_adjacent_find_if(it_first, it_last, _fun_get_binary(it_first, _EQUAL_FUN));
}
/**
* Searches for two adjacent elements that satisfy a specificed condition.
*/
forward_iterator_t algo_adjacent_find_if(forward_iterator_t it_first, forward_iterator_t it_last, bfun_t bfun_op)
{
forward_iterator_t it_next;
bool_t b_result = false;
bool_t b_less = false;
bool_t b_greater = false;
assert(_iterator_valid_range(it_first, it_last, _FORWARD_ITERATOR));
if (bfun_op == NULL) {
bfun_op = _fun_get_binary(it_first, _EQUAL_FUN);
}
if (iterator_equal(it_first, it_last)) {
return it_last;
}
if (bfun_op == fun_default_binary) {
bfun_op = _fun_get_binary(it_first, _LESS_FUN);
it_next = it_first;
it_next = iterator_next(it_next);
while (!iterator_equal(it_next, it_last)) {
(*bfun_op)(iterator_get_pointer(it_first), iterator_get_pointer(it_next), &b_less);
(*bfun_op)(iterator_get_pointer(it_next), iterator_get_pointer(it_first), &b_greater);
if (!b_less && !b_greater) {
return it_first;
}
it_first = it_next;
it_next = iterator_next(it_next);
}
} else {
it_next = it_first;
it_next = iterator_next(it_next);
while (!iterator_equal(it_next, it_last)) {
(*bfun_op)(iterator_get_pointer(it_first), iterator_get_pointer(it_next), &b_result);
if (b_result) {
return it_first;
}
it_first = it_next;
it_next = iterator_next(it_next);
}
}
return it_last;
}
/**
* Searches for the first occurrence of any of several values within a target range.
*/
input_iterator_t algo_find_first_of(
input_iterator_t it_first1, input_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2)
{
return algo_find_first_of_if(it_first1, it_last1, it_first2, it_last2, _fun_get_binary(it_first1, _EQUAL_FUN));
}
/**
* Searches for the first occurrence of any of several elements that are equivalent in a sense specified by
*/
input_iterator_t algo_find_first_of_if(
input_iterator_t it_first1, input_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2,
bfun_t bfun_op)
{
iterator_t it_index;
bool_t b_result = false;
bool_t b_less = false;
bool_t b_greater = false;
assert(_iterator_valid_range(it_first1, it_last1, _INPUT_ITERATOR));
assert(_iterator_valid_range(it_first2, it_last2, _FORWARD_ITERATOR));
assert(_iterator_same_elem_type(it_first1, it_first2));
if (bfun_op == NULL) {
bfun_op = _fun_get_binary(it_first1, _EQUAL_FUN);
}
if (bfun_op == fun_default_binary) {
bfun_op = _fun_get_binary(it_first1, _LESS_FUN);
for (; !iterator_equal(it_first1, it_last1); it_first1 = iterator_next(it_first1)) {
for (it_index = it_first2; !iterator_equal(it_index, it_last2); it_index = iterator_next(it_index)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_index), &b_less);
if (b_less) {
continue;
}
(*bfun_op)(iterator_get_pointer(it_index), iterator_get_pointer(it_first1), &b_greater);
if (b_greater) {
continue;
}
return it_first1;
}
}
} else {
for (; !iterator_equal(it_first1, it_last1); it_first1 = iterator_next(it_first1)) {
for (it_index = it_first2; !iterator_equal(it_index, it_last2); it_index = iterator_next(it_index)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_index), &b_result);
if (b_result) {
return it_first1;
}
}
}
}
return it_last1;
}
/**
* Returns the number of elements in a range whose values satisfy a specified condition.
*/
size_t algo_count_if(input_iterator_t it_first, input_iterator_t it_last, ufun_t ufun_op)
{
bool_t b_result = false;
size_t t_count = 0;
assert(_iterator_valid_range(it_first, it_last, _INPUT_ITERATOR));
if (ufun_op == NULL) {
ufun_op = fun_default_unary;
}
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
(*ufun_op)(iterator_get_pointer(it_first), &b_result);
if (b_result) {
t_count++;
}
}
return t_count;
}
/**
* Compares two ranges element by element either for equality and locates the first position where a difference occurs.
*/
range_t algo_mismatch(input_iterator_t it_first1, input_iterator_t it_last1, input_iterator_t it_first2)
{
return algo_mismatch_if(it_first1, it_last1, it_first2, _fun_get_binary(it_first1, _EQUAL_FUN));
}
/**
* Compares two ranges element by element either for equivalent in a sense specified by a binary predicate and locates
* the first position where a difference occurs.
*/
range_t algo_mismatch_if(
input_iterator_t it_first1, input_iterator_t it_last1, input_iterator_t it_first2, bfun_t bfun_op)
{
bool_t b_result = false;
bool_t b_less = false;
bool_t b_greater = false;
range_t r_range;
assert(_iterator_valid_range(it_first1, it_last1, _INPUT_ITERATOR));
assert(_iterator_limit_type(it_first2, _INPUT_ITERATOR));
assert(_iterator_same_elem_type(it_first1, it_first2));
if (bfun_op == NULL) {
bfun_op = _fun_get_binary(it_first1, _EQUAL_FUN);
}
if (bfun_op == fun_default_binary) {
bfun_op = _fun_get_binary(it_first1, _LESS_FUN);
for (;
!iterator_equal(it_first1, it_last1);
it_first1 = iterator_next(it_first1), it_first2 = iterator_next(it_first2)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_first2), &b_less);
if (b_less) {
break;
}
(*bfun_op)(iterator_get_pointer(it_first2), iterator_get_pointer(it_first1), &b_greater);
if (b_greater) {
break;
}
}
} else {
for (;
!iterator_equal(it_first1, it_last1);
it_first1 = iterator_next(it_first1), it_first2 = iterator_next(it_first2)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_first2), &b_result);
if (!b_result) {
break;
}
}
}
r_range.it_begin = it_first1;
r_range.it_end = it_first2;
return r_range;
}
/**
* Compares two ranges element by element either for equality.
*/
bool_t algo_equal(input_iterator_t it_first1, input_iterator_t it_last1, input_iterator_t it_first2)
{
return algo_equal_if(it_first1, it_last1, it_first2, _fun_get_binary(it_first1, _EQUAL_FUN));
}
/**
* Compares two ranges element by element either for equivalence in a sense specified by a binary predicate.
*/
bool_t algo_equal_if(
input_iterator_t it_first1, input_iterator_t it_last1, input_iterator_t it_first2, bfun_t bfun_op)
{
bool_t b_result = false;
bool_t b_less = false;
bool_t b_greater = false;
assert(_iterator_valid_range(it_first1, it_last1, _INPUT_ITERATOR));
assert(_iterator_limit_type(it_first2, _INPUT_ITERATOR));
assert(_iterator_same_elem_type(it_first1, it_first2));
if (bfun_op == NULL) {
bfun_op = _fun_get_binary(it_first1, _EQUAL_FUN);
}
if (bfun_op == fun_default_binary) {
bfun_op = _fun_get_binary(it_first1, _LESS_FUN);
for (; !iterator_equal(it_first1, it_last1); it_first1 = iterator_next(it_first1), it_first2 = iterator_next(it_first2)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_first2), &b_less);
if (b_less) {
return false;
}
(*bfun_op)(iterator_get_pointer(it_first2), iterator_get_pointer(it_first1), &b_greater);
if (b_greater) {
return false;
}
}
} else {
for (; !iterator_equal(it_first1, it_last1); it_first1 = iterator_next(it_first1), it_first2 = iterator_next(it_first2)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_first2), &b_result);
if (!b_result) {
return false;
}
}
}
return true;
}
/**
* Searches for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence.
*/
forward_iterator_t algo_search(
forward_iterator_t it_first1, forward_iterator_t it_last1, forward_iterator_t it_first2, forward_iterator_t it_last2)
{
return algo_search_if(it_first1, it_last1, it_first2, it_last2, _fun_get_binary(it_first1, _EQUAL_FUN));
}
/**
* Searches for the first occurrence of a sequence within a target range whose elements or whose elements are equivalent in a sense specified
*/
forward_iterator_t algo_search_if(
forward_iterator_t it_first1, forward_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2,
bfun_t bfun_op)
{
bool_t b_result = false;
bool_t b_less = false;
bool_t b_greater = false;
iterator_t it_index1;
iterator_t it_index2;
size_t t_len1 = 0;
size_t t_len2 = 0;
assert(_iterator_valid_range(it_first1, it_last1, _FORWARD_ITERATOR));
assert(_iterator_valid_range(it_first2, it_last2, _FORWARD_ITERATOR));
assert(_iterator_same_elem_type(it_first1, it_first2));
if (bfun_op == NULL) {
bfun_op = _fun_get_binary(it_first1, _EQUAL_FUN);
}
t_len1 = iterator_distance(it_first1, it_last1);
t_len2 = iterator_distance(it_first2, it_last2);
if (t_len1 == 0 || t_len1 < t_len2) {
return it_last1;
}
if (t_len2 == 0) {
return it_first1;
}
if (bfun_op == fun_default_binary) {
bfun_op = _fun_get_binary(it_first1, _LESS_FUN);
for (; !iterator_equal(it_first1, it_last1); it_first1 = iterator_next(it_first1)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_first2), &b_less);
if (b_less) {
continue;
}
(*bfun_op)(iterator_get_pointer(it_first2), iterator_get_pointer(it_first1), &b_greater);
if (b_greater) {
continue;
}
for (it_index1 = it_first1, it_index1 = iterator_next(it_index1),
it_index2 = it_first2, it_index2 = iterator_next(it_index2);
!iterator_equal(it_index1, it_last1) &&
!iterator_equal(it_index2, it_last2);
it_index1 = iterator_next(it_index1),
it_index2 = iterator_next(it_index2)) {
(*bfun_op)(iterator_get_pointer(it_index1), iterator_get_pointer(it_index2), &b_less);
if (b_less) {
break;
}
(*bfun_op)(iterator_get_pointer(it_index2), iterator_get_pointer(it_index1), &b_greater);
if (b_greater) {
break;
}
}
if (iterator_equal(it_index2, it_last2)) {
return it_first1;
}
}
} else {
for (; !iterator_equal(it_first1, it_last1); it_first1 = iterator_next(it_first1)) {
(*bfun_op)(iterator_get_pointer(it_first1), iterator_get_pointer(it_first2), &b_result);
if (b_result) {
for (it_index1 = it_first1, it_index1 = iterator_next(it_index1),
it_index2 = it_first2, it_index2 = iterator_next(it_index2);
!iterator_equal(it_index1, it_last1) &&
!iterator_equal(it_index2, it_last2);
it_index1 = iterator_next(it_index1),
it_index2 = iterator_next(it_index2)) {
(*bfun_op)(iterator_get_pointer(it_index1), iterator_get_pointer(it_index2), &b_result);
if (!b_result) {
break;
}
}
if (iterator_equal(it_index2, it_last2)) {
return it_first1;
}
}
}
}
return it_last1;
}
/**
* Looks in a range for the last subsequence that is identical to a specified sequence.
*/
forward_iterator_t algo_search_end(
forward_iterator_t it_first1, forward_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2)
{
return algo_search_end_if(it_first1, it_last1, it_first2, it_last2, _fun_get_binary(it_first1, _EQUAL_FUN));
}
/**
* Looks in a range for the last subsequence that is equivalent in a sense specified by a binary predicate.
*/
forward_iterator_t algo_search_end_if(
forward_iterator_t it_first1, forward_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2,
bfun_t bfun_op)
{
forward_iterator_t it_tmp;
forward_iterator_t it_result;
assert(_iterator_valid_range(it_first1, it_last1, _FORWARD_ITERATOR));
assert(_iterator_valid_range(it_first2, it_last2, _FORWARD_ITERATOR));
assert(_iterator_same_elem_type(it_first1, it_first2));
if (iterator_equal(it_first2, it_last2)) {
return it_last1;
}
it_result = it_tmp = it_last1;
for (;;) {
it_tmp = algo_search_if(it_first1, it_last1, it_first2, it_last2, bfun_op);
if (iterator_equal(it_tmp, it_last1)) {
return it_result;
} else {
it_result = it_tmp;
it_first1 = iterator_next(it_tmp);
}
}
}
/**
* Looks in a range for the last subsequence that is identical to a specified sequence.
*/
forward_iterator_t algo_find_end(
forward_iterator_t it_first1, forward_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2)
{
return algo_search_end(it_first1, it_last1, it_first2, it_last2);
}
/**
* Looks in a range for the last subsequence that is equivalent in a sense specified by a binary predicate.
*/
forward_iterator_t algo_find_end_if(
forward_iterator_t it_first1, forward_iterator_t it_last1,
forward_iterator_t it_first2, forward_iterator_t it_last2,
bfun_t bfun_op)
{
return algo_search_end_if(it_first1, it_last1, it_first2, it_last2, bfun_op);
}
/** eof **/