Skip to content
Snippets Groups Projects
Commit 580461a3 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Updated starter code for 2018 lab.

parent 14e416f6
No related branches found
No related tags found
No related merge requests found
...@@ -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>
......
...@@ -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)];
......
...@@ -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)}%.`);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment