-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffer.cpp
144 lines (113 loc) · 2.78 KB
/
differ.cpp
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
#include "differ.h"
namespace GENOPS
{
double CDiffer::CalcSimilarity(const CGenome &a, const CGenome &b)
{
dtl::Diff<gene_t> genome_diff(a, b);
genome_diff.compose();
CGenome lcs_genome = genome_diff.getLcsVec();
size_t total_size = a.size();
if(b.size() > total_size)
{
total_size = b.size();
}
return static_cast<double>(lcs_genome.size()) /
static_cast<double>(total_size);
}
std::vector<CGenomeDiff> CDiffer::GenerateDiffs(const CGenome &a,
const CGenome &b)
{
std::vector<CGenomeDiff> diffs;
dtl::Diff<gene_t> genome_diff(a, b);
genome_diff.compose();
const dtl::Ses<gene_t> &ses = genome_diff.getSes();
const dtl::Ses<gene_t>::sesElemVec &ses_elems = ses.getSequence();
size_t pos = 0;
bool diff_active = false;
CGenomeDiff diff;
for(size_t i = 0; i < ses_elems.size(); ++i)
{
switch(ses_elems[i].second.type)
{
case dtl::SES_DELETE:
if(!diff_active)
{
diff_active = true;
diff.m_pos = pos;
}
++(diff.m_remove_size);
++pos;
break;
case dtl::SES_ADD:
if(!diff_active)
{
diff_active = true;
diff.m_pos = pos;
}
diff.m_add_genes.push_back(ses_elems[i].first);
break;
case dtl::SES_COMMON:
if(diff_active)
{
diff_active = false;
diffs.push_back(diff);
diff.m_pos = 0;
diff.m_remove_size = 0;
diff.m_add_genes.clear();
}
++pos;
break;
default:
{
std::ostringstream error_stream;
error_stream<<__FILE__<<":"<<__LINE__<<" "
<<"Unknown SES type: "
<<ses_elems[i].second.type;
throw std::runtime_error(error_stream.str());
}
}
}
if(diff_active)
{
diffs.push_back(diff);
}
return diffs;
}
CDiffer::CDiffer(const CGenome &original)
{
for(size_t i = 0; i < original.size(); ++i)
{
m_mapped_genome[i].push_back(original[i]);
}
}
void CDiffer::Apply(const CGenomeDiff &diff)
{
if(diff.m_remove_size)
{
for(size_t i = diff.m_pos; i < diff.m_pos + diff.m_remove_size; ++i)
{
m_mapped_genome[i].pop_front();
}
}
if(!diff.m_add_genes.empty())
{
CGenes &genes = m_mapped_genome[diff.m_pos];
genes.insert(genes.begin(), diff.m_add_genes.begin(),
diff.m_add_genes.end());
}
}
CGenome CDiffer::Assemble() const
{
CGenome output;
for(CMappedGenome::const_iterator genes = m_mapped_genome.begin();
genes != m_mapped_genome.end(); ++genes)
{
for(CGenes::const_iterator gene = genes->second.begin();
gene != genes->second.end(); ++gene)
{
output.push_back(*gene);
}
}
return output;
}
};