-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorter.h
134 lines (114 loc) · 3.25 KB
/
sorter.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
#pragma once
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <string>
#include "traits.h"
#include "iterators.h"
namespace ext_sort {
template <typename T, typename Traits = ObjectTraits<T>, typename Context = typename Traits::Context>
class Sorter {
public:
typedef ResultIter<T, Traits, Context> Iterator;
public:
Sorter(size_t bufSize, const std::string& tmpPref, Context* ctx = nullptr)
: BufSize_(bufSize)
, TmpPref_(tmpPref)
, Context_(ctx)
, CurrSize_(0)
, Sorted_(false)
{
}
~Sorter()
{
Clear();
}
void Add(const T& val) {
Data_.push_back(val);
Add();
}
void Add(T&& val) {
Data_.emplace_back(std::move(val));
Add();
}
Iterator begin() {
Finish();
if (TmpFileNames_.empty()) {
return Iterator(Data_);
}
return Iterator(TmpFileNames_, Context_);
}
Iterator end() const {
return Iterator();
}
template <typename Handler>
void Iterate(Handler& handle) {
Finish();
if (TmpFileNames_.empty()) {
VectorIter<T> result(Data_);
handle(result);
} else {
HeapIter<T, Traits, Context> result(TmpFileNames_, Context_);
handle(result);
}
}
void Clear() {
std::vector<T>().swap(Data_);
CurrSize_ = 0;
for (auto fileName: TmpFileNames_) {
remove(fileName.c_str());
}
TmpFileNames_.clear();
}
private:
void Add() {
CurrSize_ += Traits::Size(Data_.back());
if (CurrSize_ >= BufSize_) {
WritePortion();
Data_.clear();
CurrSize_ = 0;
}
Sorted_ = false;
}
void Sort() {
auto ctx = Context_;
std::sort(
Data_.begin(),
Data_.end(),
[ ctx ] (const T& a, const T& b) {
return Traits::Compare(a, b, ctx) < 0;
}
);
}
void WritePortion() {
Sort();
std::ostringstream fileName;
fileName << TmpPref_ << "." << ::getpid() << "." << TmpFileNames_.size();
TmpFileNames_.push_back(fileName.str());
std::ofstream tmp(TmpFileNames_.back(), std::ios_base::out | std::ios_base::binary);
for (auto& val: Data_) {
Traits::Save(tmp, val, Context_);
}
}
void Finish() {
if (Sorted_) {
return;
}
if (TmpFileNames_.empty()) {
Sort();
} else {
WritePortion();
}
Sorted_ = true;
}
private:
const size_t BufSize_;
const std::string TmpPref_;
Context* Context_;
std::vector<std::string> TmpFileNames_;
std::vector<T> Data_;
size_t CurrSize_;
bool Sorted_;
};
} // namespace ext_sort