-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine.h
More file actions
289 lines (260 loc) · 6.53 KB
/
Copy pathMachine.h
File metadata and controls
289 lines (260 loc) · 6.53 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef MACHINE_H_
#define MACHINE_H_
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
#include "AVL.h"
template <class T>
class Finger_Table;
// Machine Class Implementation
template<class T>
class machine
{
private:
AVL_Tree<T> tree;
Finger_Table<T> Table;
ofstream machine_data;
T data_file_name;
int counter;
public:
int limit;
int machine_no;
machine* next_machine;
machine()
{
limit = 0;
counter = 0;
machine_no = 0;
next_machine = NULL;
data_file_name = "";
}
// Set machine_no
void set_machine_no(int no)
{
machine_no = no;
}
// Make unique file for each machine
void make_file()
{
T a = "machine";
a += to_string(machine_no);
a += ".txt"; // machine0.txt
data_file_name = a;
machine_data.open(a);
}
// Make extra files, if First file make more than 100 lines of data
void make_extra_file()
{
counter++;
T a = "machine";
a += to_string(machine_no);
a += "_";
a += to_string(counter);
a += ".txt"; // machine0_1.txt
data_file_name = a;
machine_data.close();
machine_data.clear();
machine_data.open(data_file_name);
return;
}
// Function to insert Key_Value parir in Machine
void insert(T val, T data)
{
machine_data << data << endl;
ifstream inputFile;
inputFile.open(data_file_name);
int line_no = 0;
T line = "";
while (!inputFile.eof())
{
getline(inputFile, line);
line_no++;
if (line == data)
{
break;
}
}
tree.insert(val, line_no, data_file_name);
}
// Function to reterive data from machine
T reterive(T val)
{
T data = "";
int line = tree.Find(val);
T file_name = tree.Find_File(val);
ifstream inputFile;
inputFile.open(file_name);
int line_no = 0;
T lines = "";
if (line == 0)
{
return "NO Data";
}
while (!inputFile.eof())
{
getline(inputFile, lines);
line_no++;
if (line == line_no)
{
data = lines;
}
}
return data;
}
// Function to mange Finger_Table for machine
void make_Finger_Table(int n, machine<T>* list)
{
Table.make_Table(n, list, this->machine_no);
}
// Function to display Finger_Table of machine
void display_Table(int a)
{
Table.display(a);
}
// Function to swap data between two machines
void swap_data(machine<T>* temp, machine<T>* temp1)
{
swap(temp->machine_no, temp1->machine_no);
swap(temp->data_file_name, temp1->data_file_name);
swap(temp->machine_data, temp1->machine_data);
swap(temp->Table, temp1->Table);
swap(temp->tree, temp1->tree);
}
// Function to reterive machine pointer from Finger Table
machine<T>* reterive_using_Lookup(int n)
{
return Table.reterive(n);
}
// Function to print AVL Tree of each machine
void print_AVL()
{
tree.display();
}
// Function to Shift_AVL_Tree_Data
void Shift_AVL_Tree_Data(machine<T>* a)
{
tree.shift_data_from_different_AVL_Tree(a->tree);
}
// Function to delete AVL Tree Node
void delete_data_from_AVL_Tree(T key)
{
tree.remove_node(key);
}
~machine()
{
}
};
template <class T>
class machine;
// Basic Structure of Table_Node
template <class T>
struct Table_Node
{
int no;
machine<T>* machine_addr;
Table_Node* next;
Table_Node* prev;
Table_Node(int val)
{
no = val;
machine_addr = NULL;
next = NULL;
prev = NULL;
}
};
// Finger_Table Class
template <class T>
class Finger_Table
{
private:
int flag;
Table_Node<T>* head;
Table_Node<T>* tail;
public:
Finger_Table()
{
flag = 0;
head = NULL;
tail = NULL;
}
// Function to make Table for each Machine
void make_Table(int n, machine<T>* active_machine, int machine_no)
{
if (flag > 0)
{
head = NULL;
tail = NULL;
}
int no = 1;
for (int i = 0; i < n; i++)
{
Table_Node<T>* temp = new Table_Node<T>(no);
if (head == NULL)
{
head = tail = temp;
}
else {
temp->prev = tail;
tail->next = temp;
tail = temp;
}
int flag = true;
int m = machine_no + pow(2, i);
machine<T>* active = active_machine;
while (active->next_machine != active_machine)
{
if (m <= active->machine_no)
{
temp->machine_addr = active;
flag = false;
break;
}
active = active->next_machine;
}
if (m <= active->machine_no && flag == true)
{
temp->machine_addr = active;
flag = false;
}
if (flag == true)
{
temp->machine_addr = active_machine;
}
no++;
}
flag++;
}
// Function to display Finger_Table
void display(int a)
{
cout << "Finger Table of machine " << a << " : " << endl;
Table_Node<T>* temp = head;
while (temp != NULL)
{
cout << temp->no << " " << temp->machine_addr << " " << temp->machine_addr->machine_no << endl;
temp = temp->next;
}
}
// Function to reterive machine pointer from machine
machine<T>* reterive(int no)
{
Table_Node<T>* temp = head;
while (temp != NULL)
{
if (no <= temp->machine_addr->machine_no)
{
return temp->machine_addr;
}
temp = temp->next;
}
return NULL;
}
~Finger_Table()
{
delete head;
delete tail;
}
};
#endif