|
| 1 | +# Custom HashMap Implementation |
| 2 | + |
| 3 | +A TypeScript implementation of a HashMap data structure using separate chaining for collision resolution. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Generic Implementation**: Supports any key-value types |
| 8 | +- **Collision Resolution**: Uses separate chaining with linked lists |
| 9 | +- **Dynamic Resizing**: Automatically resizes when load factor exceeds 0.75 |
| 10 | +- **Comprehensive API**: Full set of operations (put, get, delete, has, clear, etc.) |
| 11 | +- **Type Safety**: Full TypeScript support with type checking |
| 12 | +- **Performance Monitoring**: Built-in methods to check size, capacity, and load factor |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Basic Operations |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { CustomHashMap } from './src/HashMap'; |
| 26 | + |
| 27 | +// Create a new HashMap |
| 28 | +const map = new CustomHashMap<string, number>(); |
| 29 | + |
| 30 | +// Add key-value pairs |
| 31 | +map.put('apple', 5); |
| 32 | +map.put('banana', 3); |
| 33 | +map.put('orange', 8); |
| 34 | + |
| 35 | +// Get values |
| 36 | +const appleCount = map.get('apple'); // 5 |
| 37 | +const grapeCount = map.get('grape'); // undefined |
| 38 | + |
| 39 | +// Check if key exists |
| 40 | +const hasApple = map.has('apple'); // true |
| 41 | +const hasGrape = map.has('grape'); // false |
| 42 | + |
| 43 | +// Update existing key |
| 44 | +map.put('apple', 10); // Updates existing value |
| 45 | + |
| 46 | +// Delete a key |
| 47 | +const deleted = map.delete('banana'); // true |
| 48 | +const deletedAgain = map.delete('banana'); // false |
| 49 | + |
| 50 | +// Get size |
| 51 | +const size = map.getSize(); // 2 |
| 52 | +``` |
| 53 | + |
| 54 | +### Working with Different Data Types |
| 55 | + |
| 56 | +```typescript |
| 57 | +// String keys with object values |
| 58 | +interface Person { |
| 59 | + name: string; |
| 60 | + age: number; |
| 61 | +} |
| 62 | + |
| 63 | +const people = new CustomHashMap<string, Person>(); |
| 64 | +people.put('john', { name: 'John Doe', age: 30 }); |
| 65 | +people.put('jane', { name: 'Jane Smith', age: 25 }); |
| 66 | + |
| 67 | +// Mixed key types |
| 68 | +const mixed = new CustomHashMap<string | number, string>(); |
| 69 | +mixed.put('stringKey', 'value1'); |
| 70 | +mixed.put(42, 'value2'); |
| 71 | +``` |
| 72 | + |
| 73 | +### Iteration Methods |
| 74 | + |
| 75 | +```typescript |
| 76 | +const map = new CustomHashMap<string, number>(); |
| 77 | +map.put('a', 1); |
| 78 | +map.put('b', 2); |
| 79 | +map.put('c', 3); |
| 80 | + |
| 81 | +// Get all keys |
| 82 | +const keys = map.keys(); // ['a', 'b', 'c'] |
| 83 | + |
| 84 | +// Get all values |
| 85 | +const values = map.values(); // [1, 2, 3] |
| 86 | + |
| 87 | +// Get all entries |
| 88 | +const entries = map.entries(); // [['a', 1], ['b', 2], ['c', 3]] |
| 89 | +``` |
| 90 | + |
| 91 | +### Monitoring Performance |
| 92 | + |
| 93 | +```typescript |
| 94 | +const map = new CustomHashMap<string, number>(); |
| 95 | + |
| 96 | +// Check current statistics |
| 97 | +console.log('Size:', map.getSize()); |
| 98 | +console.log('Capacity:', map.getCapacity()); |
| 99 | +console.log('Load Factor:', map.getLoadFactor()); |
| 100 | + |
| 101 | +// Print internal structure for debugging |
| 102 | +map.printTable(); |
| 103 | +``` |
| 104 | + |
| 105 | +## API Reference |
| 106 | + |
| 107 | +### Constructor |
| 108 | + |
| 109 | +- `new CustomHashMap<K, V>(initialCapacity?: number)`: Creates a new HashMap with optional initial capacity (default: 16) |
| 110 | + |
| 111 | +### Methods |
| 112 | + |
| 113 | +- `put(key: K, value: V): void`: Insert or update a key-value pair |
| 114 | +- `get(key: K): V | undefined`: Get value by key |
| 115 | +- `has(key: K): boolean`: Check if key exists |
| 116 | +- `delete(key: K): boolean`: Remove a key-value pair, returns true if successful |
| 117 | +- `clear(): void`: Remove all entries |
| 118 | +- `keys(): K[]`: Get array of all keys |
| 119 | +- `values(): V[]`: Get array of all values |
| 120 | +- `entries(): [K, V][]`: Get array of all key-value pairs |
| 121 | +- `getSize(): number`: Get current number of entries |
| 122 | +- `getCapacity(): number`: Get current bucket array size |
| 123 | +- `getLoadFactor(): number`: Get current load factor (size/capacity) |
| 124 | +- `printTable(): void`: Print internal structure for debugging |
| 125 | + |
| 126 | +## Running the Examples |
| 127 | + |
| 128 | +### Demo Script |
| 129 | +```bash |
| 130 | +npm run dev |
| 131 | +# or |
| 132 | +npm start |
| 133 | +``` |
| 134 | + |
| 135 | +### Test Suite |
| 136 | +```bash |
| 137 | +npm test |
| 138 | +``` |
| 139 | + |
| 140 | +### Build Project |
| 141 | +```bash |
| 142 | +npm run build |
| 143 | +``` |
| 144 | + |
| 145 | +## Implementation Details |
| 146 | + |
| 147 | +### Hash Function |
| 148 | +Uses the djb2 algorithm for string hashing, which provides good distribution and minimal collisions. |
| 149 | + |
| 150 | +### Collision Resolution |
| 151 | +Implements separate chaining using linked lists. When multiple keys hash to the same bucket, they are stored in a linked list at that bucket. |
| 152 | + |
| 153 | +### Load Factor Management |
| 154 | +Automatically resizes the hash table when the load factor exceeds 0.75 to maintain good performance. Resizing doubles the capacity and rehashes all existing entries. |
| 155 | + |
| 156 | +### Time Complexity |
| 157 | +- **Average Case**: O(1) for put, get, delete operations |
| 158 | +- **Worst Case**: O(n) when all keys hash to the same bucket |
| 159 | +- **Space Complexity**: O(n) where n is the number of key-value pairs |
| 160 | + |
| 161 | +## Files Structure |
| 162 | + |
| 163 | +``` |
| 164 | +src/ |
| 165 | +├── HashMap.ts # Main HashMap implementation |
| 166 | +├── index.ts # Export file |
| 167 | +├── demo.ts # Comprehensive demo script |
| 168 | +└── test.ts # Test suite |
| 169 | +``` |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +MIT |
0 commit comments