Skip to content

Commit

Permalink
No estaba reseteando el grafo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-R0 committed Oct 30, 2020
1 parent e868009 commit 221c8e9
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion asmline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ void Asmline::setLabelsToJump(std::list<std::string> labelsToJumpGiven) {
labelsToJumpGiven.pop_front();
labelsToJump = labelsToJumpGiven;
}
}
}
2 changes: 1 addition & 1 deletion asmline.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ class Asmline {
bool esCortante();
};

#endif // ASMLINE_H_
#endif // ASMLINE_H_
27 changes: 17 additions & 10 deletions eBPF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#include "parser.h"
#include "results.h"

void EBPF::init(std::string& filename) {
std::ifstream reader;
reader.open(filename, std::ifstream::in);
void EBPF::init(std::ifstream& reader) {
int i = 1;
while (reader.good()) {
std::string myText;
Expand All @@ -22,7 +20,6 @@ void EBPF::init(std::string& filename) {
i++;
}
this->connectLostTags();
reader.close();
}

EBPF::EBPF(Results& r, FileFountain& f) : results(r), fileFountain(f) {
Expand All @@ -33,15 +30,25 @@ EBPF::EBPF(Results& r, FileFountain& f) : results(r), fileFountain(f) {
void EBPF::run() {
std::string filename;
while (!(filename = fileFountain.getNext()).empty()) {
this->init(filename);
bool hasCycles = this->hasCycle();
if (hasCycles)
results.addResult(filename, true, false);
else
results.addResult(filename, false, this->hasUnusedInstruction());
std::ifstream reader;
Graph opGraph;
reader.open(filename, std::ifstream::in);
this->init(reader);
reader.close();
bool cycle = this->hasCycle();
bool unused = this->hasUnusedInstruction();
this->restart();
results.addResult(filename, cycle, unused);
}
}

void EBPF::restart() {
referenciasColgadas.clear();
referenciasReconocidas.clear();
aristaACortar.clear();
opGraph.clear();
}

void EBPF::connectLostTags() {
for (int i : aristaACortar) {
opGraph.disconnectNext(i);
Expand Down
4 changes: 3 additions & 1 deletion eBPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <list>
#include <map>
#include <mutex>
#include <string>

#include "fileFountain.h"
Expand All @@ -20,14 +21,15 @@ class EBPF : public Thread {
Graph opGraph;
Parser parser;
FileFountain& fileFountain;
void init(std::string& filename);
void init(std::ifstream& reader);

public:
EBPF(Results& r, FileFountain& f);
void run();
void addInstructionToGraph(std::string line, int lineNumber);
void connectLostTags();
bool hasCycle();
void restart();
~EBPF() {}
bool hasUnusedInstruction();
};
Expand Down
5 changes: 2 additions & 3 deletions fileFountain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void FileFountain::sortFiles() {
std::reverse(files.begin(), files.end());
}
*/
FileFountain::FileFountain(int argc, char* argv[]) : alreadyRead(0) {
FileFountain::FileFountain(int argc, char* argv[]) {
for (int i = argc - 1; i > 1; i--) {
std::string file(argv[i]);
files.push_back(file);
Expand All @@ -24,8 +24,7 @@ int FileFountain::getNumberOfFiles() { return files.size(); }

std::string FileFountain::getNext() {
std::unique_lock<std::mutex> lock(m);
if (files.size() == 0) return {};
++alreadyRead;
if (files.size() == 0) return "";
std::string file = files.back();
files.pop_back();
return file;
Expand Down
1 change: 0 additions & 1 deletion fileFountain.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class FileFountain {
private:
std::vector<std::string> files;
size_t alreadyRead;
size_t toRead;
std::mutex m;

Expand Down
25 changes: 14 additions & 11 deletions graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <map>
#include <string>

bool Graph::hasBeenFound(int node) {
bool Graph::hasBeenFound(int node, std::list<int>& found) {
for (int i : found) {
if (node == i) return true;
}
Expand All @@ -23,7 +23,7 @@ void Graph::addEdge(int from, int to) {
int Graph::size() { return nodes.size(); }

bool Graph::isIn(int node) {
for (auto& i : nodes) {
for (auto i : nodes) {
if (node == i.first) return true;
}
return false;
Expand All @@ -35,20 +35,22 @@ void Graph::addIfItsNotIn(int node) {
}
}

bool Graph::_isCyclic(int start) {
if (hasBeenFound(start)) return true;
bool Graph::_isCyclic(int start, std::list<int>& found) {
if (hasBeenFound(start, found)) return true;
found.push_front(start);
for (int i : nodes.at(start)) {
if (Graph::_isCyclic(i)) return true;
if (Graph::_isCyclic(i, found)) return true;
}
found.remove(start);
return false;
}

void Graph::clear() { nodes.clear(); }

bool Graph::isCyclic() {
found = {};
for (auto& it : nodes) {
if (this->_isCyclic(it.first)) {
std::list<int> found;
for (auto it : nodes) {
if (this->_isCyclic(it.first, found)) {
found.clear();
return true;
}
Expand All @@ -60,16 +62,17 @@ bool Graph::isCyclic() {
void Graph::connectLast(int to) { this->addEdge(nodes.size() - 1, to); }

void Graph::dfs(int start, std::list<int>& found) {
if (hasBeenFound(start)) return;
if (hasBeenFound(start, found)) return;
found.push_front(start);
for (int i : nodes[start]) {
if (!(hasBeenFound(i))) {
if (!(hasBeenFound(i, found))) {
Graph::dfs(i, found);
}
}
}

bool Graph::hasUnusedInstructions() {
std::list<int> found;
if (nodes.size() == 0) return false;
dfs(1, found);
long unsigned int amountFound = found.size();
Expand All @@ -87,4 +90,4 @@ void Graph::disconnectNext(int nodo) {
}
}

void Graph::connect(int from, int to) { nodes[from].push_front(to); }
void Graph::connect(int from, int to) { nodes[from].push_front(to); }
6 changes: 3 additions & 3 deletions graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
class Graph {
private:
std::map<int, std::list<int>> nodes;
std::list<int> found;

public:
Graph() {}
void addVertex(int node);
bool hasBeenFound(int node);
bool hasBeenFound(int node, std::list<int>& found);
int size();
bool isIn(int node);
void addIfItsNotIn(int node);
void addEdge(int from, int to);
bool _isCyclic(int start);
bool _isCyclic(int start, std::list<int>& found);
bool isCyclic();
void dfs(int start, std::list<int>& found);
void connect(int from, int to);
void connectLast(int to);
bool hasUnusedInstructions();
void disconnectNext(int nodo);
void clear();
};

#endif // GRAPH_H_
4 changes: 0 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ static int work(int numberOfThreads, FileFountain& files) {
std::vector<EBPF*> holders;
Results results;

numberOfThreads = numberOfThreads > files.getNumberOfFiles()
? files.getNumberOfFiles()
: numberOfThreads;

holders.reserve(numberOfThreads);

for (int i = 0; i < numberOfThreads; i++) {
Expand Down
4 changes: 3 additions & 1 deletion thread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "thread.h"

#include <utility>

Thread::Thread() {}

Thread::~Thread() {}
Expand All @@ -13,4 +15,4 @@ Thread::Thread(Thread&& other) { this->thread = std::move(other.thread); }
Thread& Thread::operator=(Thread&& other) {
this->thread = std::move(other.thread);
return *this;
}
}
2 changes: 1 addition & 1 deletion thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class Thread {
std::thread thread;
};

#endif // THREAD_H_
#endif // THREAD_H_

0 comments on commit 221c8e9

Please sign in to comment.