|
| 1 | +#include <locale.h> |
| 2 | +#include "opencstl.h" |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +int count_lines(const char *filename) { |
| 7 | + FILE *fp = NULL; |
| 8 | + FOPEN(&fp, filename, "r"); |
| 9 | + if (!fp) return -1; |
| 10 | + |
| 11 | + int lines = 0; |
| 12 | + int c; |
| 13 | + while ((c = fgetc(fp)) != EOF) { |
| 14 | + if (c == '\n') lines++; |
| 15 | + } |
| 16 | + fclose(fp); |
| 17 | + return lines; |
| 18 | +} |
| 19 | + |
| 20 | +int main() { |
| 21 | + setlocale(LC_ALL, ""); |
| 22 | + |
| 23 | + int lines = count_lines("words_random.txt"); |
| 24 | + |
| 25 | + FILE *fp = NULL; |
| 26 | + |
| 27 | + FOPEN(&fp, "words_random.txt", "rt"); |
| 28 | + |
| 29 | + |
| 30 | + if (!fp) { |
| 31 | + fprintf(stderr, "Error opening file\n"); |
| 32 | + } |
| 33 | + VECTOR(char*) arr = new_vector(char*); |
| 34 | + |
| 35 | + const int LINE_SIZE = 256; |
| 36 | + char *strings = (char *) calloc(lines, sizeof(char) * LINE_SIZE); |
| 37 | + memset(strings, 0, sizeof(char) * LINE_SIZE); |
| 38 | + watch t_beg = tick(); |
| 39 | + int index = 0; |
| 40 | + char *line = strings; |
| 41 | + //int max_size = 0; |
| 42 | + while (GETLINE(fp, line, LINE_SIZE)) { |
| 43 | + size_t len = strlen(line); |
| 44 | + push_back(arr, line); |
| 45 | + line += LINE_SIZE; |
| 46 | + index++; |
| 47 | + } |
| 48 | + //printf("max size: %lld\n", max_size); |
| 49 | + double push_back_ms = lap(t_beg, tick()); |
| 50 | + printf("size: %lu\telapsed_time: %lf\n",cstl_size(arr), push_back_ms); |
| 51 | + |
| 52 | + |
| 53 | + t_beg = tick(); |
| 54 | + |
| 55 | + //char ***tree = cstl_set(char*, StringCmp); |
| 56 | + UNORDERED_SET(char*) tree = new_unordered_set(char*); |
| 57 | + for (int i = 0; i < size(arr); i++) { |
| 58 | + char *line = arr[i]; |
| 59 | + insert(tree, line); |
| 60 | + //puts(line); |
| 61 | + } |
| 62 | + double insert_time = lap(t_beg, tick()); |
| 63 | + printf("size: %lu\telapsed_time: %lf\n",size(tree), insert_time); |
| 64 | + |
| 65 | + |
| 66 | + // for (char **it = cstl_begin(tree); it != cstl_end(tree); it = cstl_next(it)) { |
| 67 | + // printf("%s\n",cstl_value(it, char*)); |
| 68 | + // } |
| 69 | + free(strings); |
| 70 | + cstl_free(tree); |
| 71 | + cstl_free(arr); |
| 72 | + return 0; |
| 73 | +} |
0 commit comments