-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentSortIterator.h
More file actions
141 lines (113 loc) · 3.41 KB
/
SegmentSortIterator.h
File metadata and controls
141 lines (113 loc) · 3.41 KB
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
#ifndef SEGMENT_SORT_ITERATOR_H
#define SEGMENT_SORT_ITERATOR_H
#include <vector>
#include <queue>
#include <stdexcept>
#include <algorithm>
// Define namespace to avoid collisions
namespace SegmentSort {
/**
* SegmentSortIterator
*
* A "Lazy" sorting iterator designed for Top-K queries and streaming.
*
* Advantages:
* 1. Zero-copy (Const reference to source).
* 2. Low auxiliary memory O(K) where K is number of segments.
* 3. O(N) initialization cost.
* 4. O(log K) cost per element extracted.
*/
class Iterator
{
private:
// Reference to original vector
const std::vector<int>& sourceRef;
// Internal cursor for Heap
struct RunCursor {
int currentIdx;
int remaining;
int step; // +1 for ascending, -1 for descending
int value;
int id; // For stability/debugging
};
// Min-Heap Comparator (inverted logic for priority_queue)
struct CompareRunCursor {
bool operator()(const RunCursor &a, const RunCursor &b) {
return a.value > b.value;
}
};
std::priority_queue<RunCursor, std::vector<RunCursor>, CompareRunCursor> minHeap;
int totalSegments = 0;
void initialize() {
int n = sourceRef.size();
if (n == 0) return;
int runStart = 0;
int direction = 0; // 0: unknown, 1: asc, -1: desc
for (int i = 1; i < n; ++i) {
long long diff = (long long)sourceRef[i] - sourceRef[i-1];
if (diff == 0) continue;
int currentDir = (diff > 0) ? 1 : -1;
if (direction == 0) {
direction = currentDir;
continue;
}
if (currentDir != direction) {
addSegmentToHeap(runStart, i - 1, direction);
runStart = i;
direction = 0;
}
}
// Add last segment
addSegmentToHeap(runStart, n - 1, (direction == 0 ? 1 : direction));
}
void addSegmentToHeap(int startIdx, int endIdx, int direction) {
if (startIdx > endIdx) return;
RunCursor cursor;
cursor.remaining = (endIdx - startIdx) + 1;
cursor.id = totalSegments++;
if (direction >= 0) {
cursor.currentIdx = startIdx;
cursor.step = 1;
} else {
cursor.currentIdx = endIdx;
cursor.step = -1;
}
cursor.value = sourceRef[cursor.currentIdx];
minHeap.push(cursor);
}
public:
Iterator(const std::vector<int>& input) : sourceRef(input) {
initialize();
}
bool hasNext() const {
return !minHeap.empty();
}
int next() {
if (minHeap.empty()) {
throw std::out_of_range("No more elements");
}
RunCursor current = minHeap.top();
minHeap.pop();
int retValue = current.value;
current.remaining--;
if (current.remaining > 0) {
current.currentIdx += current.step;
current.value = sourceRef[current.currentIdx];
minHeap.push(current);
}
return retValue;
}
std::vector<int> nextBatch(int k) {
std::vector<int> batch;
batch.reserve(k);
for (int i = 0; i < k && hasNext(); i++) {
batch.push_back(next());
}
return batch;
}
int getSegmentCount() const {
return totalSegments;
}
};
} // namespace
#endif