这里写自定义目录标题
得分展示
对象定义
cpp
// 将一个磁盘划分为多个基于标签聚合的区块
class Block{
public:
int tag = 0; // 区块标签
int start_pos;
int end_pos;
int id;
int use_size = 0;
int v;
// 为区块确定范围
Block(int id, int v) : id(id), v(v){
start_pos = (id - 1) * BLOCK_SIZE + 1;
end_pos = min(id * BLOCK_SIZE, v);
}
};
class Object{
public:
int disk[REP_NUM + 1]; // 对象的三个副本磁盘 id
int *unit[REP_NUM + 1]; // 每个副本中对象块的存储单元索引
int obj_id = 0;
int obj_size = 0;
int obj_tag = 0;
int last_request_point = 0; // 挂接未完成请求链头
bool is_del = false;
bool *block_read_status = nullptr;
Object() {
for (int i = 1; i <= REP_NUM; i++){
unit[i] = new int[MAX_DISK_SIZE];
}
}
};
class Disk{
public:
int id = 0;
pair<int, int> item[MAX_DISK_SIZE];
int p = 1; // 磁头的位置
char last_action = '$';
int last_token_cost = 0;
int used_tokens = 0;
int disk_size = 0;
vector<pair<int, int>> done_request;
vector<int> request; // 这个磁盘中的请求
vector<Block> disk_blocks;
int block_group_num;
void initBlock(int num, int v){
block_group_num = num;
disk_blocks.push_back(Block(0, 0));
for (int i = 1; i <= block_group_num; i++){
disk_blocks.push_back(Block(i, v));
}
}
Disk(){
memset(item, 0, sizeof(item));
}
int calculateReadToken(Disk &disk){
if (disk.last_action != 'r'){
return 64;
}else{
float token = disk.last_token_cost * 0.8f;
token = ceil(token);
return max(16, int(token));
}
}
};
class Request{
public:
int req_id = 0;
int obj_id = 0;
int prev_id = 0;
int arri_time = 0;
bool is_done = false;
float score = 0.0f;
bool *block_read_status = nullptr;
};
请求价值计算
cpp
float get_score(int x){
if (x <= 10){
return -0.005 * x + 1.0;
}else if(x <= 105){
return -0.01 * x + 1.05;
}else{
return -1.0;
}
}
时间同步
cpp
void handle_timestamp(){
string t;
cin >> t >> current_time;
cout << "TIMESTAMP " << current_time << endl;
fflush(stdout);
}
删除操作
cpp
void delete_action(){
// 1 读取删除的个数
int n_delete;
cin >> n_delete;
// 2 读取并记录所有删除的 对象id
vector<int> deletes(n_delete);
for (int i = 0; i < n_delete; i++)
cin >> deletes[i];
// 3 计算所有的终止 读请求 id
vector<int> abort_reqs;
for (int id : deletes){
int curr_id = objects[id].last_request_point;
while (curr_id != 0){
if (!requests[curr_id].is_done){
abort_reqs.push_back(curr_id);
requests[curr_id].is_done = true;
}
curr_id = requests[curr_id].prev_id;
}
}
// 4 输出终止的请求个数
cout << int(abort_reqs.size()) << endl;
// 5 输出所有的终止请求 id
for (int req_id : abort_reqs){
cout << req_id << endl;
}
// 6 执行删除操作
for (int id : deletes){
int curr_id = objects[id].last_request_point;
while (curr_id != 0){
if (!requests[curr_id].is_done){
cout << curr_id << endl;
}
curr_id = requests[curr_id].prev_id;
}
for (int rep = 1; rep <= REP_NUM; rep++){
int disk_id = objects[id].disk[rep];
if (disk_id < 1 || disk_id > N)
continue;
do_object_delete(objects[id].unit[rep], disks[disk_id], objects[id].obj_size);
}
objects[id].is_del = true;
}
fflush(stdout);
}