-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
executable file
·104 lines (90 loc) · 2.44 KB
/
util.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
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include "DictionaryTrie.hpp"
#include "util.hpp"
using std::istream;
using std::istringstream;
using std::string;
using std::vector;
/*
* Starts the timer. Saves the current time.
*/
void Timer::begin_timer() {
start = std::chrono::high_resolution_clock::now();
}
/*
* Ends the timer. Compares end time with the start time and returns number of nanoseconds
*/
long long Timer::end_timer() {
std::chrono::time_point<std::chrono::high_resolution_clock> end;
end = std::chrono::high_resolution_clock::now();
return (long long)std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
}
/*
* Load the words in the file into the dictionary trie
*/
void Utils::load_dict(DictionaryTrie& dict, istream& words)
{
unsigned int freq;
string data = "";
string temp_word = "";
string word = "";
vector<string> word_string;
unsigned int i = 0;
while(getline(words, data)){
if(words.eof()) break;
temp_word = "";
word = "";
data = data + " .";
istringstream iss(data);
iss >> freq;
while(1){
iss >> temp_word;
if(temp_word == ".") break;
if(temp_word.length() > 0) word_string.push_back(temp_word);
}
for(i = 0; i < word_string.size(); i++){
if(i > 0) word = word + " ";
word = word + word_string[i];
}
dict.insert(word, freq);
word_string.clear();
}
}
/*
* Load num_words from words stream into the dictionary trie
*/
void Utils::load_dict(DictionaryTrie& dict, istream& words, unsigned int num_words)
{
unsigned int freq;
string data = "";
string temp_word = "";
string word = "";
vector<string> word_string;
unsigned int i = 0;
unsigned int j = 0;
for(; j < num_words; j++){
getline(words, data);
if(words.eof()) break;
temp_word = "";
word = "";
data = data + " .";
istringstream iss(data);
iss >> freq;
while(1)
{
iss >> temp_word;
if(temp_word == ".") break;
if(temp_word.length() > 0) word_string.push_back(temp_word);
}
for(i = 0; i < word_string.size(); i++)
{
if(i > 0) word = word + " ";
word = word + word_string[i];
}
dict.insert(word, freq);
word_string.clear();
}
}