-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDGExtractor.h
103 lines (92 loc) · 3.05 KB
/
PDGExtractor.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2013 The University of Michigan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors - Chun-Hung Hsiao ([email protected])
//
#ifndef PDGEXTRACTOR_H
#define PDGEXTRACTOR_H
#include <map>
#include <utility>
#include <vector>
#include "CanonicalAst.h"
#include "DependenceGraph.h"
#include "NgramExtractor.h"
#include "Utility.h"
using std::map;
using std::pair;
using std::string;
using std::vector;
class PDGExtractor : public NgramExtractor {
public:
template <class Compare> PDGExtractor(const DependenceGraph &graph, Compare cmp, size_t limit)
: graph_(graph), size_limit_(limit) {
list<Statement*> nodes;
for (key_iterator<DependenceGraph> i = graph_.begin(); i != graph_.end(); ++i)
nodes.push_back(*i);
nodes.sort(cmp);
int rank = 0;
for (list<Statement*>::iterator i = nodes.begin(); i != nodes.end(); ++i)
lexical_order_[*i] = rank++;
}
string Extract(Statement* node, int n, bool long_desc = false);
private:
struct Node {
Node() : parent(this), rank(0) { }
Node* FindRoot() {
if (parent != this)
parent = parent->FindRoot();
return parent;
}
void Union(Node* that) {
if (FindRoot() != that->FindRoot()) {
if (parent->rank < that->parent->rank)
parent->parent = that->parent;
else if (parent->rank > that->parent->rank)
that->parent->parent = parent;
else {
that->parent->parent = parent;
++parent->rank;
}
}
}
Statement* statement;
list<Node*> predecessors;
list<Node*> successors;
pair<int,int> level;
vector<bool> adjacency;
string serialization;
Node* parent;
int rank;
};
int CompareNode(Node* const& x, Node* const& y) const;
int CompareSymmetry(Node* const& x, Node* const& y) const;
//int CompareSuccessors(Node* const& x, Node* const& y) const;
const char* ToCString(size_t index);
void SetMinLevel(Node* node);
void FindMinimalPattern();
void SearchOrder(size_t index);
Statement* focal_;
DependenceGraph graph_;
DependenceGraph neighborhood_;
map<Statement*,int> lexical_order_;
vector<Node*> min_order_;
vector<Node*> curr_order_;
vector<size_t> range_;
string min_pattern_;
string curr_pattern_;
const size_t size_limit_;
size_t count_;
map<int,map<Statement*,string> > cached_patterns_;
};
#endif // PDGEXTRACTOR_H