-
Notifications
You must be signed in to change notification settings - Fork 0
/
RowQueue.h
186 lines (157 loc) · 5.7 KB
/
RowQueue.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
//
// Created by bartanakin on 11/29/24.
//
#pragma once
#include <iostream>
#include <vector>
template<typename T>
class RowQueue {
struct ListElement {
T value;
unsigned int col;
int prev;
int next;
};
public:
RowQueue(
unsigned int capacity
):
capacity(capacity) {
this->list.reserve(capacity);
}
void mergeDown() {
int rightQueuePos = (-1) * (this->queueBeg.size() - 1) - 1;
int leftQueuePos = (-1) * (this->queueBeg.size() - 2) - 1;
int currRight = this->queueBeg[this->queueBeg.size() - 1];
int currLeft = this->queueBeg[this->queueBeg.size() - 2];
int prevLeft = this->queueBeg[this->queueBeg.size() - 2];
while (currLeft != leftQueuePos && currRight != rightQueuePos) {
if (this->list[currLeft].col < this->list[currRight].col) {
prevLeft = currLeft;
currLeft = this->list[currLeft].next;
} else if (this->list[currLeft].col == this->list[currRight].col) {
this->list[currLeft].value += this->list[currRight].value;
auto toBeRemoved = currRight;
currRight = this->list[currRight].next;
if (currRight == this->list.size() - 1) {
currRight = toBeRemoved;
}
prevLeft = currLeft;
currLeft = this->list[currLeft].next;
this->moveLastTo(toBeRemoved);
} else {
auto nextRight = this->list[currRight].next;
this->extract(currRight);
this->insertAfter(currRight, this->list[currLeft].prev);
currRight = nextRight;
}
}
while (currRight != rightQueuePos) {
auto nextRight = this->list[currRight].next;
this->extract(currRight);
this->insertAfter(currRight, prevLeft);
prevLeft = currRight;
currRight = nextRight;
}
this->queueBeg.pop_back();
}
void mergeAll() {
while (this->queueBeg.size() > 1) {
this->mergeDown();
}
}
void push(
T value,
unsigned int col
) {
if (this->list.empty()) {
this->queueBeg.push_back(0);
this->list.push_back(ListElement{value, col, -1, -1});
return;
}
if (this->list.size() < this->capacity) {
auto last = this->list.size() - 1;
auto next = this->list[last].next;
if (this->list[last].col == col && next < 0) {
this->list[last].value += value;
} else if (this->list[last].col < col && next < 0) {
this->list.push_back(ListElement{value, col, last, next});
this->list[last].next = last + 1;
} else {
auto newQueueBeg = (-1) * this->queueBeg.size() - 1;
this->queueBeg.push_back(this->list.size());
this->list.push_back(ListElement{value, col, newQueueBeg, newQueueBeg});
}
return;
}
while (this->list.size() == this->capacity) {
if (this->queueBeg.size() <= 1) {
this->capacity *= 2;
this->list.reserve(this->capacity);
} else {
this->mergeDown();
}
}
this->push(value, col);
}
const std::vector<ListElement>& getElements() const { return this->list; }
size_t size() const { return this->list.size(); }
void print() const {
for (unsigned int i = 0; i < this->list.size(); i++) {
std::cout << "(" << this->list[i].value << "," << this->list[i].col << ") " << this->list[i].prev << " " << this->list[i].next
<< std::endl;
}
std::cout << "---" << std::endl;
for (unsigned int i = 0; i < this->queueBeg.size(); i++) {
std::cout << this->queueBeg[i] << " ";
}
std::cout << std::endl;
}
const std::vector<unsigned int>& getQueueBeg() const { return queueBeg; }
private:
unsigned int capacity;
std::vector<ListElement> list;
std::vector<unsigned int> queueBeg;
void insertAfter(
int toBeInserted,
int afterPosition
) {
this->list[toBeInserted].prev = afterPosition;
if (afterPosition < 0) {
auto queueIndex = (-1) * afterPosition - 1;
this->list[toBeInserted].next = this->queueBeg[queueIndex];
this->queueBeg[queueIndex] = toBeInserted;
} else {
this->list[toBeInserted].next = this->list[afterPosition].next;
this->list[afterPosition].next = toBeInserted;
}
if (this->list[toBeInserted].next >= 0) {
this->list[this->list[toBeInserted].next].prev = toBeInserted;
}
}
void extract(
int toBeExtracted
) {
if (this->list[toBeExtracted].next >= 0) {
this->list[this->list[toBeExtracted].next].prev = this->list[toBeExtracted].prev;
}
if (this->list[toBeExtracted].prev >= 0) {
this->list[this->list[toBeExtracted].prev].next = this->list[toBeExtracted].next;
} else {
this->queueBeg[(-1) * this->list[toBeExtracted].prev - 1] = this->list[toBeExtracted].next;
}
}
void moveLastTo(
int toBeRemoved
) {
this->extract(toBeRemoved);
auto last = this->list.size() - 1;
if (last != toBeRemoved) {
auto insertAfter = this->list[last].prev;
this->extract(last);
this->list[toBeRemoved] = this->list[last];
this->insertAfter(toBeRemoved, insertAfter);
}
this->list.pop_back();
}
};