Commit 28185801 authored by Jonas Seidel's avatar Jonas Seidel

Special members and operators (assortment)

parent c8feba89
......@@ -23,6 +23,8 @@ public:
// TODO: use references to speed up constructors
Graph(){};
Graph(std::map<E,Attribute> default_values_edge_attributes, std::map<N,Attribute> default_values_node_attributes = {});
Graph(Graph<N,E>& graph);
Graph(Graph<N,E>&& graph);
Edge<N,E>* add_edge(Node<N, E>* from, Node<N, E>* to, std::map<E,Attribute> edge_attributes = {});
Edge<N,E>* add_edge(Node<N, E>* from, Node<N, E>* to, std::map<E,double> edge_attributes);
......@@ -60,6 +62,9 @@ public:
// _advanced.cpp:
void conditional_bfs_all_components(std::function<void(Node<N,E>* from, Edge<N,E>* via, bool used_in_traversal)> edge_exec, std::function<void(Node<N,E>*)> node_exec,std::function<bool(Node<N,E>* from, Edge<N,E>* via)> guide = [](Node<N,E>* n, Edge<N,E>* e)->bool {return e->from() == n;});
// _special_members_and_operators:
void operator=(Graph<N,E>& graph);
void operator=(Graph<N,E>&& graph);
};
template <typename N, typename E>
......@@ -67,5 +72,6 @@ std::ostream& operator<<(std::ostream& os, Graph<N,E>& g);
#include "Basic_Graph.ipp"
#include "Basic_Graph_advanced.ipp"
#include "Basic_Graph_special_members_and_operators.ipp"
#endif
template <typename E_N, typename E_E>
Graph<E_N,E_E>::Graph(std::map<E_E,Attribute> default_values_edge_attributes, std::map<E_N,Attribute> default_values_node_attributes) : _template_node_attributes(default_values_node_attributes), _template_edge_attributes(default_values_edge_attributes){}
template <typename N, typename E>
Graph<N,E>::Graph(std::map<E,Attribute> default_values_edge_attributes, std::map<N,Attribute> default_values_node_attributes) : _template_node_attributes(default_values_node_attributes), _template_edge_attributes(default_values_edge_attributes){}
template <typename N, typename E>
Edge<N,E>* Graph<N,E>::add_edge(Node<N, E>* from, Node<N, E>* to, std::map<E,Attribute> edge_attributes){
......
template <typename N, typename E>
Graph<N,E>::Graph(Graph<N,E>& graph){
*this = graph;
}
template <typename N, typename E>
Graph<N,E>::Graph(Graph<N,E>&& graph)
: _edges(std::move(graph._edges)), _nodes(std::move(graph._nodes)),
_template_node_attributes(std::move(graph._template_node_attributes)), _template_edge_attributes(std::move(graph._template_edge_attributes)) {
graph._nodes = {};
graph._edges = {};
graph._template_node_attributes = {};
graph._template_edge_attributes = {};
}
template <typename N, typename E>
void Graph<N,E>::operator=(Graph<N,E>& graph){
std::map<Node<N,E>*, Node<N,E>*> node_lookup;
graph.conditional_bfs_all_components(
[&node_lookup, this](Node<N,E>* node, Edge<N,E>* edge, bool used_in_traversal){
this->add_edge(edge, node_lookup);
},
[&node_lookup, this](Node<N,E>* node){
Node<N,E>* new_node = this->add_node(node);
node_lookup.insert({node, new_node});
}
);
}
template <typename N, typename E>
void Graph<N,E>::operator=(Graph<N,E>&& graph){
this->_edges = std::move(graph._edges);
this->_nodes = std::move(graph._nodes);
this->_template_node_attributes = std::move(graph._template_node_attributes);
this->_template_edge_attributes = std::move(graph._template_edge_attributes);
graph._nodes = {};
graph._edges = {};
graph._template_node_attributes = {};
graph._template_edge_attributes = {};
}
#include "Linear_Program.h"
Linear_Program::Linear_Program(std::vector<double> direction, Polyeder& polyeder)
: _direction(direction), _polyeder(polyeder){}
Linear_Program::Linear_Program(Linear_Program& lp){
*this = lp;
}
Linear_Program::Linear_Program(Linear_Program&& lp){
*this = std::move(lp);
}
Linear_Program::Linear_Program(bool maximum) : _maximum(maximum) {}
Linear_Program::Linear_Program(bool maximum, size_t direction_start, std::vector<double> direction, Polyeder& polyeder)
: _maximum(maximum), _direction_start(direction_start), _direction(direction), _polyeder(polyeder) {}
bool Linear_Program::is_maximum(){
return this->_maximum;
......@@ -19,6 +29,20 @@ std::vector<double> Linear_Program::direction(){
return this->_direction;
}
void Linear_Program::operator=(Linear_Program& lp){
this->_maximum = lp._maximum;
this->_direction_start = lp._direction_start;
this->_direction = lp._direction;
this->_polyeder = lp._polyeder;
}
void Linear_Program::operator=(Linear_Program&& lp){
this->_maximum = lp._maximum;
this->_direction_start = lp._direction_start;
this->_direction = std::move(lp._direction);
this->_polyeder = std::move(lp._polyeder);
}
std::ostream& operator<<(std::ostream& os, Linear_Program& linear_program){
if(linear_program.is_maximum()){
os << "Maximize\n";
......
......@@ -11,12 +11,17 @@ class Linear_Program{
std::vector<double> _direction;
Polyeder _polyeder;
public:
Linear_Program(std::vector<double> direction, Polyeder& polyeder);
Linear_Program(Linear_Program& lp);
Linear_Program(Linear_Program&& lp);
Linear_Program(bool maximum);
Linear_Program(bool maximum, size_t direction_start, std::vector<double> direction, Polyeder& polyeder);
bool is_maximum();
double direction_coefficient(size_t index);
Polyeder& polyeder();
std::vector<double> direction();
void operator=(Linear_Program& lp);
void operator=(Linear_Program&& lp);
};
std::ostream& operator<<(std::ostream& os, Linear_Program& linear_program);
......
......@@ -2,9 +2,13 @@
Polyeder::Polyeder(){}
Polyeder::Polyeder(const Polyeder& p){
*this = p;
}
Polyeder::Polyeder(std::vector<Constraint> constraints, std::vector<Variable> variables) : _constraints(constraints), _variables(variables) {
for(Constraint i : this->_constraints){
assert(i.lhs_last_variable() == variables.size());
for(Constraint c : this->_constraints){
assert(c.lhs_end() == variables.size());
}
}
......@@ -43,6 +47,16 @@ void Polyeder::add_constraint(Constraint constraint){
this->_constraints.push_back(constraint);
}
void Polyeder::operator=(const Polyeder& p){
this->_constraints = p._constraints;
this->_variables = p._variables;
}
void Polyeder::operator=(Polyeder&& p){
this->_constraints = std::move(p._constraints);
this->_variables = std::move(p._variables);
}
std::ostream& operator<<(std::ostream& os, Polyeder& polyeder){
os << "Subject to\n";
for(size_t index = 0; index < polyeder.number_of_constraints(); index++) {
......
......@@ -13,6 +13,7 @@ class Polyeder{
std::vector<Variable> _variables;
public:
Polyeder();
Polyeder(const Polyeder& p);
Polyeder(std::vector<Constraint> constraints, std::vector<Variable> variables);
......@@ -25,6 +26,9 @@ public:
size_t number_of_constraints();
Constraint& constraint(size_t index);
void add_constraint(Constraint inequality);
void operator=(const Polyeder& p);
void operator=(Polyeder&& p);
};
std::ostream& operator<<(std::ostream& os, Polyeder& polyeder);
......
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