RINASim  October 2016
Documentation of framework for OMNeT++
GraphCL.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <omnetpp.h>
5 #include <limits>
6 #include <string>
7 #include <vector>
8 #include <set>
9 #include <map>
10 
11 #include "common/nhLMetric.h"
12 
13 namespace common_GraphCL {
14 
15 using namespace std;
16 
17 template <class T>
18 struct Link {
19  string dst;
20  T metric;
21  Link(const string & d, const T & m) : dst(d), metric(m) {}
22 };
23 
24 template <class T>
25 class GraphCL {
26 
27 protected:
28  struct TNode {
29  string name;
30  T metric;
31  set<TNode *> parents, childs;
32  bool known;
33 
34  TNode() : known(false) {}
35  };
36 
37 
38  map<string, vector<Link<T> > > links;
39  map<string, TNode > nodes;
40 
41 public:
42  void addNode(const string &n, const vector<Link<T> > &ls) {
43  links[n] = ls;
44  }
45 
46  void addLink(const string &n, const Link<T> &l) {
47  links[n].push_back(l);
48  }
49 
50  map<string, nhLMetric<T> > getNextHops (const string & root, const T & infMetric) {
51  computeMinG(infMetric, root);
52 
53  map<string, nhLMetric<T> > ret;
54 
55  for(TNode * n : nodes[root].childs){
56  ret[n->name].metric = n->metric;
57  ret[n->name].nh.insert(n->name);
58  recursiveLook(ret, n->name, n->name);
59  }
60 
61  return ret;
62  }
63 
64  void print(const string & root, const T & infMetric) {
65 
66  map<string, nhLMetric<T> > res = getNextHops (root, infMetric);
67 
68  EV << "Print tree : " << endl;
69  EV << "" << root << " (0) " << endl;
70 
71  for(TNode * n : nodes[root].childs){
72  printRecursive(n->name, 0);
73  }
74 
75  EV << endl;
76  EV << "Print nextHops : "<< endl;
77  for(auto n : res) {
78  EV << n.first << " (" << n.second.metric << ") ::";
79  for (string d : n.second.nh) {
80  EV << " <" << d << ">";
81  }
82  EV << endl;
83  }
84  }
85 
86 protected:
87 
88  void computeMinG(const T & infMetric, const string & root) {
89  nodes.clear();
90 
91  for(const auto & vl : links) {
92  nodes[vl.first].name = vl.first;
93  nodes[vl.first].metric = infMetric;
94  nodes[vl.first].known = true;
95  for(const Link<T> & l : vl.second) {
96  nodes[l.dst].name = l.dst;
97  nodes[l.dst].metric = infMetric;
98  }
99  }
100 
101  set<TNode *> waiting;
102 
103  nodes[root].metric = 0;
104  waiting.insert(&nodes[root]);
105 
106  while(!waiting.empty()) {
107  T minCost = USHRT_MAX;
108  set<TNode *> minSet;
109 
110  for(TNode * n : waiting) {
111  if(n->metric < minCost) {
112  minCost = n->metric;
113  minSet.clear();
114  }
115  if(n->metric == minCost){
116  minSet.insert(n);
117  }
118  }
119  for(TNode * n : minSet) {
120  waiting.erase(n);
121 
122  for(TNode * p : n->parents){
123  p->childs.insert(n);
124  }
125 
126  if(n->known){
127  for(const Link<T> & l : links[n->name]) {
128  TNode * d = &nodes[l.dst];
129  T tCost = minCost + l.metric;
130  if(d->metric > tCost){
131  d->metric = tCost;
132  d->parents.clear();
133  }
134  if(d->metric == tCost){
135  d->parents.insert(n);
136  waiting.insert(d);
137  }
138  }
139  }
140  }
141  }
142  }
143 
144  void recursiveLook(map<string, nhLMetric<T> > & ret, const string & node, const string & src) {
145  for(TNode * n : nodes[node].childs){
146  ret[n->name].metric = n->metric;
147  ret[n->name].nh.insert(src);
148  recursiveLook(ret, n->name, src);
149  }
150  }
151 
152  void printRecursive(const string & node, const int & index) {
153  for(int i = 0; i< index; i++) { EV << " "; }
154 
155  EV << "|-" << node << " ("<< nodes[node].metric << ") " << endl;
156 
157  for(TNode * n : nodes[node].childs){
158  printRecursive(n->name, index + 1);
159  }
160  }
161 
162 };
163 
164 } /* namespace common_GraphCL */
map< string, nhLMetric< T > > getNextHops(const string &root, const T &infMetric)
Definition: GraphCL.h:50
map< string, TNode > nodes
Definition: GraphCL.h:39
void printRecursive(const string &node, const int &index)
Definition: GraphCL.h:152
void print(const string &root, const T &infMetric)
Definition: GraphCL.h:64
void computeMinG(const T &infMetric, const string &root)
Definition: GraphCL.h:88
set< TNode * > parents
Definition: GraphCL.h:31
void addLink(const string &n, const Link< T > &l)
Definition: GraphCL.h:46
map< string, vector< Link< T > > > links
Definition: GraphCL.h:38
void addNode(const string &n, const vector< Link< T > > &ls)
Definition: GraphCL.h:42
void recursiveLook(map< string, nhLMetric< T > > &ret, const string &node, const string &src)
Definition: GraphCL.h:144