#include "cryptomap.h" #include "wordsource.h" #include "globals.h" const int CryptoMap::ALPH_SIZE = 128; const char CryptoMap::BLANK = '*'; tvector CryptoMap::ourMap(ALPH_SIZE, BLANK); tvector CryptoMap::ourUsed(ALPH_SIZE,false); CryptoMap::CryptoMap() // post: empty map { } string CryptoMap::decode(const string& code) // post: return string that's a decoded version of code { string copy(code); unsigned k; for(k=0; k < copy.length(); k++) { copy[k] = ourMap[copy[k]]; } return copy; } bool CryptoMap::mappable(const string& code, const string& clear) // pre: code.length() == clear.length() // post: return true if code maps to clear consistently with // current global mapping, otherwise return false, if true returned // then the mapping code->clear is recorded in the global mapping { unsigned int k; myCodeChars.clear(); if (code.length() != clear.length()) return false; for(k=0; k < code.length(); k++) { if (! ourUsed[clear[k]] && ourMap[code[k]] == BLANK) { ourMap[code[k]] = clear[k]; ourUsed[clear[k]] = true; myCodeChars.push_back(code[k]); myClearChars.push_back(clear[k]); } else if (ourMap[code[k]] != clear[k]) { unmap(); return false; } } return true; } void CryptoMap::unmap() // post: last mapping is undone { int k; for(k=0; k < myCodeChars.size(); k++) { ourMap[myCodeChars[k]] = BLANK; ourUsed[myClearChars[k]] = false; } myCodeChars.clear(); myClearChars.clear(); }