Coding_for_Crosswords_in_Py.../a.c

179 lines
4.7 KiB
C

#include <iostream> // library for printing
#include <string> // support for strings
#include <vector> // support for vectors
#include <unordered_map> // support for hash-tables
#include <assert.h>
#include <fstream> // support for reading files
using namespace std;
// For compiling C++ code
// g++ a.c -o a
string ToUpper(string s) {
string s2;
for (char c : s) {
s2.push_back(toupper(c));
}
return s2;
}
// --------------------------------------------------------------------------------
//-Word
struct Word {
Word() {} // default empty constructor
Word(string s) : word(s) {} // standard way of initialization
int len() const { return word.length(); }
string word;
};
typedef vector<Word*> Words; // will store pointers to Words (will not delete by itself)
typedef unordered_map<string, Words> WordMap; // hash-table from stl
// --------------------------------------------------------------------------------
//-Library
class Library {
public:
Library() {} // hash-tables are automatically initialized
~Library() { // destructor of the pointers (instead of the unique pointer)
for (Word* w : words_) {
delete w;
}
}
void FindWord(const string& s) const { // references are prefered instead of copies
auto it = word_map_.find(s);
if (it != word_map_.end()) {
for (const Word* w : it->second) {
cout << " " << w->word;
}
cout << "\n";
}
}
bool IsWord(string s) const {
auto it = word_map_.find(s); // use iterator
if (it == word_map_.end()) {
return false; // if word is not found on hast-table
} else {
return true;
}
//return word_map_.count(s) > 0; // True if word exists
}
void ComputeStats() {
assert(counts_.empty());
counts_.resize(18);
for (const Word* w : words_) { // Word is a pointer!
int len = w->word.length(); // w is not an actual object (it's a pointer)
if (len < 18) {
counts_[len]++;
}
}
}
void PrintStats() const {
cout << "Here are the counts of each word length:\n";
for (int i = 1; i < counts_.size(); i++) {
cout << "[" << i << "] " << counts_[i] << "\n";
}
}
string GetWord(int i) const {
assert(i >= 0 && i < words_.size());
return words_[i]->word;
}
void CreatePatternHash(Word* w) {
int len = w->len();
int num_patterns = 1 << len; // create 2^len patterns
// cout << "PATTERN HASH on " << w->word << "\n";
for (int i=0; i<num_patterns; i++) {
// cout << " " << i << "\n";
string temp = w->word;
for (int j=0; j<len; j++) {
if ((i >> j) & 1) { // get every bit and check if it's 1
temp[j] = '-';
}
}
// cout << " " << temp << "\n";
word_map_[temp].push_back(w);
}
}
void ReadFromFile(string filename) {
ifstream f;
f.open(filename);
while (f.is_open() && !f.eof()) { // check for the file!
string line;
getline(f, line);
// cout << line << "\n";
if (!line.empty()) {
line = ToUpper(line);
int len = line.length();
if (line[len - 1] == '\r') {
line = line.substr(0, len - 1);
}
Word* w = new Word(line);
words_.push_back(w); // Word would be allocated on the heap
CreatePatternHash(w);
}
}
cout << "Read " << words_.size() << " words from file '"
<< filename << "'\n";
}
void DebugBuckets() const {
for (int i = 0; i < word_map_.bucket_count(); i++) {
cout << "[" << i << "] " << word_map_.bucket_size(i) << "\n";
}
}
private: // _ is used to indicate privacy
Words words_; // master vector of words
WordMap word_map_; // pattern hash
vector<int> counts_;
};
struct Grid {
Grid(string n) {
name = n;
}
int rows() const { return lines.size(); }
int cols() const {
if (lines.empty()) {
return 0;
} else {
return lines[0].size();
}
}
void LoadFromFile(string filename) {
ifstream f;
f.open("test");
while (f.is_open() && !f.eof()) { // check for the file
string line;
getline(f, line);
// cout << line << "\n";
if (!line.empty() && line[0] != '#') {
lines.push_back(line);
}
}
}
void Check() const {
for (string s : lines) {
assert(s.size() == cols());
}
}
void Print() const {
cout << "Grid: " << name
<< " (rows=" << rows()
<< ", cols=" << cols() << ")\n";
for (string s : lines) {
cout << " " << s << "\n";
}
}
string name; // strings are initialized empty
vector<string> lines;
};
int main() {
Library lib;
lib.ReadFromFile("top_12000.txt");
lib.FindWord("D--");
//Grid grid("MY GRID");
//grid.LoadFromFile("test");
//grid.Check();
//grid.Print();
}