diff --git a/Makefile b/Makefile index c82e9d6d..8267ac74 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,14 @@ ############################################################################################################## # Compiler -CC := g++ +CC := g++ #output directory -BIN := bin +BIN := bin #include directory -INCLUDE := include +INCLUDE := include #Libraries -LIB := lib -LIBRARIES := +LIB := lib +LIBRARIES := ifndef PYTHON_VERSION PYTHON_VERSION := $(shell python3 -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)";) #3.8 @@ -20,34 +20,21 @@ endif PYTHON_VERSION_ :=$(shell python$(PYTHON_VERSION) -c "import sys;t='{v[0]}{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)";) #38 PYTHON_VERSION3 :=$(shell python$(PYTHON_VERSION) -c "import sys;t='{v[0]}.{v[1]}.{v[2]}'.format(v=list(sys.version_info[:3]));sys.stdout.write(t)";) #3.8.s2 -############################################################################################################## -##################################### MarkovPassword project options ################################# -############################################################################################################## - -MP_C_FLAGS := -Wall -Wextra -g -MP_EXEC := Markov -MP_SRC := $(shell find ./MarkovPasswords/src/ -name '*.cpp') $(shell find ./MarkovModel/src/ -name '*.cpp') - -#build pattern -$(BIN)/$(MP_EXEC): $(MP_SRC) - $(CC) $(MP_C_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES) - -PYTHON_VERSION_ :=$(shell python$(PYTHON_VERSION) -c "import sys;t='{v[0]}{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)";) #38 -PYTHON_VERSION3 :=$(shell python$(PYTHON_VERSION) -c "import sys;t='{v[0]}.{v[1]}.{v[2]}'.format(v=list(sys.version_info[:3]));sys.stdout.write(t)";) #3.8.s2 ############################################################################################################## ##################################### MarkovPassword project options ################################# ############################################################################################################## MP_C_FLAGS := -Wall -Wextra -g MP_EXEC := Markov -MP_SRC := $(shell find ./MarkovPasswords/src/ -name '*.cpp') $(shell find ./MarkovModel/src/ -name '*.cpp') +MP_SRC := $(shell find ./MarkovPasswords/src/ -name '*.cpp') MP_INC := MP_LIB := -lboost_program_options +MP_INC := $(shell pwd) + #build pattern $(BIN)/$(MP_EXEC): $(MP_SRC) - $(CC) $(MP_C_FLAGS) -I$(MP_INC) -L$(LIB) $^ -o $@ $(MP_LIB) - + $(CC) $(MP_C_FLAGS) -I$(MP_INC) -L$(LIB) $^ -o $@ $(MP_LIB) ############################################################################################################## ##################################### MarkovModel project options ################################# @@ -77,12 +64,13 @@ $(BIN)/%.cpp.o:%.cpp ##################################### Markopy Options ################################# ############################################################################################################## -MPY_SRC := $(shell find MarkovModel/src/ -name '*.cpp') MarkovPasswords/src/markovPasswords.cpp $(shell find Markopy/src/Module/ -name '*.cpp') +MPY_SRC_DIR := Markopy/src/ +MPY_SRC := MarkovPasswords/src/markovPasswords.cpp $(shell find Markopy/src/Module/ -name '*.cpp') MPY_OBJS := $(MPY_SRC:%=$(BIN)/%.o) MPY_DEPS := $(MPY_OBJS:.o=.d) MPY_LDFLAGS := -shared -lboost_python$(PYTHON_VERSION_) -lpython$(PYTHON_VERSION) MPY_C_FLAGS := $(MPY_INC_FLAGS) -MMD -MP -fPIC -I/usr/include/python$(PYTHON_VERSION) -MPY_INC_DIRS := $(shell find $(MPY_SRC_DIR) -type d) +MPY_INC_DIRS := $(shell find $(MPY_SRC_DIR) -type d) $(shell pwd) MPY_INC_FLAGS := $(addprefix -I,$(MPY_INC_DIRS)) MPY_SO := markopy.so @@ -92,7 +80,7 @@ $(BIN)/$(MPY_SO): $(MPY_OBJS) # Build step for C++ source $(BIN)/%.cpp.o:%.cpp mkdir -p $(dir $@) - $(CC) $(MPY_C_FLAGS) -c $< -o $@ + $(CC) $(MPY_C_FLAGS) $(MPY_INC_FLAGS) -c $< -o $@ -include $(MPY_DEPS) diff --git a/Markopy/Markopy.vcxproj b/Markopy/Markopy.vcxproj index 866a6c59..230cb0fd 100644 --- a/Markopy/Markopy.vcxproj +++ b/Markopy/Markopy.vcxproj @@ -84,6 +84,7 @@ false .pyd + $(SolutionDir)$(Platform)\intermediate\Markopy @@ -140,7 +141,8 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - $(pythonLocation)\include;$(PYTHON_PATH)\include;$(BOOST_ROOT);$(SolutionDir)MarkovModel\src\;$(SolutionDir)\MarkovPasswords\src\; + $(SolutionDir);$(SolutionDir)\MarkovPasswords\src\;$(SolutionDir)MarkovModel\src\;$(pythonLocation)\include;$(PYTHON_PATH)\include;$(BOOST_ROOT);$(SolutionDir)MarkovModel\src\;$(SolutionDir)\MarkovPasswords\src\; + Default Console @@ -152,9 +154,6 @@ - - - diff --git a/Markopy/Markopy.vcxproj.filters b/Markopy/Markopy.vcxproj.filters index a7f1e7ac..5c063a6c 100644 --- a/Markopy/Markopy.vcxproj.filters +++ b/Markopy/Markopy.vcxproj.filters @@ -22,9 +22,6 @@ {9fe04840-08b7-4f78-8ae7-6513c3349fd5} - - {e1af0caf-1390-43cd-b7d4-57fe1d3ff9dd} - {ab5a1a08-ca20-48d3-9c82-4fd5e43bb20a} @@ -33,15 +30,6 @@ Source Files\Module - - Dependencies\MarkovModel - - - Dependencies\MarkovModel - - - Dependencies\MarkovModel - Dependencies\MarkovPasswords diff --git a/Markopy/src/Module/markopy.cpp b/Markopy/src/Module/markopy.cpp index ed33e2aa..b4704f6e 100644 --- a/Markopy/src/Module/markopy.cpp +++ b/Markopy/src/Module/markopy.cpp @@ -7,9 +7,6 @@ using namespace boost::python; -std::random_device rd; -std::default_random_engine generator(rd()); -std::uniform_int_distribution distribution(0, 0xffffFFFF); BOOST_PYTHON_MODULE(markopy) { diff --git a/MarkovModel/MarkovModel.vcxproj b/MarkovModel/MarkovModel.vcxproj index 31c3df45..42186f68 100644 --- a/MarkovModel/MarkovModel.vcxproj +++ b/MarkovModel/MarkovModel.vcxproj @@ -112,7 +112,7 @@ false $(SolutionDir)$(Platform)\Release\ - $(Platform)\Release\ + $(SolutionDir)$(Platform)\intermediate\MarkovModel\ false @@ -204,7 +204,7 @@ true true false - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies);cudart.lib + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) $(IntDir)$(TargetName).device-link.obj @@ -221,6 +221,7 @@ NotUsing pch.h %(AdditionalIncludeDirectories);$(SolutionDir)MarkovModel\src + Default Windows @@ -243,11 +244,7 @@ - - - - diff --git a/MarkovModel/MarkovModel.vcxproj.filters b/MarkovModel/MarkovModel.vcxproj.filters index 0d54954f..212cb18f 100644 --- a/MarkovModel/MarkovModel.vcxproj.filters +++ b/MarkovModel/MarkovModel.vcxproj.filters @@ -38,17 +38,5 @@ Source Files - - Source Files - - - Source Files - - - Source Files - - - Header Files - \ No newline at end of file diff --git a/MarkovModel/src/MarkovModel.h b/MarkovModel/src/MarkovModel.h deleted file mode 100644 index 0cb0b0af..00000000 --- a/MarkovModel/src/MarkovModel.h +++ /dev/null @@ -1,13 +0,0 @@ -/* @brief -* Templates are instantiated during compile time, -* so alternative to this approach is instantiating the classes/methods -* with supported/allowed types to avoid a linker error -*/ - -#pragma once -#include "model.cpp" -#include "node.cpp" -#include "edge.cpp" - -//fixed code smell - diff --git a/MarkovModel/src/dllmain.cpp b/MarkovModel/src/dllmain.cpp index 89dd7648..64afc084 100644 --- a/MarkovModel/src/dllmain.cpp +++ b/MarkovModel/src/dllmain.cpp @@ -2,10 +2,6 @@ #include "model.h" #include -// dllmain.cpp : Defines the entry point for the DLL application. -//std::random_device rd; -//std::default_random_engine generator(rd()); -//std::uniform_int_distribution distribution(0, 0xffffFFFF); #ifdef _WIN32 __declspec(dllexport) void dll_loadtest() { diff --git a/MarkovModel/src/edge.cpp b/MarkovModel/src/edge.cpp deleted file mode 100644 index 2b48d3f6..00000000 --- a/MarkovModel/src/edge.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "edge.h" -#include - -//default constructor of edge -template -Markov::Edge::Edge() { - this->_left = NULL; - this->_right = NULL; - this->_weight = 0; -} -//constructor of edge -template -Markov::Edge::Edge(Markov::Node* _left, Markov::Node* _right) { - this->_left = _left; - this->_right = _right; - this->_weight = 0; -} -//to AdjustEdge the edges by the edge with its offset -template -void Markov::Edge::AdjustEdge(uint64_t offset) { - this->_weight += offset; - this->LeftNode()->UpdateTotalVerticeWeight(offset); -} -//to TraverseNode the node -template -Markov::Node* Markov::Edge::TraverseNode() { - if (this->RightNode()->NodeValue() == 0xff) //terminator node - return NULL; - return _right; -} -//to set the LeftNode of the node -template -void Markov::Edge::SetLeftEdge(Markov::Node* n) { - this->_left = n; -} -//to set the RightNode of the node -template -void Markov::Edge::SetRightEdge(Markov::Node* n) { - this->_right = n; -} -//to get the EdgeWeight of the node -template -uint64_t Markov::Edge::EdgeWeight() { - return this->_weight; -} -//to get the LeftNode of the node -template -Markov::Node* Markov::Edge::LeftNode() { - return this->_left; -} -//to get the RightNode of the node -template -Markov::Node* Markov::Edge::RightNode() { - return this->_right; -} - - diff --git a/MarkovModel/src/edge.h b/MarkovModel/src/edge.h index e4f8e2a6..cbffa815 100644 --- a/MarkovModel/src/edge.h +++ b/MarkovModel/src/edge.h @@ -1,6 +1,6 @@ #pragma once #include - +#include namespace Markov { @@ -69,3 +69,57 @@ namespace Markov { }; +//default constructor of edge +template +Markov::Edge::Edge() { + this->_left = NULL; + this->_right = NULL; + this->_weight = 0; +} +//constructor of edge +template +Markov::Edge::Edge(Markov::Node* _left, Markov::Node* _right) { + this->_left = _left; + this->_right = _right; + this->_weight = 0; +} +//to AdjustEdge the edges by the edge with its offset +template +void Markov::Edge::AdjustEdge(uint64_t offset) { + this->_weight += offset; + this->LeftNode()->UpdateTotalVerticeWeight(offset); +} +//to TraverseNode the node +template +Markov::Node* Markov::Edge::TraverseNode() { + if (this->RightNode()->NodeValue() == 0xff) //terminator node + return NULL; + return _right; +} +//to set the LeftNode of the node +template +void Markov::Edge::SetLeftEdge(Markov::Node* n) { + this->_left = n; +} +//to set the RightNode of the node +template +void Markov::Edge::SetRightEdge(Markov::Node* n) { + this->_right = n; +} +//to get the EdgeWeight of the node +template +uint64_t Markov::Edge::EdgeWeight() { + return this->_weight; +} +//to get the LeftNode of the node +template +Markov::Node* Markov::Edge::LeftNode() { + return this->_left; +} +//to get the RightNode of the node +template +Markov::Node* Markov::Edge::RightNode() { + return this->_right; +} + + diff --git a/MarkovModel/src/model.cpp b/MarkovModel/src/model.cpp deleted file mode 100644 index c08c2c1b..00000000 --- a/MarkovModel/src/model.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#pragma once -#include "model.h" -#include "node.h" -#include -#include -#include -#include - -template -Markov::Model::Model() { - this->starterNode = new Markov::Node(0); - this->nodes.insert({ 0, this->starterNode }); - - std::random_device rd; - generator = new std::default_random_engine(rd()); - distribution = std::uniform_int_distribution(0, 0xffffFFFF); -} - -template -bool Markov::Model::Import(std::ifstream *f) { - std::string cell; - - char src; - char target; - int oc; - - while (std::getline(*f, cell) ) { - //std::cout << "cell: " << cell << std::endl; - src = cell[0]; - target = cell[cell.length()-1]; - oc = std::atoi(cell.substr(2, cell.length() - 2).c_str()); - - - Markov::Node* srcN; - Markov::Node* targetN; - Markov::Edge* e; - if (this->nodes.find(src) == this->nodes.end()) { - srcN = new Markov::Node(src); - this->nodes.insert(std::pair*>(src, srcN)); - //std::cout << "Creating new node at start.\n"; - }else { - srcN = this->nodes.find(src)->second; - } - - if (this->nodes.find(target) == this->nodes.end()) { - targetN = new Markov::Node(target); - this->nodes.insert(std::pair*>(target, targetN)); - //std::cout << "Creating new node at end.\n"; - } - else { - targetN = this->nodes.find(target)->second; - } - e = srcN->Link(targetN); - e->AdjustEdge(oc); - this->edges.push_back(e); - - //std::cout << int(srcN->NodeValue()) << " --" << e->EdgeWeight() << "--> " << int(targetN->NodeValue()) << "\n"; - - - } - - //std::cout << "Total number of nodes: " << this->nodes.size() << std::endl; - //std::cout << "Total number of edges: " << this->edges.size() << std::endl; - - return true; -} - -template -bool Markov::Model::Import(const char* filename) { - std::ifstream importfile; - importfile.open(filename); - return this->Import(&importfile); - -} - -template -bool Markov::Model::Export(std::ofstream* f) { - Markov::Edge* e; - for (std::vector::size_type i = 0; i != this->edges.size(); i++) { - e = this->edges[i]; - //std::cout << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n"; - *f << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n"; - } - - return true; -} - -template -bool Markov::Model::Export(const char* filename) { - std::ofstream exportfile; - exportfile.open(filename); - return this->Export(&exportfile); -} - -template -NodeStorageType* Markov::Model::RandomWalk(int minSetting, int maxSetting) { - Markov::Node* n = this->starterNode; - int len = 0; - NodeStorageType *ret = new NodeStorageType[64]; - Markov::Node *temp_node; - while (n != NULL) { - //n = n->RandomNext(); - temp_node = n->RandomNext(); - //dirty cutoff, needs better solution - if (len == 60) - break; - if (len > maxSetting) { - //std::cout<<"MAX ->"<< "node*: " << temp_node << ", len: " << len << "\n"; - break; - } - - if ((temp_node == NULL) && (len < minSetting)) { - //std::cout << "node*: " << temp_node << ", len: " << len << "\n"; - continue; - } - - if (temp_node == NULL) - break; - n = temp_node; - - //std::cout << n->NodeValue(); - ret[len++] = n->NodeValue(); - - //maximum character length exceeded and stack will overflow. - //assert(len<32 && "return buffer overflowing, this will segfault if not aborted."); - } - - //null terminate the string - ret[len] = 0x00; - - //do something with the generated string - return ret; //for now -} - -template -void Markov::Model::AdjustEdge(const NodeStorageType* payload, long int occurrence) { - NodeStorageType p = payload[0]; - Markov::Node* curnode = this->starterNode; - Markov::Edge *e; - int i = 0; - - if(p == 0) return; - while (p != 0) { - e = curnode->FindEdge(p); - if(e==NULL) return; - e->AdjustEdge(occurrence); - curnode = e->RightNode(); - p = payload[++i]; - } - - e = curnode->FindEdge('\xff'); - e->AdjustEdge(occurrence); - return; -} - diff --git a/MarkovModel/src/model.h b/MarkovModel/src/model.h index 1e4be6ca..7732a939 100644 --- a/MarkovModel/src/model.h +++ b/MarkovModel/src/model.h @@ -7,12 +7,10 @@ #include #include #include -#include "edge.h" +#include +#include #include "node.h" - - - - +#include "edge.h" /** @brief Namespace for model related classes. @@ -22,6 +20,12 @@ namespace Markov { * * This class will be *templated later to work with other data types than char*. */ + template + class Node; + + template + class Edge; + template class Model { public: @@ -93,17 +97,153 @@ namespace Markov { /** @brief A list of all edges in this model. */ std::vector*> edges; - - /** @brief A default generator. */ - std::default_random_engine* generator; - - /** @brief A uniform distribution. */ - std::uniform_int_distribution distribution; }; }; - +template +Markov::Model::Model() { + this->starterNode = new Markov::Node(0); + this->nodes.insert({ 0, this->starterNode }); +} + +template +bool Markov::Model::Import(std::ifstream* f) { + std::string cell; + + char src; + char target; + int oc; + + while (std::getline(*f, cell)) { + //std::cout << "cell: " << cell << std::endl; + src = cell[0]; + target = cell[cell.length() - 1]; + oc = std::atoi(cell.substr(2, cell.length() - 2).c_str()); + + + Markov::Node* srcN; + Markov::Node* targetN; + Markov::Edge* e; + if (this->nodes.find(src) == this->nodes.end()) { + srcN = new Markov::Node(src); + this->nodes.insert(std::pair*>(src, srcN)); + //std::cout << "Creating new node at start.\n"; + } + else { + srcN = this->nodes.find(src)->second; + } + + if (this->nodes.find(target) == this->nodes.end()) { + targetN = new Markov::Node(target); + this->nodes.insert(std::pair*>(target, targetN)); + //std::cout << "Creating new node at end.\n"; + } + else { + targetN = this->nodes.find(target)->second; + } + e = srcN->Link(targetN); + e->AdjustEdge(oc); + this->edges.push_back(e); + + //std::cout << int(srcN->NodeValue()) << " --" << e->EdgeWeight() << "--> " << int(targetN->NodeValue()) << "\n"; + + + } + + //std::cout << "Total number of nodes: " << this->nodes.size() << std::endl; + //std::cout << "Total number of edges: " << this->edges.size() << std::endl; + + return true; +} + +template +bool Markov::Model::Import(const char* filename) { + std::ifstream importfile; + importfile.open(filename); + return this->Import(&importfile); + +} + +template +bool Markov::Model::Export(std::ofstream* f) { + Markov::Edge* e; + for (std::vector::size_type i = 0; i != this->edges.size(); i++) { + e = this->edges[i]; + //std::cout << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n"; + *f << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n"; + } + + return true; +} + +template +bool Markov::Model::Export(const char* filename) { + std::ofstream exportfile; + exportfile.open(filename); + return this->Export(&exportfile); +} + +template +NodeStorageType* Markov::Model::RandomWalk(int minSetting, int maxSetting) { + Markov::Node* n = this->starterNode; + int len = 0; + NodeStorageType* ret = new NodeStorageType[64]; + Markov::Node* temp_node; + while (n != NULL) { + //n = n->RandomNext(); + temp_node = n->RandomNext(); + //dirty cutoff, needs better solution + if (len == 60) + break; + if (len > maxSetting) { + //std::cout<<"MAX ->"<< "node*: " << temp_node << ", len: " << len << "\n"; + break; + } + + if ((temp_node == NULL) && (len < minSetting)) { + //std::cout << "node*: " << temp_node << ", len: " << len << "\n"; + continue; + } + + if (temp_node == NULL) + break; + n = temp_node; + + //std::cout << n->NodeValue(); + ret[len++] = n->NodeValue(); + + //maximum character length exceeded and stack will overflow. + //assert(len<32 && "return buffer overflowing, this will segfault if not aborted."); + } + + //null terminate the string + ret[len] = 0x00; + + //do something with the generated string + return ret; //for now +} + +template +void Markov::Model::AdjustEdge(const NodeStorageType* payload, long int occurrence) { + NodeStorageType p = payload[0]; + Markov::Node* curnode = this->starterNode; + Markov::Edge* e; + int i = 0; + + if (p == 0) return; + while (p != 0) { + e = curnode->FindEdge(p); + if (e == NULL) return; + e->AdjustEdge(occurrence); + curnode = e->RightNode(); + p = payload[++i]; + } + + e = curnode->FindEdge('\xff'); + e->AdjustEdge(occurrence); + return; +} diff --git a/MarkovModel/src/node.cpp b/MarkovModel/src/node.cpp deleted file mode 100644 index 837af78c..00000000 --- a/MarkovModel/src/node.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once -#include "node.h" -#include -#include -#include // To use runtime_error - -template -Markov::Node::Node(storageType _value) { - this->_value = _value; -}; - -template -Markov::Node::Node() { - this->_value = 0; - this->total_edge_weights = 0; -}; - -template -unsigned char Markov::Node::NodeValue() { - return _value; -} - -template -Markov::Edge* Markov::Node::Link(Markov::Node* n) { - Markov::Edge *v = new Markov::Edge(this, n); - this->UpdateEdges(v); - return v; -} - -template -Markov::Edge* Markov::Node::Link(Markov::Edge* v) { - v->SetLeftEdge(this); - this->UpdateEdges(v); - return v; -} - -template -Markov::Node* Markov::Node::RandomNext() { - - //get a random NodeValue in range of total_vertice_weight - int rnd = distribution(generator);// distribution(generator); - - int selection = rnd % this->total_edge_weights; //add division by zero execption handling //replace with next lines while not empty file - /*if(this->total_edge_weights==0) - throw std::runtime_error("Math error: Attempted to divide by zero\n"); - try { - int selection = rnd % this->total_edge_weights; - } - catch (std::runtime_error e) { - - // prints that exception has occurred - // calls the what function using object of - // runtime_error class - std::cout << "Exception occurred" << std::endl - << e.what(); - }*/ - - - //make absolute, no negative modulus values wanted - selection = (selection>=0)? selection : (selection + this->total_edge_weights); - - //iterate over the Edge map - //Subtract the Edge EdgeWeight from the selection at each Edge - //when selection goes below 0, pick that node - //(Fast random selection with EdgeWeight bias) - //std::cout << "Rand: " << rnd << "\n"; - //std::cout << "Total: " << this->total_edge_weights << "\n"; - //std::cout << "Total edges: " << this->edges.size() << "\n"; - for ( std::pair*> const& x : this->edges) { - //std::cout << selection << "\n"; - selection -= x.second->EdgeWeight(); - //std::cout << selection << "\n"; - if (selection < 0) return x.second->TraverseNode(); - } - - //if this assertion is reached, it means there is an implementation error above - assert(true && "This should never be reached (node failed to walk to next)"); - return NULL; -} - -template -bool Markov::Node::UpdateEdges(Markov::Edge* v) { - this->edges.insert({ v->RightNode()->NodeValue(), v }); - //this->total_edge_weights += v->EdgeWeight(); - return v->TraverseNode(); -} - -template -Markov::Edge* Markov::Node::FindEdge(storageType repr) { - auto e = this->edges.find(repr); - if(e==this->edges.end()) return NULL; - return e->second; -}; - -template -void Markov::Node::UpdateTotalVerticeWeight(long int offset) { - this->total_edge_weights += offset; -} - -template -std::map*>* Markov::Node::Edges() { - return &(this->edges); -} - -template -uint64_t Markov::Node::TotalEdgeWeights() { - return this->total_edge_weights; -} - diff --git a/MarkovModel/src/node.h b/MarkovModel/src/node.h index ca47c346..65c21a02 100644 --- a/MarkovModel/src/node.h +++ b/MarkovModel/src/node.h @@ -1,12 +1,11 @@ #pragma once #include #include -#include "edge.h" #include - -extern std::random_device rd; -extern std::default_random_engine generator; -extern std::uniform_int_distribution distribution; +#include +#include +#include // To use runtime_error +#include "edge.h" namespace Markov { @@ -102,3 +101,126 @@ namespace Markov { }; }; + +inline std::random_device& rd() { + static std::random_device _rd; + return _rd; +} + +inline std::default_random_engine& generator() { + static std::default_random_engine _generator; + return _generator; +} + + +inline std::uniform_int_distribution& distribution() { + static std::uniform_int_distribution _distribution; + return _distribution; +} + + + + +template +Markov::Node::Node(storageType _value) { + this->_value = _value; +}; + +template +Markov::Node::Node() { + this->_value = 0; + this->total_edge_weights = 0; +}; + +template +unsigned char Markov::Node::NodeValue() { + return _value; +} + +template +Markov::Edge* Markov::Node::Link(Markov::Node* n) { + Markov::Edge* v = new Markov::Edge(this, n); + this->UpdateEdges(v); + return v; +} + +template +Markov::Edge* Markov::Node::Link(Markov::Edge* v) { + v->SetLeftEdge(this); + this->UpdateEdges(v); + return v; +} + +template +Markov::Node* Markov::Node::RandomNext() { + + //get a random NodeValue in range of total_vertice_weight + int rnd = distribution()(generator());// distribution(generator); + + int selection = rnd % this->total_edge_weights; //add division by zero execption handling //replace with next lines while not empty file + /*if(this->total_edge_weights==0) + throw std::runtime_error("Math error: Attempted to divide by zero\n"); + try { + int selection = rnd % this->total_edge_weights; + } + catch (std::runtime_error e) { + + // prints that exception has occurred + // calls the what function using object of + // runtime_error class + std::cout << "Exception occurred" << std::endl + << e.what(); + }*/ + + + //make absolute, no negative modulus values wanted + selection = (selection >= 0) ? selection : (selection + this->total_edge_weights); + + //iterate over the Edge map + //Subtract the Edge EdgeWeight from the selection at each Edge + //when selection goes below 0, pick that node + //(Fast random selection with EdgeWeight bias) + //std::cout << "Rand: " << rnd << "\n"; + //std::cout << "Total: " << this->total_edge_weights << "\n"; + //std::cout << "Total edges: " << this->edges.size() << "\n"; + for (std::pair*> const& x : this->edges) { + //std::cout << selection << "\n"; + selection -= x.second->EdgeWeight(); + //std::cout << selection << "\n"; + if (selection < 0) return x.second->TraverseNode(); + } + + //if this assertion is reached, it means there is an implementation error above + assert(true && "This should never be reached (node failed to walk to next)"); + return NULL; +} + +template +bool Markov::Node::UpdateEdges(Markov::Edge* v) { + this->edges.insert({ v->RightNode()->NodeValue(), v }); + //this->total_edge_weights += v->EdgeWeight(); + return v->TraverseNode(); +} + +template +Markov::Edge* Markov::Node::FindEdge(storageType repr) { + auto e = this->edges.find(repr); + if (e == this->edges.end()) return NULL; + return e->second; +}; + +template +void Markov::Node::UpdateTotalVerticeWeight(long int offset) { + this->total_edge_weights += offset; +} + +template +std::map*>* Markov::Node::Edges() { + return &(this->edges); +} + +template +uint64_t Markov::Node::TotalEdgeWeights() { + return this->total_edge_weights; +} + diff --git a/MarkovPasswords.sln b/MarkovPasswords.sln index b7766dc5..7dda3071 100644 --- a/MarkovPasswords.sln +++ b/MarkovPasswords.sln @@ -4,9 +4,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 16.0.29806.167 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MarkovPasswords", "MarkovPasswords\MarkovPasswords.vcxproj", "{B356D85A-CAC3-4EF2-824B-59E3E49110CB}" - ProjectSection(ProjectDependencies) = postProject - {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048} = {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048} - EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MarkovModel", "MarkovModel\MarkovModel.vcxproj", "{A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}" EndProject @@ -20,9 +17,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTests", "UnitTests\UnitTests.vcxproj", "{26260E27-B240-4E67-AE52-A6919A8DBF5C}" - ProjectSection(ProjectDependencies) = postProject - {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048} = {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048} - EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{AEB3CC8A-A675-4CCA-BF3C-DA486B448C1C}" ProjectSection(SolutionItems) = preProject @@ -70,8 +64,8 @@ Global {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Lib Model|x64.Build.0 = Lib Model|x64 {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Lib Model|x86.ActiveCfg = Lib Model|Win32 {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Lib Model|x86.Build.0 = Lib Model|Win32 - {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Release|x64.ActiveCfg = Lib Model|x64 - {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Release|x64.Build.0 = Lib Model|x64 + {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Release|x64.ActiveCfg = DLL Model|x64 + {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Release|x64.Build.0 = DLL Model|x64 {A5D3FA06-3885-41F2-AD3D-F3D8FBC4C048}.Release|x86.ActiveCfg = Release|x64 {A9ECF03B-D971-47D9-B0A9-D0B4A3DBB114}.Debug|x64.ActiveCfg = Debug|x64 {A9ECF03B-D971-47D9-B0A9-D0B4A3DBB114}.Debug|x86.ActiveCfg = Debug|x64 diff --git a/MarkovPasswords/MarkovPasswords.vcxproj b/MarkovPasswords/MarkovPasswords.vcxproj index 514e948c..05588693 100644 --- a/MarkovPasswords/MarkovPasswords.vcxproj +++ b/MarkovPasswords/MarkovPasswords.vcxproj @@ -81,6 +81,7 @@ false $(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(SolutionDir)x64/lib/; + $(SolutionDir)$(Platform)\intermediate\MarkovPasswords\ @@ -132,7 +133,10 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions); true - $(BOOST_ROOT); + $(BOOST_ROOT);$(SolutionDir)MarkovModel/src/;$(SolutionDir) + Default + + Console @@ -150,6 +154,9 @@ + + + diff --git a/MarkovPasswords/MarkovPasswords.vcxproj.filters b/MarkovPasswords/MarkovPasswords.vcxproj.filters index 31f12dcd..a0ada259 100644 --- a/MarkovPasswords/MarkovPasswords.vcxproj.filters +++ b/MarkovPasswords/MarkovPasswords.vcxproj.filters @@ -22,6 +22,9 @@ {f32c89b1-88f7-47e3-91a3-18b2f45dd01a} + + {53549677-4457-4e63-9e1b-ba68ca41324d} + @@ -47,6 +50,15 @@ Header Files + + Header Files\model + + + Header Files\model + + + Header Files\model + diff --git a/MarkovPasswords/src/main.cpp b/MarkovPasswords/src/main.cpp index 7a20aeba..e0a2f6fa 100644 --- a/MarkovPasswords/src/main.cpp +++ b/MarkovPasswords/src/main.cpp @@ -7,9 +7,6 @@ #include #include "markovPasswords.h" -std::random_device rd; -std::default_random_engine generator(rd()); -std::uniform_int_distribution distribution(0, 0xffffFFFF); /** @brief Launch CLI tool. */ diff --git a/MarkovPasswords/src/markovPasswords.cpp b/MarkovPasswords/src/markovPasswords.cpp index fe2bea5a..254d87d9 100644 --- a/MarkovPasswords/src/markovPasswords.cpp +++ b/MarkovPasswords/src/markovPasswords.cpp @@ -1,6 +1,9 @@ #pragma once #include "markovPasswords.h" #include +#include + +Markov::Model* junk; MarkovPasswords::MarkovPasswords() : Markov::Model(){ diff --git a/MarkovPasswords/src/markovPasswords.h b/MarkovPasswords/src/markovPasswords.h index 5836567f..856a0ed9 100644 --- a/MarkovPasswords/src/markovPasswords.h +++ b/MarkovPasswords/src/markovPasswords.h @@ -1,5 +1,5 @@ #pragma once -#include "../../MarkovModel/src/MarkovModel.h" +#include "MarkovModel/src/model.h" /** @brief Markov::Model with char represented nodes. * diff --git a/MarkovPasswordsGUI/Generate.cpp b/MarkovPasswordsGUI/Generate.cpp index 67da8fe5..a0c24ada 100644 --- a/MarkovPasswordsGUI/Generate.cpp +++ b/MarkovPasswordsGUI/Generate.cpp @@ -18,10 +18,6 @@ Generate::Generate(QWidget* parent) QObject::connect(ui.pushButton, &QPushButton::clicked, this, [this] {generation(); }); - /* std::random_device rd; - std::default_random_engine generator(rd()); - std::uniform_int_distribution distribution(0, 0xffffFFFF); - */ } void Generate::generation() { diff --git a/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj b/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj index cb384820..91256114 100644 --- a/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj +++ b/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj @@ -44,6 +44,17 @@ + + + Default + $(Qt_INCLUDEPATH_);%(AdditionalIncludeDirectories);$(SolutionDir)MarkovModel/src/;$(SolutionDir)MarkovPasswords/src/;$(SolutionDir); + + + + + stdcpp17 + + core;gui;widgets;webenginewidgets; debug diff --git a/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj.filters b/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj.filters index 1bbae0d0..ccdb887d 100644 --- a/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj.filters +++ b/MarkovPasswordsGUI/MarkovPasswordsGUI.vcxproj.filters @@ -25,6 +25,9 @@ {3c3f933a-cec8-4731-b7a0-77b5b4b2e1cb} + + {a16bc5c3-bf52-491c-ab24-3439caad494e} + @@ -71,7 +74,7 @@ Source Files - Source Files + Source Files\dependencies diff --git a/MarkovPasswordsGUI/Train.cpp b/MarkovPasswordsGUI/Train.cpp index 7e582812..300885fe 100644 --- a/MarkovPasswordsGUI/Train.cpp +++ b/MarkovPasswordsGUI/Train.cpp @@ -6,14 +6,10 @@ #include #include #include "src/CLI.h" -#include "../MarkovPasswords/src/markovPasswords.h" +#include "MarkovPasswords/src/markovPasswords.h" #include -std::random_device rd; -std::default_random_engine generator(rd()); -std::uniform_int_distribution distribution(0, 0xffffFFFF); - Train::Train(QWidget* parent) : QMainWindow(parent) { diff --git a/UnitTests/UnitTests.cpp b/UnitTests/UnitTests.cpp index d1d7b0c1..f6e6bbcc 100644 --- a/UnitTests/UnitTests.cpp +++ b/UnitTests/UnitTests.cpp @@ -6,10 +6,6 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; -std::random_device rd; -std::default_random_engine generator(rd()); -std::uniform_int_distribution distribution(0, 0xffffFFFF); - /** @brief Namespace for Microsoft Native Unit Testing Classes */ namespace Testing {