Commit 4230e6f1 authored by Jonas Seidel's avatar Jonas Seidel

Proper Attribute scanning in >> operators

parent 0de0ab72
#include "Attribute.h"
Attribute::Attribute(const Attribute& attr) : _dir(attr._dir), _integrality(attr._integrality), _value(attr._value) {}
Attribute::Attribute(Attribute&& attr) : _dir(std::move(attr._dir)), _integrality(std::move(attr._integrality)), _value(std::move(attr._value)) {}
Attribute::Attribute(opt_dir optimization_direction, double attribute, integrality integrality)
: _dir(optimization_direction), _integrality(integrality), _value(attribute) {}
Attribute::Attribute(std::istream& is){
std::string curr;
is >> curr;
if(curr != "Attribute") goto failed;
is >> curr;
if(curr != "{") goto failed;
is >> curr;
if(curr == "max") {
this->_dir = max;
}else if(curr == "fix"){
this->_dir = fix;
}else if (curr == "min"){
this->_dir = min;
}else{
goto failed;
}
is >> curr;
if(curr == "Integral"){
this->_integrality = Integral;
}else if(curr == "Continuous"){
this->_integrality = Continuous;
}else{
goto failed;
}
is >> curr;
this->_value = std::stod(curr);
is >> curr;
if(curr != "}") goto failed;
return;
failed:
throw std::invalid_argument("bad format (Attribute::Attribute(std::istream&))");
}
void Attribute::operator=(const Attribute& attr){
this->_dir = attr._dir;
this->_integrality = attr._integrality;
this->_value = attr._value;
}
void Attribute::operator=(Attribute&& attr){
this->_dir = std::move(attr._dir);
this->_integrality = std::move(attr._integrality);
this->_value = std::move(attr._value);
}
const opt_dir& Attribute::optimization_direction() const {
return this->_dir;
}
opt_dir& Attribute::optimization_direction(){
return this->_dir;
}
bool Attribute::is_dependent(){
const bool& Attribute::is_dependent() const{
return this->_dependent;
}
bool& Attribute::is_dependent(){
return this->_dependent;
}
const integrality& Attribute::is_integral() const {
return this->_integrality;
}
integrality& Attribute::is_integral(){
return this->_integrality;
}
const double& Attribute::value() const {
return this->_value;
}
double& Attribute::value(){
return this->_value;
}
std::ostream& operator<<(std::ostream& os, const Attribute& attr){
os << "Attribute { ";
switch (attr._dir) {
case max:
os << "max ";
break;
case fix:
os << "fix ";
break;
case min:
os << "min ";
break;
default:
throw std::range_error("enum value is not defined for opt_dir (Attribute::operator<<)");
}
switch (attr._integrality) {
case Integral:
os << "Integral ";
break;
case Continuous:
os << "Continuous ";
break;
default:
throw std::range_error("enum value is not defined for integrality (Attribute::operator<<)");
}
os << attr._value << " }";
return os;
}
std::istream& operator>>(std::istream& is, Attribute& attr){ attr = Attribute(is); return is;}
#ifndef ATTRIBUTE_H
#define ATTRIBUTE_H
#include <string>
#include <iostream>
#include <stdexcept>
#include "../Common/integrality.h"
enum opt_dir : int {max = 1, fix = 0, min = -1};
......@@ -11,12 +15,29 @@ class Attribute{
integrality _integrality;
double _value;
public:
Attribute(const Attribute& attr);
Attribute(Attribute&& attr);
Attribute(opt_dir optimization_direction, double attribute, integrality integrality = Continuous);
Attribute(std::istream& is);
void operator=(const Attribute& attr);
void operator=(Attribute&& attr);
const opt_dir& optimization_direction() const;
opt_dir& optimization_direction();
bool is_dependent();
const bool& is_dependent() const;
bool& is_dependent();
const integrality& is_integral() const;
integrality& is_integral();
const double& value() const;
double& value();
friend std::ostream& operator<<(std::ostream& os, const Attribute& attr);
friend std::istream& operator>>(std::istream& os, Attribute& attr);
};
std::ostream& operator<<(std::ostream& os, const Attribute& attr);
std::istream& operator>>(std::istream& os, Attribute& attr);
#endif
......@@ -21,11 +21,11 @@ Edge::Edge(std::istream& is, const std::map<std::string, Node* >& name_lookup){
assert(curr == "(");
is >> curr;
while(curr != ")"){
std::string attribute = curr;
std::string attr_name = curr;
is >> curr;
assert(curr == "=");
is >> curr;
this->_attributes.insert({attribute, {fix, std::stod(curr), Continuous}});
Attribute attr(is);
this->_attributes.insert({attr_name, attr});
is >> curr;
}
is >> curr;
......@@ -62,9 +62,9 @@ std::ostream& operator<<(std::ostream& os, Edge& e){
}else{
os << "Edge " << e.description() << " ( ";
for(std::pair<std::string, Attribute> pair : e.attributes()){
os << pair.first << " = " << pair.second.value() << " ";
os << pair.first << " = " << pair.second << " ";
}
os << " ) { " << e.from()->description() << " " << e.to()->description() << " }";
os << ") { " << e.from()->description() << " " << e.to()->description() << " }";
}
......
......@@ -11,11 +11,11 @@ Node::Node(std::istream& is){
assert(curr == "(");
is >> curr;
while(curr != ")"){
std::string attribute = curr;
std::string attr_name = curr;
is >> curr;
assert(curr == "=");
is >> curr;
this->_attributes.insert({attribute, {fix, std::stod(curr), Continuous}});
Attribute attr(is);
this->_attributes.insert({attr_name, attr});
is >> curr;
}
is >> curr;
......@@ -69,9 +69,10 @@ std::ostream& operator<<(std::ostream& os, Node& n){
}else{
os << "Node " << n.description() << " ( ";
for(std::pair<std::string, Attribute> pair : n.attributes()){
os << pair.first << " = " << pair.second.value() << ", ";
os << pair.first << " = " << pair.second << " ";
}
os << " ) {\n \tEdges {\n";
os << ") {\n \tEdges {\n";
for(Edge* e : n.incident()){
os << *e << "\n";
}
......
......@@ -6,6 +6,8 @@
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <string>
#include <iostream>
#include "Attribute.h"
#include "Edge.h"
......
CXXFLAGS := -O0 -g -std=c++17 #-Wall -Wextra -Wpedantic
CXXFLAGS := -O0 -g -std=c++2a #-Wall -Wextra -Wpedantic
GRAPH_DEP := $(patsubst %.cpp,%.o,$(shell find -type f -wholename "./Graphtheory/*"))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment