What’s Fallacious with This Code
I’ve been looking to put in force the HashTable information construction in dart and I applied this code.
However some a part of this code isn’t operating and I will be able to’t see any downside however nonetheless, some a part of code isn’t operating. if someone can lend a hand I’d be very grateful.
[GistLink](https://gist.github.com/ayoubzulfiqar/50a0b0dfc290231642a332aba44b0d83)
elegance HashNode<Okay, V> {
Okay key;
V worth;
int hashCode;
HashNode<Okay, V>? subsequent;
HashNode(Okay key, V worth, int hashCode)
: this.key = key,
this.worth = worth,
this.hashCode = hashCode;
}
elegance HashTable<Okay, V> {
overdue Listing<HashNode<Okay, V>?> bucketArray;
overdue int numBuckets;
overdue int length;
HashTable() {
bucketArray = [];
numBuckets = 100;
length = 0;
for (int i = 0; i < numBuckets; i++) {
bucketArray.upload(null);
}
}
int sizes() {
go back length;
}
bool isEmpty() {
go back sizes() == 0;
}
int hashCodes(Okay key) {
go back Object.hashAll([key]);
}
int getBucketIndex(Okay key) {
// int hashCode = hashCode(key);
int hashCode = hashCodes(key);
int index = hashCode % numBuckets;
// key.hashCode() may well be detrimental.
index = index < 0 ? index * -1 : index;
go back index;
}
V? take away(Okay key) {
// Observe hash serve as to search out index for given key
int bucketIndex = getBucketIndex(key);
int hashCode = hashCodes(key);
// Get head of chain
HashNode<Okay, V>? head = bucketArray.elementAt(bucketIndex);
// Seek for key in its chain
HashNode<Okay, V>? prev = null;
whilst (head != null) {
// If Key discovered
if (head.key == key && hashCode == head.hashCode) wreck;
// Else stay transferring in chain
prev = head;
head = head.subsequent;
}
// If key used to be now not there
if (head == null) go back null;
// Cut back length
size–;
// Take away key
if (prev != null)
prev.subsequent = head.subsequent;
else
bucketArray.insert(bucketIndex, head.subsequent);
go back head.worth;
}
V? getKey(Okay key) {
// To find head of chain for given key
int bucketIndex = getBucketIndex(key);
int hashCode = hashCodes(key);
HashNode<Okay, V>? head = bucketArray.elementAt(bucketIndex);
// Seek key in chain
whilst (head != null) {
if (head.key == key && head.hashCode == hashCode) go back head.worth;
head = head.subsequent;
}
// If key now not discovered
print(“KeyAt ${key}”);
go back null;
}
void upload(Okay key, V worth) {
// To find head of chain for given key
int bucketIndex = getBucketIndex(key);
int hashCode = hashCodes(key);
HashNode<Okay, V>? head = bucketArray.elementAt(bucketIndex);
// Take a look at if key’s already provide
whilst (head != null) {
if (head.key == key && head.hashCode == hashCode) {
head.worth = worth;
go back;
}
head = head.subsequent;
}
// Insert key in chain
length++;
head = bucketArray.elementAt(bucketIndex);
HashNode<Okay, V> newNode = HashNode<Okay, V>(key, worth, hashCode);
newNode.subsequent = head;
bucketArray.insert(bucketIndex, newNode);
// If load issue is going past threshold, then
// double hash desk length
if ((1.0 * length) / numBuckets >= 0.7) {
Listing<HashNode<Okay, V>?> temp = bucketArray;
bucketArray = [];
numBuckets = 2 * numBuckets;
length = 0;
for (int i = 0; i < numBuckets; i++) bucketArray.upload(null);
for (HashNode<Okay, V>? headNode in temp) {
whilst (headNode != null) {
upload(headNode.key, headNode.worth);
headNode = headNode.subsequent;
}
}
}
print(“Key: ${key}” + ” ” + “Price: ${worth}”);
}
}
void major() {
HashTable hashTable = HashTable();
hashTable.hashCode;
hashTable.isEmpty();
hashTable.upload(10, “Object”);
}
View Reddit by means of ayoubzulfiqar – View Supply