Exemplo n.º 1
0
void test_pop_delete(void)
{
     List list = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     list_sort_by_item(&list);
     list_unique(&list);
     printf("Original List ::: ");
     list_print(&list);

     List duplicate = list_duplicate(&list);
     list_pop(&duplicate);
     printf("%sList 1 without last item ::: ", cyan);
     list_print(&duplicate);
   
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint random_value = rand() % duplicate.size;
     list_pop_multi(&duplicate, random_value);
     printf("%sList without last %d items ::: ", brown, random_value);
     list_print(&duplicate);
	      
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint random_end = rand() % duplicate.size;
     list_pop_until(&duplicate, random_end);
     printf("%sList until %d ::: ", magenta, random_end);
     list_print(&duplicate);

     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint random_position = rand() % (duplicate.size);
     list_delete_position(&duplicate, random_position);
     printf("%sList wihout %d-d item ::: ", green, random_position);
     list_print(&duplicate);
     printf("%s", none);

     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     Item random_item;
     random_position = rand() % (duplicate.size);
     random_item = duplicate.data[random_position];
     list_delete_item(&duplicate, random_item);
     printf("%sList without %d ::: ", red, random_item.item);
     list_print(&duplicate);
     printf("%s", none);
     
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint low = rand() % duplicate.size;
     uint high = (rand() % (duplicate.size - low)) + low;
     list_delete_range(&duplicate, low, high);
     printf("%sList without range %d - %d ::: ", blue, low, high);
     list_print(&duplicate);
     printf("%s", none);
}
Exemplo n.º 2
0
/**
 * @brief Deletes repeated items in a list
 *
 * @param list List where the repeated items will be deleted
 */
void list_unique(List *list)
{
     uint i;

     if (list->size > 0) {
          for (i = 0; i < list->size - 1; i++) {
               if (list->data[i].item == list->data[i + 1].item) {
                    int k = 1;
                    while(i + k < list->size &&
                          list->data[i].item == list->data[i + k].item) {
                         list->data[i].freq += list->data[i + k].freq;
                         k++;
                    }
                    list_delete_range(list, i + 1, i + k - 1);
               }
          }
     }
}
Exemplo n.º 3
0
///
/// Performs preprocessing using the internally
/// constructed state object.
///
void ppimpl_process(state_t* state)
{
    char c;
    match_t* m;
    bool reprocess = false;
    size_t i;
    scope_t* current;

    while (ppimpl_has_input(state))
    {
        // Fetch a character from the input.
        c = ppimpl_get_input(state);

        // Push it onto the output.
        list_append(&state->cached_output, (void*)c);

        // Update string tracking states.
        ppimpl_track_strings(state);

        // If we are in a single or double string, no matching
        // operations can be performed.
        if (state->in_single_string || state->in_double_string)
            continue;

        // We keep process once unless we're asked to restart
        // by a match.  This is used when a match changes the
        // handlers (such as appending a new handler or removing
        // an old one), and we need to restart the matching process.
        do
        {
            reprocess = false;

            // Iterate through all of our match handlers.
            for (i = 0; i < list_size(&state->handlers); i++)
            {
                m = (match_t*)list_get_at(&state->handlers, i);

                // Test for a match and if so, handle it.
                if (ppimpl_match_test(state, m))
                {
                    // Remove the characters from the cached input
                    // depending on the match text length.
                    list_delete_range(&state->cached_output,
                                      list_size(&state->cached_output) - blength(m->text.ref),
                                      list_size(&state->cached_output) - 1);

                    // Reset the printing index.
                    state->print_index = 0;

                    // Call the match handler.
                    m->handler(state, m, &reprocess);
                    if (reprocess)
                        break;
                }
            }
        }
        while (reprocess);
    }

    // We have finished processing; everything is stored in the cached output,
    // so now we push everything that was in the cached output.
    while (list_size(&state->cached_output) > 0)
        state->output((char)list_extract_at(&state->cached_output, 0));
}