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; }
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; }
void buffer_clear(buffer *b) { free(b->data); buffer_construct(b); }
int main(int argc, char **argv) { size_t bufPos; size_t read; short buffer[BUFFERSIZE]; short encoded[BUFFERSIZE]; struct wavpcm_input input; struct wavpcm_output output; struct decode_chunk_struct decode_chunk_left; struct decode_chunk_struct decode_chunk_right; struct encode_chunk_struct encode_chunk_left; struct encode_chunk_struct encode_chunk_right; // Initializations srand(time(NULL)); _convFromOctets(); // Construct buffer_construct(); channel_construct(); sender_construct(); receiver_construct(); // Handshake #ifndef __ENC_NO_PRINTS__ printf("\n# Key Exchange\n"); printf("--------------\n\n"); #endif handshakeState = SENDER_HELLO; while (HANDSHAKE_FINISHED != handshakeState) _handshake(); // Transmit memset(&input, 0, sizeof(struct wavpcm_input)); input.resource = INPUTWAVFILE; memset(&output, 0, sizeof(struct wavpcm_output)); output.resource = OUTPUTWAVFILE; /* initialize structs */ memset(&encode_chunk_left, 0, sizeof(struct encode_chunk_struct)); memset(&encode_chunk_right, 0, sizeof(struct encode_chunk_struct)); memset(&decode_chunk_left, 0, sizeof(struct decode_chunk_struct)); memset(&decode_chunk_right, 0, sizeof(struct decode_chunk_struct)); /* initializing for quantisation */ encode_chunk_left.Qstep[0] = QSTART; encode_chunk_right.Qstep[0] = QSTART; decode_chunk_left.Qstep[0] = QSTART; decode_chunk_right.Qstep[0] = QSTART; encode_chunk_left.Qstep[1] = QSTART; encode_chunk_right.Qstep[1] = QSTART; decode_chunk_left.Qstep[1] = QSTART; decode_chunk_right.Qstep[1] = QSTART; encode_chunk_left.Qstep[2] = QSTART; encode_chunk_right.Qstep[2] = QSTART; decode_chunk_left.Qstep[2] = QSTART; decode_chunk_right.Qstep[2] = QSTART; encode_chunk_left.Qstep[3] = QSTART; encode_chunk_right.Qstep[3] = QSTART; decode_chunk_left.Qstep[3] = QSTART; decode_chunk_right.Qstep[3] = QSTART; wavpcm_input_open(&input); wavpcm_output_copy_settings(&input, &output); wavpcm_output_open(&output); for (bufPos = 0; bufPos < input.samplesAvailable ; bufPos += (BUFFERSIZE/2)) { read = wavpcm_input_read(&input, buffer); encode(buffer, &encode_chunk_left, &encode_chunk_right, encoded); while (buffer_isModified()) {} buffer_write((field_t *) buffer, BUFFERSIZE*sizeof(short)); _transmit(); receiver_receiveData(); buffer_read((field_t *) buffer, BUFFERSIZE*sizeof(short)); decode(&decode_chunk_left, &decode_chunk_right, encoded, buffer); wavpcm_output_write(&output, buffer, read); } wavpcm_output_close(&output); exit(EXIT_SUCCESS); }