From 5e5d414c57d5c7dd8a3037dda1555db5fb7eb486 Mon Sep 17 00:00:00 2001 From: John Fultz Date: Sat, 22 Aug 2015 01:39:47 -0500 Subject: Add versioning of DAWGs. If we're going to start writing these into user directories, then we'd better start versioning them so we don't end up generating bugs in the future. LexiconParameters::loadDawg() implements a tiny class factory which allows backward compatibility of DAWGs. I'll soon be adding a "version 1" in addition to the legacy "version 0". For now, version 1 is just dummied in. --- lexiconparameters.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 6 deletions(-) (limited to 'lexiconparameters.cpp') diff --git a/lexiconparameters.cpp b/lexiconparameters.cpp index b32f632..9826ca3 100644 --- a/lexiconparameters.cpp +++ b/lexiconparameters.cpp @@ -25,6 +25,64 @@ using namespace Quackle; +class V0DawgInterpreter : public DawgInterpreter +{ + + virtual void loadDawg(ifstream &file, unsigned char *dawg) + { + int i = 0; + while (!file.eof()) + { + file.read((char*)(dawg) + i, 7); + i += 7; + } + } + + virtual void dawgAt(const unsigned char *dawg, int index, unsigned int &p, Letter &letter, bool &t, bool &lastchild, bool &british, int &playability) const + { + index *= 7; + p = (dawg[index] << 16) + (dawg[index + 1] << 8) + (dawg[index + 2]); + letter = dawg[index + 3]; + + t = (letter & 32) != 0; + lastchild = (letter & 64) != 0; + british = !(letter & 128); + letter = (letter & 31) + QUACKLE_FIRST_LETTER; + + playability = (dawg[index + 4] << 16) + (dawg[index + 5] << 8) + (dawg[index + 6]); + } + virtual int versionNumber() const { return 0; } +}; + +class V1DawgInterpreter : public DawgInterpreter +{ + + virtual void loadDawg(ifstream &file, unsigned char *dawg) + { + int i = 0; + while (!file.eof()) + { + file.read((char*)(dawg) + i, 7); + i += 7; + } + } + + virtual void dawgAt(const unsigned char *dawg, int index, unsigned int &p, Letter &letter, bool &t, bool &lastchild, bool &british, int &playability) const + { + index *= 7; + p = (dawg[index] << 16) + (dawg[index + 1] << 8) + (dawg[index + 2]); + letter = dawg[index + 3]; + + t = (letter & 32) != 0; + lastchild = (letter & 64) != 0; + british = !(letter & 128); + letter = (letter & 31) + QUACKLE_FIRST_LETTER; + + playability = (dawg[index + 4] << 16) + (dawg[index + 5] << 8) + (dawg[index + 6]); + } + virtual int versionNumber() const { return 1; } +}; + LexiconParameters::LexiconParameters() : m_dawg(0), m_gaddag(0) { @@ -45,6 +103,7 @@ void LexiconParameters::unloadDawg() { delete[] m_dawg; m_dawg = 0; + delete m_interpreter; } void LexiconParameters::unloadGaddag() @@ -64,14 +123,24 @@ void LexiconParameters::loadDawg(const string &filename) return; } - m_dawg = new unsigned char[7000000]; - - int i = 0; - while (!file.eof()) + char versionByte = file.get(); + file.unget(); + switch(versionByte) { - file.read((char*)(m_dawg) + i, 7); - i += 7; + case 0: + m_interpreter = new V0DawgInterpreter(); + break; + case 1: + m_interpreter = new V1DawgInterpreter(); + break; + default: + UVcout << "couldn't open dawg " << filename.c_str() << endl; + return; } + + m_dawg = new unsigned char[7000000]; + + m_interpreter->loadDawg(file, m_dawg); } void LexiconParameters::loadGaddag(const string &filename) -- cgit v1.2.3