forked from whr-a/Ticket-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_database.hpp
185 lines (173 loc) · 4.85 KB
/
train_database.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef TRAIN_DATABASE
#define TRAIN_DATABASE
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdio>
#include "vector.hpp"
struct Node {
int data;
Node* next;
};
// 队列类
class Queue {
public:
Node* front; // 队首指针
Node* rear; // 队尾指针
Queue() : front(nullptr), rear(nullptr) {}
~Queue() {
while (front != nullptr) {
Node* temp = front;
front = front->next;
delete temp;
}
}
// 判断队列是否为空
bool isEmpty() {
return (front == nullptr);
}
// 入队
void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
// 如果队列为空,新节点既是队首也是队尾
if (isEmpty()) {
front = rear = newNode;
} else {
// 将新节点添加到队尾,并更新队尾指针
rear->next = newNode;
rear = newNode;
}
}
// 出队
void dequeue() {
if (isEmpty()) return;
Node* temp = front; // 保存当前队首节点的指针
front = front->next; // 更新队首指针
// 如果队列只有一个节点,出队后队列为空,需要更新队尾指针
if (front == nullptr) {
rear = nullptr;
}
delete temp; // 释放原队首节点的内存
}
// 获取队首元素
int getFront() {
if (isEmpty()) return -1;
return front->data;
}
};
class vec_block
{
public:
int data[100000];
vec_block(){memset(data,0,sizeof(data));}
vec_block(const vec_block &obj){
for(int i=0;i<20000;i++)data[i]=obj.data[i];
}
vec_block operator= (const vec_block &obj){
for(int i=0;i<20000;i++)data[i]=obj.data[i];
return *this;
}
};
template <class T>
class train_database
{
public:
std::fstream opfile;
int max_num;
Queue q;
train_database(std::string s){
std::ifstream in;
in.open(s);
if(!in){
std::ofstream outfile(s);
outfile.seekp(0);
int t1=1;
outfile.write(reinterpret_cast<char*>(&t1),sizeof(int));
vec_block tem;
outfile.seekp(sizeof(int));
outfile.write(reinterpret_cast<char*>(&tem),sizeof(vec_block));
}
opfile.open(s);
opfile.seekg(0);
opfile.read(reinterpret_cast<char*>(&max_num),sizeof(int));
opfile.seekg(sizeof(int));
vec_block temp;
opfile.read(reinterpret_cast<char*>(&temp),sizeof(vec_block));
q.front=nullptr;
q.rear=nullptr;
for(int i=0;i<20000;i++){
if(temp.data[i]==0)break;
q.enqueue(temp.data[i]);
}
}
~train_database(){
opfile.seekp(0);
opfile.write(reinterpret_cast<char*>(&max_num),sizeof(int));
vec_block temp;
int cnt=-1;
while(!q.isEmpty()){
temp.data[++cnt]=q.getFront();
q.dequeue();
}
opfile.seekp(sizeof(int));
opfile.write(reinterpret_cast<char*>(&temp),sizeof(vec_block));
}
int insert(const T &obj){
T obj_(obj);
if(q.isEmpty()){
opfile.seekp(sizeof(int)+sizeof(vec_block)+(max_num-1)*sizeof(T));
opfile.write(reinterpret_cast<char*>(&obj_),sizeof(T));
++max_num;
return max_num-1;
}
else{
int x=q.getFront();
q.dequeue();
opfile.seekp(sizeof(int)+sizeof(vec_block)+(x-1)*sizeof(T));
opfile.write(reinterpret_cast<char*>(&obj_),sizeof(T));
return x;
}
}
T find(int num){
T temp;
opfile.seekg(sizeof(int)+sizeof(vec_block)+(num-1)*sizeof(T));
opfile.read(reinterpret_cast<char*>(&temp),sizeof(T));
return temp;
}
void erase(int num){
q.enqueue(num);
}
void clear(std::string s){
std::ofstream file(s, std::ios::trunc);
file.close();
while(!q.isEmpty())q.dequeue();
std::ifstream in;
in.open(s);
if(!in){
std::ofstream outfile(s);
outfile.seekp(0);
int t1=1;
outfile.write(reinterpret_cast<char*>(&t1),sizeof(int));
vec_block tem;
outfile.seekp(sizeof(int));
outfile.write(reinterpret_cast<char*>(&tem),sizeof(vec_block));
}
opfile.open(s);
opfile.seekg(0);
opfile.read(reinterpret_cast<char*>(&max_num),sizeof(int));
opfile.seekg(sizeof(int));
vec_block temp;
opfile.read(reinterpret_cast<char*>(&temp),sizeof(vec_block));
q.front=nullptr;
q.rear=nullptr;
for(int i=0;i<100000;i++){
if(temp.data[i]==0)break;
q.enqueue(temp.data[i]);
}
}
};
#endif