Edge.cpp 2.16 KB
Newer Older
Jonas Seidel's avatar
Jonas Seidel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include "Edge.h"

Edge::Edge(const Edge& remote_edge, const std::map<Node*, Node*>& node_lookup) : _description(remote_edge._description), _from(node_lookup.find(remote_edge._from)->second), _to(node_lookup.find(remote_edge._to)->second), _attributes(remote_edge._attributes) {
  this->_from->add_incident(this);
  this->_to->add_incident(this);
}

Edge::Edge(Node* from, Node* to, std::string description, std::map<std::string, Attribute> attributes) : _description(std::move(description)), _from(from), _to(to), _attributes(std::move(attributes)){
  from->add_incident(this);
  to->add_incident(this);
};

Edge::Edge(std::istream& is, const std::map<std::string, Node* >& name_lookup){
  std::string curr;
  is >> curr;
  assert(curr == "Edge");

  is >> this->_description;

  is >> curr;
  assert(curr == "(");
  is >> curr;
  while(curr != ")"){
    std::string attribute = curr;
    is >> curr;
    assert(curr == "=");
    is >> curr;
    this->_attributes.insert({attribute, {fix, std::stod(curr), Continuous}});
    is >> curr;
  }
  is >> curr;
  assert(curr == "{");
  is >> curr;
  this->_from = name_lookup.find(curr)->second;
  this->_from->add_incident(this);
  is >> curr;
  this->_to = name_lookup.find(curr)->second;
  this->_to->add_incident(this);
  is >> curr;
  assert(curr == "}");
}

void Edge::disconnect(){
  this->from()->incident().erase(this);
  this->to()->incident().erase(this);
}

void Edge::reconnect(){
  this->from()->incident().insert(this);
  this->to()->incident().insert(this);
}

std::ostream& operator<<(std::ostream& os, Edge& e){
  if(&os == &std::cout){
    os << "\033[0;32m";
    os << "Edge " << e.description() << " ( ";
    for(std::pair<std::string, Attribute> pair : e.attributes()){
      os << pair.first << " = " << pair.second.value() << ", ";
    }
    os << ") {" << e.from()->description() << ", " << e.to()->description() << "}";
    os << "\033[0m";
  }else{
    os << "Edge " << e.description() << " ( ";
    for(std::pair<std::string, Attribute> pair : e.attributes()){
      os << pair.first << " = " << pair.second.value() << " ";
    }
    os << " ) { " << e.from()->description() << " " << e.to()->description() << " }";
  }


  return os;
}