summaryrefslogtreecommitdiff
path: root/quackleio
diff options
context:
space:
mode:
Diffstat (limited to 'quackleio')
-rw-r--r--quackleio/dawgfactory.cpp60
-rw-r--r--quackleio/dawgfactory.h17
2 files changed, 27 insertions, 50 deletions
diff --git a/quackleio/dawgfactory.cpp b/quackleio/dawgfactory.cpp
index 869ef8e..4dd4ec3 100644
--- a/quackleio/dawgfactory.cpp
+++ b/quackleio/dawgfactory.cpp
@@ -28,6 +28,8 @@
DawgFactory::DawgFactory(const QString &alphabetFile)
+ : m_encodableWords(0), m_unencodableWords(0), m_duplicateWords(0),
+ m_countsByLength(Quackle::FixedLengthString::maxSize, 0)
{
QuackleIO::FlexibleAlphabetParameters *flexure = new QuackleIO::FlexibleAlphabetParameters;
flexure->load(alphabetFile);
@@ -40,7 +42,6 @@ DawgFactory::DawgFactory(const QString &alphabetFile)
m_root.lastchild = true;
m_hash.int32ptr[0] = m_hash.int32ptr[1] = m_hash.int32ptr[2] = m_hash.int32ptr[3] = 0;
- m_encodableWords = m_unencodableWords = m_duplicateWords = m_wordCount = 0;
}
DawgFactory::~DawgFactory()
@@ -48,7 +49,7 @@ DawgFactory::~DawgFactory()
delete m_alphas;
}
-bool DawgFactory::pushWord(const UVString& word, bool inSmaller, int playability)
+bool DawgFactory::pushWord(const UVString &word, bool inSmaller, int playability)
{
UVString leftover;
Quackle::LetterString encodedWord = m_alphas->encode(word, &leftover);
@@ -59,11 +60,12 @@ bool DawgFactory::pushWord(const UVString& word, bool inSmaller, int playability
return false;
}
-bool DawgFactory::pushWord(const Quackle::LetterString& word, bool inSmaller, int playability)
+bool DawgFactory::pushWord(const Quackle::LetterString &word, bool inSmaller, int playability)
{
if (m_root.pushWord(word, inSmaller, playability))
{
++m_encodableWords;
+ ++m_countsByLength[word.length()];
hashWord(word);
return true;
}
@@ -133,7 +135,7 @@ void DawgFactory::generate()
m_root.print(m_nodelist);
}
-void DawgFactory::writeIndex(const UVString& filename)
+void DawgFactory::writeIndex(const string &filename)
{
ofstream out(filename.c_str(), ios::out | ios::binary);
unsigned char bytes[7];
@@ -187,37 +189,26 @@ void DawgFactory::writeIndex(const UVString& filename)
}
}
-void DawgFactory::computeWordCount() const
-{
- m_countsByLength.resize(0);
- m_wordCount = m_root.wordCount(0, m_countsByLength);
-}
-
string DawgFactory::letterCountString() const
{
ostringstream str;
- if (m_countsByLength.size() < 16)
- m_countsByLength.resize(16, 0);
- str << "2s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[2];
- str << "\t6s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[6];
- str << "\t10s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[10];
- str << "\t14s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[14];
- str << "\n3s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[3];
- str << "\t7s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[7];
- str << "\t11s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[11];
- str << "\t15s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[15];
- str << "\n4s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[4];
- str << "\t8s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[8];
- str << "\t12s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[12];
- str << "\n5s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[5];
- str << "\t9s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[9];
- str << "\t13s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[13];
- str << "\n";
+ for (int i = 0; i < 4; i++)
+ {
+ for (int j = 0; j < 4; j++)
+ {
+ const int letterCount = j * 4 + i + 2;
+ if (j != 0)
+ str << "\t";
+ if (m_countsByLength[letterCount] > 0)
+ str << letterCount << "s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[letterCount];
+ }
+ str << "\n";
+ }
return str.str();
}
-void DawgFactory::Node::print(vector< Node* >& nodelist)
+void DawgFactory::Node::print(vector< Node* > &nodelist)
{
written = true;
@@ -255,7 +246,7 @@ void DawgFactory::Node::print(vector< Node* >& nodelist)
// returns true if the word was actually added...false if it's a duplicate.
-bool DawgFactory::Node::pushWord(const Quackle::LetterString& word, bool inSmaller, int pb)
+bool DawgFactory::Node::pushWord(const Quackle::LetterString &word, bool inSmaller, int pb)
{
bool added;
if (word.length() == 0) {
@@ -318,17 +309,6 @@ bool DawgFactory::Node::equals(const Node &n) const
return true;
}
-int DawgFactory::Node::wordCount(unsigned int depth, vector<unsigned int> &countsByLength) const
-{
- int wordCount = ((playability == 0) ? 0 : 1);
- if (countsByLength.size() < depth + 1)
- countsByLength.resize(depth + 1, 0);
- countsByLength[depth] += wordCount;
- for (size_t i = 0; i < children.size(); i++)
- wordCount += children[i].wordCount(depth + 1, countsByLength);
- return wordCount;
-}
-
int DawgFactory::Node::letterSum() const
{
if (sumexplored)
diff --git a/quackleio/dawgfactory.h b/quackleio/dawgfactory.h
index 5872dc3..efcc455 100644
--- a/quackleio/dawgfactory.h
+++ b/quackleio/dawgfactory.h
@@ -30,19 +30,18 @@ public:
DawgFactory(const QString &alphabetFile);
~DawgFactory();
- void computeWordCount() const;
- int wordCount() const { return m_wordCount; };
+ int wordCount() const { return m_encodableWords; };
string letterCountString() const;
int nodeCount() const { return m_nodelist.size(); };
int encodableWords() const { return m_encodableWords; };
int unencodableWords() const { return m_unencodableWords; };
int duplicateWords() const { return m_duplicateWords; };
- bool pushWord(const UVString& word, bool inSmaller, int playability);
- bool pushWord(const Quackle::LetterString& word, bool inSmaller, int playability);
+ bool pushWord(const UVString &word, bool inSmaller, int playability);
+ bool pushWord(const Quackle::LetterString &word, bool inSmaller, int playability);
void hashWord(const Quackle::LetterString &word);
void generate();
- void writeIndex(const UVString& filename);
+ void writeIndex(const string &filename);
const char* hashBytes() { return m_hash.charptr; };
@@ -50,10 +49,9 @@ private:
class Node {
public:
bool pushWord(const Quackle::LetterString& word, bool inSmaller, int pb);
- void print(vector< Node* >& m_nodelist);
+ void print(vector< Node* > &m_nodelist);
int letterSum() const;
- int wordCount(unsigned int depth, vector<unsigned int> &countsByLength) const;
bool equals(const Node &n) const;
Quackle::Letter c;
@@ -67,7 +65,7 @@ private:
bool lastchild;
mutable bool sumexplored;
- mutable int sum;
+ mutable unsigned int sum;
mutable vector<int> counts;
bool deleted;
@@ -79,8 +77,7 @@ private:
int m_unencodableWords;
int m_duplicateWords;
vector< Node* > m_nodelist;
- mutable int m_wordCount;
- mutable vector<unsigned int> m_countsByLength;
+ vector<unsigned int> m_countsByLength;
Quackle::AlphabetParameters *m_alphas;
Node m_root;
union {