about summary refs log tree commit diff stats
path: root/src/network_set.cpp
blob: 45911e3dc9cb9a834396f5caf03b88b086c1ad4c (plain) (blame)
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
#include "network_set.h"

void NetworkSet::Clear() {
  network_by_item_.clear();
}

void NetworkSet::AddLink(int id1, int id2, bool two_way) {
  if (two_way && id2 > id1) {
    std::swap(id1, id2);
  }

  if (!network_by_item_.count(id1)) {
    network_by_item_[id1] = {};
  }
  if (!network_by_item_.count(id2)) {
    network_by_item_[id2] = {};
  }

  NetworkNode node = {id1, id2, two_way};

  network_by_item_[id1].insert(node);
  network_by_item_[id2].insert(node);
}

void NetworkSet::AddLinkToNetwork(int network_id, int id1, int id2, bool two_way) {
  if (two_way && id2 > id1) {
    std::swap(id1, id2);
  }

  if (!network_by_item_.count(network_id)) {
    network_by_item_[network_id] = {};
  }

  NetworkNode node = {id1, id2, two_way};

  network_by_item_[network_id].insert(node);
}

bool NetworkSet::IsItemInNetwork(int id) const {
  return network_by_item_.count(id);
}

const std::set<NetworkNode>& NetworkSet::GetNetworkGraph(int id) const {
  return network_by_item_.at(id);
}

bool NetworkNode::operator<(const NetworkNode& rhs) const {
  if (entry != rhs.entry) return entry < rhs.entry;
  if (exit != rhs.exit) return exit < rhs.exit;
  if (two_way != rhs.two_way) return two_way < rhs.two_way;
  return false;
}