public HashMapEntry(int h, object key, object val, HashMapEntry n) { _key = key; _value = val; _hash = h; _next = n; }
public object this[object key] { get { object k = maskNull(key); int thehash = hash(k); int i = indexFor(thehash, table.Length); HashMapEntry e = table[i]; while (true) { if (e == null) { return(e); } if (e.Hash == thehash && eq(k, e.Key)) { return(e.Value); } e = e.Next; } } set { Put(key, value); } }
/// <summary> /// removes an entry for the specified key /// returns the removed entry /// </summary> HashMapEntry RemoveEntryForKey(object key) { object k = maskNull(key); int thehash = hash(k); int i = indexFor(thehash, table.Length); HashMapEntry prev = table[i]; HashMapEntry e = prev; while (e != null) { HashMapEntry next = e.Next; if (e.Hash == thehash && eq(k, e.Key)) { modCount++; size--; if (prev == e) { table[i] = next; } else { prev.Next = next; } return(e); } prev = e; e = next; } return(e); }
/// <summary> /// Returns whether this map contains the specified value. /// </summary> public override bool ContainsValue(V val) { if (val != null) { for (int i = 0; i < elementData.Length; i++) { HashMapEntry entry = elementData[i]; while (entry != null) { if (AreEqualValues(val, entry.Value)) { return(true); } entry = entry.next; } } } else { for (int i = 0; i < elementData.Length; i++) { HashMapEntry entry = elementData[i]; while (entry != null) { if (entry.Value == null) { return(true); } entry = entry.next; } } } return(false); }
/// <summary> /// add a new entry with the key, value and hashcode, to the specified bucket /// if needed this method will resize the table /// </summary> /// <param name="hash"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="bucketIndex"></param> private void AddEntry(int hash, K key, V value, int bucketIndex) { table[bucketIndex] = new HashMapEntry <K, V>(hash, key, value, table[bucketIndex]); if (size++ >= threshold) { Resize(2 * table.Length); } }
protected virtual Entry <K, V> CreateEntry(K key, int index, V value) { HashMapEntry entry = new HashMapEntry(key, value); entry.next = elementData[index]; elementData[index] = entry; return(entry); }
void PutAllForCreate(HashMap m) { for (IEnumerator i = m.TheEntrySet.GetEnumerator(); i.MoveNext();) { HashMapEntry e = (HashMapEntry)i.Current; PutForCreate(e.Key, e.Value); } }
protected virtual Entry <K, V> CreateHashedEntry(K key, int index, int hash) { HashMapEntry entry = new HashMapEntry(key, hash); entry.next = elementData[index]; elementData[index] = entry; return(entry); }
/// <summary> /// add a new entry with the key, value and hashcode, to the specified bucket /// if needed this method will resize the table /// </summary> /// <param name="hash"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="bucketIndex"></param> void AddEntry(int hash, object key, object value, int bucketIndex) { table[bucketIndex] = new HashMapEntry(hash, key, value, table[bucketIndex]); if (size++ >= threshold) { Resize(2 * table.Length); } }
public override Object Clone() { HashMapEntry entry = (HashMapEntry)base.Clone(); if (next != null) { entry.next = next.Clone() as HashMapEntry; } return(entry); }
protected virtual Entry <K, V> FindNullKeyEntry() { HashMapEntry m = elementData[0]; while (m != null && m.Key != null) { m = m.next; } return(m); }
protected virtual Entry <K, V> FindNonNullKeyEntry(K key, int index, int keyHash) { HashMapEntry m = elementData[index]; while (m != null && (m.origKeyHash != keyHash || !AreEqualKeys(key, m.Key))) { m = m.next; } return(m); }
public bool Contains(Object o) { if (!(o is HashMapEntry <K, V>)) { return(false); } HashMapEntry <K, V> e = (HashMapEntry <K, V>)o; IHashMapEntry <K, V> candidate = _map.GetEntry(e.Key); return(candidate != null && candidate.Equals(e)); }
/// <summary> /// reutrns the entry associated with the key /// </summary> HashMapEntry GetEntry(object key) { object k = maskNull(key); int theHash = hash(k); int i = indexFor(theHash, table.Length); HashMapEntry e = table[i]; while (e != null && !(e.Hash == theHash && eq(k, e.Key))) { e = e.Next; } return(e); }
/// <summary> /// special case code for containsValue with null argument /// </summary> /// <returns></returns> private bool ContainsNullValue() { HashMapEntry[] tab = table; for (int i = 0; i < tab.Length; i++) { for (HashMapEntry e = tab[i]; e != null; e = e.Next) { if (e.Value == null) { return(true); } } } return(false); }
/// <summary> /// rehashes the the contents of this map into a new HashMap with a larger capacity /// called automatically when the number of keys exceedts the capacity and load factor /// /// </summary> /// <param name="newCapacity">new capacity, must be a power of 2</param> private void Resize(int newCapacity) { IHashMapEntry <K, V>[] oldTable = table; int oldCapacity = oldTable.Length; // check if needed if (size < threshold || oldCapacity > newCapacity) { return; } HashMapEntry <K, V>[] newTable = new HashMapEntry <K, V> [newCapacity]; Transfer(newTable); table = newTable; threshold = (int)(newCapacity * loadFactor); }
public bool Contains(object key) { object k = maskNull(key); int thehash = hash(k); int i = indexFor(thehash, table.Length); HashMapEntry e = table[i]; while (e != null) { if (e.Hash == thehash && eq(k, e.Key)) { return(true); } e = e.Next; } return(false); }
internal HashEnumerator(HashMap theMap) { map = theMap; expectedModCount = map.modCount; HashMapEntry[] t = theMap.table; int i = t.Length; HashMapEntry n = null; if (theMap.size != 0) { // advance to first entry while (i > 0 && (n = t[--i]) == null) { ; } } next = n; index = i; }
protected Entry <K, V> RemoveEntry(K key) { int index = 0; HashMapEntry entry; HashMapEntry last = null; if (key != null) { int hash = ComputeHashCode(key); index = hash & (elementData.Length - 1); entry = elementData[index]; while (entry != null && !(entry.origKeyHash == hash && AreEqualKeys(key, entry.Key))) { last = entry; entry = entry.next; } } else { entry = elementData[0]; while (entry != null && entry.Key != null) { last = entry; entry = entry.next; } } if (entry == null) { return(null); } if (last == null) { elementData[index] = entry.next; } else { last.next = entry.next; } modCount++; elementCount--; return(entry); }
/// <summary> /// add a key value pair to this map /// </summary> /// <param name="key"></param> /// <param name="entry"></param> public void Put(object key, object entry) { Object k = maskNull(key); int theHash = hash(k); int i = indexFor(theHash, table.Length); for (HashMapEntry e = table[i]; e != null; e = e.Next) { if (e.Hash == theHash && eq(k, e.Key)) { Object oldValue = e.Value; e.Value = entry; return; } } modCount++; AddEntry(theHash, k, entry, i); }
/// <summary> /// returns true if this Map has a mapping for the object /// </summary> /// <param name="value"></param> /// <returns></returns> public bool ContainsValue(Object value) { if (value == null) { return(ContainsNullValue()); } HashMapEntry[] tab = table; for (int i = 0; i < tab.Length; i++) { for (HashMapEntry e = tab[i]; e != null; e = e.Next) { if (value.Equals(e.Value)) { return(true); } } } return(false); }
protected void RemoveEntry(Entry <K, V> entry) { HashMapEntry ientry = entry as HashMapEntry; int index = ientry.origKeyHash & (elementData.Length - 1); HashMapEntry m = elementData[index]; if (ReferenceEquals(m, ientry)) { elementData[index] = ientry.next; } else { while (m.next != ientry) { m = m.next; } m.next = ientry.next; } modCount++; elementCount--; }
protected void Rehash(int capacity) { int length = CalculateCapacity((capacity == 0 ? 1 : capacity << 1)); HashMapEntry[] newData = NewElementArray(length); for (int i = 0; i < elementData.Length; i++) { HashMapEntry entry = elementData[i]; elementData[i] = null; while (entry != null) { int index = entry.origKeyHash & (length - 1); HashMapEntry next = entry.next; entry.next = newData[index]; newData[index] = entry; entry = next; } } elementData = newData; ComputeThreshold(); }
public override bool Equals(object o) { if (!(o is HashMapEntry <K, V>)) { return(false); } HashMapEntry <K, V> e = (HashMapEntry <K, V>)o; object k1 = Key; object k2 = e.Key; if (k1 == k2 || (k1 != null && k1.Equals(k2))) { object v1 = Value; object v2 = e.Value; if (v1 == v2 || (v1 != null && v1.Equals(v2))) { return(true); } } return(false); }
public void Remove() { CheckConcurrentMod(); if (currentEntry == null) { throw new IllegalStateException(); } if (prevEntry == null) { int index = currentEntry.origKeyHash & (associatedMap.elementData.Length - 1); associatedMap.elementData[index] = associatedMap.elementData[index].next; } else { prevEntry.next = currentEntry.next; } currentEntry = null; expectedModCount++; associatedMap.modCount++; associatedMap.elementCount--; }
/// <summary> /// used instead of Add by Constructors and Clones, does not resize the table /// calls CreateEntry instead of AddEntry /// </summary> /// <param name="key"></param> /// <param name="value"></param> private void PutForCreate(Object key, object value) { object k = maskNull(key); int thehash = hash(k); int i = indexFor(thehash, table.Length); /** * Look for preexisting entry for key. This will never happen for * clone or deserialize. It will only happen for construction if the * input Map is a sorted map whose ordering is inconsistent w/ equals. */ for (HashMapEntry e = table[i]; e != null; e = e.Next) { if (e.Hash == thehash && eq(k, e.Key)) { e.Value = value; return; } } CreateEntry(thehash, k, value, i); }
/// <summary> /// transfer all entries from current table into the newTable /// </summary> /// <param name="newTable"></param> void Transfer(HashMapEntry[] newTable) { HashMapEntry[] src = table; int newCapacity = newTable.Length; for (int j = 0; j < src.Length; j++) { HashMapEntry e = src[j]; if (e != null) { src[j] = null; do { HashMapEntry next = e.Next; int i = indexFor(e.Hash, newCapacity); e.Next = newTable[i]; newTable[i] = e; e = next; } while (e != null); } } }
/// <summary> /// remove the mapping for a specified entry /// </summary> /// <param name="o"></param> /// <returns></returns> public IHashMapEntry <K, V> RemoveMapping(object o) { if (!(o is HashMapEntry <K, V>)) { return(null); } HashMapEntry <K, V> entry = (HashMapEntry <K, V>)o; //object k = maskNull(entry.Key); int thehash = hash(entry.Key); int i = indexFor(thehash, table.Length); IHashMapEntry <K, V> prev = table[i]; IHashMapEntry <K, V> e = prev; while (e != null) { IHashMapEntry <K, V> next = e.Next; if (e.Hash == thehash && e.Equals(entry)) { modCount++; size--; if (prev == e) { table[i] = next; } else { prev.Next = next; } return(e); } prev = e; e = next; } return(e); }
protected void MakeNext() { CheckConcurrentMod(); if (!HasNext) { throw new NoSuchElementException(); } if (futureEntry == null) { currentEntry = associatedMap.elementData[position++]; futureEntry = currentEntry.next; prevEntry = null; } else { if (currentEntry != null) { prevEntry = currentEntry; } currentEntry = futureEntry; futureEntry = futureEntry.next; } }
HashMapEntry next; // next entry to return #endregion Fields #region Constructors internal HashEnumerator(HashMap theMap) { map = theMap; expectedModCount = map.modCount; HashMapEntry[] t = theMap.table; int i = t.Length; HashMapEntry n = null; if (theMap.size != 0) { // advance to first entry while (i > 0 && (n = t[--i]) == null) ; } next = n; index = i; }
/// <summary> /// this is used when creating entries as part of a constructor or Clone call /// </summary> /// <param name="hash"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="bucketIndex"></param> void CreateEntry(int hash, object key, object value, int bucketIndex) { table[bucketIndex] = new HashMapEntry(hash, key, value, table[bucketIndex]); size++; }
/// <summary> /// rehashes the the contents of this map into a new HashMap with a larger capacity /// called automatically when the number of keys exceedts the capacity and load factor /// /// </summary> /// <param name="newCapacity">new capacity, must be a power of 2</param> void Resize(int newCapacity) { HashMapEntry[] oldTable = table; int oldCapacity = oldTable.Length; // check if needed if (size < threshold || oldCapacity > newCapacity) return; HashMapEntry[] newTable = new HashMapEntry[newCapacity]; Transfer(newTable); table = newTable; threshold = (int)(newCapacity * loadFactor); }
/// <summary> /// this is used when creating entries as part of a constructor or Clone call /// </summary> /// <param name="hash"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="bucketIndex"></param> private void CreateEntry(int hash, K key, V value, int bucketIndex) { table[bucketIndex] = new HashMapEntry <K, V>(hash, key, value, table[bucketIndex]); size++; }
/// <summary> /// add a new entry with the key, value and hashcode, to the specified bucket /// if needed this method will resize the table /// </summary> /// <param name="hash"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="bucketIndex"></param> void AddEntry(int hash, object key, object value, int bucketIndex) { table[bucketIndex] = new HashMapEntry(hash, key, value, table[bucketIndex]); if (size++ >= threshold) Resize(2 * table.Length); }