From 566c9a29cff9ed40ee745c3b02176280ecbe164d Mon Sep 17 00:00:00 2001 From: ekuryatenko Date: Fri, 11 Sep 2015 11:15:18 +0300 Subject: [PATCH 1/3] First commit --- RandomWriter/src/RandomWriter.cpp | 289 +++++++++++++++++++++++++++++- WordLadder/src/WordLadder.cpp | 251 +++++++++++++++++++++++++- 2 files changed, 536 insertions(+), 4 deletions(-) diff --git a/RandomWriter/src/RandomWriter.cpp b/RandomWriter/src/RandomWriter.cpp index e591dac..22acf61 100644 --- a/RandomWriter/src/RandomWriter.cpp +++ b/RandomWriter/src/RandomWriter.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "console.h" @@ -6,9 +6,292 @@ #include "random.h" #include "strlib.h" #include "vector.h" +#include "simpio.h" +#include "filelib.h" +#include "stack.h" +#include "queue.h" +#include "timer.h" + using namespace std; +/* FILE READING */ +string fileInput(string promptText); +int levelInput(string promptText); + +Map> getFileMap(istream & infile, int modelLevel); +string getLevelSeed(istream & infile, + char seedChar, + int modelLevel, + Stack& stack); +string getSymbolAfterSeed(istream & infile); +void returnCharsToStream(istream & infile,Stack& stack); + +void modifyMap(Map>& mainMap, + string seed, + string nextSymbol); +/* Level 0-model Text GENERATION */ +void generate_0_levelText(Map>& fileMap, int printSymbQty); +Map> getStatisticMap(Map>& fileMap); + +/* Level N-model Text GENERATION */ +void generate_N_levelText(Map>& fileMap, int printSymbQty); +string getFrequentSeed(Map>& fileMap); +string getRandomSymbol(string key, Map>& map); + +// Console settings +void consoleSettings(); +const string programTitle = "RandomWriter"; +const bool consoleEcho = false; + int main() { - // TODO: fill in the code - return 0; + consoleSettings(); + + /* User enters name of file from project directory */ + string fileName = fileInput("Enter file name: "); + //string fileName = "MiddleMarch.txt"; + //string fileName = "Readme.txt"; + + int modelLevel = levelInput("Enter model level range (1-10): "); + /* Generated text symbols qty */ + int resultTextSymbolsQty = 2000; + + cout << "Processing..." << endl; + Timer timer1(true); + cout << "=========================================" << endl; + /* Prepare input stream object */ + ifstream infile; + /* Lunch stream of file reading */ + infile.open(fileName.c_str()); + /* Main read text process - seeds map creating */ + Map> fileMap = getFileMap(infile, modelLevel); + /* If whole file is read program closes stream */ + infile.close(); + + cout << "MAP BUILD TIME IS " << timer1.elapsed() << endl; + + timer1.stop(); + Timer timer2(true); + + cout << "=========================================" << endl; + + if(modelLevel > 0){ + generate_N_levelText(fileMap, resultTextSymbolsQty); + }else{ + generate_0_levelText(fileMap, resultTextSymbolsQty); + } + + cout << endl; + cout << "=========== END OF GENERATION ==========="<< endl; + cout << "TEXT BUILD TIME IS " << timer2.elapsed() << endl; + timer2.stop(); + + return 0; +} + +/* Main read text process - seeds map creating */ +Map> getFileMap(istream & infile, int modelLevel){ + Map> resultMap; + char startSeedChar; + while ((startSeedChar = infile.get()) != EOF) { + /* Stack for chars from seed */ + Stack stack; + /* Take chars from stream to build seed for required level */ + string seed = getLevelSeed(infile, startSeedChar, modelLevel, stack); + /* Get next symbol after such seed */ + string nextSymbol = getSymbolAfterSeed(infile); + /* Return chars from stack to stream */ + returnCharsToStream(infile, stack); + /* Fills resultMap by obtained seed as key and nextSymbol as result */ + modifyMap(resultMap, seed, nextSymbol); + } + return resultMap; +} + +/* This function calls after seed getting from stream + * Returns next symbol, after seed chars, from stream */ +string getSymbolAfterSeed(istream & infile){ + string result = ""; + char nextChar = infile.peek(); + if(nextChar != EOF){ + result = charToString(nextChar); + } + return result; +} + +/* Get seedChar - next char from stream, and builds seed of + * required length */ +string getLevelSeed(istream & infile, + char seedChar, + int modelLevel, + Stack& stack){ + /* seed begins from seedChar */ + string result = charToString(seedChar); + if(modelLevel > 0){ + for(int i = 1; i < modelLevel; i++){ + char nextChar; + if ((nextChar = infile.get()) != EOF){ + /* Adds next text symbols to seed */ + result += charToString(nextChar); + /* Stores taken seed chars to stack + * It'll enables to return them back to stream letter */ + stack.push(nextChar); + } + } + } + return result; +} + +/* Modifies main file map - adds seeds and modifies nexy chars vectors */ +void modifyMap(Map>& mainMap, + string seed, + string nextSymbol){ + if(mainMap.containsKey(seed)){ + /* Modifies map entry - adds next symbol to vector */ + mainMap[seed].add(nextSymbol); + }else{ + /* Adds new map entry with such next symbol value */ + Vector newSeedVector; + newSeedVector.add(nextSymbol); + mainMap.put(seed, newSeedVector); + } +} + +/* This function calls after seed creation by getLevelSeed + * Return chars ffrom stack to stream */ +void returnCharsToStream(istream & infile,Stack& stack){ + while(!stack.isEmpty()){ + char symbol = stack.pop(); + infile.putback(symbol); + } +} + +/* Unfortunately I haven't managed to make it more rapid - dedaline time is coming + * + * 0-level generating depends of + * - every new symbol of text appear after seed due to it's statistic probability */ +void generate_0_levelText(Map>& fileMap, int printSymbQty){ + string startSeed = getFrequentSeed(fileMap); + cout << "THE MOST FREQUENT SEED IS: \"" << startSeed << "\"" << endl; + cout << "============= GENERATED TEXT ============" << endl; + cout <> statisticMap = getStatisticMap(fileMap); + + while(counter < printSymbQty){ + string nextSymbol = getRandomSymbol("someDefalutKey", statisticMap); + if(nextSymbol == "no symbols"){ + cout << endl; + cout << "PROGRAM STOPS DUE TO BREAK CONDITION"; + break; + } + cout << nextSymbol; + counter++; + } + cout << endl; +} + +/* Prepares map with single key and single next symbols vector + * Vector stores all possible next symbols from text + * Random taking from this vector returns symbols due to their probabylity */ +Map> getStatisticMap(Map>& fileMap){ + /* This key is not important for our tasks */ + string key = "someDefalutKey"; + /* Vector of all next symbols */ + Vector totalVec; + for(string key: fileMap){ + Vector keyVec = fileMap[key]; + for(string i: keyVec){ + totalVec.add(i); + } + } + Map> result; + result.put(key, totalVec); + return result; +} + +/* N-level generating depends of + * - every new symbol of text appear after such seed more frequently */ +void generate_N_levelText(Map>& fileMap, int printSymbQty){ + string startSeed = getFrequentSeed(fileMap); + cout << "THE MOST FREQUENT SEED IS: \"" << startSeed << "\"" << endl; + cout << "============= GENERATED TEXT ============" << endl; + cout <>& fileMap){ + int maxSize = 0; + string result = ""; + for(string key: fileMap){ + int keySize = fileMap[key].size(); + if(keySize > maxSize){ + result = key; + maxSize = keySize; + } + } + return result; +} + +/* Next symbol is get from seed statistic vector due to its probability in vector */ +string getRandomSymbol(string key, Map>& map){ + if(map.containsKey(key)){ + int thisKeyVecSize = map[key].size(); + int randomInt = randomInteger(0, (thisKeyVecSize - 1)); + Vector vec = map[key]; + return vec[randomInt]; + }else{ + return "no symbols"; + } +} + + +/* User input function + * Defines if this file exist in project directory */ +string fileInput(string promptText){ + while(true){ + string result = getLine(promptText); + if(fileExists(result)){ + return result; + break; + }else{ + cout << "Unfortunately your input is failed" << endl; + } + } +} + +/* User input function + * Defines if this model level value is correct */ +int levelInput(string promptText){ + while(true){ + int result = getInteger(promptText); + if((result >= 0) && ((result <= 10))){ + return result; + break; + }else{ + cout << "Unfortunately your input is failed" << endl; + } + } +} + +void consoleSettings(){ + setConsoleWindowTitle(programTitle); + setConsoleEcho(consoleEcho); } diff --git a/WordLadder/src/WordLadder.cpp b/WordLadder/src/WordLadder.cpp index 01d35d7..fcb526a 100644 --- a/WordLadder/src/WordLadder.cpp +++ b/WordLadder/src/WordLadder.cpp @@ -4,9 +4,258 @@ #include "queue.h" #include "simpio.h" #include "vector.h" +#include "strlib.h" + using namespace std; +/*Functions prototypes */ +void wordLadderGame(); +void userInput(string& startWord, string& endWord, Lexicon englishWords); +string validInput(string prompt, Lexicon englishWords); +void addLadderToQueue(Vector vec, + Queue> & mainQueue, + Lexicon & wordsInQueue); +Vector getNewValidWords(string inputWord, Lexicon & englishWords); +string nextFromAlphabet(string inputWord, int index); +Vector mainQueueProcess(Queue> & mainQueue, + Lexicon & wordsInQueue, + Lexicon & englishWords, + string endWord); +void programOutput(Vector& resultLadder); + +/* Program console settings */ +void consoleSettings(); +const string programTitle = "WordLadder"; +const bool consoleEcho = false; + int main() { - // TODO: fill in the code + consoleSettings(); + /* "WordLadder game" function + * The main task is to modify words from user due to game rules + * At the end of process result cosequence of word have be printed + */ + wordLadderGame(); + return 0; } + +/* "WordLadder game" function + * The main task is to get pair of words from user - start word and end word + * Function search variant to modify start word into end word + * The main rules of game modifications is: + * - each new mofification of start word can change only one letter in it + * - each word is valid english word + * Words from user must have same length + * At the end of process result cosequence of word have be printed + */ +void wordLadderGame(){ + Lexicon englishWords("EnglishWords.txt"); + + /* User input */ + string startWord = ""; + string endWord = ""; + userInput(startWord, endWord, englishWords); + + /* Main program store - stores all posible dicisions of the main task */ + Queue> mainQueue; + /* Creates lexicon of all words which queue contain */ + Lexicon wordsInQueue; + /* Creates start possible variant of the task */ + Vector newLadder(1, startWord); + /* Fills main program queue by start possible variant of the task */ + addLadderToQueue(newLadder, mainQueue, wordsInQueue); + /* Main program process */ + Vector resultLadder = mainQueueProcess(mainQueue, + wordsInQueue, + englishWords, + endWord); + programOutput(resultLadder); +} + +/* Main program process + * - get next ladder + * - check there last word for end case + * - get mofified variants of this word + * - adds them into vectors + * - adds these vectors to the queue + * @param mainQueue - reference of main queue of program - store of possible + * program decision variants + * @param wordsInQueue - reference - store of all posible words from queue + * @param wordsInQueue - reference - store of possible valid english words + * @param endWord - copy of the result user word + * + * @return - vector of first program decision, ladder of words from start word to end word + */ +Vector mainQueueProcess(Queue> & mainQueue, + Lexicon & wordsInQueue, + Lexicon & englishWords, + string endWord){ + + Vector resultLadder; + while (!mainQueue.isEmpty()){ + /* Gets next queue element */ + Vector queueLastLadder = mainQueue.dequeue(); + /* Gets word from this ladder, which have being put last there */ + string lastLadderWord = queueLastLadder[(queueLastLadder.size() - 1)]; + /* Checks an end case of programm */ + if (lastLadderWord == endWord){ + /* put this ladder into the programm output */ + resultLadder = queueLastLadder; + break; + }else{ + /* Gets vector of all new possible variants of this word */ + Vector newWordsVec = getNewValidWords(lastLadderWord, englishWords); + /* Adds all new values to new ladders, and adds them to the queue */ + for(string modifiedWord: newWordsVec){ + /* Each new ladder is made as a copy of the taken ladder */ + Vector nextNewLadder = queueLastLadder; + /* Put new mofified word into new copy */ + nextNewLadder.add(modifiedWord); + /* Adds such new ladder into the queue */ + addLadderToQueue(nextNewLadder, mainQueue, wordsInQueue); + } + } + } + return resultLadder; +} + +/* Adds each new ladder to main queue + * Creates lexicon of the queue's words + * + * Modifies mainQueue and queueWordList by reference + * + * @param vec - ladder to put into queue + * @param mainQueue - main queue of the programm + * @param queueWordsList - queue lexicon, saves all kinds of words from queue */ +void addLadderToQueue(Vector vec, + Queue> & mainQueue, + Lexicon & queueLexicon){ + /* For this version suppose that such vector has only one new kind of word - last word */ + string lastWord = vec[(vec.size() - 1)]; + /* If queue lexicon doesn't contain such word... */ + if(!queueLexicon.contains(lastWord)){ + /* ...then this word is put to queue lexicon */ + queueLexicon.add(lastWord); + /* And add this vec to queue */ + mainQueue.enqueue(vec); + } +} + +/* Creates list of all possible input word modifications + * Get word and modifies one letter from it + * If modified word is valid - save it into result vector + * inputWord isn't saved into result + * + * @param inputWord - simple word + * @param englishLexicon - vocabulary of valid english words + * + * @return - vector of all possible modifications */ +Vector getNewValidWords(string inputWord, Lexicon & englishLexicon){ + Vector resultVector; + /* Counter of modification proccess */ + int modificationIndex = 0; + string modificatedWord = "defalut"; + /* loop of new words generating + * It stops if last modification was equal empty string */ + while (modificatedWord != ""){ + /* Gets new modification of input word */ + modificatedWord = nextFromAlphabet(inputWord, modificationIndex); + /* Checks new modification + * If it's starts from letter - for case of damaged words + * like "-ba" during input */ + if(isalpha(modificatedWord[0])){ + /* If new modification dosen't equal input word */ + if(modificatedWord != inputWord){ + /* If new modification is valid english word */ + if(englishLexicon.contains(modificatedWord)){ + resultVector.add(modificatedWord); + } + } + }else{ + break; + } + /* Shifts counter to direct modification process */ + modificationIndex++; + } + return resultVector; +} + +/* Modifies each letter in input word due to alphabet + * + * @param inputWord - simple word + * @param index - gives position of modification. If value of index bigger + * then possible variants of modifications - function returns "" + * + * @return - modification of input word or "" if there are no modifications for + * such index */ +string nextFromAlphabet(string inputWord, int index){ + string alphabet = "abcdefghijklmnopqrstuvwqxyz"; + /* Initiate result string */ + string outputWord = inputWord; + /* Index of the next letter posirion to be changed */ + int letterIndex = index / alphabet.length(); + /* Index of the next letter from alphabet to be put into modification position */ + int alphabetIndex = index % alphabet.length(); + /* While process index alows to generate modifications */ + if(index < ((alphabet.length()) * (inputWord.length()))){ + /* Create new modification - change letter at this position */ + outputWord[letterIndex] = alphabet[alphabetIndex]; + return outputWord; + }else{ + /* If process index value is more then possible return end case value */ + return ""; + } +} + +void userInput(string& startWord, string& endWord, Lexicon englishWords){ + //startWord = "sleep"; + //endWord = "awake"; + + //startWord = "airplane"; + //endWord = "tricycle"; + + //startWord = "work"; + //endWord = "play"; + + //startWord = "code"; + //endWord = "data"; + + //startWord = "never"; + //endWord = "event"; + + startWord = validInput("Enter start word: ", englishWords); + endWord = validInput("Enter end word: ", englishWords); +} + +string validInput(string prompt, Lexicon englishWords){ + while(true){ + string result = getLine(prompt); + if(englishWords.contains(result)){ + return result; + break; + }else{ + cout << "Unfortunately your input is failed" << endl; + } + } +} + +/* Programm output - prints results of process */ +void programOutput(Vector& resultLadder){ + /* End case - if there no variants of modification */ + if(resultLadder.isEmpty()){ + cout << "No ladder found"; + }else{ + /* Loop of the result printing */ + for(int i = 0; i < (resultLadder.size() - 1); i++){ + cout << resultLadder[i]; + cout << " -> "; + } + cout << resultLadder[(resultLadder.size() - 1)]; + } +} + +/* Make console output window more convenient */ +void consoleSettings(){ + setConsoleWindowTitle(programTitle); + setConsoleEcho(consoleEcho); +} From 5afc83f7b57eb9cc5c90d19bf2cdd4751193aa06 Mon Sep 17 00:00:00 2001 From: ekuryatenko Date: Sat, 12 Sep 2015 10:16:37 +0300 Subject: [PATCH 2/3] Improved productivity RandomWriter --- RandomWriter/src/RandomWriter.cpp | 89 +++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/RandomWriter/src/RandomWriter.cpp b/RandomWriter/src/RandomWriter.cpp index 22acf61..1998da4 100644 --- a/RandomWriter/src/RandomWriter.cpp +++ b/RandomWriter/src/RandomWriter.cpp @@ -11,9 +11,10 @@ #include "stack.h" #include "queue.h" #include "timer.h" +#include "hashmap.h" using namespace std; - +/* ==== Version 2 (improved productivity of program) ==== */ /* FILE READING */ string fileInput(string promptText); int levelInput(string promptText); @@ -24,7 +25,7 @@ string getLevelSeed(istream & infile, int modelLevel, Stack& stack); string getSymbolAfterSeed(istream & infile); -void returnCharsToStream(istream & infile,Stack& stack); +void returnCharsToStream(istream & infile, Stack& stack); void modifyMap(Map>& mainMap, string seed, @@ -32,6 +33,8 @@ void modifyMap(Map>& mainMap, /* Level 0-model Text GENERATION */ void generate_0_levelText(Map>& fileMap, int printSymbQty); Map> getStatisticMap(Map>& fileMap); +void generate_0_levelText_v2(Map>& fileMap, int printSymbQty); +HashMap getStatisticMap_v2(Map>& fileMap); /* Level N-model Text GENERATION */ void generate_N_levelText(Map>& fileMap, int printSymbQty); @@ -57,6 +60,7 @@ int main() { cout << "Processing..." << endl; Timer timer1(true); + cout << "=========================================" << endl; /* Prepare input stream object */ ifstream infile; @@ -67,23 +71,22 @@ int main() { /* If whole file is read program closes stream */ infile.close(); - cout << "MAP BUILD TIME IS " << timer1.elapsed() << endl; - timer1.stop(); - Timer timer2(true); + cout << "MAP BUILD TIME IS " << timer1.elapsed() << " ms" << endl;; + Timer timer2(true); cout << "=========================================" << endl; if(modelLevel > 0){ generate_N_levelText(fileMap, resultTextSymbolsQty); }else{ - generate_0_levelText(fileMap, resultTextSymbolsQty); + generate_0_levelText_v2(fileMap, resultTextSymbolsQty); } cout << endl; cout << "=========== END OF GENERATION ==========="<< endl; - cout << "TEXT BUILD TIME IS " << timer2.elapsed() << endl; timer2.stop(); + cout << "TEXT BUILD TIME IS " << timer2.elapsed() << " ms" << endl; return 0; } @@ -158,7 +161,7 @@ void modifyMap(Map>& mainMap, /* This function calls after seed creation by getLevelSeed * Return chars ffrom stack to stream */ -void returnCharsToStream(istream & infile,Stack& stack){ +void returnCharsToStream(istream & infile, Stack& stack){ while(!stack.isEmpty()){ char symbol = stack.pop(); infile.putback(symbol); @@ -218,7 +221,8 @@ Map> getStatisticMap(Map>& fileMap void generate_N_levelText(Map>& fileMap, int printSymbQty){ string startSeed = getFrequentSeed(fileMap); cout << "THE MOST FREQUENT SEED IS: \"" << startSeed << "\"" << endl; - cout << "============= GENERATED TEXT ============" << endl; + cout << "============= GENERATED TEXT ============" << endl; + /* Text generation begins */ cout <>& fileMap){ return result; } +/* Mdified for version 2 - improved productivity */ /* Next symbol is get from seed statistic vector due to its probability in vector */ string getRandomSymbol(string key, Map>& map){ if(map.containsKey(key)){ int thisKeyVecSize = map[key].size(); int randomInt = randomInteger(0, (thisKeyVecSize - 1)); - Vector vec = map[key]; - return vec[randomInt]; + //Vector vec = map[key]; + string result = map[key].get(randomInt); + return result; }else{ return "no symbols"; } @@ -295,3 +301,64 @@ void consoleSettings(){ setConsoleWindowTitle(programTitle); setConsoleEcho(consoleEcho); } + +/* Attempt to improve function productivity + * For 0-level text generating we have rule: + * - every new symbol of text appear after seed due to this symbol + * statistic probability */ +void generate_0_levelText_v2(Map>& fileMap, int printSymbQty){ + string startSeed = getFrequentSeed(fileMap); + cout << "THE MOST FREQUENT SEED IS: \"" << startSeed << "\"" << endl; + cout << "============= GENERATED TEXT ============" << endl; + + /* First text symbol generation */ + cout < statisticMap = getStatisticMap_v2(fileMap); + /* Main text generation process */ + while(counter < printSymbQty){ + for (string key: statisticMap){ + if(counter < printSymbQty){ + /* Decide if this symbol will be printed to text */ + float probability = statisticMap[key]; + string nextPrintSymbol = key; + bool isPrint = randomChance(probability); + if(isPrint){ + cout << nextPrintSymbol; + counter++; + } + }else{ + break; + } + } + } +} + +/* Returns map of all kind of symbols in text with their probabilites as values */ +HashMap getStatisticMap_v2(Map>& fileMap){ + HashMap result; + Map statisticTable; + int symbolsInTextQty = 0; + /* Count total quantity of symbols in text, and + * quantity of their apearence - as a map - statisticTable */ + for(string key: fileMap){ + int keyVectorSize = fileMap[key].size(); + symbolsInTextQty += keyVectorSize; + /* We supose that apearence of every symbol equal it's vector size */ + statisticTable.put(key, keyVectorSize); + } + /* Now we get total quantity of symbols in text + * Create map of symbols with their probabilites as values */ + for(string key: statisticTable){ + int thisSymbolQty = statisticTable[key]; + float thisSymbolProbability = (thisSymbolQty /((float) symbolsInTextQty)); + result.put(key, thisSymbolProbability); + } + return result; +} From 794b909499da25729a1f2d8f71e7204544a088db Mon Sep 17 00:00:00 2001 From: ekuryatenko Date: Sun, 13 Sep 2015 23:35:15 +0300 Subject: [PATCH 3/3] RandomWriter - improved productivity (file reading) --- RandomWriter/src/RandomWriter.cpp | 71 +++++++++++++++---------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/RandomWriter/src/RandomWriter.cpp b/RandomWriter/src/RandomWriter.cpp index 1998da4..eac55ff 100644 --- a/RandomWriter/src/RandomWriter.cpp +++ b/RandomWriter/src/RandomWriter.cpp @@ -14,16 +14,14 @@ #include "hashmap.h" using namespace std; -/* ==== Version 2 (improved productivity of program) ==== */ +/* == Version 3 (improved productivity of program - reading == */ /* FILE READING */ string fileInput(string promptText); int levelInput(string promptText); Map> getFileMap(istream & infile, int modelLevel); string getLevelSeed(istream & infile, - char seedChar, - int modelLevel, - Stack& stack); + int modelLevel); string getSymbolAfterSeed(istream & infile); void returnCharsToStream(istream & infile, Stack& stack); @@ -53,14 +51,16 @@ int main() { string fileName = fileInput("Enter file name: "); //string fileName = "MiddleMarch.txt"; //string fileName = "Readme.txt"; + //string fileName = "Hamlet.txt"; + //string fileName = "TomSawyer.txt"; - int modelLevel = levelInput("Enter model level range (1-10): "); + int modelLevel = levelInput("Enter model level range (0-10): "); /* Generated text symbols qty */ int resultTextSymbolsQty = 2000; - + /* Start of main process and timing */ cout << "Processing..." << endl; Timer timer1(true); - + /* Program reads some text file, processes it, and generate new text due to data */ cout << "=========================================" << endl; /* Prepare input stream object */ ifstream infile; @@ -70,19 +70,19 @@ int main() { Map> fileMap = getFileMap(infile, modelLevel); /* If whole file is read program closes stream */ infile.close(); - + /* Map building time stops */ timer1.stop(); cout << "MAP BUILD TIME IS " << timer1.elapsed() << " ms" << endl;; - + /* New text generating time starting */ Timer timer2(true); cout << "=========================================" << endl; - + /* Acidentally I make 0-level process too */ if(modelLevel > 0){ generate_N_levelText(fileMap, resultTextSymbolsQty); }else{ generate_0_levelText_v2(fileMap, resultTextSymbolsQty); } - + /* End of generation - timing stops */ cout << endl; cout << "=========== END OF GENERATION ==========="<< endl; timer2.stop(); @@ -94,18 +94,18 @@ int main() { /* Main read text process - seeds map creating */ Map> getFileMap(istream & infile, int modelLevel){ Map> resultMap; - char startSeedChar; - while ((startSeedChar = infile.get()) != EOF) { - /* Stack for chars from seed */ - Stack stack; - /* Take chars from stream to build seed for required level */ - string seed = getLevelSeed(infile, startSeedChar, modelLevel, stack); + /* Create start seed due to maodel level */ + string seed = getLevelSeed(infile, modelLevel); + + char nextChar; + while ((nextChar = infile.get()) != EOF) { /* Get next symbol after such seed */ - string nextSymbol = getSymbolAfterSeed(infile); - /* Return chars from stack to stream */ - returnCharsToStream(infile, stack); + string nextSymbol = charToString(nextChar); /* Fills resultMap by obtained seed as key and nextSymbol as result */ modifyMap(resultMap, seed, nextSymbol); + /* Modify seed - take away first sumbol and add nextSymbol to end */ + seed.erase(0,1); + seed += nextSymbol; } return resultMap; } @@ -121,29 +121,26 @@ string getSymbolAfterSeed(istream & infile){ return result; } -/* Get seedChar - next char from stream, and builds seed of - * required length */ -string getLevelSeed(istream & infile, - char seedChar, - int modelLevel, - Stack& stack){ - /* seed begins from seedChar */ - string result = charToString(seedChar); - if(modelLevel > 0){ - for(int i = 1; i < modelLevel; i++){ - char nextChar; - if ((nextChar = infile.get()) != EOF){ - /* Adds next text symbols to seed */ - result += charToString(nextChar); - /* Stores taken seed chars to stack - * It'll enables to return them back to stream letter */ - stack.push(nextChar); +/* Builds first seed of required length */ +string getLevelSeed(istream & infile, int modelLevel){ + string result = ""; + char nextChar; + /* Take one symbol from stream anyway, for 0 model or 1 model */ + if((nextChar = infile.get()) != EOF){ + result += nextChar; + } + /* For 2 model and higher - seed chars quantity equal model level value */ + if(modelLevel > 1){ + for (int i = 1; i < modelLevel; i++){ + if((nextChar = infile.get()) != EOF){ + result += nextChar; } } } return result; } + /* Modifies main file map - adds seeds and modifies nexy chars vectors */ void modifyMap(Map>& mainMap, string seed,