使用无序映射

时间:2017-07-27 02:44:49

标签: c++ nodes boolean-logic unordered-map

我一直在构建一个使用Graph模拟布尔电路的程序。图中的每个节点都有一个ID,逻辑节点的类型(来自typedef,它定义每个节点为主输入,And,Or,Not)以及节点和节点输出的向量。我将包含以下标题的代码:

#ifndef BOOLEAN_CIRCUIT_HH_
#define BOOLEAN_CIRCUIT_HH_
#include <string>
#include <vector>

enum class NodeType{PI, AND, OR, NOT};

class Node {
public:
    Node(unsigned id ,NodeType type);
    void addFanIn(Node * node);
    void addFanOut(Node * node);
    void printNode();

private:
    //bool flag;
    NodeType type;
    unsigned id;
    std::vector<Node*> fanIn;
    std::vector<Node*> fanOut;
};

class Boolean_Circuit{
public:
    Boolean_Circuit();
    Boolean_Circuit(std::string filename);
    ~Boolean_Circuit();
    void addNode( unsigned id, NodeType type, std::vector<unsigned> fanIn);
    //bool satisfy();
    bool simulate(std::vector<bool> inputs);
    int randomSuccessCount();
    int countLogicalNodes();
    void printCircuit();

protected:

private:
    bool evaluate(Node * node);
    std::vector<Node *> nodeList;
    std::string toString();//loop through nodelist
};
#endif // BOOLEAN_CIRCUIT_HH_

目前我已正确构建电路并根据需要打印出来,现在我只需要在Boolean_Circuit类中完成评估和模拟方法。我建议的教授实施是使用无序映射来存储每个节点的逻辑值,因为模拟通过电路运行其输入。目前的实施:

#include <unordered_map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "BooleanCircuit.hh"

Node::Node(unsigned idIn, NodeType typeIn){
    id = idIn;
    type = typeIn;
}

Boolean_Circuit::Boolean_Circuit(){}

Boolean_Circuit::Boolean_Circuit(std::string filename){
   std::ifstream in(filename);
   if(in.is_open()){
        std::string line;
        while(getline(in, line)){
            std::stringstream is(line);
            unsigned id;
            is>>id;
            std::string tag;
            is>>tag;
            NodeType type;
            if(tag == std::string("PI")) type = NodeType::PI;
            else if(tag == std::string("AND")) type = NodeType::AND;
            else if(tag == std::string("OR")) type = NodeType::OR;
            else if(tag == std::string("NOT")) type = NodeType::NOT;
            else{
                throw std::invalid_argument("Incorrect Inputs1");
            }
            std::vector<unsigned> fanIns;
            unsigned i;
            while(!is.eof()){
                is>>i;
                fanIns.push_back(i);
            }
            addNode(id, type, fanIns);

        }
   }
}

void Boolean_Circuit::addNode(unsigned id, NodeType type, std::vector<unsigned> fanIn){
    if (id != nodeList.size()){
        std::cout<< "ID = " << id << "NodeList Size = " << nodeList.size()<< std::endl;
        throw std::invalid_argument("Incorrect Inputs8");
    }

    if (type == NodeType::PI && fanIn.size()>0) throw std::invalid_argument("Incorrect Inputs2");
    else if (type == NodeType::AND && fanIn.size()<1) throw std::invalid_argument("Incorrect Inputs3");
    else if (type == NodeType::OR && fanIn.size()<1) throw std::invalid_argument("Incorrect Inputs4");
    else if (type == NodeType::NOT && fanIn.size()!=1) throw std::invalid_argument("Incorrect Inputs5");

    Node *newNode = new Node(id, type);
    for(size_t i = 0; i<fanIn.size(); i++){
        if(i>=id) throw std::invalid_argument("Incorrect Inputs6");
        newNode->addFanIn(nodeList[fanIn[i]]);
        nodeList[fanIn[i]]->addFanOut(newNode);
    }
    nodeList.push_back(newNode);

}

Boolean_Circuit::~Boolean_Circuit(){
    for(size_t i =0; i< nodeList.size();i++){
        delete(nodeList[i]);
     }
   nodeList.clear();
}


void Boolean_Circuit::printCircuit(){
    std::cout<< "Full circuit in order:"<<std::endl;
    for(size_t i = 0; i<nodeList.size(); i++){
        nodeList[i]->printNode();
    }
}

void Node::printNode(){
    std::string typeString;
    if(type == NodeType::PI) typeString = "PI";
    else if(type == NodeType::AND) typeString = "AND";
    else if(type == NodeType::OR) typeString = "OR";
    else if(type == NodeType::NOT) typeString = "NOT";
    else{
        throw std::invalid_argument("Incorrect Inputs7");
    }
    std::cout<< id << " " << typeString<< " In: ";
    for(size_t i=0; i<fanIn.size(); i++){
        std::cout<<fanIn[i]->id<<" ";
    }
    std::cout<<"Out: ";
    for(size_t i=0; i<fanOut.size(); i++){
        std::cout<<fanOut[i]->id<<" ";
    }
    std::cout<< "" << std::endl;
}

/*bool Boolean_Circuit::evaluate(Node node){ 
    if (!node) return 0;
    else if (node.type == NodeType::AND){
        for (int i=0; i<node.incoming.size(); i++){
            if(!node.incoming[i].status??) return 0; //relies on incoming returning bools
        }
        return 1;
    }

    else if(node.type == NodeType::OR){
       for (int i=0; i<node.incoming.size(); i++){
            if(node.incoming[i].status??) return 1; //relies on incoming returning bools
        }
        return 0;
    }

    else if (node.type == NodeType::NOT){
        //only ideas for this function rely on statuses
    }
}*/

bool Boolean_Circuit::simulate(std::vector<bool> inputs){
    std::unordered_map<unsigned, bool> mymap; //the unsigned is the node id, bool is the status of the node based of of inputs.
    return false;
}


void Node::addFanIn(Node * soloFan){
    fanIn.push_back(soloFan);
}

void Node::addFanOut(Node * soloFan){
    fanOut.push_back(soloFan);
}

目前的主要内容:

#include "BooleanCircuit.hh"
#include <iostream>

int main(int argc, char *argv[]){
   if (argc >2){
    std::cout << "Too many arguments"<< std::endl;
    return -1;
   }
   else if (argc == 2){
       Boolean_Circuit circuit(argv[1]);
   }
   else{
       std::cout << "File name not provided" << std::endl;
       Boolean_Circuit circuit;
       circuit.addNode(0, NodeType::PI, {});
       circuit.addNode(1, NodeType::PI, {});
       circuit.addNode(2, NodeType::NOT, {0});
       circuit.addNode(3, NodeType::NOT, {1});
       circuit.addNode(4, NodeType::OR, {0,1});
       circuit.addNode(5, NodeType::OR, {0,3});
       circuit.addNode(6, NodeType::OR, {2,1});
       circuit.addNode(7, NodeType::OR, {2,3});
       circuit.addNode(8, NodeType::AND, {4,5,6,7});
       circuit.printCircuit();

   }
    return 0;
}

我不确定如何使用地图实现模拟和评估函数,因为结构对我来说是非常新的。一些帮助将非常感激。我想要的函数应该成对使用,评估有助于逻辑地解决每个节点,因为模拟通过电路遍历其输入(作为布尔矢量给出)。使用无序地图作为状态参考,我该如何实施?

如果需要更多关于设计意图的细节,请告诉我。

0 个答案:

没有答案