-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTask.java
More file actions
178 lines (167 loc) · 5.13 KB
/
Copy pathHashTask.java
File metadata and controls
178 lines (167 loc) · 5.13 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
import java.util.*;
import java.security.*;
//Alexius Adhitya K_18/424179/PA/181284
public class HashTask{
public static void main(String[] args){
Hash fooHash=new Hash(2);
boolean stop=false;
while(!stop){
System.out.println("Hash Operation Menu");
System.out.println("1. Input");
System.out.println("2. Search");
System.out.println("3. Exit");
System.out.print("Menu ");
Scanner option=new Scanner(System.in);
int choose=option.nextInt();
switch(choose){
case 1:
System.gc();
int counter=1;
boolean isPrime;
int size;
Scanner inSize;
do{
System.out.print("Table size (Prime number): ");
inSize=new Scanner(System.in);
size=inSize.nextInt();
isPrime=checkPrime(size);
}while(!isPrime);
System.out.println();
boolean isSmaller;
int data;
Scanner inData;
do{
System.out.print("Data count (<=Table size): ");
inData=new Scanner(System.in);
data=inData.nextInt();
isSmaller=true;
if(data>size){
isSmaller=false;
}
}while(!isSmaller);
fooHash=new Hash(size);
SecureRandom randomer=new SecureRandom();
while(counter<=data){
fooHash.hashIn(randomer.nextInt(999999999));
++counter;
}
fooHash.rehashIn();
System.out.println();
fooHash.showHash();
System.out.println();
System.out.print("Probing Attempt: ");
System.out.printf("%.3f",fooHash.probeStats());
System.out.println();
break;
case 2:
System.out.print("Query: ");
Scanner inQuery=new Scanner(System.in);
int query=inQuery.nextInt();
if(fooHash.searchHash(query)==true){
System.out.println("Data found!");
}
else{
System.out.println("Data not found!");
}
break;
default:
stop=true;
option.close();
break;
}
System.out.println();
}
}
private static boolean checkPrime(int query){
boolean isPrime=true;
if(query==0 || query==1){
isPrime=false;
}
else if(query==2 || query==3){
isPrime=true;
}
else{
for(int div=2;div<=query/2;div++){
if(query%div==0){
isPrime=false;
break;
}
}
}
return isPrime;
}
}
class Hash extends HashTest{
private int[] hashTable;
private ArrayList queue=new ArrayList();
private int length;
private int qLine=0;
private float dataIn=0;
private float probeTry=0;
public Hash(int length){
this.length=length;
hashTable=new int[length];
}
public void showHash(){
for(int counter=0;counter<length;counter++){
System.out.print(hashTable[counter]+" ");
}
System.out.println();
}
public void hashIn(int input){
++dataIn;
if(hashTable[input%length]==0){
hashTable[input%length]=input;
++probeTry;
}
else{
queueIn(input);
}
}
public void rehashIn(){
for(int counter=0;counter<queue.size();counter++){
int e=(int) queue.get(counter);
linearHandle(e,e%length);
}
queue.clear();
}
private void queueIn(int input){
queue.add(input);
}
private void linearHandle(int input,int pos){
boolean stop=false;
while(!stop){
++probeTry;
pos++;
if(hashTable[pos%length]==0){
hashTable[pos%length]=input;
stop=true;
}
}
}
public float probeStats(){
float stats=probeTry/dataIn;
return stats;
}
public boolean searchHash(int query){
boolean found=false;
int pos=query%length;
if(hashTable[pos]==query){
found=true;
}
else{
int count=length-1;
while(count>=0){
if(++pos>length-1){
pos=pos%length;
}
if(hashTable[pos]==query){
found=true;
break;
}
--count;
}
}
return found;
}
}