Spatial
Simple Static Analysis in LLVM
Graph.h
Go to the documentation of this file.
1#ifndef GRAPH_H
2#define GRAPH_H
3
4#include "iostream"
5#include "map"
6#include "set"
7#include "vector"
8
9namespace spatial {
10
11template <typename Node> class Graph {
12private:
13 std::map<Node *, std::set<Node *>> GraphBase;
14
15public:
16 using GraphType = std::map<Node *, std::set<Node *>>;
17 using iterator = typename GraphType::iterator;
18
19 bool hasEdgeBetween(Node *, Node *);
20 void insert(Node *, Node *, int, int);
21 bool insert(Node *, Node *);
22 void insert(Node *, std::set<Node *>);
23 std::set<Node *> getPointee(Node *);
24 Node *getUniquePointee(Node *);
25 void merge(std::vector<Graph<Node>> Graphs);
26 void erase(Node *);
27 iterator begin() { return GraphBase.begin(); }
28 iterator end() { return GraphBase.end(); }
29 bool operator==(const Graph<Node> &TheGarph) const;
30 bool operator<(const Graph<Node> &TheGarph) const;
31 template <typename GraphNode>
32 friend std::ostream &operator<<(std::ostream &OS, const Graph<GraphNode> &G);
33};
34
38template <typename Node>
39bool Graph<Node>::hasEdgeBetween(Node *Src, Node *Dest) {
40 if (GraphBase.find(Src) == GraphBase.end())
41 return false;
42 auto Pointee = this->getPointee(Src);
43 return (Pointee.find(Dest) != Pointee.end());
44}
45
50template <typename Node>
51void Graph<Node>::insert(Node *Src, Node *Dest, int Left, int Right) {
52 if (Left == 1 && Right == 1) {
53 for (auto P : this->getPointee(Dest)) {
54 this->insert(Src, P);
55 }
56 } else if (Left == 2 && Right == 1) {
57 for (auto S : this->getPointee(Src)) {
58 for (auto D : this->getPointee(Dest)) {
59 this->insert(S, D);
60 }
61 }
62 } else if (Left == 1 && Right == 0) {
63 this->insert(Src, Dest);
64 } else if (Left == 1 && Right == 2) {
65 for (auto D : this->getPointee(Dest)) {
66 for (auto DestDest : this->getPointee(D)) {
67 this->insert(Src, DestDest);
68 }
69 }
70 }
71}
72
75template <typename Node> bool Graph<Node>::insert(Node *Src, Node *Dest) {
76 if (this->hasEdgeBetween(Src, Dest))
77 return false;
78 this->GraphBase[Src].insert(Dest);
79 return true;
80}
81
83template <typename Node>
84void Graph<Node>::insert(Node *N, std::set<Node *> Pointee) {
85 if (this->GraphBase.find(N) == this->GraphBase.end()) {
86 this->GraphBase[N] = Pointee;
87 } else {
88 this->GraphBase[N].insert(Pointee.begin(), Pointee.end());
89 }
90}
91
94template <typename Node> std::set<Node *> Graph<Node>::getPointee(Node *N) {
95 std::set<Node *> PointeeSet;
96 if (this->GraphBase.find(N) != this->GraphBase.end())
97 PointeeSet = this->GraphBase[N];
98 return PointeeSet;
99}
100
103template <typename Node> Node *Graph<Node>::getUniquePointee(Node *N) {
104 Node *Pointee = nullptr;
105 if (this->GraphBase.find(N) != this->GraphBase.end())
106 if (this->GraphBase[N].size() == 1)
107 Pointee = *((this->GraphBase[N]).begin());
108 return Pointee;
109}
110
111template <typename Node>
112bool Graph<Node>::operator==(const Graph<Node> &TheGraph) const {
113 return this->GraphBase.size() == TheGraph.GraphBase.size() &&
114 std::equal(this->GraphBase.begin(), this->GraphBase.end(),
115 TheGraph.GraphBase.begin());
116}
117
118template <typename Node>
119bool Graph<Node>::operator<(const Graph<Node> &TheGraph) const {
120 return this->GraphBase.size() < TheGraph.GraphBase.size();
121}
122
123template <typename Node>
124std::ostream &operator<<(std::ostream &OS, const Graph<Node> &G) {
125 for (auto X : G.GraphBase) {
126 OS << *(X.first) << " -> {";
127 for (auto Y : X.second) {
128 OS << *(Y) << ", ";
129 }
130 OS << "}\n";
131 }
132 return OS;
133}
134
136template <typename Node> void Graph<Node>::erase(Node *N) {
137 this->GraphBase[N].clear();
138}
139
141template <typename Node>
142void Graph<Node>::merge(std::vector<Graph<Node>> Maps) {
143 for (auto Map : Maps) {
144 for (auto N : Map.GraphBase) {
145 if (this->GraphBase.find(N.first) == this->GraphBase.end()) {
146 this->GraphBase[N.first] = N.second;
147 } else {
148 this->GraphBase[N.first].insert(N.second.begin(), N.second.end());
149 }
150 }
151 }
152}
153
154} // namespace spatial
155#endif
Definition: Graph.h:11
Node * getUniquePointee(Node *)
Definition: Graph.h:103
typename GraphType::iterator iterator
Definition: Graph.h:17
void merge(std::vector< Graph< Node > > Graphs)
merge - Merges the Graphs from Graphs
Definition: Graph.h:142
friend std::ostream & operator<<(std::ostream &OS, const Graph< GraphNode > &G)
bool operator==(const Graph< Node > &TheGarph) const
Definition: Graph.h:112
void erase(Node *)
erase - Erases the node Node
Definition: Graph.h:136
std::set< Node * > getPointee(Node *)
Definition: Graph.h:94
bool operator<(const Graph< Node > &TheGarph) const
Definition: Graph.h:119
void insert(Node *, Node *, int, int)
Definition: Graph.h:51
iterator end()
Definition: Graph.h:28
iterator begin()
Definition: Graph.h:27
std::map< Node *, std::set< Node * > > GraphType
Definition: Graph.h:16
bool hasEdgeBetween(Node *, Node *)
hasEdgeBetween - Returns true if there is a edge between Src and Dest
Definition: Graph.h:39
Definition: PointsToBenchmark.cpp:19
std::ostream & operator<<(std::ostream &OS, const PointsToBenchmarkRunner &B)
Definition: PointsToBenchmark.cpp:49