Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hashing_lab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brady James Garvin
hashing_lab
Commits
580461a3
Commit
580461a3
authored
6 years ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Updated starter code for 2018 lab.
parent
14e416f6
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
index.html
+2
-1
2 additions, 1 deletion
index.html
js/hash_table.js
+17
-4
17 additions, 4 deletions
js/hash_table.js
js/index.js
+10
-2
10 additions, 2 deletions
js/index.js
with
29 additions
and
7 deletions
index.html
+
2
−
1
View file @
580461a3
...
@@ -9,7 +9,8 @@
...
@@ -9,7 +9,8 @@
</head>
</head>
<body>
<body>
<div
id=
"result"
></div>
<div
id=
"time"
></div>
<div
id=
"freeness"
></div>
<script
src=
"libraries/jquery/jquery.min.js"
></script>
<script
src=
"libraries/jquery/jquery.min.js"
></script>
<script
src=
"js/hash_table.js"
></script>
<script
src=
"js/hash_table.js"
></script>
<script
src=
"js/words.js"
></script>
<script
src=
"js/words.js"
></script>
...
...
This diff is collapsed.
Click to expand it.
js/hash_table.js
+
17
−
4
View file @
580461a3
...
@@ -60,6 +60,19 @@ class HashTable {
...
@@ -60,6 +60,19 @@ class HashTable {
return
this
.
_size
;
return
this
.
_size
;
}
}
get
collisionFreeness
()
{
if
(
this
.
_size
===
0
)
{
return
1.0
;
}
let
occupiedBucketCount
=
0
;
for
(
const
bucket
of
this
.
_buckets
)
{
if
(
bucket
.
length
>
0
)
{
++
occupiedBucketCount
;
}
}
return
occupiedBucketCount
/
this
.
_size
;
}
has
(
element
)
{
has
(
element
)
{
return
this
.
_buckets
[
this
.
_hashFunction
(
element
)].
includes
(
element
);
return
this
.
_buckets
[
this
.
_hashFunction
(
element
)].
includes
(
element
);
}
}
...
@@ -68,12 +81,12 @@ class HashTable {
...
@@ -68,12 +81,12 @@ class HashTable {
const
bucket
=
this
.
_buckets
[
this
.
_hashFunction
(
element
)];
const
bucket
=
this
.
_buckets
[
this
.
_hashFunction
(
element
)];
if
(
!
bucket
.
includes
(
element
))
{
if
(
!
bucket
.
includes
(
element
))
{
bucket
.
push
(
element
);
bucket
.
push
(
element
);
}
++
this
.
_size
;
++
this
.
_size
;
if
(
this
.
_size
/
this
.
_buckets
.
length
>
MAXIMUM_LOAD_FACTOR
)
{
if
(
this
.
_size
/
this
.
_buckets
.
length
>
MAXIMUM_LOAD_FACTOR
)
{
this
.
_resize
();
this
.
_resize
();
}
}
}
}
}
delete
(
element
)
{
delete
(
element
)
{
const
bucket
=
this
.
_buckets
[
this
.
_hashFunction
(
element
)];
const
bucket
=
this
.
_buckets
[
this
.
_hashFunction
(
element
)];
...
...
This diff is collapsed.
Click to expand it.
js/index.js
+
10
−
2
View file @
580461a3
...
@@ -4,11 +4,19 @@
...
@@ -4,11 +4,19 @@
const
startTime
=
Date
.
now
();
const
startTime
=
Date
.
now
();
const
table
=
new
HashTable
((
word
)
=>
0
);
const
table
=
new
HashTable
((
word
)
=>
0
);
// TODO: Design a better hash function
for
(
const
word
of
DICTIONARY
)
{
for
(
const
word
of
DICTIONARY
)
{
table
.
add
(
word
);
table
.
add
(
word
);
}
}
for
(
const
word
of
DICTIONARY
)
{
if
(
!
table
.
has
(
word
))
{
throw
new
Error
(
`Found an inconsistency in the hash function when applying it to "
${
word
}
"`
);
}
}
const
endTime
=
Date
.
now
();
const
endTime
=
Date
.
now
();
$
(
'
#result
'
).
text
(
`Constructed hash table of American English words in
${
endTime
-
startTime
}
ms.`
);
$
(
'
#time
'
).
text
(
`Constructed and verified a hash table of American English words in
${
endTime
-
startTime
}
ms.`
);
$
(
'
#freeness
'
).
text
(
`Collision freeness using this hash function:
${(
100
*
table
.
collisionFreeness
).
toFixed
(
3
)}
%.`
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment