gistlib
main.cspublic class MyHashMap<TKey, TValue> { private const int InitialCapacity = 16; private readonly LinkedList<KeyValuePair<TKey, TValue>>[] _buckets; private readonly int _capacity; private int _count; public MyHashMap() { _capacity = InitialCapacity; _buckets = new LinkedList<KeyValuePair<TKey, TValue>>[_capacity]; } public void Add(TKey key, TValue value) { var entry = new KeyValuePair<TKey, TValue>(key, value); var bucketIndex = GetBucketIndex(key); if (_buckets[bucketIndex] == null) { _buckets[bucketIndex] = new LinkedList<KeyValuePair<TKey, TValue>>(); } foreach (var existingEntry in _buckets[bucketIndex]) { if (existingEntry.Key.Equals(key)) { throw new InvalidOperationException("Key already exists in the hash map"); } } _buckets[bucketIndex].AddLast(entry); _count++; } public bool Remove(TKey key) { var bucketIndex = GetBucketIndex(key); if (_buckets[bucketIndex] == null) { return false; } var currentNode = _buckets[bucketIndex].First; while (currentNode != null) { if (currentNode.Value.Key.Equals(key)) { _buckets[bucketIndex].Remove(currentNode); _count--; return true; } currentNode = currentNode.Next; } return false; } public bool TryGetValue(TKey key, out TValue value) { var bucketIndex = GetBucketIndex(key); if (_buckets[bucketIndex] == null) { value = default(TValue); return false; } foreach (var entry in _buckets[bucketIndex]) { if (entry.Key.Equals(key)) { value = entry.Value; return true; } } value = default(TValue); return false; } public void Clear() { for (var i = 0; i < _capacity; i++) { _buckets[i] = null; } _count = 0; } public int Count => _count; private int GetBucketIndex(TKey key) { var hash = key.GetHashCode(); var index = hash & (_capacity - 1); return index; } } 2398 chars103 lines
public class MyHashMap<TKey, TValue> { private const int InitialCapacity = 16; private readonly LinkedList<KeyValuePair<TKey, TValue>>[] _buckets; private readonly int _capacity; private int _count; public MyHashMap() { _capacity = InitialCapacity; _buckets = new LinkedList<KeyValuePair<TKey, TValue>>[_capacity]; } public void Add(TKey key, TValue value) { var entry = new KeyValuePair<TKey, TValue>(key, value); var bucketIndex = GetBucketIndex(key); if (_buckets[bucketIndex] == null) { _buckets[bucketIndex] = new LinkedList<KeyValuePair<TKey, TValue>>(); } foreach (var existingEntry in _buckets[bucketIndex]) { if (existingEntry.Key.Equals(key)) { throw new InvalidOperationException("Key already exists in the hash map"); } } _buckets[bucketIndex].AddLast(entry); _count++; } public bool Remove(TKey key) { var bucketIndex = GetBucketIndex(key); if (_buckets[bucketIndex] == null) { return false; } var currentNode = _buckets[bucketIndex].First; while (currentNode != null) { if (currentNode.Value.Key.Equals(key)) { _buckets[bucketIndex].Remove(currentNode); _count--; return true; } currentNode = currentNode.Next; } return false; } public bool TryGetValue(TKey key, out TValue value) { var bucketIndex = GetBucketIndex(key); if (_buckets[bucketIndex] == null) { value = default(TValue); return false; } foreach (var entry in _buckets[bucketIndex]) { if (entry.Key.Equals(key)) { value = entry.Value; return true; } } value = default(TValue); return false; } public void Clear() { for (var i = 0; i < _capacity; i++) { _buckets[i] = null; } _count = 0; } public int Count => _count; private int GetBucketIndex(TKey key) { var hash = key.GetHashCode(); var index = hash & (_capacity - 1); return index; } }
gistlibby LogSnag