示例#1
0
static TArray *
_timsort_merge (TArray * a, TArray * b,TCompDataFunc cmp_func, tpointer cmp_data)
{
    int i, j, k, first_end, second_end;
    TArray *result;
    result = t_array_new();

    i = 0;
    j = 0;
    first_end = a->len;
    second_end = b->len;
    while ((i < first_end) && (j < second_end))
        if (cmp_func (a->vector[i], b->vector[j], cmp_data) > 0)
            t_array_append(result ,b->vector[j++]);
        else
            t_array_append(result ,a->vector[i++]);

    if (i >= first_end)
        for (k = j; k < second_end; k++)
            t_array_append(result ,b->vector[k]);

    if (j >= second_end)
        for (k = i; k < first_end ; k++)
            t_array_append(result ,a->vector[k]);

    return result;
}
示例#2
0
/*
 * t_array_filter_with_data
 * Filter a given target element for ordered arrays using binary search method.
 */
TArray *
t_array_filter_with_data (TArray * array, tpointer target,
                          TCompDataFunc cmp_func, tpointer user_data)
{
    TArray *ret = t_array_new ();
    tpointer elem;
    int mid;
    TBoolean found;

    mid = t_array_binary_lookup_index_with_data (array, target, cmp_func,
            user_data, &found);

    if (found) {
        int i = mid - 1, j = mid + 1;

        elem = array->vector[mid];
        t_array_append (ret, elem);
        elem = t_array_index (array, i);
        while ((i >= 0) && (cmp_func (elem, target, user_data) == 0)) {
            t_array_append (ret, elem);
            i--;
            elem = t_array_index (array, i);
        }

        elem = t_array_index (array, j);
        while ((j <= t_array_length (array) - 1) &&
                (cmp_func (elem, target, user_data) == 0)) {
            t_array_append (ret, elem);
            j++;
            elem = t_array_index (array, j);
        }
    }
    return ret;
}
示例#3
0
TArray *
t_array_new_with_length (int length)
{
    TArray *array;
    int i;

    array = t_array_new ();
    for (i = 0; i < length; i++)
        t_array_append (array, NULL);

    return array;
}
示例#4
0
文件: main.c 项目: degva/NonGlitcher
int main(int argc, char *argv[])
{
  TArr *test = t_array_new();
  char msg[] = "This is a list of things. Select one of them";
  TMsg *opt;
 
  t_array_append(test, t_msg_new("This is it..."));
  t_array_append(test, t_msg_new("This is another option"));
  t_array_append(test, t_msg_new("Opt 3"));

  opt = retrieve_opt(test, msg);
  printf("You've selected \"%s\"\n", opt->msg);

  return 0;
}
示例#5
0
/*
 * t_array_filter2_with_data
 * Filter a given target element for non-ordered arrays.
 */
TArray *
t_array_filter2_with_data (TArray * array, tpointer target, TEqDataFunc eq_func,
                           tpointer user_data)
{
    int i;
    TArray *ret;
    ret = t_array_new ();

    for (i = 0; i < t_array_length (array); i++) {
        tpointer elem;
        elem = t_array_index (array, i);
        if (eq_func (elem, target, user_data))
            t_array_append (ret, elem);
    }
    return ret;
}
示例#6
0
TArray *
lok_hero_create_heros ()
{
  TArray *heros;
  LokHero *hero;
  int i;

  heros = t_array_new ();

  for (i = 0; LOK_HERO_ACTORS[i].name != NULL; i++) {
    hero = lok_hero_new (NULL);
    lok_hero_set_actor (hero, i);
    t_array_append (heros, hero);
  }

  return heros;
}
示例#7
0
void
t_array_tim_sort_with_data (TArray * array, TCompDataFunc cmp_func,
                            tpointer cmp_data)
{
    TArray *run_x, *run_y, *run_z;
    TArray *runs;
    int i = 0; /* indice para el array*/
    int minrun;

    if (array->len < 64) {
        t_array_insertion_sort_with_data (array,cmp_func, cmp_data);
        return;
    }

    minrun = _calc_minrun(array->len); /*Definido */


    runs = t_array_new ();
    TArray *run;

    while (i < array->len) {
        TArray *merged;
        run = _get_run_with_data (array, &i, minrun, cmp_func, cmp_data);
        t_array_append (runs, run);
        if (runs->len >= 3) {
            run_x = t_array_index (runs, 2); /*primero*/
            run_y = t_array_index (runs, 1); /*segundo*/
            run_z = t_array_index (runs, 0); /*tercero*/
            if (!(run_x->len > run_y->len + run_z->len && run_y->len > run_z->len)) {
                TArray *smaller, *larger;

                if (run_x->len < run_z->len) {
                    smaller = run_x;
                    larger = run_z;
                } else {
                    smaller = run_z;
                    larger = run_x;
                }
                /*Remove all
                t_array_remove_last (runs);
                t_array_remove_last (runs);
                t_array_remove_last (runs);
                */
                free (runs);
                runs = t_array_new (); /*clean*/
                merged = _timsort_merge (run_y, smaller, cmp_func, cmp_data);
                t_array_append(runs, merged);
                t_array_append(runs, larger);
            } else {
                merged = _timsort_merge (run_x, run_y, cmp_func, cmp_data);
                /*Remove all
                t_array_remove_last (runs);
                t_array_remove_last (runs);
                t_array_remove_last (runs);
                */
                free (runs);
                runs = t_array_new (); /*clean*/
                t_array_append(runs, merged);
                t_array_append(runs, run_z);
            }
        }
    }
    run_x = t_array_index (runs, 1); /*primero*/
    run_y = t_array_index (runs, 0); /*segundo*/
    array->vector = _timsort_merge(run_x, run_y, cmp_func, cmp_data)->vector; /*mezcla final*/
}
示例#8
0
static TArray *
_get_run_with_data (TArray *array, int * i, int minrun,
                    TCompDataFunc cmp_func, tpointer cmp_data)
{
    TArray *run;
    run = t_array_new ();

    /*el ultimo elemento*/
    if (*i == array->len- 1) {
        t_array_append (run, array->vector[*i]);
        (*i)++;
        return run;
    }


    /*swap first and second*/
    if (cmp_func (array->vector[*i], array->vector[*i + 1], cmp_data) > 0) {
        t_array_append (run, array->vector[*i + 1]);
        t_array_append (run, array->vector[*i]);
    } else {
        t_array_append (run, array->vector[*i]);
        t_array_append (run, array->vector[*i + 1]);
    }
    (*i)++;

    if (*i == array->len - 1) {
        (*i)++;
        return run;
    }
    /*add*/
    while (cmp_func(run->vector[run->len - 1],
                    array->vector[*i + 1], cmp_data)<=0) {
        t_array_append (run, array->vector[*i + 1]);
        (*i)++;
        if (*i == array->len - 1) {
            (*i)++;
            return run;
        }
    }
    if (run->len >= minrun) {
        (*i)++;
        return run;
    }

    while ((*i < array->len - 1) && (run->len < minrun)) {
        t_array_insert_sorted (run, array->vector[*i + 1], cmp_func, cmp_data);
        (*i)++;
        if (*i == array->len - 1) {
            (*i)++;
            return run;
        }
    }

    /*add*/
    while ((run->len >= minrun) && (cmp_func (run->vector[run->len - 1],
                                    array->vector[*i + 1], cmp_data) <= 0)) {

        t_array_append (run, array->vector[*i + 1]);
        (*i)++;
        if (*i == array->len - 1) {
            (*i)++;
            return run;
        }
    }
    (*i)++;
    return run;
}