mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-26 11:32:31 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
139 lines
3.5 KiB
Dart
139 lines
3.5 KiB
Dart
/**
|
|
* File: hash_map_chaining.dart
|
|
* Created Time: 2023-06-24
|
|
* Author: liuyuxin (gvenusleo@gmail.com)
|
|
*/
|
|
|
|
import 'array_hash_map.dart';
|
|
|
|
/* Hash table with separate chaining */
|
|
class HashMapChaining {
|
|
late int size; // Number of key-value pairs
|
|
late int capacity; // Hash table capacity
|
|
late double loadThres; // Load factor threshold for triggering expansion
|
|
late int extendRatio; // Expansion multiplier
|
|
late List<List<Pair>> buckets; // Bucket array
|
|
|
|
/* Constructor */
|
|
HashMapChaining() {
|
|
size = 0;
|
|
capacity = 4;
|
|
loadThres = 2.0 / 3.0;
|
|
extendRatio = 2;
|
|
buckets = List.generate(capacity, (_) => []);
|
|
}
|
|
|
|
/* Hash function */
|
|
int hashFunc(int key) {
|
|
return key % capacity;
|
|
}
|
|
|
|
/* Load factor */
|
|
double loadFactor() {
|
|
return size / capacity;
|
|
}
|
|
|
|
/* Query operation */
|
|
String? get(int key) {
|
|
int index = hashFunc(key);
|
|
List<Pair> bucket = buckets[index];
|
|
// Traverse bucket, if key is found, return corresponding val
|
|
for (Pair pair in bucket) {
|
|
if (pair.key == key) {
|
|
return pair.val;
|
|
}
|
|
}
|
|
// If key is not found, return null
|
|
return null;
|
|
}
|
|
|
|
/* Add operation */
|
|
void put(int key, String val) {
|
|
// When load factor exceeds threshold, perform expansion
|
|
if (loadFactor() > loadThres) {
|
|
extend();
|
|
}
|
|
int index = hashFunc(key);
|
|
List<Pair> bucket = buckets[index];
|
|
// Traverse bucket, if specified key is encountered, update corresponding val and return
|
|
for (Pair pair in bucket) {
|
|
if (pair.key == key) {
|
|
pair.val = val;
|
|
return;
|
|
}
|
|
}
|
|
// If key does not exist, append key-value pair to the end
|
|
Pair pair = Pair(key, val);
|
|
bucket.add(pair);
|
|
size++;
|
|
}
|
|
|
|
/* Remove operation */
|
|
void remove(int key) {
|
|
int index = hashFunc(key);
|
|
List<Pair> bucket = buckets[index];
|
|
// Traverse bucket and remove key-value pair from it
|
|
for (Pair pair in bucket) {
|
|
if (pair.key == key) {
|
|
bucket.remove(pair);
|
|
size--;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Expand hash table */
|
|
void extend() {
|
|
// Temporarily store the original hash table
|
|
List<List<Pair>> bucketsTmp = buckets;
|
|
// Initialize expanded new hash table
|
|
capacity *= extendRatio;
|
|
buckets = List.generate(capacity, (_) => []);
|
|
size = 0;
|
|
// Move key-value pairs from original hash table to new hash table
|
|
for (List<Pair> bucket in bucketsTmp) {
|
|
for (Pair pair in bucket) {
|
|
put(pair.key, pair.val);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Print hash table */
|
|
void printHashMap() {
|
|
for (List<Pair> bucket in buckets) {
|
|
List<String> res = [];
|
|
for (Pair pair in bucket) {
|
|
res.add("${pair.key} -> ${pair.val}");
|
|
}
|
|
print(res);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Driver Code */
|
|
void main() {
|
|
/* Initialize hash table */
|
|
HashMapChaining map = HashMapChaining();
|
|
|
|
/* Add operation */
|
|
// Add key-value pair (key, value) to the hash table
|
|
map.put(12836, "Xiao Ha");
|
|
map.put(15937, "Xiao Luo");
|
|
map.put(16750, "Xiao Suan");
|
|
map.put(13276, "Xiao Fa");
|
|
map.put(10583, "Xiao Ya");
|
|
print("\nAfter adding is complete, hash table is\nKey -> Value");
|
|
map.printHashMap();
|
|
|
|
/* Query operation */
|
|
// Input key into hash table to get value
|
|
String? name = map.get(13276);
|
|
print("\nInput student ID 13276, found name ${name}");
|
|
|
|
/* Remove operation */
|
|
// Remove key-value pair (key, value) from hash table
|
|
map.remove(12836);
|
|
print("\nAfter removing 12836, hash table is\nKey -> Value");
|
|
map.printHashMap();
|
|
}
|