From cc85e73456fee27236f0fa18ad7bc9ccd6877c19 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 15 Jun 2024 19:36:15 +0200 Subject: clean up a little bit --- Circuit.cpp | 6 +----- Exception.cpp | 5 +++-- GateOr.cpp | 1 + GateXor.cpp | 11 ++++++----- LoopDetection.cpp | 18 ++++++------------ LoopDetection.h | 13 +++++++++---- Net.cpp | 5 +---- Node.cpp | 17 ++++++----------- NodeFactory.cpp | 3 --- NodeInput.cpp | 1 - NodeInput.h | 2 -- Observer.cpp | 10 ++-------- main.cpp | 3 --- prut.h | 13 ------------- 14 files changed, 35 insertions(+), 73 deletions(-) delete mode 100644 prut.h diff --git a/Circuit.cpp b/Circuit.cpp index 731e79f..dfbb178 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -5,7 +5,6 @@ #include "NodeFactory.h" #include "NodeOutputVisitor.h" -#include "prut.h" using std::format; @@ -26,8 +25,6 @@ void Circuit::new_node(string label, string type) { throw CircuitException("unknown type \"%s\"", type.c_str()); nodes[label] = node; - - prutprintf("[%s] (%s)", label.c_str(), type.c_str()); } void Circuit::new_net(string src, vector dests) { @@ -44,7 +41,6 @@ void Circuit::new_net(string src, vector dests) { if (node == nullptr) throw CircuitException("unknown destination node \"%s\"", dest.c_str()); node->addInput(net); - prutprintf("%s -> %s", src.c_str(), dest.c_str()); } } @@ -74,7 +70,7 @@ string Circuit::result() { n.second->accept(visitor); if (!visitor.output_node) continue; - output += std::format("{}: {}\n", n.first, std::to_string(visitor.level)); + output += format("{}: {}\n", n.first, std::to_string(visitor.level)); } return output; diff --git a/Exception.cpp b/Exception.cpp index 371aaa7..46f420e 100644 --- a/Exception.cpp +++ b/Exception.cpp @@ -1,9 +1,9 @@ -#include "Exception.h" - #include #include #include +#include "Exception.h" + Exception::~Exception() { if (error != NULL) free(error); @@ -43,3 +43,4 @@ ParserException::ParserException(const char * fmt, ...) { va_format(args, fmt); va_end(args); } + diff --git a/GateOr.cpp b/GateOr.cpp index 0aafe50..46b8fdb 100644 --- a/GateOr.cpp +++ b/GateOr.cpp @@ -18,3 +18,4 @@ GateOr::GateOr(const GateOr * prototype) : Node() { } GateOr * GateOr::clone() const { return new GateOr(this); } + diff --git a/GateXor.cpp b/GateXor.cpp index 84fb1ec..44154bd 100644 --- a/GateXor.cpp +++ b/GateXor.cpp @@ -3,18 +3,19 @@ GateXor GateXor::instance(GateXor::type); SignalLevel GateXor::level() { - int highCount = 0; + bool on = false; for (int i = 0; i < this->inputs.size(); i++) { SignalLevel l = this->inputs[i]->getLevel(); if (l == UNDEFINED) return UNDEFINED; - if (l == HIGH) highCount++; + if (l == HIGH) on = !on; } - return (highCount % 2 == 1) ? HIGH : LOW; + return on ? HIGH : LOW; } -GateXor::GateXor(const GateXor *prototype) : Node() {} +GateXor::GateXor(const GateXor * prototype) : Node() {} -GateXor *GateXor::clone() const { +GateXor * GateXor::clone() const { return new GateXor(this); } + diff --git a/LoopDetection.cpp b/LoopDetection.cpp index ac4ab93..e256754 100644 --- a/LoopDetection.cpp +++ b/LoopDetection.cpp @@ -1,30 +1,24 @@ -#include -#include -#include -#include - #include "LoopDetection.h" #include "Exception.h" LoopDetection::~LoopDetection(){} -void LoopDetection::add_connection(const std::string &src, const std::vector &dests) -{ +void LoopDetection::add_connection(const string &src, const vector &dests) { for (const auto &dest : dests) adj_list[src].push_back(dest); if (detect_cycle(src)) - throw CircuitException( "Cycle detected starting from node: %s", src.c_str()); + throw CircuitException("cycle detected starting from node: %s", src.c_str()); } -bool LoopDetection::detect_cycle(const std::string &start) { - std::unordered_set visited; - std::unordered_set rec_stack; +bool LoopDetection::detect_cycle(const string &start) { + unordered_set visited; + unordered_set rec_stack; return is_cyclic(start, visited, rec_stack); } -bool LoopDetection::is_cyclic(const std::string &node, std::unordered_set &visited, std::unordered_set &rec_stack) { +bool LoopDetection::is_cyclic(const string &node, unordered_set &visited, unordered_set &rec_stack) { if (rec_stack.find(node) != rec_stack.end()) return true; // Cycle/Loop detected diff --git a/LoopDetection.h b/LoopDetection.h index c25887e..cff3395 100644 --- a/LoopDetection.h +++ b/LoopDetection.h @@ -5,17 +5,22 @@ #include #include +using std::string; +using std::vector; +using std::unordered_map; +using std::unordered_set; + class LoopDetection { public: LoopDetection() = default; virtual ~LoopDetection(); //! Add connection and throws CircuitError if it creates a loop - virtual void add_connection(const std::string &src, const std::vector &dests); + virtual void add_connection(const string &src, const vector &dests); private: - std::unordered_map> adj_list; + unordered_map> adj_list; - virtual bool detect_cycle(const std::string &start); - virtual bool is_cyclic(const std::string &node, std::unordered_set &visited, std::unordered_set &rec_stack); + virtual bool detect_cycle(const string &start); + virtual bool is_cyclic(const string &node, unordered_set &visited, unordered_set &rec_stack); }; diff --git a/Net.cpp b/Net.cpp index e423c66..cd6f4df 100644 --- a/Net.cpp +++ b/Net.cpp @@ -1,10 +1,7 @@ #include "Net.h" -#include - -#include "prut.h" void Net::setLevel(SignalLevel level){ - // if (this->level == level) return; + if (this->level == level) return; this->level = level; this->notify(); } diff --git a/Node.cpp b/Node.cpp index 7832d30..eb3428e 100644 --- a/Node.cpp +++ b/Node.cpp @@ -1,12 +1,8 @@ -#include - #include "Node.h" #include "NodeFactory.h" #include "Net.h" #include "Exception.h" -#include "prut.h" - Node::Node(const char * type) { NodeFactory::assign(type, this); } @@ -17,19 +13,18 @@ void Node::addInput(Net * net) { } void Node::setOutput(Net * net){ - if (this->output == nullptr) { - this->output = net; - } else { - throw CircuitException("Net already assigned"); - } + if (this->output != nullptr) + throw CircuitException("net already assigned"); + + this->output = net; } void Node::sim() { size_t input_size = this->inputs.size(); if (this->min_inputs >= 0 && input_size < min_inputs) - throw CircuitException("Too few inputs"); + throw CircuitException("too few inputs"); if (this->max_inputs >= 0 && input_size > max_inputs) - throw CircuitException("Too many inputs"); + throw CircuitException("too many inputs"); // NodeOutput does not have an output itself if (this->output == nullptr) return; diff --git a/NodeFactory.cpp b/NodeFactory.cpp index 6105c2d..44e86ae 100644 --- a/NodeFactory.cpp +++ b/NodeFactory.cpp @@ -1,6 +1,4 @@ #include -#include -#include #include #include "NodeFactory.h" @@ -26,7 +24,6 @@ void NodeFactory::assign(const char * _type, const Node * node) { // ensure there is only one class that registers a type assert(!has_type(type)); - // printf("map[\"%s\"] = %p\n", type.c_str(), node); map[type] = node; } diff --git a/NodeInput.cpp b/NodeInput.cpp index a106319..591c2ca 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -1,5 +1,4 @@ #include "NodeInput.h" -#include "prut.h" NodeInputLow NodeInputLow::instance(NodeInputLow::type); NodeInputHigh NodeInputHigh::instance(NodeInputHigh::type); diff --git a/NodeInput.h b/NodeInput.h index 9b87f3b..29ef091 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -2,8 +2,6 @@ #include "Node.h" -#include "prut.h" - class NodeInput : public Node { public: NodeInput(); diff --git a/Observer.cpp b/Observer.cpp index 39a8245..cd8589f 100644 --- a/Observer.cpp +++ b/Observer.cpp @@ -1,18 +1,12 @@ -#include - #include "Observer.h" -void Subject::attach(Observer * obs){ - // std::cout << "added" << std::endl; +void Subject::attach(Observer * obs) { this->observers.push_back(obs); } -void Subject::detach(Observer *){ - -} +void Subject::detach(Observer *) { } int Subject::size() { - // std::cout << "subject list size " << this->observers.size() << std::endl; return this->observers.size(); } diff --git a/main.cpp b/main.cpp index d8720c1..b87eab7 100644 --- a/main.cpp +++ b/main.cpp @@ -5,8 +5,6 @@ #include "Parser.h" #include "Circuit.h" -#include "prut.h" - using std::cout; using std::endl; using std::ifstream; @@ -38,7 +36,6 @@ int main(int argc, char** argv) { cout << "Parser error: " << e.what() << endl; return EXIT_FAILURE; } - prutprint("parsing done!"); try { circuit.sim(); diff --git a/prut.h b/prut.h deleted file mode 100644 index 2bd2ba0..0000000 --- a/prut.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -// Disable prutprint -#define prutprintf(fmt, ...) - -// Short version "file:line: message" -// #define prutprintf(fmt, ...) printf("\x1b[37m%s:%d:\x1b[0m " fmt "\n", __FILE_NAME__, __LINE__, __VA_ARGS__) - -// Long version "function (file:line): message" -// #define prutprintf(fmt, ...) printf("\x1b[37m%s (%s:%d):\x1b[0m " fmt "\n", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__, __VA_ARGS__) - -#define prutprint(s) prutprintf("%s", s) - -- cgit v1.2.3