-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.cpp
209 lines (168 loc) · 4.83 KB
/
buffer.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include"buffer.h"
#include<iostream>
#include<algorithm>
#define siz 16
// HashQueue
std::unordered_map <int,std::vector<bufferHeader>> HashQueue;
//vector of bufferheader in freeList
std::vector<bufferHeader> freeList;
std::unordered_map<int,bufferHeader> map;
// initialization function
void init(){
bufferHeader* tmp = new bufferHeader(-1,1,1);
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
HashQueue[i].emplace_back(*tmp);
}
}
for(int i=0; i<16; i++) freeList.emplace_back(*tmp);
}
//---------------------HASHQUEUE METHODS---------//
//Print all the blocks present in hashqueue
void displayHQ(){
cout<<"\n---------HASH_QUEUE---------\n\n";
bool check =false;
for(int i=0; i<4; i++){
if(HashQueue[i].begin()->blk_num!=-1){
check=true;
break;
}
}
if(check){
for(int i=0;i<4;i++){
vector<bufferHeader> tmp = HashQueue[i];
cout<<i<<"->";
auto it = tmp.begin();
for (int j=0; j< tmp.size() ; j++){
if(it->blk_num!=-1)
std::cout<<"|"<<it->blk_num<<"|->";
it++;
}
std::cout<<"\n";
}
}else{
for(int i=0;i<4;i++){
vector<bufferHeader> tmp = HashQueue[i];
cout<<i<<"->";
auto it = tmp.begin();
for (int j=0; j< tmp.size() ; j++){
std::cout<<" |"<<it->blk_num<<"|->";
it++;
}
std::cout<<"\n";
}
}
std::cout<<"\n";
}
// Add block to hash queue
void addToHQ(int blk_num,int dNum,int status){
bufferHeader *block = new bufferHeader(blk_num,dNum, status);
int hash = (dNum+blk_num)%4;
// if the block is not found in hashqueue then only insert it into hashqueue
if(!searchHQ(blk_num)){
HashQueue[hash].push_back(*block);
}
}
// delete block from hash queue
void deleteFromHQ(int blk_num){
int hash = (blk_num+1)%4;
for(auto i = HashQueue[hash].begin() ; i!= HashQueue[hash].end() ; i++){
if(i->blk_num==blk_num){
HashQueue[hash].erase(i);
return;
}
}
}
// search block in hash queue
bool searchHQ(int blk_num){
int hash = (blk_num+1)%4;
vector<bufferHeader> tmp = HashQueue[hash];
for(auto bfheader = tmp.begin() ; bfheader!=tmp.end() ;bfheader++){
if(bfheader->blk_num==blk_num){
return true;
}
}
return false;
}
// check hash queue is empty or not
bool emptyHQ(){
return HashQueue.empty();
}
//---------------------FREELIST METHODS---------//
// add to tail in the freelist
void addToTailFL(int blk_num,int dNum,int status){
bufferHeader *block = new bufferHeader(blk_num,dNum, status);
freeList.push_back(*block);
}
// add to head in freelist
void addToHeadFL(int blk_num,int dNum,int status){
bufferHeader *block = new bufferHeader(blk_num,dNum, status);
freeList.insert(freeList.begin(),*block);
}
// delete from head in freelist
bufferHeader deleteToheadFL(){
bufferHeader tmp;
if(!emptyFL()){
tmp = freeList[0];
freeList.erase(freeList.begin());
}
return tmp;
}
// get the head content of freelist
bufferHeader getHeadToFL(){
// if freelist is not empty then return head block otherwise, return dummy block
return !freeList.empty() ? freeList[0] : *new bufferHeader(-1,-1,-1);
}
// delete from any position in freelist
bufferHeader deleteAnyPositionFL(int blk_num){
bufferHeader tmp;
for(auto i=freeList.begin() ; i!=freeList.end() ; i++){
if(i->blk_num == blk_num){
tmp = *i;
freeList.erase(i);
return tmp;
}
}
return tmp;
}
// search block in freelist
bool searchFL(int blk_num){
for(auto i=freeList.begin() ; i!=freeList.end() ; i++){
if(i->blk_num == blk_num){
return true;
}
}
return false;
}
// display the block of freelist
void displayFL(){
cout<<"\n-----------FREE_LIST-----------\n";
auto it=freeList.begin();
for(int j=0; j<16; j++){
std::cout<<"->|"<<it->blk_num<<"|";
it++;
}
std::cout<<"\n\n";
}
// check whether freelist is empty or not
bool emptyFL(){
return freeList.empty();
}
// get status of the block
int getStatus(int blk_num){
for(bufferHeader bh : HashQueue[(blk_num+1)%4]){
if(bh.blk_num==blk_num){
return bh.status;
}
}
}
// set status of the block
void setStatus(int blk_num, int sts){
for(bufferHeader bh : HashQueue[(blk_num+1)%4]){
if(bh.blk_num==blk_num){
deleteFromHQ(blk_num);
addToHQ(blk_num,1,sts);
break;
}
}
}