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
Branches
No related tags found
No related merge requests found
......@@ -9,7 +9,8 @@
</head>
<body>
<div id="result"></div>
<div id="time"></div>
<div id="freeness"></div>
<script src="libraries/jquery/jquery.min.js"></script>
<script src="js/hash_table.js"></script>
<script src="js/words.js"></script>
......
......@@ -60,6 +60,19 @@ class HashTable {
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) {
return this._buckets[this._hashFunction(element)].includes(element);
}
......@@ -68,12 +81,12 @@ class HashTable {
const bucket = this._buckets[this._hashFunction(element)];
if (!bucket.includes(element)) {
bucket.push(element);
}
++this._size;
if (this._size / this._buckets.length > MAXIMUM_LOAD_FACTOR) {
this._resize();
}
}
}
delete(element) {
const bucket = this._buckets[this._hashFunction(element)];
......
......@@ -4,11 +4,19 @@
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) {
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();
$('#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