/
ival_list.h
215 lines (215 loc) · 4.11 KB
/
ival_list.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef IVAL_LIST_H_
#define IVAL_LIST_H_
#include "ds.h"
#include "ref_coord.h"
#include <utility>
class EIvalMergeList
{
public:
static const size_t DEFAULT_UNSORTED_SZ = 16;
explicit EIvalMergeList(int cat = 0) : sorted_(cat),
sortedLhs_(cat),
unsorted_(cat),
unsortedSz_(DEFAULT_UNSORTED_SZ)
{
}
explicit EIvalMergeList(size_t unsortedSz, int cat = 0) : sorted_(cat),
sortedLhs_(cat),
unsorted_(cat),
unsortedSz_(unsortedSz)
{
}
void setUnsortedSize(size_t usz)
{
unsortedSz_ = usz;
}
void add(const Interval &i)
{
assert_leq(unsorted_.size(), unsortedSz_);
if (unsorted_.size() < unsortedSz_)
{
unsorted_.push_back(i);
}
if (unsorted_.size() == unsortedSz_)
{
flush();
}
}
void flush()
{
for (size_t i = 0; i < unsorted_.size(); i++)
{
sorted_.push_back(unsorted_[i]);
}
sorted_.sort();
merge();
sortedLhs_.clear();
for (size_t i = 0; i < sorted_.size(); i++)
{
sortedLhs_.push_back(sorted_[i].upstream());
}
assert(sortedLhs_.sorted());
unsorted_.clear();
}
#ifndef NDEBUG
bool repOk() const
{
assert_eq(sorted_.size(), sortedLhs_.size());
return true;
}
#endif
void reset()
{
clear();
}
void clear()
{
sorted_.clear();
sortedLhs_.clear();
unsorted_.clear();
}
bool locusPresent(const Coord &loc) const
{
return locusPresentUnsorted(loc) || locusPresentSorted(loc);
}
size_t size() const
{
return sorted_.size() + unsorted_.size();
}
bool empty() const
{
return sorted_.empty() && unsorted_.empty();
}
protected:
void merge()
{
size_t nmerged = 0;
for (size_t i = 1; i < sorted_.size(); i++)
{
if (sorted_[i - 1].downstream() >= sorted_[i].upstream())
{
nmerged++;
assert_leq(sorted_[i - 1].upstream(), sorted_[i].upstream());
Coord up = std::min(sorted_[i - 1].upstream(), sorted_[i].upstream());
Coord dn = std::max(sorted_[i - 1].downstream(), sorted_[i].downstream());
sorted_[i].setUpstream(up);
sorted_[i].setLength(dn.off() - up.off());
sorted_[i - 1].reset();
}
}
sorted_.sort();
assert_lt(nmerged, sorted_.size());
sorted_.resize(sorted_.size() - nmerged);
#ifndef NDEBUG
for (size_t i = 0; i < sorted_.size(); i++)
{
assert(sorted_[i].inited());
}
#endif
}
bool locusPresentSorted(const Coord &loc) const
{
assert(repOk());
if (sorted_.empty())
{
return false;
}
size_t beg = sortedLhs_.bsearchLoBound(loc);
if (beg == sortedLhs_.size() || sortedLhs_[beg] > loc)
{
if (beg == 0)
{
return false;
}
return sorted_[beg - 1].contains(loc);
}
else
{
assert_eq(loc, sortedLhs_[beg]);
return true;
}
}
bool locusPresentUnsorted(const Coord &loc) const
{
for (size_t i = 0; i < unsorted_.size(); i++)
{
if (unsorted_[i].contains(loc))
{
return true;
}
}
return false;
}
EList<Interval> sorted_;
EList<Coord> sortedLhs_;
EList<Interval> unsorted_;
size_t unsortedSz_;
};
class EIvalMergeListBinned
{
public:
static const size_t NBIN = 7;
explicit EIvalMergeListBinned(int cat = 0) : bins_(1 << NBIN, cat)
{
bins_.resize(1 << NBIN);
}
explicit EIvalMergeListBinned(
size_t unsortedSz,
int cat = 0) : bins_(1 << NBIN, cat)
{
bins_.resize(1 << NBIN);
for (size_t i = 0; i < (1 << NBIN); i++)
{
bins_[i].setUnsortedSize(unsortedSz);
}
}
void add(const Interval &i)
{
size_t bin = i.ref() & ~(0xffffffff << NBIN);
assert_lt(bin, bins_.size());
bins_[bin].add(i);
}
#ifndef NDEBUG
bool repOk() const
{
for (size_t i = 0; i < bins_.size(); i++)
{
assert(bins_[i].repOk());
}
return true;
}
#endif
void reset()
{
clear();
}
void clear()
{
for (size_t i = 0; i < bins_.size(); i++)
{
bins_[i].clear();
}
}
bool locusPresent(const Coord &loc) const
{
size_t bin = loc.ref() & ~(0xffffffff << NBIN);
assert_lt(bin, bins_.size());
return bins_[bin].locusPresent(loc);
}
size_t size() const
{
size_t sz = 0;
for (size_t i = 0; i < bins_.size(); i++)
{
sz += bins_[i].size();
}
return sz;
}
bool empty() const
{
return size() == 0;
}
protected:
EList<EIvalMergeList> bins_;
};
#endif