Slow version of pattern hash function
This commit is contained in:
parent
8269a15a56
commit
a8c5c425be
34
a.c
34
a.c
@ -17,6 +17,8 @@ string ToUpper(string s) {
|
||||
return s2;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
//-Word
|
||||
struct Word {
|
||||
Word() {} // default empty constructor
|
||||
Word(string s) : word(s) {} // standard way of initialization
|
||||
@ -25,9 +27,27 @@ struct Word {
|
||||
typedef vector<Word> Words;
|
||||
typedef unordered_map<string, Word> WordMap; // hash-table from stl
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
//-Library
|
||||
class Library {
|
||||
public:
|
||||
Library() {} // hash-tables are automatically initialized
|
||||
void FindWord(const string& s) const { // references are prefered instead of copies
|
||||
int len = s.length(); // cache the length to avoid multiple calls
|
||||
for (const Word& w : words_) { // references are prefered instead of copies
|
||||
string temp = w.word;
|
||||
if (len == temp.length()) {
|
||||
for (int i=0; i<len; i++) {
|
||||
if (s[i] == '-') {
|
||||
temp[i] = '-';
|
||||
}
|
||||
}
|
||||
if (s == temp) {
|
||||
cout << w.word << "\n"; // print the original word
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bool IsWord(string s) const {
|
||||
auto it = word_map_.find(s); // use iterator
|
||||
if (it == word_map_.end()) {
|
||||
@ -60,7 +80,7 @@ public:
|
||||
void ReadFromFile(string filename) {
|
||||
ifstream f;
|
||||
f.open(filename);
|
||||
while (!f.eof()) {
|
||||
while (f.is_open() && !f.eof()) { // check for the file!
|
||||
string line;
|
||||
getline(f, line);
|
||||
// cout << line << "\n";
|
||||
@ -104,7 +124,7 @@ struct Grid {
|
||||
void LoadFromFile(string filename) {
|
||||
ifstream f;
|
||||
f.open("test");
|
||||
while (!f.eof()) {
|
||||
while (f.is_open() && !f.eof()) { // check for the file
|
||||
string line;
|
||||
getline(f, line);
|
||||
// cout << line << "\n";
|
||||
@ -133,14 +153,8 @@ struct Grid {
|
||||
int main() {
|
||||
Library lib;
|
||||
lib.ReadFromFile("top_12000.txt");
|
||||
//lib.ComputeStats();
|
||||
//lib.PrintStats();
|
||||
|
||||
cout << lib.IsWord("DOG") << "\n";
|
||||
cout << lib.IsWord("CAT") << "\n";
|
||||
cout << lib.IsWord("THANKS") << "\n";
|
||||
cout << lib.IsWord("TFAFAS") << "\n";
|
||||
lib.DebugBuckets();
|
||||
|
||||
lib.FindWord("D--");
|
||||
|
||||
//Grid grid("MY GRID");
|
||||
//grid.LoadFromFile("test");
|
||||
|
Loading…
x
Reference in New Issue
Block a user