-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
334 lines (317 loc) · 5.57 KB
/
hash.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include<iostream>
#include<bits/stdc++.h>
#include<fstream>
#include<string>
using namespace std;
struct bucket
{
string ip;
bucket* next;
};
bucket* newNode(string ip)
{
bucket* temp = new bucket;
temp->ip = ip;
temp->next = NULL;
return temp;
}
class Hash_Table
{
public:
Hash_Table();
bucket* chain[65536];
long long int hash_size;
int hash_func(string s);
int net_part(string s);
int byteSize();
bool search(string a);
bool searchRangeAll(string a, string b);
bool searchRangeAny(string a, string b);
void insert(string a);
void remove(string a);
};
Hash_Table::Hash_Table()
{
hash_size = 65536*sizeof(bucket);
for(int i=0;i<65536;i++)
{
chain[i]=new bucket;
chain[i]=NULL;
}
}
int Hash_Table::net_part(string s)
{
string s1 = "";
string s2 = "";
int count = 0;
for(int i = 0;count!=2;i++)
{
if(s[i] == '.')
{
count++;
continue;
}
if(count == 0)
{
s1 = s1 + s[i];
}
if(count == 1)
{
s2 = s2+s[i];
}
}
int x1=stoi(s1);
int x2=stoi(s2);
int value=x1*256+x2;
return value;
}
int Hash_Table::hash_func(string s)
{
int index;
string s1="";
string s2="";
int count=0;
for(int i=s.length()-1;count!=2;i--)
{
if(s[i] == '.')
{
count++;
continue;
}
if(count==0)
{
s2 = s[i] + s2 ;
}
if(count==1)
{
s1 = s[i] + s1 ;
}
}
int x1 = stoi(s1);
int x2 = stoi(s2);
index = x1*256 + x2;
return index;
}
void Hash_Table::insert(string a)
{
int index = hash_func(a);
bucket* node = newNode(a);
node->next = chain[index];
chain[index] = node;
hash_size += sizeof(bucket);
}
void Hash_Table::remove(string a)
{
int index = hash_func(a);
if(chain[index] == NULL)
return;
bucket* temp =chain[index];
if(chain[index]->ip==a){
temp =chain[index];
chain[index]=chain[index]->next;
hash_size -= sizeof(bucket);
delete temp;
return;
}
while(temp->next){
if(temp->next->ip==a){
bucket* node=temp->next;
temp->next=temp->next->next;
delete node;
hash_size-=sizeof(bucket);
return;
}
temp=temp->next;
}
}
bool Hash_Table::search(string a)
{
int index = hash_func(a);
if(chain[index] == NULL)
return 0;
bucket* temp = chain[index];
while(temp!=NULL)
{
if(temp->ip == a)
{
return 1;
}
temp = temp->next;
}
return 0;
}
bool Hash_Table::searchRangeAll(string a, string b)
{
int hosta = hash_func(a);
int hostb = hash_func(b);
int neta = net_part(a);
int netb = net_part(b);
long long int x1 = neta*256*256 + hosta;
long long int x2 = netb*256*256 + hostb;
if(x1 > x2)
{
int temp;
temp = hosta;
hosta = hostb;
hostb = temp;
temp = neta;
neta = netb;
netb = temp;
}
bool flag = true;
bucket* temp = new bucket;
while(neta < netb)
{
temp = chain[hosta];
flag = false;
while(temp!=NULL)
{
if(net_part(temp->ip) == neta)
{
flag = true;
break;
}
temp = temp->next;
}
if(flag == true)
{
hosta++;
if(hosta == 65536)
{
hosta = 0;
neta ++;
}
}
else
{
return false;
}
}
if(neta == netb)
{
while(hosta <= hostb)
{
flag = false;
temp = chain[hosta];
while(temp!=NULL)
{
if(net_part(temp->ip) == neta)
{
flag = true;
break;
}
temp = temp->next;
}
if(flag == true)
{
hosta ++;
}
else
{
return false;
}
}
}
return true;
}
bool Hash_Table::searchRangeAny(string a, string b)
{
int hosta = hash_func(a);
int hostb = hash_func(b);
int neta = net_part(a);
int netb = net_part(b);
long long int x1 = neta*256*256 + hosta;
long long int x2 = netb*256*256 + hostb;
if(x1 > x2)
{
int temp;
temp = hosta;
hosta = hostb;
hostb = temp;
temp = neta;
neta = netb;
netb = temp;
}
bool flag = true;
bucket* temp = new bucket;
while(neta < netb)
{
temp = chain[hosta];
flag = false;
while(temp!=NULL)
{
if(net_part(temp->ip) == neta)
{
return true;
}
temp = temp->next;
}
hosta++;
if(hosta==65536){
hosta=0;
neta++;
}
}
if(neta == netb)
{
while(hosta <= hostb)
{
flag = false;
temp = chain[hosta];
while(temp!=NULL)
{
if(net_part(temp->ip) == neta)
{
return true;
}
temp = temp->next;
}
hosta ++;
}
}
return false;
}
int Hash_Table::byteSize()
{
return hash_size;
}
int main()
{
Hash_Table DS;
/*
DS.insert("192.168.3.1");
cout<<DS.byteSize()<<endl;
DS.insert("192.168.3.2");
DS.insert("192.168.3.3");
DS.insert("192.168.3.4");*/
cout<<DS.byteSize()<<endl;
for(int k=0;k<256;k++){
string s="192." ;
s=s + to_string(k) +".";
for(int i=0;i<=255;i++){
string a=s+to_string(i)+".";
for(int j=0;j<256;j++){
string b=a+to_string(j);
DS.insert(b);
b=s;
}
a=s;
}}
/*DS.insert("192.168.3.5");
DS.insert("192.168.3.6");
DS.insert("192.168.3.7");
DS.insert("192.168.3.8");
DS.insert("192.168.3.9");
DS.insert("192.168.3.10");
DS.insert("255.168.255.253");
DS.insert("255.168.255.254");
DS.insert("255.168.255.255");
DS.insert("255.169.0.0");
DS.insert("255.169.0.1");
DS.insert("255.169.0.2");
DS.insert("255.169.0.3");*/
cout<<DS.byteSize()<<endl;
cout<<DS.searchRangeAny("192.255.0.0","192.255.255.255")<<endl;
DS.remove("192.255.255.128");
cout<<DS.searchRangeAll("192.255.0.0","192.255.255.255")<<endl;
}