Use unordered_map instead of map

This commit is contained in:
Joscha 2016-11-04 02:47:06 +00:00
parent dfa3eabc47
commit 1564e18c4a
4 changed files with 7 additions and 6 deletions

View file

@ -37,11 +37,11 @@ void MarkovMatrix::reset(int length)
}
std::map<std::string, int> MarkovMatrix::hiccup(std::string& previous)
std::unordered_map<std::string, int> MarkovMatrix::hiccup(std::string& previous)
{
int size = previous.size();
int pos = std::max(size-this->length, 0);
int len = size - pos;
std::string before = previous.substr(pos, len);
return this->matrix[before];
return this->matrix.at(before);
}

View file

@ -1,7 +1,7 @@
#ifndef MARKOVMATRIX_HPP
#define MARKOVMATRIX_HPP
#include <map>
#include <unordered_map>
#include <string>
@ -13,11 +13,11 @@ public:
void feed(std::string& line);
void reset(int length=0); // reset the matrix, length>0 -> set a new length
std::map<std::string, int> hiccup(std::string& previous);
std::unordered_map<std::string, int> hiccup(std::string& previous);
private:
int length;
std::map<std::string, std::map<std::string, int> > matrix;
std::unordered_map<std::string, std::unordered_map<std::string, int> > matrix;
};
#endif

View file

@ -66,7 +66,7 @@ std::string SimpleMarkov::regurgitateLine(std::string start)
std::string next = "";
do {
// get the possible next characters from the matrix
std::map<std::string, int> possibles = this->matrix.hiccup(line);
std::unordered_map<std::string, int> possibles = this->matrix.hiccup(line);
// calculate sum of all possibilities
int sum = 0;

View file

@ -2,6 +2,7 @@
#include <string>
#include "SimpleMarkov.hpp"
int main(int argc, char* argv[])
{
if (argc > 5 || argc < 3) {