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
|
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include"minheap.h"
#include"rbtree.h"
using namespace std;
RBtree rbt;
minHeap heap;
tree mark=NULL;
job running_job=NULL;
int cur_time = 0;
int running_time = 0;
fstream output("output.txt", fstream::out | ios_base::trunc);
void out(tree t) {
//cout << "(" << t->key << "," << t->execute_time << "," << t->total_time << ")\n";
output << "(" << t->key << "," << t->execute_time << "," << t->total_time << ")";
}
void out(job j) {
//cout << "(" << j->jobID << "," << j->execute_time << "," << j->total_time << ")\n";
output << "(" << j->jobID << "," << j->execute_time << "," << j->total_time << ")";
}
void Run() {
if (heap.Empty())
return;
if (running_job == NULL) {
running_job = heap.jobs[ROOT];
}
if (mark == NULL || mark->key != running_job->jobID)
mark = rbt.search(running_job->jobID);
running_job->execute_time++;
mark->execute_time++;
running_time++;
if (running_job->execute_time == running_job->total_time) {
running_time = 0;
running_job = NULL;
rbt.delnode(mark);
heap.Pop();
}
else if(running_time==5){
running_time = 0;
heap.ShiftDown(ROOT);
running_job = heap.jobs[ROOT];
}
//printf("At %d ms: Run!\n", cur_time);
}
void InsertJob(int jobID, int total_time) {
heap.InsertHeap(jobID, total_time);
rbt.insert(jobID, total_time);
if (running_time == 0) {
running_job = heap.jobs[ROOT];
}
//printf("At %d ms: Insert! jobID: %d total_time: %d\n", cur_time, jobID, total_time);
}
void PrintJob(int jobID) {
tree res = rbt.search(jobID);
out(res);
output << "\n";
//printf("At %d ms: PrintJob! jobID: %d\n", cur_time, jobID);
}
void PrintJob(int low, int high) {
vector<Job*> output_vec;
int tot = 0;
for (int i = ROOT; i <= heap.len; i++) {
if (low <= heap.jobs[i]->jobID&&heap.jobs[i]->jobID <= high) {
output_vec.push_back(heap.jobs[i]);
tot++;
}
}
for (int i = 0; i < output_vec.size(); i++){
out(output_vec[i]);
if (i == (output_vec.size()-1)) break;
output << ",";
}
if (!tot)
out(empty_node());
output << "\n";
}
void NextJob(int jobID) {
tree res = rbt.upper_bound(jobID);
out(res);
output << "\n";
//printf("At %d ms: NextJob! jobID: %d\n", cur_time, jobID);
}
void PreviousJob(int jobID) {
tree res = rbt.lower_bound(jobID);
out(res);
output << "\n";
//printf("At %d ms: PreviousJob! jobID: %d\n", cur_time, jobID);
}
bool in(string s,string p) {
return s.find(p) != -1;
}
void run2present(int run_time) {
while (cur_time < run_time)
Run(), cur_time++;
}
void LoadJobs(const string &path) {
string s;
ifstream input(path);
int jobID, low, high, total_time, run_time;
while (getline(input, s)) {
if (in(s, "Insert")) {
sscanf(s.c_str(), "%d: Insert(%d,%d)", &run_time, &jobID, &total_time);
run2present(run_time);
InsertJob(jobID, total_time);
}
else if (in(s, "PreviousJob")) {
sscanf(s.c_str(), "%d: PreviousJob(%d)", &run_time, &jobID);
run2present(run_time);
PreviousJob(jobID);
}
else if (in(s, "NextJob")) {
sscanf(s.c_str(), "%d: NextJob(%d)", &run_time, &jobID);
run2present(run_time);
NextJob(jobID);
}
else if (in(s, "PrintJob")) {
if (in(s, ",")) {
sscanf(s.c_str(), "%d: PrintJob(%d,%d)", &run_time, &low, &high);
run2present(run_time);
PrintJob(low, high);
}
else {
sscanf(s.c_str(), "%d: PrintJob(%d)", &run_time, &jobID);
run2present(run_time);
PrintJob(jobID);
}
}
}
}
int main(int argc, char* argv[]){
LoadJobs(argv[1]);
output.close();
//getchar();
return 0;
}
|