-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
142 lines (127 loc) · 4.18 KB
/
utils.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
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
#ifndef UTILS_HPP
#define UTILS_HPP
#include <chrono>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
namespace func {
template<typename T>
void copy(T &a, const T &b)
{
std::memcpy((char *) &a, (char *) &b, sizeof(T));
}
template<typename T>
void copy(T &a, T &b)
{
std::memcpy((char *) &a, (char *) &b, sizeof(T));
}
template<typename T>
void copy(T &a, char *&b)
{
std::memcpy((char *) &a, b, sizeof(T));
}
} // namespace func
template<typename Return>
struct TimedResult
{
Return result;
double duration;
TimedResult(Return &_result, double &_duration) : result(_result), duration(_duration) {}
};
template<>
struct TimedResult<void> {
double duration;
TimedResult(double &_duration) : duration(_duration) {}
};
template<typename Return, typename Fun, typename ...Args>
TimedResult<Return> time_function(Fun &function, Args... args) {
auto start = std::chrono::steady_clock::now();
Return result = function(args...);
auto end = std::chrono::steady_clock::now();
double duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
return {result, duration};
}
template<typename Fun, typename ...Args>
TimedResult<void> time_function(Fun &function, Args... args) {
auto start = std::chrono::steady_clock::now();
function(args...);
auto end = std::chrono::steady_clock::now();
double duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
return {duration};
}
template<typename KeyType, typename RecordType, typename Index, typename Equal = std::equal_to<KeyType>>
std::vector<RecordType> linear_search(std::string filename,
KeyType key,
Index index,
Equal equal = std::equal_to<KeyType>{})
{
std::vector<RecordType> result;
std::fstream file(filename, std::ios::in | std::ios::binary);
while (!file.eof()) {
RecordType tmp{};
file.read((char *) &tmp, sizeof(tmp));
if (!file.eof() && equal(index(tmp), key) && !tmp.removed) {
result.push_back(tmp);
}
}
file.close();
return result;
}
template<typename KeyType, typename RecordType, typename Index, typename Greater = std::greater<KeyType>>
std::vector<RecordType> range_search(std::string filename,
KeyType start,
KeyType end,
Index index,
Greater greater = std::greater<KeyType>{})
{
std::vector<RecordType> result;
std::fstream file(filename, std::ios::in | std::ios::binary);
while (!file.eof()) {
RecordType tmp{};
file.read((char *) &tmp, sizeof(tmp));
if (!file.eof() && !greater(start, index(tmp)) && !greater(index(tmp), end)
&& !tmp.removed) {
result.push_back(tmp);
}
}
file.close();
return result;
}
template<typename RecordType>
long insert_register_on_file(std::string filename, RecordType &record)
{
std::fstream file(filename, std::ios::out | std::ios::binary | std::ios::in);
file.seekp(0, std::ios::end);
long pos = file.tellp();
file.write((char *) &record, sizeof(record));
file << std::flush;
file.close();
return pos;
}
template<typename KeyType, typename RecordType, typename Index, typename Equal = std::equal_to<KeyType>>
void linear_delete(std::string filename,
KeyType key,
Index index,
Equal equal = std::equal_to<KeyType>{})
{
std::fstream file(filename, std::ios::in | std::ios::binary | std::ios::out);
file.seekg(0);
file.seekp(0);
while (!file.eof()) {
RecordType tmp{};
long pos = file.tellg();
file.read((char *) &tmp, sizeof(tmp));
std::cout << "here" << std::endl;
if (!file.eof() && equal(index(tmp), key)) {
std::cout << tmp.to_string() << std::endl;
file.seekg(pos);
file.seekp(pos);
tmp.removed = true;
file.write((char *) &tmp, sizeof(tmp));
}
}
file.close();
}
#endif