-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemoryRun.cpp
300 lines (249 loc) · 6.86 KB
/
MemoryRun.cpp
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
/*
* MemoryRun.cpp
*/
#include "MemoryRun.h"
#include "ResultSet.h"
#include <limits.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstring>
using namespace std;
MemoryRun::MemoryRun(int size) {
entries = new Entry[size];
// memset(entries, size*sizeof(Entry), 0);
nextPos = 0;
maxEntries = size;
}
MemoryRun::MemoryRun(Entry *entries, int size) {
this->entries = entries;
this->maxEntries = size;
this->nextPos = size;
}
int MemoryRun::numElements() {
return this->nextPos;
}
bool MemoryRun::insert(Entry e) {
/* insert and increment */
entries[nextPos].setEntry(&e);
nextPos += 1;
/* if full: */
return nextPos != maxEntries;
}
bool MemoryRun::insert(Entry *e) {
memcpy(&entries[nextPos], e, sizeof(Entry));
nextPos++;
return nextPos != maxEntries;
}
void MemoryRun::insertAtPos(Entry e, int pos) {
entries[pos].setEntry(&e);
}
Entry *MemoryRun::get(int key) {
for (int i = nextPos; i >= 0; i--) {
if (entries[i].getKey() == key) {
/* if the entry is a delete entry, we will get the delete sentinal */
return &entries[i];
}
}
return nullptr;
}
Entry *MemoryRun::getBinarySearch(int key) {
int left = 0;
int right = nextPos - 1;
int mid;
while (left <= right) {
mid = (right + left) / 2;
if (entries[mid].getKey() == key) {
return new Entry(entries[mid].getKey(), entries[mid].getValue(), 0);
}
else if (entries[mid].getKey() < key) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return nullptr;
}
Entry MemoryRun::at(int pos) {
// TODO throw error if out of range?
return entries[pos];
}
MemoryRun *MemoryRun::getRange(int low, int high) {
MemoryRun *results = new MemoryRun(maxEntries);
for (int i = 0; i < nextPos; i++) {
if ((entries[i].getKey() >= low) &&
(entries[i].getKey() <= high)) {
results->insert(entries[i]);
}
}
return results;
}
bool MemoryRun::remove(int key) {
for (int i = nextPos; i >= 0; i--) {
if (entries[i].getKey() == key) {
if (entries[i].isRemove()) {
return true;
}
entries[i].updateRemove(true);
return true;
}
}
/* we didn't find it */
Entry *e = new Entry(key, 0, true);
return this->insert(*e);
}
Entry *MemoryRun::getEntries() {
return entries;
}
bool MemoryRun::isEmpty() {
return (nextPos == 0);
}
// Merges two memoryRuns
// This should always be run such that the older run is
// the first argument and the newer one is second
MemoryRun *MemoryRun::merge(MemoryRun *older, MemoryRun *newer) {
int olderSize = older->getSize();
int newerSize = newer->getSize();
MemoryRun *merged = new MemoryRun(olderSize + newerSize);
int i = 0;
int j = 0;
while (i < olderSize && j < newerSize) {
/* REMOVING DUPLICATES
*
* The right-side run is newer. Therefore, if the keys are the same,
* we always skip the left-side entry, because the newer instruction,
* whether it be an insert or a delete, should be what propagates down
* the tree.
*/
if (older->at(i).getKey() == newer->at(j).getKey()) {
//cout << "dupe!" << endl;
i++;
}
else if (older->at(i).getKey() < newer->at(j).getKey()) {
merged->insert(older->at(i));
i++;
}
else {
merged->insert(newer->at(j));
j++;
}
}
/* Copy the remaining elements, if any */
while (i < olderSize) {
merged->insert(older->at(i));
i++;
}
while (j < newerSize) {
merged->insert(newer->at(j));
j++;
}
return merged;
}
void MemoryRun::removeDuplicates() {
this->sort();
//------------------------Keep only latest version in memory run after sorting ---------------------
Entry *entries = this->getEntries();
Entry *entries2 = new Entry[this->getSize()];
int j = 0;
for (int i = 0; i < this->getSize() - 1; i++) {
if (entries[i].getKey() != entries[i + 1].getKey()) {
memcpy(&entries2[j], &entries[i], sizeof(Entry));
j++;
}
}
memcpy(&entries2[j], &entries[this->getSize() - 1], sizeof(Entry));
j++;
memcpy(this->getEntries(), entries2, j * sizeof(Entry));
this->setSize(j);
//------------------------------------------------------------
}
// https://stackoverflow.com/questions/1557894/non-recursive-merge-sort
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void MemoryRun::sort() {
MemoryRun *temp = new MemoryRun(nextPos);
int size = nextPos;
int rght, wid, rend;
int i, j, m, t;
for (int k = 1; k < size; k *= 2) {
for (int left = 0; left + k < size; left += k * 2) {
rght = left + k;
rend = rght + k;
if (rend > size) {
rend = size;
}
m = left;
i = left;
j = rght;
while (i < rght && j < rend) {
if (entries[i].getKey() <= entries[j].getKey()) {
temp->insertAtPos(entries[i], m);
i++;
}
else {
temp->insertAtPos(entries[j], m);
j++;
}
m++;
}
while (i < rght) {
temp->insertAtPos(entries[i], m);
i++;
m++;
}
while (j < rend) {
temp->insertAtPos(entries[j], m);
j++;
m++;
}
for (m = left; m < rend; m++) {
entries[m] = temp->at(m);
}
}
}
delete temp;
}
void MemoryRun::reset() {
// delete entries;
// entries = new Entry[maxEntries];
nextPos = 0;
}
void MemoryRun::print() {
cout << "[";
for (int i = 0; i < nextPos; i++) {
cout << " " << entries[i].getValue();
}
cout << "]";
}
void MemoryRun::removeDeletes() {
Entry *cleaned = new Entry[maxEntries];
int cleanedNext = 0;
for (int i = 0; i < nextPos; i++) {
if (!entries[i].isRemove()) {
cleaned[cleanedNext] = entries[i];
cleanedNext++;
}
}
delete[] entries;
entries = cleaned;
}
int MemoryRun::getSize() {
return this->nextPos;
}
void MemoryRun::printer() {
printf("Memrun contains %d entries : ", nextPos);
for (int i = 0; i < nextPos; i++) {
cout << entries[i].getKey();
cout << ":";
cout << entries[i].getValue();
cout << "|";
}
cout << endl;
}
void MemoryRun::setSize(int size) {
this->nextPos = size;
}
MemoryRun::~MemoryRun() {
// delete[] this->entries;
}