FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaDataStructures/HashTable/HashTableInterface.java at master · AbduAlmenan/JavaDataStructures · GitHub
AbduAlmenan
/
JavaDataStructures
Public
forked from
benaich/JavaDataStructures
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaDataStructures
/
HashTable
/
HashTableInterface.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
172 lines (143 loc) · 4.91 KB
Breadcrumbs
JavaDataStructures
/
HashTable
/
HashTableInterface.java
Copy path
File metadata and controls
172 lines (143 loc) · 4.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
HashTable
;
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
File
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
IOException
;
import
java
.
util
.
Random
;
import
java
.
util
.
function
.
Function
;
public
interface
HashTableInterface
<
K
,
V
> {
public
static
enum
HASH_FUNCTION
{
UNIVERSAL
,
MODULO
,
MULTIPLICATIF
};
public
static
final
int
INITIAL_TABLE_SIZE
=
10
;
public
static
final
int
MAX_TABLE_SIZE
=
10000000
;
public
static
final
double
LOAD_FACTOR
=
0.6
;
public
static
final
int
BIGNUMBER
=
9161
;
public
static
final
Random
r
=
new
Random
();
public
static
final
String
BLACK_CIRCLE
=
"
\u25CF
"
;
public
static
final
double
GOLDEN_RATIO
= (
1
+
Math
.
sqrt
(
5
)) /
2
;
/* get the position of key */
int
indexOf
(
K
key
);
/* check whether the specified key exist in the hashtable */
boolean
contains
(
K
key
);
/* update or insert a new item to the hashtable */
void
put
(
K
key
,
V
val
);
/* get the value of the key*/
V
get
(
K
key
);
/* remove the specified item */
void
delete
(
K
key
);
/* remove all data from the hashtable */
void
clear
();
/* Double the size of the hashtable */
void
resize
();
/* get the number of elements in the table */
int
getSize
();
/* get the table size */
int
getTableSize
();
/* get the number of collisions */
int
getNumCollisions
();
/* get the number of resizes */
int
getNumResizes
();
/* get the current load factor */
default
float
loadFactor
() {
return
getSize
() / (
float
)
getTableSize
();
}
/* return de distrubution of the hash table */
public
double
getDistribution
();
/* print the table */
void
print
();
/* print the table like a "graph" */
void
printGraph
();
/* print the table curent status */
default
void
printStatus
() {
System
.
out
.
println
(
"------------- Table Status -------------------"
);
System
.
out
.
println
(
"Table size : "
+
getTableSize
() +
" Actual size : "
+
getSize
());
//System.out.println("Numbre of collisions : " + getNumCollisions());
//System.out.println("Numbre of resises : " + getNumResizes());
System
.
out
.
println
(
"Deviation : "
+
getDistribution
());
}
/* return a random universal hashing function */
default
Function
<
K
,
Integer
>
getHashingFunction
() {
return
getHashingFunction
(
HASH_FUNCTION
.
UNIVERSAL
);
}
/* return a hashing function (modulo, universal or multiplicatif) */
default
Function
<
K
,
Integer
>
getHashingFunction
(
HASH_FUNCTION
type
) {
Function
<
K
,
Integer
>
f
;
if
(
type
==
HASH_FUNCTION
.
MODULO
) {
f
=
x
->
Math
.
abs
(
x
.
hashCode
()) %
getTableSize
();
}
else
if
(
type
==
HASH_FUNCTION
.
UNIVERSAL
) {
int
a
,
b
,
p
;
do
{
a
=
r
.
nextInt
();
b
=
r
.
nextInt
();
}
while
(
a
==
0
||
b
==
0
);
final
int
r1
=
a
,
r2
=
b
;
p
=
nextPrime
(
BIGNUMBER
);
f
=
x
->
Math
.
abs
(((
r1
*
x
.
hashCode
() +
r2
) %
p
) %
getTableSize
());
}
else
{
f
=
x
-> {
double
c
=
GOLDEN_RATIO
-
1
;
double
tmp
=
x
.
hashCode
() *
c
;
return
(
int
) (((
Math
.
abs
(
tmp
- (
int
)
tmp
))) *
getTableSize
());
};
}
return
f
;
}
/* get the next prime number after a*/
static
int
nextPrime
(
int
a
) {
while
(!
isPrime
(
a
)) {
a
++;
}
return
a
;
}
/* check if a number is prime */
static
boolean
isPrime
(
int
a
) {
if
(
a
==
2
) {
return
true
;
}
else
if
(
a
%
2
==
0
) {
return
false
;
}
else
{
int
i
=
3
,
end
= (
int
)
Math
.
sqrt
(
a
+
0.0
);
while
(
a
%
i
!=
0
&&
i
<=
end
) {
i
=
i
+
2
;
}
return
i
>
end
;
}
}
/* return a random string (for testing) */
static
String
randomString
() {
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
10
;
i
++) {
char
c
= (
char
) (
r
.
nextInt
((
int
) (
Character
.
MAX_VALUE
)));
sb
.
append
(
c
);
}
return
sb
.
toString
();
}
static
void
importFromFile
(
File
file
,
HashTableInterface
<
String
,
String
>
ht
) {
BufferedReader
reader
;
try
{
reader
=
new
BufferedReader
(
new
FileReader
(
file
));
String
line
;
while
((
line
=
reader
.
readLine
()) !=
null
) {
String
[]
parts
=
line
.
split
(
"
\\
s"
);
ht
.
put
(
parts
[
0
],
parts
[
1
]);
}
reader
.
close
();
}
catch
(
IOException
ex
) {
System
.
out
.
println
(
"file not found"
);
}
}
static
double
getStdDeviation
(
int
[]
data
) {
double
sum
=
0L
;
for
(
double
a
:
data
) {
sum
+=
a
;
}
double
mean
=
sum
/
data
.
length
;
sum
=
0L
;
for
(
double
a
:
data
) {
sum
+= (
mean
-
a
) * (
mean
-
a
);
}
return
Math
.
sqrt
(
sum
/
data
.
length
);
}
}
Back
|
FazBrowse Home
|
New Git URL