-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorts.h
168 lines (147 loc) · 4.13 KB
/
sorts.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
#ifndef __SORTS_H__
#define __SORTS_H__
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using std::cout;
using std::endl;
using std::vector;
using std::sort;
using std::unordered_map;
using std::pair;
using std::max;
/*******************************************************************************
* class QSortLarge
*/
template <typename T>
class QSortLarge {
public:
QSortLarge() {}
static vector<T *> * sort(vector<T> &v) {
vector<T *> *ptrs = (vector<T *> *) new vector<T *>();
for(T &t : v) {
ptrs->emplace_back(&t);
}
std::sort(ptrs->begin(), ptrs->end(),
[] (const T *a, const T *b) -> bool { return *a < *b; });
return ptrs;
}
};
/*******************************************************************************
* class QSortLarge
*/
template <typename T>
struct Groupable {
public:
T key;
T name;
};
template <typename T>
class GroupByKey {
public:
GroupByKey() {
}
static void group(vector<Groupable<T>> &v) {
unordered_map<T, int> group_per_key;
/*
for_each(v.begin(), v.end(),
[](Groupable<T> &grp) -> void { cout << grp.key << endl; } );
*/
/**
* Get counts per key
*/
cout << "size " << v.size() << endl;
for(const Groupable<T> &ref : v) {
auto grpIt = group_per_key.find(ref.key);
if(grpIt != group_per_key.end()) {
grpIt->second++;
} else {
group_per_key[ref.key] = 1;
}
}
/**
* Calculate offsets
*/
unordered_map<T,int> key_to_offset;
int offset = 0;
for(const pair<T,int> &p : group_per_key) {
key_to_offset[p.first] = offset;
offset += p.second;
}
/*
*/
cout << "Offsets & Counts -----------------------" << endl;
for_each(key_to_offset.begin(), key_to_offset.end(),
[&group_per_key](const pair<T, int> &p) -> void {
cout << p.first << "=" << p.second << ":";
cout << group_per_key[p.first] << endl;
} );
/**
* re-rder
*/
while(key_to_offset.size()) {
auto from = key_to_offset.begin();
iLog(0, "Process group %s with offset %d",
from->first.c_str(), from->second);
int from_offset = from->second;
auto to = key_to_offset.find(v[from_offset].key);
iLog(1, "swap from:[%d]=%s:%s with to:[%d]=%s:%s",
from->second,
v[from->second].key.c_str(), v[from->second].name.c_str(),
to->second,
v[to->second].key.c_str(), v[to->second].name.c_str());
swap(v[from->second], v[to->second]);
if(--group_per_key[to->first]) {
to->second++;
iLog(2, "New to->second %s->%d", to->first.c_str(), to->second);
} else {
iLog(2, "Done with to %s", to->first.c_str());
key_to_offset.erase(to);
}
for_each(v.begin(), v.end(),
[](Groupable<T> &grp) -> void {
cout << "\t\t" << grp.key << ":" << grp.name << endl; } );
}
cout << "Sorted ---------------------------------" << endl;
for_each(v.begin(), v.end(),
[](Groupable<T> &grp) -> void {
cout << grp.key << ":" << grp.name << endl; } );
/*
*/
}
private:
};
/*******************************************************************************
* class CalendarConcurrency
*/
class CalendarConcurrency {
public:
struct TimePoint {
int time;
bool isStart;
bool operator < (const TimePoint &that) const {
return time != that.time ? time < that.time : (isStart && !that.isStart);
}
};
static int getMaxConcurrency(vector<vector<int>> &m) {
vector<TimePoint> points;
for_each(m.cbegin(), m.cend(),
[&points](const vector<int> &v) -> void {
points.emplace_back(TimePoint{v[0], true});
points.emplace_back(TimePoint{v[1], false});
});
sort(points.begin(), points.end());
int maxConcurr = 0, concurr = 0;
for(const TimePoint &pt : points) {
if(pt.isStart) {
maxConcurr = max(++concurr, maxConcurr);
} else {
--concurr;
}
}
return maxConcurr;
}
private:
};
#endif /* __SORTS_H__ */