Added CellCollection

Moved Hazard detection algorithm to this class to save lines in
user_main
This commit is contained in:
Jonny007-MKD 2013-12-02 00:09:51 +01:00
parent dda518f45a
commit effa28cb2d
7 changed files with 198 additions and 99 deletions

View file

@ -0,0 +1,89 @@
#include "stdafx.h"
#include <vector>
#include <iostream>
#include "Cell.h"
#include "CellCollection.h"
using namespace std;
extern uint dimension;
extern uint numElements;
void CellCollection::add(Cell* &cell)
{
this->cells.push_back(cell);
}
void CellCollection::init(PrimImplikantCollection* &globalPIC)
{
this->globalPIC = globalPIC;
this->cells.resize(numElements);
for (uint i = 0; i < numElements; i++)
this->cells[i] = new Cell(i, globalPIC);
}
void CellCollection::findHazards()
{
for (uint i = 0; i < numElements; i += 2) // we only need to check every 2nd as long as it's gray
{
uint I = i ^ (i/2); // transform to gray code --> Schachbrettmuster
Cell* currentCell = cells[I]; // this is the cell we are currently checking
if (currentCell->value == false) // no hazard can occur
continue;
cout << " Checking cell " << I << endl;
vector<Cell*>* hazardousNeighbors = currentCell->getHazards(this->cells);
if (hazardousNeighbors->size() == 0) // no hazard found
{
delete hazardousNeighbors;
continue;
}
for (vector<Cell*>::iterator nc = hazardousNeighbors->begin(); nc < hazardousNeighbors->end(); nc++)
{
printf("Hazard found! Cell %d <--> Cell %d\n", I, (*nc)->index);
this->globalPIC->add(I, (*nc)->index); // add PI that solves hazard. Not quite smart...
(*nc)->refresh(this->globalPIC); // refresh the foreign PIC (a PI was added)
}
currentCell->refresh(this->globalPIC); // refresh the current PIC (a PI was added)
delete hazardousNeighbors;
}
}
void CellCollection::Dispose()
{
for (uint i = 0; i < this->cells.size(); i++)
delete cells[i];
}
uint CellCollection::size()
{
return cells.size();
}
Cell* CellCollection::back()
{
return cells.back();
}
Cell* CellCollection::front()
{
return cells.front();
}
Cell* CellCollection::at(uint index)
{
return cells.at(index);
}
Cell * CellCollection::operator[](uint &index)
{
return cells.at(index);
}

View file

@ -0,0 +1,37 @@
#pragma once
#include "stdafx.h"
#include "Cell.h"
#include "CellCollection.h"
using namespace std;
#ifndef _CELLCOLLEC_H
#define _CELLCOLLEC_H
class CellCollection {
public:
void add(Cell* &cell);
void init(PrimImplikantCollection* &globalPIC);
void findHazards();
void Dispose();
uint size();
Cell* back();
Cell* front();
Cell* at(uint index);
Cell* operator[](uint &index);
CellCollection(PrimImplikantCollection* &globalPIC)
{
this->init(globalPIC);
};
vector<Cell*> cells;
PrimImplikantCollection* globalPIC;
};
#endif

View file

@ -1,6 +1,3 @@
// Hazard.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "stdafx.h"
#include <iostream>
#include <string>
@ -9,6 +6,7 @@
#include "PrimImplikant.h"
#include "PrimImplikantCollection.h"
#include "Cell.h"
#include "CellCollection.h"
#include "Wertetabelle.h"
#include "KV.h"
@ -23,124 +21,89 @@ void user_main(void)
FILE * input;
FILE * error;
FILE * list;
fopen_s(&input, "res\\input.txt", "r");
if (input == 0)
if (open_files(input, error, list) != 0)
{
cout << "Fehler Inputdatei";
system("pause");
return;
}
fopen_s(&error, "res\\errorParser.txt", "a");
if (error == 0)
{
cout << "Fehler Fehlerdatei";
system("pause");
return;
}
fopen_s(&list, "res\\listParser.txt", "w");
if (list == 0)
{
cout << "Fehler Listdatei";
system("pause");
pause();
return;
}
PrimImplikantCollection* globalPIC = new PrimImplikantCollection();
vector<string>* variables = new vector<string>();
CParser parser;
parser.IP_init_token_table();
parser.InitParse(input, error, list);
if (parser.yyparse(globalPIC, variables) != 0)
{
system("pause");
return;
}
CParser* parser = new CParser(input, error, list);
bool parseFailure = parser->yyparse(globalPIC, variables) != 0;
fclose(input);
fclose(error);
fclose(list);
//system("pause");
/*pic.add(7);
pic.add("0x1");
pic.add("100");
pic.add("00x");
pic.add(4);
delete parser;
PrimImplikant prim7(7);
PrimImplikant prim13("0x1");
PrimImplikant prim4("100");
PrimImplikant prim4567("1xx");
pic.add(prim4567);*/
/*for (int p = 0; p < numElements; p++)
if (parseFailure)
{
//printf("Pos %d: prim7=%d, prim13=%d, prim4=%d, prim4567=%d, pic=%d\n", p, prim7.valueAt(p), prim13.valueAt(p), prim4.valueAt(p), prim4567.valueAt(p), pic.valueAt(p));
printf("Pos %d: Matching collections: ", p);
PrimImplikantCollection matchingPIs = globalPIC->primImplikantenAt(p);
for (int i = 0; i < matchingPIs.size(); i++)
//cout << i->name < ", ";
printf("%s, ", matchingPIs[i]->name.c_str());
cout << endl;
}*/
pause();
return;
}
// initialize Cells
vector<Cell*> cells;
cells.resize(numElements);
uint numOnes = 0;
for (uint i = 0; i < numElements; i++)
{
cells[i] = new Cell(i, globalPIC);
}
CellCollection* allCells = new CellCollection(globalPIC);
Wertetabelle* wt = new Wertetabelle(&cells, variables);
// print Wertetabelle and KV of imported data
Wertetabelle* wt = new Wertetabelle(allCells, variables);
wt->Print();
delete wt;
KV* kv = new KV(globalPIC, cells, 30);
KV* kv = new KV(globalPIC, allCells, 30);
kv->Print(30);
//system("pause");
// find hazards
for (uint i = 0; i < numElements; i += 2)
{
uint grayI = i ^ (i/2); // transform to gray code --> Schachbrettmuster
Cell* currentCell = cells[grayI]; // this is the cell we are currently checking
if (currentCell->value == false) // no hazard can occur
continue;
cout << " Checking cell " << grayI << endl;
vector<Cell*>* hazardousNeighbors = currentCell->getHazards(cells);
if (hazardousNeighbors->size() == 0) // no hazard found
{
delete hazardousNeighbors;
continue;
}
for (vector<Cell*>::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...
(*c)->refresh(globalPIC); // refresh the local PIC (a PI was added)
}
currentCell->refresh(globalPIC);
delete hazardousNeighbors;
}
// find and solve Hazards
allCells->findHazards();
//system("pause");
wt = new Wertetabelle(&cells, variables);
// print Wertetabelle and KV of corrected data
wt->Print();
delete wt;
kv->Print(30*2 + kv->width(), 30);
kv->Print(30 + kv->width() + 30, 30); // Diagramm neben dem vorherigen
delete kv;
globalPIC->Dispose();
//system("pause");
allCells->Dispose();
delete globalPIC;
delete allCells;
pause();
return;
}
int open_files(FILE * &input, FILE * &error, FILE * &list)
{
fopen_s(&input, "res\\input.txt", "r");
if (input == 0)
{
cout << "Fehler Inputdatei";
return 1;
}
fopen_s(&error, "res\\errorParser.txt", "a");
if (error == 0)
{
cout << "Fehler Fehlerdatei";
return 1;
}
fopen_s(&list, "res\\listParser.txt", "w");
if (list == 0)
{
cout << "Fehler Listdatei";
return 1;
}
}
void pause()
{
#if DEBUG
system("pause");
#endif
}

View file

@ -19,7 +19,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v100</PlatformToolset>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
@ -81,6 +81,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Cell.h" />
<ClInclude Include="CellCollection.h" />
<ClInclude Include="CParser.h" />
<ClInclude Include="PrimImplikant.h" />
<ClInclude Include="PrimImplikantCollection.h" />
@ -90,6 +91,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="Cell.cpp" />
<ClCompile Include="CellCollection.cpp" />
<ClCompile Include="CParser.cpp" />
<ClCompile Include="Hazard.cpp" />
<ClCompile Include="PrimImplikant.cpp" />

View file

@ -39,6 +39,9 @@
<ClInclude Include="Wertetabelle.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="CellCollection.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@ -62,6 +65,9 @@
<ClCompile Include="Cell.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="CellCollection.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\res\input.txt">

View file

@ -3,6 +3,7 @@
#include <string>
#include <WinGDI.h>
#include "Cell.h"
#include "CellCollection.h"
#include "PrimImplikantCollection.h"
extern uint dimension;
@ -18,7 +19,7 @@ public:
uint height(); // Gibt die Höhe eines KV-Diagramms zurück (heightPx)
// Konstruktor
KV(PrimImplikantCollection * globalPic, vector<Cell*> allCells, uint size)
KV(PrimImplikantCollection * globalPic, CellCollection allCells, uint size)
: edgeLength(size),
numVarX(((uint)floor(dimension/2.0f))), numVarY(((uint)ceil(dimension/2.0f))),
numFieldX((uint)pow(2,(float)numVarX)), numFieldY((uint)pow(2,(float)numVarY)),
@ -31,7 +32,7 @@ public:
private:
PrimImplikantCollection* globalPic;
vector<Cell*> allCells;
CellCollection allCells;
uint offsetX; // Der freie Platz nach links in Pixeln
uint offsetY; // Der freie Platz nach rechts in Pixeln
@ -53,7 +54,8 @@ private:
void Line(uint x1, uint y1, uint x2, uint y2, int color); // Zeichnet eine Linie mit Offset
void Text(uint x, uint y, uint size, int color, int bkcolor, int angle, int align, char* theText); // Zeichnet einen Text mit Offset
void KV::TextBox(uint x1, uint y1, uint x2, uint y2, uint size, int ctext, int cframe, int cfill, int flags, char* theText); // Zeichnet eine TextBox mit Offset
void KV::TextBoxBold(uint x1, uint y1, uint x2, uint y2, uint size, int ctext, int cframe, int cfill, int flags, char* theText); // Zeichnet eine TextBox mit Offset und fetter Schrift
void KV::TextBoxBold(uint x1, uint y1, uint x2, uint y2, uint size, int ctext, int cframe, int cfill, int flags, char* theText); // Zeichnet eine TextBox mit Offset und fetter Schrift
void KV::Rechteck(uint x1, uint y1, uint x2, uint y2, int cframe, int cfill); // Zeichnet ein Rechteck mit Offset und fetter Schrift
char* Binary(uint x, char length); // Wie itoa(x, char[], 2), allerdings mit fester Breite
};

View file

@ -2,9 +2,9 @@
#include <vector>
#include <string>
#include <iostream>
#include "Cell.h"
#include "PrimImplikant.h"
#include "Cell.h"
#include "CellCollection.h"
using namespace std;
@ -16,7 +16,7 @@ class Wertetabelle
public:
void Print();
Wertetabelle(vector<Cell*>* cells, vector<string>* variables)
Wertetabelle(CellCollection* cells, vector<string>* variables)
{
this->cells = cells;
this->variables = variables;
@ -28,7 +28,7 @@ private:
void printI(unsigned int i);
void printPrimImplikanten(unsigned int i);
vector<Cell*>* cells;
CellCollection* cells;
vector<string>* variables;
vector<float> padding;
uint width;