-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
68 lines (57 loc) · 1.56 KB
/
stack.h
File metadata and controls
68 lines (57 loc) · 1.56 KB
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
#ifndef SJTU_STACK_HPP
#define SJTU_STACK_HPP
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <fstream>
namespace sjtu {
template<class Key>
class Stack {
private:
int sz,Size;
std::string file_name;
std::fstream file;
public:
explicit Stack(const std::string &file_name_) : file_name(file_name_) {
Size = sizeof(Key);
file.open(file_name);
if (!file.is_open()) {
file.open(file_name, std::ios::out);
sz = 0;
file.write(reinterpret_cast<char *>(&sz), 4);
} else file.read(reinterpret_cast<char *>(&sz), 4);
file.close();
}
bool empty(){
return sz==0;
}
void push(Key key) {
file.open(file_name);
file.seekp(4 + 1LL*sz * Size, std::ios::beg);
file.write(reinterpret_cast<char * >(&key), Size);
sz++;
file.close();
}
Key top() {
file.open(file_name);
file.seekp(4 + 1LL*(sz-1) * Size, std::ios::beg);
Key ret;
file.read(reinterpret_cast<char *>(&ret), Size);
file.close();
return ret;
}
void pop() {
sz--;
}
~Stack() {
file.open(file_name);
file.write(reinterpret_cast<char *>(&sz), 4);
file.close();
}
int size(){
return sz;
}
};
}
#endif