Example #1
0
 public HashMapEntry(int h, object key, object val, HashMapEntry n)
 {
     _key   = key;
     _value = val;
     _hash  = h;
     _next  = n;
 }
Example #2
0
 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);
     }
 }
Example #3
0
        /// <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);
        }
Example #4
0
 /// <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);
 }
Example #5
0
 /// <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);
     }
 }
Example #6
0
        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);
        }
Example #7
0
 void PutAllForCreate(HashMap m)
 {
     for (IEnumerator i = m.TheEntrySet.GetEnumerator(); i.MoveNext();)
     {
         HashMapEntry e = (HashMapEntry)i.Current;
         PutForCreate(e.Key, e.Value);
     }
 }
Example #8
0
        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);
        }
Example #9
0
 /// <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);
     }
 }
Example #10
0
            public override Object Clone()
            {
                HashMapEntry entry = (HashMapEntry)base.Clone();

                if (next != null)
                {
                    entry.next = next.Clone() as HashMapEntry;
                }
                return(entry);
            }
Example #11
0
        protected virtual Entry <K, V> FindNullKeyEntry()
        {
            HashMapEntry m = elementData[0];

            while (m != null && m.Key != null)
            {
                m = m.next;
            }
            return(m);
        }
Example #12
0
        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);
        }
Example #13
0
            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));
            }
Example #14
0
        /// <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);
        }
Example #15
0
 /// <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);
 }
Example #16
0
        /// <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);
        }
Example #17
0
        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);
        }
Example #18
0
            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;
            }
Example #19
0
        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);
        }
Example #20
0
        /// <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);
        }
Example #21
0
        /// <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);
        }
Example #22
0
        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--;
        }
Example #23
0
        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();
        }
Example #24
0
            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);
            }
Example #25
0
            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--;
            }
Example #26
0
        /// <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);
        }
Example #27
0
        /// <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);
                }
            }
        }
Example #28
0
        /// <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);
        }
Example #29
0
            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;
                }
            }
Example #30
0
            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;
            }
Example #31
0
 /// <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++;
 }
Example #32
0
        /// <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);
        }
Example #33
0
 /// <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++;
 }
Example #34
0
 /// <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);
         }
     }
 }
Example #35
0
 public HashMapEntry(int h, object key, object val, HashMapEntry n)
 {
     _key = key;
     _value = val;
     _hash = h;
     _next = n;
 }
Example #36
0
 /// <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);
 }