-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSorting.h
321 lines (271 loc) · 8.05 KB
/
Sorting.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef SORTING_H_
#define SORTING_H_
#include <algorithm>
#include "Types.h"
#include <vector>
#include <iterator>
#include <numeric>
using std::sort;
using std::pair;
using std::tuple;
template<typename Tup>
class DiagonalIndexSort {
public:
typename vector<pair<Tup, Tup> >::iterator tuples;
long operator()(const int &a, const int &b) {
typename vector<std::pair<Tup, Tup> >::iterator ap=tuples+a;
typename vector<std::pair<Tup, Tup> >::iterator bp=tuples+b;
long aDiag = (long)ap->first.pos - (long)ap->second.pos,
bDiag= (long)bp->first.pos - (long)bp->second.pos;
if (aDiag != bDiag) {
return aDiag < bDiag;
}
else {
return ap->first.pos < bp->first.pos;
}
}
};
template<typename Tup>
class DiagonalSortOp {
public:
long operator()(const pair<Tup, Tup> &a, const pair<Tup, Tup> &b) {
long aDiag = (long)a.first.pos - (long)a.second.pos,
bDiag= (long)b.first.pos - (long)b.second.pos;
if (aDiag != bDiag) {
return aDiag < bDiag;
}
else {
return a.first.pos < b.first.pos;
}
}
};
template<typename Tup>
void DiagonalSort(typename vector<pair<Tup, Tup> >::iterator begin, typename vector<pair<Tup, Tup> >::iterator end, int minRange=0) {
if (minRange == 0 or end-begin < minRange) {
sort(begin, end, DiagonalSortOp<Tup>());
}
else {
DiagonalIndexSort<Tup> sorter;
sorter.tuples=begin;
vector<int> index(end-begin);
std::iota(index.begin(), index.end(), 0);
sort(index.begin(), index.end(), sorter);
GenomePos pos;
vector<pair<Tup, Tup> > temp(end-begin);
copy(begin,end, temp.begin());
for (int i=0; i < index.size(); i++) {
temp[i]=*(begin+index[i]);
}
copy(temp.begin(), temp.end(), begin);
}
}
template<typename Tup>
void DiagonalSort(vector<pair<Tup, Tup> > &vals, int minRange=0) {
DiagonalSort<Tup>(vals.begin(), vals.end(), minRange);
}
template<typename Tup>
class AntiDiagonalSortOp {
public:
AntiDiagonalSortOp() {}
GenomePos operator()(const pair<Tup, Tup> &a, const pair<Tup, Tup> &b) {
GenomePos aDiag = a.first.pos + a.second.pos,
bDiag= b.first.pos + b.second.pos;
if (aDiag != bDiag) {
return aDiag < bDiag;
}
else {
return a.first.pos < b.first.pos;
}
}
};
template<typename Tup>
class AntiDiagonalIndexSort {
public:
typename vector<pair<Tup, Tup> >::iterator tuples;
GenomePos operator()(const int &a, const int &b) {
typename vector<std::pair<Tup, Tup> >::iterator ap=tuples+a;
typename vector<std::pair<Tup, Tup> >::iterator bp=tuples+b;
GenomePos aDiag = ap->first.pos + ap->second.pos;
GenomePos bDiag = bp->first.pos + bp->second.pos;
if (aDiag != bDiag) {
return aDiag < bDiag;
}
else {
return ap->first.pos < bp->first.pos;
}
}
};
template<typename Tup>
void AntiDiagonalSort(typename vector<pair<Tup, Tup> >::iterator begin,
typename vector<pair<Tup, Tup> >::iterator end, int sortByIndex=0) {
if (sortByIndex == 0 or end-begin < sortByIndex) {
sort(begin, end, AntiDiagonalSortOp<Tup>());
}
else {
AntiDiagonalIndexSort<Tup> sorter;
sorter.tuples=begin;
// sorter.length=genomeLength;
vector<int> index(end-begin);
std::iota(index.begin(), index.end(), 0);
sort(index.begin(), index.end(), sorter);
GenomePos pos;
vector<pair<Tup, Tup> > temp(end-begin);
copy(begin,end, temp.begin());
for (int i=0; i < index.size(); i++) {
temp[i]=*(begin+index[i]);
}
copy(temp.begin(), temp.end(), begin);
}
}
template<typename Tup>
void AntiDiagonalSort(vector<pair<Tup, Tup> > &vals, int sortByIndex=0) {
AntiDiagonalSort<Tup>(vals.begin(), vals.end(), sortByIndex);
}
template<typename Tup>
class CartesianSortOp {
public:
int operator()(const pair<Tup, Tup> &a, const pair<Tup, Tup> &b) {
if (a.first.pos != b.first.pos) {
return a.first.pos < b.first.pos;
}
else {
return a.second.pos < b.second.pos;
}
}
};
template<typename Tup>
void CartesianSort(vector<pair<Tup, Tup> > &vals, int s, int e) {
sort(vals.begin() + s, vals.begin() + e, CartesianSortOp<Tup>());
}
template<typename Tup>
void CartesianSort(typename vector<pair<Tup, Tup> >::iterator begin, typename vector<pair<Tup, Tup> >::iterator end) {
sort(begin, end, CartesianSortOp<Tup>());
}
template<typename Tup>
void CartesianSort(vector<pair<Tup, Tup> > &vals) {
CartesianSort<Tup>(vals.begin(), vals.end());
}
template<typename Tup>
int CartesianLowerBound(typename vector<pair<Tup, Tup> >::iterator begin,
typename vector<pair<Tup, Tup> >::iterator end, int64_t query) {
pair<Tup, Tup> queryTup;
queryTup.first.pos = query;
queryTup.second.pos = 0;
return lower_bound(begin, end, queryTup, CartesianSortOp<Tup>()) - begin;
}
template<typename Tup>
class CartesianTargetSortOp {
public:
int operator()(const pair<Tup, Tup> &a, const pair<Tup, Tup> &b) {
if (a.second.pos != b.second.pos) {
return a.second.pos < b.second.pos;
}
else {
return a.first.pos < b.first.pos;
}
}
};
template<typename Tup>
void CartesianTargetSort(vector<pair<Tup, Tup> > &vals) {
CartesianTargetSort<Tup>(vals.begin(), vals.end());
}
template<typename Tup>
void CartesianTargetSort(typename vector<pair<Tup, Tup> >::iterator begin, typename vector<pair<Tup, Tup> >::iterator end) {
sort(begin, end, CartesianTargetSortOp<Tup>());
}
template<typename Tup>
void CartesianTargetSort(vector<pair<Tup, Tup>> &matches, int s, int e) {
sort(matches.begin() + s, matches.begin() + e, CartesianTargetSortOp<Tup>());
}
template<typename Tup>
int CartesianTargetLowerBound(typename vector<pair<Tup, Tup> >::iterator begin, typename vector<pair<Tup, Tup> >::iterator end, int64_t query) {
pair<Tup, Tup> queryTup;
queryTup.second.pos = query; queryTup.first.pos = 0;
return lower_bound(begin, end, queryTup, CartesianTargetSortOp<Tup>()) - begin;
}
template<typename Tup>
int CartesianTargetUpperBound(typename vector<pair<Tup, Tup> >::iterator begin, typename vector<pair<Tup, Tup> >::iterator end, int64_t query) {
pair<Tup, Tup> queryTup;
queryTup.second.pos = query; queryTup.first.pos = 0;
return upper_bound(begin, end, queryTup, CartesianTargetSortOp<Tup>()) - begin;
}
template<typename T>
class SortByRowOp {
public:
int operator()(const T & a, const T & b) {
if (a.se.first != b.se.first) {
return a.se.first < b.se.first;
}
else if (a.se.second != b.se.second){
return a.se.second < b.se.second;
}
else {
return a.ind < b.ind;
}
}
};
template<typename T1, typename T2>
class SortByColOp {
public:
SortByColOp(std::vector<T1> & H);// constructor
std::vector<T1> * Hp;
int operator()(const T2 & a, const T2 & b) {
if ((*Hp)[a].se.second != (*Hp)[b].se.second) {
return (*Hp)[a].se.second < (*Hp)[b].se.second;
}
else if ((*Hp)[a].se.first != (*Hp)[b].se.first){
return (*Hp)[a].se.first < (*Hp)[b].se.first;
}
else {
return (*Hp)[a].ind < (*Hp)[b].ind;
}
}
};
// constructor
template<typename T1, typename T2>
SortByColOp<T1, T2>::SortByColOp(std::vector<T1> & H) {
Hp = & H;
}
template<typename T1, typename T2>
class SortByBackDiagOp
{
public:
SortByBackDiagOp(std::vector<T1> & H); // constructor && initialization list
std::vector<T1> * Hp;
int operator()(const T2 & a, const T2 & b) {
long int aBackDiag = (*Hp)[a].se.first + (*Hp)[a].se.second;
long int bBackDiag = (*Hp)[b].se.first + (*Hp)[b].se.second;
if (aBackDiag != bBackDiag) {
return aBackDiag < bBackDiag;
}
else if ((*Hp)[a].se.first != (*Hp)[b].se.first){
return (*Hp)[a].se.first < (*Hp)[b].se.first;
}
else {
return (*Hp)[a].ind < (*Hp)[b].ind;
}
}
};
// Constructor
template<typename T1, typename T2>
SortByBackDiagOp<T1, T2>::SortByBackDiagOp(std::vector<T1> & H) {
Hp = & H;
}
// This Lower_bound function return the index of the element
// the first element in the range [first,last) which is greater than or equal to val.
template <typename T1, typename T2>
T1 Lower_Bound (T1 first, T1 last, long int val, std::vector<T2> & E_1) {
T1 it;
unsigned int count, step;
count = std::distance(first, last);
while (count > 0) {
it = first; step = count/2; std::advance(it, step);
if ( E_1[*it] < val) {
first = ++it;
count -= step + 1;
}
else count = step;
}
return first;
}
#endif