From b2ccc3aa98381a83e240a6636cb38b9e169f7003 Mon Sep 17 00:00:00 2001 From: Daniel Knittl-Frank Date: Sat, 2 Oct 2010 17:55:28 +0200 Subject: [PATCH] Start hacking on a C(++) implementation of the optimizer start easy by porting recheck_all_result_layouts.py to C++ --- C/.gitignore | 4 ++++ C/File.cpp | 25 +++++++++++++++++++++ C/Layout.cpp | 24 +++++++++++++++++++++ C/Makefile | 6 ++++++ C/recheck_layouts.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 C/.gitignore create mode 100644 C/File.cpp create mode 100644 C/Layout.cpp create mode 100644 C/Makefile create mode 100644 C/recheck_layouts.cpp diff --git a/C/.gitignore b/C/.gitignore new file mode 100644 index 0000000..cecbd49 --- /dev/null +++ b/C/.gitignore @@ -0,0 +1,4 @@ +*~ +*.o + +*.py diff --git a/C/File.cpp b/C/File.cpp new file mode 100644 index 0000000..cb183aa --- /dev/null +++ b/C/File.cpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +class File { +public: + File(const std::string &name) + : _stream(name.c_str()) { + if(!_stream.is_open()) { + //throw new std::runtime_error("Error opening file"); + } + } + virtual ~File() { + this->_stream.close(); + } + + std::fstream &stream() { + return this->_stream; + } +private: + std::fstream _stream; + // no copying + File(const File &); + File &operator=(const File &); +}; diff --git a/C/Layout.cpp b/C/Layout.cpp new file mode 100644 index 0000000..10c7113 --- /dev/null +++ b/C/Layout.cpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +class Layout { +public: + Layout(const std::string &s = "") { + this->layout.reserve(11*3+4); // 11 chars per row average + newlines + this->layout = s; + } + virtual ~Layout() {} + //Layout(const Layout &l) {} + //Layout &operator=(const Layout &l) {} + + void addLine(const std::string &s) { + this->layout.append(s+"\n"); + } + + const std::string &toString() const { + return this->layout; + } +private: + std::string layout; +}; diff --git a/C/Makefile b/C/Makefile new file mode 100644 index 0000000..ce735e4 --- /dev/null +++ b/C/Makefile @@ -0,0 +1,6 @@ +recheck_layouts: recheck_layouts.cpp File.cpp Layout.cpp + g++ $< -o $@ + +all: recheck_all_result_layouts + +.PHONY: all diff --git a/C/recheck_layouts.cpp b/C/recheck_layouts.cpp new file mode 100644 index 0000000..453c70f --- /dev/null +++ b/C/recheck_layouts.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +#include "Layout.cpp" +#include "File.cpp" + +using namespace std; + +typedef list layoutlist; + +// return a list of all layouts in the file +// TODO better memory management +layoutlist getAllLayouts(const string &file) { + layoutlist l; + File f(file); + string line; + + while(f.stream().good()) { + getline(f.stream(), line); + if(line == "# Evolved Layout ") { + Layout layout; + int lines = 3; + while(lines-- && f.stream().good()) { + getline(f.stream(), line); + layout.addLine(line); + } + l.push_back(layout); + } + } + + return l; +} + + +// TODO implement +layoutlist getAllLayoutsInTextFilesIn(const string &folder = "results") { + throw new string("Not Implemented"); + layoutlist l; + return l; +} + +int main(int argc, char **argv) { + // check options + if(argc < 2) { + cerr << "Usage: " << argv[0] << " " << endl; + return 1; + } + + string file(argv[1]); + layoutlist l = getAllLayouts(file); + + // debugging output + for(layoutlist::const_iterator it = l.begin(); + it != l.end(); ++it) { + cout << "Evolved: " << endl << it->toString() << endl; + } + + return 0; +} -- 2.11.4.GIT