Below is a basic implementation of a BiMap (Bidirectional Map) in TypeScript. A BiMap is a data structure that allows for efficient two-way mapping between two sets of elements, meaning you can both get a value by its key and a key by its value. This implementation assumes that both keys and values are unique.
typescript
class BiMap<K, V> {
private keyToValue: Map<K, V>;
private valueToKey: Map<V, K>;
constructor() {
this.keyToValue = new Map<K, V>();
this.valueToKey = new Map<V, K>();
}
/**
* Sets a key-value pair in the BiMap. If either the key or the value already exists,
* it will be overwritten.
* @param key The key to set.
* @param value The value associated with the key.
*/
set(key: K, value: V): void {
// Check if the key already exists and remove it to ensure consistency
if (this.keyToValue.has(key)) {
const oldValue = this.keyToValue.get(key);
if (oldValue !== undefined) {
this.valueToKey.delete(oldValue);
}
}
// Check if the value already exists and remove it to ensure consistency
if (this.valueToKey.has(value)) {
const oldKey = this.valueToKey.get(value);
if (oldKey !== undefined) {
this.keyToValue.delete(oldKey);
}
}
// Set the new key-value pair in both maps
this.keyToValue.set(key, value);
this.valueToKey.set(value, key);
}
/**
* Gets the value associated with the provided key.
* @param key The key to look up.
* @returns The value associated with the key, or undefined if not found.
*/
getKeyValue(key: K): V | undefined {
return this.keyToValue.get(key);
}
/**
* Gets the key associated with the provided value.
* @param value The value to look up.
* @returns The key associated with the value, or undefined if not found.
*/
getValueKey(value: V): K | undefined {
return this.valueToKey.get(value);
}
/**
* Deletes a key-value pair by key.
* @param key The key to delete.
* @returns True if the key was found and deleted, false otherwise.
*/
deleteByKey(key: K): boolean {
if (this.keyToValue.has(key)) {
const value = this.keyToValue.get(key);
this.keyToValue.delete(key);
if (value !== undefined) {
this.valueToKey.delete(value);
}
return true;
}
return false;
}
/**
* Deletes a key-value pair by value.
* @param value The value to delete.
* @returns True if the value was found and deleted, false otherwise.
*/
deleteByValue(value: V): boolean {
if (this.valueToKey.has(value)) {
const key = this.valueToKey.get(value);
this.valueToKey.delete(value);
if (key !== undefined) {
this.keyToValue.delete(key);
}
return true;
}
return false;
}
/**
* Checks if a key exists in the BiMap.
* @param key The key to check.
* @returns True if the key exists, false otherwise.
*/
hasKey(key: K): boolean {
return this.keyToValue.has(key);
}
/**
* Checks if a value exists in the BiMap.
* @param value The value to check.
* @returns True if the value exists, false otherwise.
*/
hasValue(value: V): boolean {
return this.valueToKey.has(value);
}
/**
* Returns the size of the BiMap (number of key-value pairs).
*/
get size(): number {
return this.keyToValue.size;
}
/**
* Clears the BiMap.
*/
clear(): void {
this.keyToValue.clear();
this.valueToKey.clear();
}
}
// Example usage:
const biMap = new BiMap<string, number>();
biMap.set('apple', 1);
biMap.set('banana', 2);
console.log(biMap.getKeyValue('apple')); // Outputs: 1
console.log(biMap.getValueKey(2)); // Outputs: "banana"
console.log(biMap.size); // Outputs: 2
biMap.deleteByKey('apple');
console.log(biMap.size); // Outputs: 1
biMap.clear();
console.log(biMap.size); // Outputs: 0
This BiMap class provides the following methods for managing and querying the bidirectional mapping:
set(key, value): Sets or updates a key-value pair.
getKeyValue(key): Retrieves the value associated with a key.
getValueKey(value): Retrieves the key associated with a value.
deleteByKey(key): Deletes a key-value pair by key.
deleteByValue(value): Deletes a key-value pair by value.
hasKey(key): Checks if a key exists.
hasValue(value): Checks if a value exists.
size: Returns the number of key-value pairs.
clear(): Removes all key-value pairs.
Important Considerations:
Uniqueness: Both keys and values must be unique. If a duplicate key or value is added, the previous entry will be overwritten.
Type Compatibility: Ensure that the types K and V are compatible with the data you're working with. For example, if you're using objects as keys or values, you might encounter unexpected behavior due to how JavaScript handles object equality.
Iteration: This implementation does not provide built-in iteration methods. If you need to iterate over the entries, you can use the keyToValue map's entries() method, like Array.from(this.keyToValue.entries()).