Skip to content

Commit 2383ed9

Browse files
committed
refactoring algo_search_n and algo_search_n_if.
1 parent 262a92b commit 2383ed9

5 files changed

+412
-96
lines changed

AUTHORS

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ The current maintainers:
44
55

66
Contributors:
7-
Yang Yu
7+
88

src/cstl_algo_nonmutating_private.c

+61-92
Original file line numberDiff line numberDiff line change
@@ -188,164 +188,133 @@ size_t _algo_count_varg(input_iterator_t it_first, input_iterator_t it_last, va_
188188
/**
189189
* Searches for the first subsequence in a range that of a specified number of elements having a particular value.
190190
*/
191-
forward_iterator_t _algo_search_n(
192-
forward_iterator_t t_first, forward_iterator_t t_last,
193-
size_t t_count, ...)
191+
forward_iterator_t _algo_search_n(forward_iterator_t it_first, forward_iterator_t it_last, size_t t_count, ...)
194192
{
195-
forward_iterator_t t_iter;
193+
forward_iterator_t it_iter;
196194
va_list val_elemlist;
197195

198196
va_start(val_elemlist, t_count);
199-
t_iter = _algo_search_n_if_varg(t_first, t_last, t_count,
200-
_fun_get_binary(t_first, _EQUAL_FUN), val_elemlist);
197+
it_iter = _algo_search_n_if_varg(it_first, it_last, t_count, _fun_get_binary(it_first, _EQUAL_FUN), val_elemlist);
201198
va_end(val_elemlist);
202199

203-
return t_iter;
200+
return it_iter;
204201
}
205202

206203
/**
207204
* Searches for the first subsequence in a range that of a specified number of elements having a relation to that value as specified by a binary predicate.
208205
*/
209206
forward_iterator_t _algo_search_n_if(
210-
forward_iterator_t t_first, forward_iterator_t t_last,
211-
size_t t_count, binary_function_t t_binary_op, ...)
207+
forward_iterator_t it_first, forward_iterator_t it_last, size_t t_count, binary_function_t bfun_op, ...)
212208
{
213-
forward_iterator_t t_iter;
209+
forward_iterator_t it_iter;
214210
va_list val_elemlist;
215211

216-
va_start(val_elemlist, t_binary_op);
217-
t_iter = _algo_search_n_if_varg(
218-
t_first, t_last, t_count, t_binary_op, val_elemlist);
212+
va_start(val_elemlist, bfun_op);
213+
it_iter = _algo_search_n_if_varg(it_first, it_last, t_count, bfun_op, val_elemlist);
219214
va_end(val_elemlist);
220215

221-
return t_iter;
216+
return it_iter;
222217
}
223218

224219
/**
225220
* Searches for the first subsequence in a range that of a specified number of elements having a relation to that value as specified by a binary predicate.
226221
*/
227222
forward_iterator_t _algo_search_n_if_varg(
228-
forward_iterator_t t_first, forward_iterator_t t_last,
229-
size_t t_count, binary_function_t t_binary_op, va_list val_elemlist)
223+
forward_iterator_t it_first, forward_iterator_t it_last, size_t t_count, binary_function_t bfun_op, va_list val_elemlist)
230224
{
231225
void* pv_value = NULL;
232-
bool_t t_result = false;
233-
bool_t t_less = false;
234-
bool_t t_greater = false;
235-
iterator_t t_index;
236-
size_t t_i = 0;
226+
bool_t b_result = false;
227+
bool_t b_less = false;
228+
bool_t b_greater = false;
229+
iterator_t it_index;
230+
size_t i = 0;
237231

238-
assert(_iterator_valid_range(t_first, t_last, _FORWARD_ITERATOR));
232+
assert(_iterator_valid_range(it_first, it_last, _FORWARD_ITERATOR));
239233

240-
if(t_count == 0)
241-
{
242-
return t_last;
234+
if (t_count == 0) {
235+
return it_first;
243236
}
244237

245-
if(t_binary_op == NULL)
246-
{
247-
t_binary_op = _fun_get_binary(t_first, _EQUAL_FUN);
238+
if (bfun_op == NULL) {
239+
bfun_op = _fun_get_binary(it_first, _EQUAL_FUN);
248240
}
249241

250-
pv_value = _iterator_allocate_init_elem(t_first);
251-
_type_get_varg_value(_iterator_get_typeinfo(t_first), val_elemlist, pv_value);
252-
253-
if(t_binary_op == fun_default_binary)
254-
{
255-
t_binary_op = _fun_get_binary(t_first, _LESS_FUN);
256-
for(; !iterator_equal(t_first, t_last); t_first = iterator_next(t_first))
257-
{
258-
(*t_binary_op)(iterator_get_pointer(t_first), pv_value, &t_less);
259-
if(t_less)
260-
{
242+
pv_value = _iterator_allocate_init_elem(it_first);
243+
_type_get_varg_value(_iterator_get_typeinfo(it_first), val_elemlist, pv_value);
244+
245+
if (bfun_op == fun_default_binary) {
246+
bfun_op = _fun_get_binary(it_first, _LESS_FUN);
247+
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
248+
(*bfun_op)(iterator_get_pointer(it_first), pv_value, &b_less);
249+
if (b_less) {
261250
continue;
262251
}
263-
(*t_binary_op)(pv_value, iterator_get_pointer(t_first), &t_greater);
264-
if(t_greater)
265-
{
252+
(*bfun_op)(pv_value, iterator_get_pointer(it_first), &b_greater);
253+
if (b_greater) {
266254
continue;
267255
}
268256

269-
for(t_i = 1, t_index = t_first, t_index = iterator_next(t_index);
270-
t_i < t_count && !iterator_equal(t_index, t_last);
271-
++t_i, t_index = iterator_next(t_index))
272-
{
273-
(*t_binary_op)(iterator_get_pointer(t_index), pv_value, &t_less);
274-
if(t_less)
275-
{
257+
for (i = 1, it_index = iterator_next(it_first);
258+
i < t_count && !iterator_equal(it_index, it_last);
259+
++i, it_index = iterator_next(it_index)) {
260+
(*bfun_op)(iterator_get_pointer(it_index), pv_value, &b_less);
261+
if (b_less) {
276262
break;
277263
}
278-
(*t_binary_op)(pv_value, iterator_get_pointer(t_index), &t_greater);
279-
if(t_greater)
280-
{
264+
(*bfun_op)(pv_value, iterator_get_pointer(it_index), &b_greater);
265+
if (b_greater) {
281266
break;
282267
}
283268
}
284269

285-
if(t_i == t_count)
286-
{
270+
if (i == t_count) {
287271
break;
288272
}
289273
}
290-
}
291-
else
292-
{
293-
if(strncmp(_iterator_get_typebasename(t_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0)
294-
{
295-
for(; !iterator_equal(t_first, t_last); t_first = iterator_next(t_first))
296-
{
297-
(*t_binary_op)(iterator_get_pointer(t_first), string_c_str((string_t*)pv_value), &t_result);
298-
if(t_result)
299-
{
300-
for(t_i = 1, t_index = t_first, t_index = iterator_next(t_index);
301-
t_i < t_count && !iterator_equal(t_index, t_last);
302-
++t_i, t_index = iterator_next(t_index))
303-
{
304-
(*t_binary_op)(iterator_get_pointer(t_index), string_c_str((string_t*)pv_value), &t_result);
305-
if(!t_result)
306-
{
274+
} else {
275+
if (strncmp(_iterator_get_typebasename(it_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
276+
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
277+
(*bfun_op)(iterator_get_pointer(it_first), string_c_str((string_t*)pv_value), &b_result);
278+
if (b_result) {
279+
for (i = 1, it_index = iterator_next(it_first);
280+
i < t_count && !iterator_equal(it_index, it_last);
281+
++i, it_index = iterator_next(it_index)) {
282+
(*bfun_op)(iterator_get_pointer(it_index), string_c_str((string_t*)pv_value), &b_result);
283+
if (!b_result) {
307284
break;
308285
}
309286
}
310287

311-
if(t_i == t_count)
312-
{
288+
if (i == t_count) {
313289
break;
314290
}
315291
}
316292
}
317-
}
318-
else
319-
{
320-
for(; !iterator_equal(t_first, t_last); t_first = iterator_next(t_first))
321-
{
322-
(*t_binary_op)(iterator_get_pointer(t_first), pv_value, &t_result);
323-
if(t_result)
324-
{
325-
for(t_i = 1, t_index = t_first, t_index = iterator_next(t_index);
326-
t_i < t_count && !iterator_equal(t_index, t_last);
327-
++t_i, t_index = iterator_next(t_index))
328-
{
329-
(*t_binary_op)(iterator_get_pointer(t_index), pv_value, &t_result);
330-
if(!t_result)
331-
{
293+
} else {
294+
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
295+
(*bfun_op)(iterator_get_pointer(it_first), pv_value, &b_result);
296+
if (b_result) {
297+
for (i = 1, it_index = iterator_next(it_first);
298+
i < t_count && !iterator_equal(it_index, it_last);
299+
++i, it_index = iterator_next(it_index)) {
300+
(*bfun_op)(iterator_get_pointer(it_index), pv_value, &b_result);
301+
if (!b_result) {
332302
break;
333303
}
334304
}
335305

336-
if(t_i == t_count)
337-
{
306+
if (i == t_count) {
338307
break;
339308
}
340309
}
341310
}
342311
}
343312
}
344313

345-
_iterator_deallocate_destroy_elem(t_first, pv_value);
314+
_iterator_deallocate_destroy_elem(it_first, pv_value);
346315
pv_value = NULL;
347316

348-
return t_first;
317+
return it_first;
349318
}
350319

351320
/** eof **/

0 commit comments

Comments
 (0)