-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternalSort.h
More file actions
39 lines (35 loc) · 902 Bytes
/
externalSort.h
File metadata and controls
39 lines (35 loc) · 902 Bytes
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
#pragma once
#include <string>
#include <vector>
#include <algorithm>
#include "sources.h"
#include "fileStorage.h"
#include "externalAlgo.h"
#include "serialize.h"
template<class T>
class ExternalSorter : public ExternalAlgorithms<T> {
private:
void preprocessing_(std::vector<T> &a)
{
std::sort(a.begin(), a.end());
}
void merging_() {
std::priority_queue<std::pair<T, int> > mySet;
for (size_t i = 0; i < numberOfBlocks_; ++i)
{
mySet.push(std::make_pair(buffers_[i]->getData(), i));
}
while (!mySet.empty())
{
std::pair <T, int> tmp = mySet.top();
mySet.pop();
int ind = tmp.second;
totalOut_->push(tmp.first);
if (buffers_[ind]->canTakeData())
mySet.push(std::make_pair(buffers_[ind]->getData(), ind));
}
}
public:
ExternalSorter(IDataSource<T> *in, IDataTransmitter<T> *out, int memoryInBlock) :
ExternalAlgorithms(in, out, memoryInBlock) {}
};