/
SortItem.hpp
48 lines (40 loc) · 950 Bytes
/
SortItem.hpp
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
#pragma once
#include <fstream>
template < int Size,int KeySize,int BufferSize >
class SortItem
{
public:
SortItem(std::ifstream& in)
{
read(in);
}
void read(std::ifstream& in)
{
in.read(data, Size);
}
void write(std::ofstream& out) const
{
memcpy(_outBuffer + _outBufferSize, data, Size);
_outBufferSize += Size;
if (_outBufferSize == BufferSize)
{
flush(out);
}
}
bool operator <(const SortItem& other) const
{
int res = memcmp(data, other.data, KeySize);
return res < 0;
}
static void flush(std::ofstream& out)
{
out.write(_outBuffer, _outBufferSize);
_outBufferSize = 0;
}
char data[Size];
private:
static int _outBufferSize;
static char _outBuffer[BufferSize];
};
template < int Size,int KeySize,int BufferSize > int SortItem < Size,KeySize,BufferSize > :: _outBufferSize = 0;
template < int Size,int KeySize,int BufferSize > char SortItem < Size,KeySize,BufferSize > :: _outBuffer[BufferSize];