Skip to content

Commit

Permalink
refactoring algo_search_n and algo_search_n_if.
Browse files Browse the repository at this point in the history
  • Loading branch information
activesys committed Oct 13, 2012
1 parent 262a92b commit 2383ed9
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 96 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Expand Up @@ -4,5 +4,5 @@ The current maintainers:
Bo Wang <activesys.wb@gmail.com>

Contributors:
Yang Yu
Yang Yu <I@reyoung.me>

153 changes: 61 additions & 92 deletions src/cstl_algo_nonmutating_private.c
Expand Up @@ -188,164 +188,133 @@ size_t _algo_count_varg(input_iterator_t it_first, input_iterator_t it_last, va_
/**
* Searches for the first subsequence in a range that of a specified number of elements having a particular value.
*/
forward_iterator_t _algo_search_n(
forward_iterator_t t_first, forward_iterator_t t_last,
size_t t_count, ...)
forward_iterator_t _algo_search_n(forward_iterator_t it_first, forward_iterator_t it_last, size_t t_count, ...)
{
forward_iterator_t t_iter;
forward_iterator_t it_iter;
va_list val_elemlist;

va_start(val_elemlist, t_count);
t_iter = _algo_search_n_if_varg(t_first, t_last, t_count,
_fun_get_binary(t_first, _EQUAL_FUN), val_elemlist);
it_iter = _algo_search_n_if_varg(it_first, it_last, t_count, _fun_get_binary(it_first, _EQUAL_FUN), val_elemlist);
va_end(val_elemlist);

return t_iter;
return it_iter;
}

/**
* 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.
*/
forward_iterator_t _algo_search_n_if(
forward_iterator_t t_first, forward_iterator_t t_last,
size_t t_count, binary_function_t t_binary_op, ...)
forward_iterator_t it_first, forward_iterator_t it_last, size_t t_count, binary_function_t bfun_op, ...)
{
forward_iterator_t t_iter;
forward_iterator_t it_iter;
va_list val_elemlist;

va_start(val_elemlist, t_binary_op);
t_iter = _algo_search_n_if_varg(
t_first, t_last, t_count, t_binary_op, val_elemlist);
va_start(val_elemlist, bfun_op);
it_iter = _algo_search_n_if_varg(it_first, it_last, t_count, bfun_op, val_elemlist);
va_end(val_elemlist);

return t_iter;
return it_iter;
}

/**
* 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.
*/
forward_iterator_t _algo_search_n_if_varg(
forward_iterator_t t_first, forward_iterator_t t_last,
size_t t_count, binary_function_t t_binary_op, va_list val_elemlist)
forward_iterator_t it_first, forward_iterator_t it_last, size_t t_count, binary_function_t bfun_op, va_list val_elemlist)
{
void* pv_value = NULL;
bool_t t_result = false;
bool_t t_less = false;
bool_t t_greater = false;
iterator_t t_index;
size_t t_i = 0;
bool_t b_result = false;
bool_t b_less = false;
bool_t b_greater = false;
iterator_t it_index;
size_t i = 0;

assert(_iterator_valid_range(t_first, t_last, _FORWARD_ITERATOR));
assert(_iterator_valid_range(it_first, it_last, _FORWARD_ITERATOR));

if(t_count == 0)
{
return t_last;
if (t_count == 0) {
return it_first;
}

if(t_binary_op == NULL)
{
t_binary_op = _fun_get_binary(t_first, _EQUAL_FUN);
if (bfun_op == NULL) {
bfun_op = _fun_get_binary(it_first, _EQUAL_FUN);
}

pv_value = _iterator_allocate_init_elem(t_first);
_type_get_varg_value(_iterator_get_typeinfo(t_first), val_elemlist, pv_value);

if(t_binary_op == fun_default_binary)
{
t_binary_op = _fun_get_binary(t_first, _LESS_FUN);
for(; !iterator_equal(t_first, t_last); t_first = iterator_next(t_first))
{
(*t_binary_op)(iterator_get_pointer(t_first), pv_value, &t_less);
if(t_less)
{
pv_value = _iterator_allocate_init_elem(it_first);
_type_get_varg_value(_iterator_get_typeinfo(it_first), val_elemlist, pv_value);

if (bfun_op == fun_default_binary) {
bfun_op = _fun_get_binary(it_first, _LESS_FUN);
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
(*bfun_op)(iterator_get_pointer(it_first), pv_value, &b_less);
if (b_less) {
continue;
}
(*t_binary_op)(pv_value, iterator_get_pointer(t_first), &t_greater);
if(t_greater)
{
(*bfun_op)(pv_value, iterator_get_pointer(it_first), &b_greater);
if (b_greater) {
continue;
}

for(t_i = 1, t_index = t_first, t_index = iterator_next(t_index);
t_i < t_count && !iterator_equal(t_index, t_last);
++t_i, t_index = iterator_next(t_index))
{
(*t_binary_op)(iterator_get_pointer(t_index), pv_value, &t_less);
if(t_less)
{
for (i = 1, it_index = iterator_next(it_first);
i < t_count && !iterator_equal(it_index, it_last);
++i, it_index = iterator_next(it_index)) {
(*bfun_op)(iterator_get_pointer(it_index), pv_value, &b_less);
if (b_less) {
break;
}
(*t_binary_op)(pv_value, iterator_get_pointer(t_index), &t_greater);
if(t_greater)
{
(*bfun_op)(pv_value, iterator_get_pointer(it_index), &b_greater);
if (b_greater) {
break;
}
}

if(t_i == t_count)
{
if (i == t_count) {
break;
}
}
}
else
{
if(strncmp(_iterator_get_typebasename(t_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0)
{
for(; !iterator_equal(t_first, t_last); t_first = iterator_next(t_first))
{
(*t_binary_op)(iterator_get_pointer(t_first), string_c_str((string_t*)pv_value), &t_result);
if(t_result)
{
for(t_i = 1, t_index = t_first, t_index = iterator_next(t_index);
t_i < t_count && !iterator_equal(t_index, t_last);
++t_i, t_index = iterator_next(t_index))
{
(*t_binary_op)(iterator_get_pointer(t_index), string_c_str((string_t*)pv_value), &t_result);
if(!t_result)
{
} else {
if (strncmp(_iterator_get_typebasename(it_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
(*bfun_op)(iterator_get_pointer(it_first), string_c_str((string_t*)pv_value), &b_result);
if (b_result) {
for (i = 1, it_index = iterator_next(it_first);
i < t_count && !iterator_equal(it_index, it_last);
++i, it_index = iterator_next(it_index)) {
(*bfun_op)(iterator_get_pointer(it_index), string_c_str((string_t*)pv_value), &b_result);
if (!b_result) {
break;
}
}

if(t_i == t_count)
{
if (i == t_count) {
break;
}
}
}
}
else
{
for(; !iterator_equal(t_first, t_last); t_first = iterator_next(t_first))
{
(*t_binary_op)(iterator_get_pointer(t_first), pv_value, &t_result);
if(t_result)
{
for(t_i = 1, t_index = t_first, t_index = iterator_next(t_index);
t_i < t_count && !iterator_equal(t_index, t_last);
++t_i, t_index = iterator_next(t_index))
{
(*t_binary_op)(iterator_get_pointer(t_index), pv_value, &t_result);
if(!t_result)
{
} else {
for (; !iterator_equal(it_first, it_last); it_first = iterator_next(it_first)) {
(*bfun_op)(iterator_get_pointer(it_first), pv_value, &b_result);
if (b_result) {
for (i = 1, it_index = iterator_next(it_first);
i < t_count && !iterator_equal(it_index, it_last);
++i, it_index = iterator_next(it_index)) {
(*bfun_op)(iterator_get_pointer(it_index), pv_value, &b_result);
if (!b_result) {
break;
}
}

if(t_i == t_count)
{
if (i == t_count) {
break;
}
}
}
}
}

_iterator_deallocate_destroy_elem(t_first, pv_value);
_iterator_deallocate_destroy_elem(it_first, pv_value);
pv_value = NULL;

return t_first;
return it_first;
}

/** eof **/
Expand Down

0 comments on commit 2383ed9

Please sign in to comment.