From 264b46172af18f47d41d40b10a4eea072493ea5a Mon Sep 17 00:00:00 2001 From: Jonny007-MKD <1-23-4-5@web.de> Date: Thu, 14 Nov 2013 14:58:00 +0100 Subject: [PATCH] Algorithmus, der GetHazards() aufruft implementiert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Schleife, welche die Zellen iteriert und für jede GetHazards() aufruft. Wenn ein oder mehrere Hazards gefunden wurden, werden PrimImplikanten der PrimImplikantenCollection hinzugefügt, um den Hazards zu beheben. Dies wirkt sich allerdings nicht auf die PrimImplikanten in den Zellen aus, weshalb Hazards doppelt gefunden werden können! Außerdem ist das hinzufügen eines nur 2fachen PrimImplikanten mitunter nicht optimal. Zwei Optimierungen wurden eingebaut: "Schachbrett" und "Ãœberspringe Nullen". Hierdurch wird die Geschwindigkeit mehr als verdoppelt. --- Hazard/Hazard/Hazard.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Hazard/Hazard/Hazard.cpp b/Hazard/Hazard/Hazard.cpp index 50413d9..5da206e 100644 --- a/Hazard/Hazard/Hazard.cpp +++ b/Hazard/Hazard/Hazard.cpp @@ -80,14 +80,58 @@ int _tmain(int argc, _TCHAR* argv[]) cout << endl; }*/ + + // initialize Cells vector cells; cells.resize(numElements); + unsigned int numOnes = 0; for (unsigned int i = 0; i < numElements; i++) { cells[i] = new Cell(i, globalPIC); printf("Pos %2d: %d\n", i, cells[i]->value); + if (cells[i]->value) + numOnes++; } + + // find hazards + if (numOnes > numElements / 2) // we have more 1 than 0 --> checkerboard --> 50% of cells are checked + { + for (unsigned int i = 0; i < numElements; i++) + { + cout << "\nSchachbrettmuster\n"; + unsigned int grayI = i ^ (i/2); // transform to gray code + vector hazardousNeighbors = cells[grayI]->GetHazards(); + + if (hazardousNeighbors.size() == 0) // we found no hazard + continue; + + for (vector::iterator c = hazardousNeighbors.begin(); c < hazardousNeighbors.end(); c++) + { + printf("Hazard found! Cell %d <--> Cell %d\n", grayI, (*c)->index); + globalPIC->add(grayI, (*c)->index); // add PI that solves hazard. Not quite smart... + } + } + } + else // less 1 than 0 --> only check every 1 --> less than 50% (numOnes/numElements) of cells are checked + { + for (unsigned int i = 0; i < numElements; i++) + { + cout << "\nÜberspringe Nullen\n"; + if (!cells[i]->value) + continue; + vector hazardousNeighbors = cells[i]->GetHazards(); + + if (hazardousNeighbors.size() == 0) // we found no hazard + continue; + + for (vector::iterator c = hazardousNeighbors.begin(); c < hazardousNeighbors.end(); c++) + { + printf("Hazard found! Cell %d <--> Cell %d\n", i, (*c)->index); + globalPIC->add(i, (*c)->index); // add PI that solves hazard. Not quite smart... + } + } + } system("pause"); return 0; } \ No newline at end of file