-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cc
166 lines (134 loc) · 3.17 KB
/
utils.cc
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "utils.hpp"
#include "graph.hpp"
#include <algorithm>
#include <list>
#include <set>
#include <boost/graph/connected_components.hpp>
using namespace std;
bool
includes(const SSC &a, const SSC &b)
{
return includes(a.begin(), a.end(), b.begin(), b.end());
}
bool
excludes(const SSC &a, const SSC &b)
{
return intersection(a, b).empty();
}
SSC
intersection(const SSC &a, const SSC &b)
{
SSC r;
set_intersection(a.begin(), a.end(),
b.begin(), b.end(),
inserter(r, r.begin()));
return r;
}
SSC
exclude(const SSC &ssc, int nsc)
{
SSC result;
SSC::const_iterator i = ssc.begin();
while(i != ssc.end())
{
SSC partial;
// This loop builds the "partial" set with contiguous
// slices. In an interation we examine a single
// slice.
while(i != ssc.end())
{
int sc = *i;
// The break condition for this loop, i.e. the condition to
// stop building the partial set, because the previously
// inserted element is not one less small than sc. If we
// break the loop, we don't add the sc element and don't
// increment i. The element pointed to by i will be
// inserted into a new partial set.
if (!partial.empty() && ((*partial.rbegin() + 1) != sc))
break;
partial.insert(sc);
++i;
}
// Add the partial set only if has at least p slices.
// Otherwise just ignore it.
if (partial.size() >= nsc)
result.insert(partial.begin(), partial.end());
}
return result;
}
void
exclude(SSC &ssc, const SSC &e)
{
for(SSC::const_iterator i = e.begin(); i != e.end(); ++i)
ssc.erase(*i);
}
void
include(SSC &ssc, const SSC &e)
{
for(SSC::const_iterator i = e.begin(); i != e.end(); ++i)
ssc.insert(*i);
}
SSC
find_path_ssc(const graph &g, const path &p)
{
SSC ssc;
path::const_iterator i = p.begin();
if (i != p.end())
{
ssc = boost::get(boost::edge_ssc, g, *i);
while(++i != p.end())
ssc = intersection(ssc, boost::get(boost::edge_ssc, g, *i));
}
return ssc;
}
// The function for sorting the list of sets.
static bool
stlos(const set<vertex> &s1, const set<vertex> &s2)
{
return s1.size() > s2.size();
}
SSSC
split(const SSC &ssc, int nsc)
{
SSSC sssc;
for(SSC::const_iterator i = ssc.begin(); i != ssc.end();)
{
SSC tmp;
int sc;
do
{
sc = *i;
tmp.insert(sc);
++i;
} while (i != ssc.end() && sc + 1 == *i);
// Return this SSC only if it has at least nsc slices.
if (tmp.size() >= nsc)
sssc.insert(tmp);
}
return sssc;
}
int
calculate_fragments(const SSC &ssc)
{
// Split the SSC into fragments of size 1 at least.
return split(ssc, 1).size();
}
/**
* This is the << operator for a sscpath.
*/
std::ostream &
operator << (std::ostream &os, const sscpath &p)
{
os << "sscpath(" << p.first << ", " << p.second << ")";
}
/**
* This is the << operator for a CEV.
*/
std::ostream &
operator << (std::ostream &os, const CEV &cev)
{
os << "CEV("
<< get<0>(cev) << ", "
<< get<1>(cev) << ", "
<< get<2>(cev) << ")";
}