Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jron53 committed Feb 3, 2018
1 parent bca84ad commit 984217d
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.o
*.a
*.obj
*.exe
*.lib
*.dll
*.depend
*.layout
29 changes: 29 additions & 0 deletions Graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include"Graph.h"
#include"util.h"
namespace Ron
{
void Graph::add_vertex(const std::string& name, const R& x, const R& y, const R& z)
{
Vertex *v = new Vertex(name, x, y, z);
auto it = vertices.find(name);
if (it != vertices.end())
{
panic("duplicate vertex name");
}
vertices[name] = v;
}
void Graph::add_edge(const std::string& from, const std::string& to)
{
auto it_1 = vertices.find(from);
auto it_2 = vertices.find(to);
if(it_1 == vertices.end() || it_2 == vertices.end() )
{
panic("no such vertex exists!!!");
}

Edge* e = new Edge (it_1->second, it_2->second);

it_1->second->add_edge(e);
it_2->second->add_edge(e);
}
}
60 changes: 60 additions & 0 deletions Graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef GRAPH_H
#define GRAPH_H

#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>

#include <boost/multiprecision/gmp.hpp>

namespace Ron
{
using R = boost::multiprecision::mpq_rational;

class Vertex;
class Edge;
class Graph
{
public:
Graph() {};
~Graph() {};
void add_vertex (const std::string& name, const R& x, const R& y, const R& z);
void add_edge (const std::string& from, const std::string& to);
void read(const std::string& filename);
private:

std::unordered_map<std::string, Vertex*> vertices;
std::vector<Edge*> edges;
};

class Vertex
{
public:
Vertex(const std::string& label,const R& x, const R& y, const R& z) : label(label)
{
coord[0] = x;
coord[1] = y;
coord[2] = z;
}
void add_edge(Edge* e)
{
edges.push_back(e);
}

private:
R coord[3];
std::vector<Edge*> edges;
std::string label;
};

class Edge
{
public:
Edge(Vertex* p1, Vertex* p2) : p1(p1), p2(p2) {}
private:
Vertex *p1, *p2;
};
}

#endif // GRAPH_H
56 changes: 56 additions & 0 deletions Polyhedra.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Polyhedra" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Polyhedra" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-std=c++11" />
<Add option="-g" />
<Add directory="D:/User/Projects/boost_1_66_0/include/boost-1_66" />
<Add directory="D:/User/Projects/gmp/include" />
</Compiler>
<Linker>
<Add library="libgmp.a" />
<Add directory="D:/User/Projects/gmp/lib" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/Polyhedra" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="Graph.cpp" />
<Unit filename="Graph.h" />
<Unit filename="cube.poly" />
<Unit filename="main.cpp" />
<Unit filename="readgraph.cpp" />
<Unit filename="util.cpp" />
<Unit filename="util.h" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
23 changes: 23 additions & 0 deletions cube.poly
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Vertices
one 0 0 0
two 0 0 1
three 0 1 0
four 0 1 1
five 1 0 0
six 1 0 1
seven 1 1 0
eight 1 1 1
Edges
one two
one three
one five
two four
two six
three four
three seven
four eight
five six
five seven
six eight
seven eight

20 changes: 20 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

#include <boost/multiprecision/gmp.hpp>

#include "Graph.h"


using namespace std;

using R = boost::multiprecision::mpq_rational;

int main(int argc, char** argv)
{
for (int i = 1; i < argc; ++i)
{
Ron::Graph g;
g.read(argv[i]);
}
return 0;
}
62 changes: 62 additions & 0 deletions readgraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "Graph.h"
#include "util.h"

namespace Ron {

void Graph::read (const std::string& filename)
{
std::ifstream input (filename.c_str());
if (!input)
{
panic ("Could not open input file");
}

std::string line;

enum state { vertices, edges };

while (std::getline(input, line))
{
state st = vertices;
if (line == "")
continue;
else if (line == "Vertices")
st = vertices;
else if (line == "Edges")
st = edges;
else if (st == vertices)
{
std::istringstream is{line};
std::string name;
R x, y, z;
if (is >> name >> x >> y >> z)
{
add_vertex(name, x, y, z);
}
else
{
panic ("Error reading vertex");
}
}
else
{
std::istringstream is{line};
std::string name1, name2;

if (is >> name1 >> name2)
{
add_edge(name1, name2);
}
else
{
panic ("Error reading edge");
}
}
}

}

} // Ron
13 changes: 13 additions & 0 deletions util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <cstdlib>
#include <iostream>
#include "util.h"


namespace Ron
{
void panic (std::string what)
{
std::cerr << "Fatal error " << what << std::endl;
std::exit(EXIT_FAILURE);
}
}
8 changes: 8 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef UTIL_H
#define UTIL_H

namespace Ron
{
void panic (std::string what);
}
#endif // UTIL_H

0 comments on commit 984217d

Please sign in to comment.