include cstdio include cstring include immintrin include HashTable cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#include <cstdio>
#include <cstring>
#include <immintrin.h>
#include "HashTable.cpp"
#include <fstream>
int count_chars(FILE* file);
char* read_text(FILE* file);
void change_divider(char* whole_text, long int num_chars, char old_divider, char new_divider);
long int count_lines(char* whole_text, long int num_chars, char divider);
char** divide_lines(char* whole_text, long int num_lines, long int num_chars);
int main(void){
FILE* input = fopen("../opt_words_alpha.txt", "r");
size_t len = MAX_NUM_CHARS;
size_t num_lines = MAX_NUM_LINES;
char* whole_text = read_text(input);
char** text = divide_lines(whole_text, num_lines, len);
setvbuf(input, nullptr, _IOFBF, MAX_NUM_CHARS);
auto t = HashTable<const char*, size_t, 509>();
for (size_t i = 0; i < num_lines; ++i){
t.insert(text[i], i);
}
size_t d = 0;
for (size_t i = 0; i < num_lines; ++i){
d = t.find(text[i])->val.second;
}
fclose(input);
delete [] text;
delete [] whole_text;
return 0;
}
int count_chars(FILE* file){
int num_chars = 0;
fseek(file, 0L, SEEK_END);
num_chars = ftell(file);
rewind(file);
return num_chars;
}
char* read_text(FILE* file){
char* whole_text = new char[MAX_NUM_CHARS + 10];
//char* whole_text = (char*)aligned_alloc(32, MAX_NUM_CHARS + 10);
char* start = whole_text;
for (unsigned int i = 0; i < MAX_NUM_LINES; ++i){
fscanf(file, "%s", whole_text);
whole_text += 32;
}
return start;
}
void change_divider(char* whole_text, long int num_chars, char old_divider, char new_divider){
for(int i = 0; i < num_chars; ++i){
if (whole_text[i] == old_divider){
whole_text[i] = new_divider;
}
}
}
long int count_lines(char* whole_text, long int num_chars, char divider){
long int num_lines = 0;
for (int i = 0; i < num_chars; ++i){
if (whole_text[i] == divider)
num_lines++;
}
return num_lines;
}
char** divide_lines(char* whole_text, long int num_lines, long int num_chars){
char** lines = new char* [num_lines + 32]();
for (int i = 0; i < num_lines; ++i){
lines[i] = whole_text;
whole_text += 32;
}
return lines;
}