Beispiel #1
0
static void reactor_stream_release(reactor_stream *stream)
{
  stream->refs --;
  if (!stream->refs)
    {
      buffer_destruct(&stream->read);
      buffer_destruct(&stream->write);
      reactor_user_dispatch(&stream->user, REACTOR_STREAM_DESTRUCT, stream);
    }
}
Beispiel #2
0
int main (int argc, char* argv[])
{
    clock_t begin, end;
    double time_spent;

    //^^^^^^^^^^^^^^^^^^^^^^^^^
    //Default part BEGIN
    //^^^^^^^^^^^^^^^^^^^^^^^^^
    INFO();
    CHECK_DEFAULT_ARGS();
    char inName[NAME_MAX] = {}, outName[NAME_MAX] = {};
    switch (argc)
    {
    case 3:
        strcpy (inName, argv[1]);
        strcpy (outName, argv[2]);
        break;
    case 2:
        strcpy (inName, argv[1]);
        strcpy (outName, "info.csv");
        break;
    default:
        WRITE_WRONG_USE();
    }
    // Words will be loaded here
    Buffer words = {};
    if (!buffer_construct(&words, inName))
        return WRONG_USE;
    // We'll store lists here
    list_t* lists = (list_t*)calloc (HASH_TABLE_SIZE, sizeof(list_t));
        for (int i = 0; i < HASH_TABLE_SIZE; i++)\
            list_t_construct(lists + i, 0);
    //buffer_dump(&words);
    size_t counter = 0, total = words.length;
    #ifndef NO_OUTPUT
    printf ("Progress:\n");
    #endif // NO_OUTPUT
    // Get the chars array from buffer

    begin = clock();
    for (char *word = words.chars; counter < words.length; word++){
        #ifndef NO_OUTPUT
        if (counter % (size_t)total / 1000){
            printf ("\r%.1f%%", 100.0*counter/total);
            fflush(stdout);
        }
        #endif // NO_OUTPUT
        counter++;
        // Locating first letter
        if (isalnum (*word)){
            //Pointer to the word
            char *new_word = word;
            // Finding first non-letter
            while (isalnum (*word) || strchr(OK_PUNCT, *word)){
                word++;
                counter++;
            }
            //Indicating the end
            *word = '\0';
            //printf ("Trying to add: %s\n", new_word);
            // Finding the right place to insert
            size_t position;
            position = hash_xor_opt(new_word) % HASH_TABLE_SIZE;
            list_t_add(lists + position, new_word);
            //list_t_dump(lists + position);
        }
    }
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Hash table creation: %lfms\n", time_spent*1000);
    // Newline after progress bar
    printf ("\n");
    /// Some tests
    /*
    size_t position;
    position = hash_xor_opt("herdersleven") % HASH_TABLE_SIZE;
    list_t_dump(lists + position);
    list_node_t_remove(lists[position].storage + 3);
    list_t_dump(lists + position);
    list_t_exchange_num(lists+position, 2,4);
    list_t_dump(lists + position);
    */
    /// TIME MEASUREMENT
    begin = clock();
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Empty timer: %lfms\n", time_spent*1000);

    list_node_t* found = NULL;
    begin = clock();
    found = hash_table_get_node (lists, HASH_TABLE_SIZE, "draagberries");
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Search time: %lfms\n", time_spent*1000);
    if (found)
        list_node_t_dump(found);
    else
        printf ("Not found");

    /// TIME MEASUREMENT END
    #ifndef NO_OUTPUT
    open_file (out, outName, "w", "#Output error");
    fprintf (out, "Hash value, ");
        for (size_t i = 0; i < HASH_TABLE_SIZE; i++)
            fprintf (out, "%lu, ", i);
    fprintf (out, "\n");

    fprintf (out, "Hash type: " "xor_opt" ", ");
    for (size_t i = 0; i < HASH_TABLE_SIZE; i++){
        fprintf (out, "%lu, ", lists[i].size);
        list_t_destruct(lists + i);
    }
    fprintf (out, "\n");
    close_file(out);
    #endif // NO_OUTPUT

    printf ("\nThe info has been successfully written to %s.\n", outName);
    free (lists);
    buffer_destruct(&words);

    return NO_ERROR;
}
Beispiel #3
0
int main (int argc, char* argv[])
{
    clock_t begin, end;
    double time_spent;
    //^^^^^^^^^^^^^^^^^^^^^^^^^
    //Default part BEGIN
    //^^^^^^^^^^^^^^^^^^^^^^^^^
    CHECK_DEFAULT_ARGS();
    char inName[NAME_MAX] = {}, outName[NAME_MAX] = {};
    switch (argc)
    {
    case 3:
        strcpy (inName, argv[1]);
        strcpy (outName, argv[2]);
        break;
    case 2:
        strcpy (inName, argv[1]);
        strcpy (outName, "info.csv");
        break;
    default:
        WRITE_WRONG_USE();
    }

    Buffer words = {};
    buffer_construct(&words, inName);
    char *word = words.chars;

    #define LISTS(HASH_TYPE) \
        ListHead lists_ ## HASH_TYPE [HASH_TABLE_SIZE];\
        for (int i = 0; i < HASH_TABLE_SIZE; i++)\
            list_head_construct(lists_ ## HASH_TYPE + i);
    LISTS(xor);
    //LISTS(const);
    //LISTS(length);
    //LISTS(char);
    //LISTS(sum);

    int counter = 0, total = words.length;
    #ifndef NO_OUTPUT
    printf ("Progress:\n");
    #endif // NO_OUTPUT
    begin = clock();
    while (*word)
    {
        #ifndef NO_OUTPUT
        if (counter % (int)total / 1000)
            printf ("\r%3.1f%%", 100.0*counter/total);
        #endif // NO_OUTPUT
        counter++;
        // Locating first letter
        if (!isalnum (*word))
            word++;
        else
        {
            //Pointer to the word
            char *new_word = word;
            // Finding first non-letter
            while (isalnum (*word) || strchr(OK_PUNCT, *word))
            {
                word++;
                counter++;
            }
            //Indicating the end
            *word = '\0';
            size_t position;
            #define HASH_ADD(HASH_TYPE) \
                position = hash_ ## HASH_TYPE(new_word) % HASH_TABLE_SIZE;\
                list_head_add(&lists_ ## HASH_TYPE[position], new_word);
            HASH_ADD(xor);
            //HASH_ADD(const);
            //HASH_ADD(length);
            //HASH_ADD(char);
            //HASH_ADD(sum);
            word++;
        }
    }
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf ("Hash table creation: %lfms\n", time_spent*1000);
    buffer_destruct(&words);

    #ifndef NO_OUTPUT
    open_file (out, outName, "w", "#Output error");
    fprintf (out, "Hash value, ");
        for (int i = 0; i < HASH_TABLE_SIZE; i++)
            fprintf (out, "%d, ", i);
    fprintf (out, "\n");
    #define PRINT_INFO(HASH_TYPE) \
        fprintf (out, "Hash type: " #HASH_TYPE ", ");\
        for (int i = 0; i < HASH_TABLE_SIZE; i++)\
            fprintf (out, "%lu, ", lists_ ## HASH_TYPE[i].amount);\
        fprintf (out, "\n");

    PRINT_INFO(xor);
    //PRINT_INFO(const);
    //PRINT_INFO(length);
    //PRINT_INFO(char);
    //PRINT_INFO(sum);
    close_file(out);
    #endif // NO_OUTPUT
    printf ("\nThe info has been successfully written to %s.\n", outName);

    list_head_destruct(lists_xor);
    //list_head_destruct(lists_const);
    //list_head_destruct(lists_length);
    //list_head_destruct(lists_char);
    //list_head_destruct(lists_sum);

    return NO_ERROR;
}