示例#1
0
        /// <summary>
        /// removes an entry for the specified key
        /// returns the removed entry
        /// </summary>
        private IHashMapEntry <K, V> RemoveEntryForKey(K key)
        {
            if (key == null)
            {
                return(null);
            }
            int thehash = hash(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 && eq(key, e.Key))
                {
                    modCount++;
                    size--;
                    if (prev == e)
                    {
                        table[i] = next;
                    }
                    else
                    {
                        prev.Next = next;
                    }
                    return(e);
                }
                prev = e;
                e    = next;
            }

            return(e);
        }
示例#2
0
 public HashMapEntry(int h, K key, V val, IHashMapEntry <K, V> n)
 {
     _key   = key;
     _value = val;
     _hash  = h;
     _next  = n;
 }
示例#3
0
        public V this[K key]
        {
            get
            {
                //object k = maskNull(key);
                if (key == null)
                {
                    return(default(V));
                }

                int thehash            = hash(key);
                int i                  = indexFor(thehash, table.Length);
                IHashMapEntry <K, V> e = table[i];
                while (true)
                {
                    if (e == null)
                    {
                        return(default(V));
                    }
                    if (e.Hash == thehash && eq(key, e.Key))
                    {
                        return(e.Value);
                    }
                    e = e.Next;
                }
            }
            set { Put(key, value); }
        }
示例#4
0
 private void PutAllForCreate(GenericHashMap <K, V> m)
 {
     for (IEnumerator <IHashMapEntry <K, V> > i = m.GetEnumerator(); i.MoveNext();)
     {
         IHashMapEntry <K, V> e = i.Current;
         PutForCreate(e.Key, e.Value);
     }
 }
示例#5
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));
            }
示例#6
0
        /// <summary>
        /// reutrns the entry associated with the key
        /// </summary>
        private IHashMapEntry <K, V> GetEntry(K key)
        {
            //K k = maskNull(key);
            int theHash            = hash(key);
            int i                  = indexFor(theHash, table.Length);
            IHashMapEntry <K, V> e = table[i];

            while (e != null && !(e.Hash == theHash && eq(key, e.Key)))
            {
                e = e.Next;
            }
            return(e);
        }
示例#7
0
 /// <summary>
 /// special case code for containsValue with null argument
 /// </summary>
 /// <returns></returns>
 private bool ContainsNullValue()
 {
     IHashMapEntry <K, V>[] tab = table;
     for (int i = 0; i < tab.Length; i++)
     {
         for (IHashMapEntry <K, V> e = tab[i]; e != null; e = e.Next)
         {
             if (e.Value.Equals(default(V)))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
示例#8
0
            private IHashMapEntry <K, V> _next;    // next entry to return

            internal HashEnumerator(GenericHashMap <K, V> theMap)
            {
                _map = theMap;
                _expectedModCount = _map.modCount;
                IHashMapEntry <K, V>[] t = theMap.table;
                int i = t.Length;
                IHashMapEntry <K, V> n = null;

                if (theMap.size != 0)
                {
                    // advance to first entry
                    while (i > 0 && (n = t[--i]) == null)
                    {
                        ;
                    }
                }
                _next  = n;
                _index = i;
            }
示例#9
0
        /// <summary>
        /// transfer all entries from current table into the newTable
        /// </summary>
        /// <param name="newTable"></param>
        private void Transfer(IHashMapEntry <K, V>[] newTable)
        {
            IHashMapEntry <K, V>[] src = table;
            int newCapacity            = newTable.Length;

            for (int j = 0; j < src.Length; j++)
            {
                IHashMapEntry <K, V> e = src[j];
                if (e != null)
                {
                    src[j] = null;
                    do
                    {
                        IHashMapEntry <K, V> next = e.Next;
                        int i = indexFor(e.Hash, newCapacity);
                        e.Next      = newTable[i];
                        newTable[i] = e;
                        e           = next;
                    } while (e != null);
                }
            }
        }
示例#10
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(K key, V value)
        {
            //K k = maskNull(key);
            int thehash = hash(key);
            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 (IHashMapEntry <K, V> e = table[i]; e != null; e = e.Next)
            {
                if (e.Hash == thehash && eq(key, e.Key))
                {
                    e.Value = value;
                    return;
                }
            }

            CreateEntry(thehash, key, value, i);
        }
示例#11
0
        /// <summary>
        /// add a key value pair to this map
        /// </summary>
        /// <param name="key"></param>
        /// <param name="entry"></param>
        public void Put(K key, V entry)
        {
            if (key == null)
            {
                throw new NullReferenceException("Key cannot be null");
            }
            int theHash = hash(key);
            int i       = indexFor(theHash, table.Length);

            for (IHashMapEntry <K, V> e = table[i]; e != null; e = e.Next)
            {
                if (e.Hash == theHash && eq(key, e.Key))
                {
                    Object oldValue = e.Value;
                    e.Value = entry;
                    return;
                }
            }

            modCount++;
            AddEntry(theHash, key, entry, i);
        }
示例#12
0
        public bool Contains(K key)
        {
            //object k = maskNull(key);

            if (key == null)
            {
                return(false);
            }

            int thehash            = hash(key);
            int i                  = indexFor(thehash, table.Length);
            IHashMapEntry <K, V> e = table[i];

            while (e != null)
            {
                if (e.Hash == thehash && eq(key, e.Key))
                {
                    return(true);
                }
                e = e.Next;
            }
            return(false);
        }
示例#13
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);
        }
示例#14
0
 public void Dispose()
 {
     _current = null;
     _next    = null;
 }
示例#15
0
        public V RemoveWithReturn(K key)
        {
            IHashMapEntry <K, V> e = RemoveEntryForKey(key);

            return(e == null ? default(V) : e.Value);
        }