Пример #1
0
        public virtual int[] GetKeys()
        {
            int[] res   = new int[count];
            int   ptr   = 0;
            int   index = table.Length;

            IntHashtable.Entry entry = null;
            while (true)
            {
                if (entry == null)
                {
                    while (index-- > 0 && (entry = table[index]) == null)
                    {
                    }
                }
                if (entry == null)
                {
                    break;
                }
                IntHashtable.Entry e = entry;
                entry      = e.next;
                res[ptr++] = e.key;
            }
            return(res);
        }
Пример #2
0
        /// <summary>
        /// <p>Maps the specified <code>key</code> to the specified
        /// <code>value</code> in this hashtable.
        /// </summary>
        /// <remarks>
        /// <p>Maps the specified <code>key</code> to the specified
        /// <code>value</code> in this hashtable. The key cannot be
        /// <code>null</code>. </p>
        /// <p>The value can be retrieved by calling the <code>get</code> method
        /// with a key that is equal to the original key.</p>
        /// </remarks>
        /// <param name="key">the hashtable key.</param>
        /// <param name="value">the value.</param>
        /// <returns>
        /// the previous value of the specified key in this hashtable,
        /// or <code>null</code> if it did not have one.
        /// </returns>
        /// <exception cref="System.NullReferenceException">if the key is <code>null</code>.</exception>
        /// <seealso cref="Get(int)"/>
        public virtual int Put(int key, int value)
        {
            // Makes sure the key is not already in the hashtable.
            IntHashtable.Entry[] tab = table;
            int index = (key & 0x7FFFFFFF) % tab.Length;

            for (IntHashtable.Entry e = tab[index]; e != null; e = e.next)
            {
                if (e.key == key)
                {
                    int old = e.value;
                    //e.addValue(old);
                    e.value = value;
                    return(old);
                }
            }
            if (count >= threshold)
            {
                // Rehash the table if the threshold is exceeded
                Rehash();
                tab   = table;
                index = (key & 0x7FFFFFFF) % tab.Length;
            }
            // Creates the new entry.
            IntHashtable.Entry e_1 = new IntHashtable.Entry(key, value, tab[index]);
            tab[index] = e_1;
            count++;
            return(0);
        }
Пример #3
0
 /// <summary><p>Create a new entry with the given values.</p></summary>
 /// <param name="key">The key used to enter this in the table</param>
 /// <param name="value">The value for this key</param>
 /// <param name="next">A reference to the next entry in the table</param>
 internal Entry(int key, int value, IntHashtable.Entry next)
 {
     //ArrayList<Integer> values = new ArrayList<Integer>();
     this.key   = key;
     this.value = value;
     this.next  = next;
 }
Пример #4
0
        /// <summary><p>Returns the value to which the specified key is mapped in this map.</p></summary>
        /// <param name="key">a key in the hashtable.</param>
        /// <returns>
        /// the value to which the key is mapped in this hashtable;
        /// 0 if the key is not mapped to any value in
        /// this hashtable.
        /// </returns>
        /// <seealso cref="Put(int, int)"/>
        public virtual int Get(int key)
        {
            IntHashtable.Entry[] tab = table;
            int index = (key & 0x7FFFFFFF) % tab.Length;

            for (IntHashtable.Entry e = tab[index]; e != null; e = e.next)
            {
                if (e.key == key)
                {
                    return(e.value);
                }
            }
            return(0);
        }
Пример #5
0
        /// <summary><p>Tests if the specified int is a key in this hashtable.</p></summary>
        /// <param name="key">possible key.</param>
        /// <returns>
        /// <code>true</code> if and only if the specified int is a
        /// key in this hashtable, as determined by the <tt>equals</tt>
        /// method; <code>false</code> otherwise.
        /// </returns>
        /// <seealso cref="Contains(int)"/>
        public virtual bool ContainsKey(int key)
        {
            IntHashtable.Entry[] tab = table;
            int index = (key & 0x7FFFFFFF) % tab.Length;

            for (IntHashtable.Entry e = tab[index]; e != null; e = e.next)
            {
                if (e.key == key)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #6
0
 /// <summary><p>Tests if some key maps into the specified value in this hashtable.</summary>
 /// <remarks>
 /// <p>Tests if some key maps into the specified value in this hashtable.
 /// This operation is more expensive than the <code>containsKey</code>
 /// method.</p>
 /// <p>Note that this method is identical in functionality to containsValue,
 /// (which is part of the Map interface in the collections framework).</p>
 /// </remarks>
 /// <param name="value">a value to search for.</param>
 /// <returns>
 /// <code>true</code> if and only if some key maps to the
 /// <code>value</code> argument in this hashtable as
 /// determined by the <tt>equals</tt> method;
 /// <code>false</code> otherwise.
 /// </returns>
 /// <exception cref="System.NullReferenceException">if the value is <code>null</code>.</exception>
 /// <seealso cref="ContainsKey(int)"/>
 /// <seealso cref="ContainsValue(int)"/>
 /// <seealso cref="System.Collections.IDictionary{K, V}"/>
 public virtual bool Contains(int value)
 {
     IntHashtable.Entry[] tab = table;
     for (int i = tab.Length; i-- > 0;)
     {
         for (IntHashtable.Entry e = tab[i]; e != null; e = e.next)
         {
             if (e.value == value)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #7
0
        public virtual int GetOneKey()
        {
            if (count == 0)
            {
                return(0);
            }
            int index = table.Length;

            IntHashtable.Entry entry = null;
            while (index-- > 0 && (entry = table[index]) == null)
            {
            }
            if (entry == null)
            {
                return(0);
            }
            return(entry.key);
        }
Пример #8
0
        /// <summary>
        /// <p>Increases the capacity of and internally reorganizes this
        /// hashtable, in order to accommodate and access its entries more
        /// efficiently.</p>
        /// <p>This method is called automatically when the number of keys
        /// in the hashtable exceeds this hashtable's capacity and load
        /// factor.</p>
        /// </summary>
        protected internal virtual void Rehash()
        {
            int oldCapacity = table.Length;

            IntHashtable.Entry[] oldMap = table;
            int newCapacity             = oldCapacity * 2 + 1;

            IntHashtable.Entry[] newMap = new IntHashtable.Entry[newCapacity];
            threshold = (int)(newCapacity * loadFactor);
            table     = newMap;
            for (int i = oldCapacity; i-- > 0;)
            {
                for (IntHashtable.Entry old = oldMap[i]; old != null;)
                {
                    IntHashtable.Entry e = old;
                    old = old.next;
                    int index = (e.key & 0x7FFFFFFF) % newCapacity;
                    e.next        = newMap[index];
                    newMap[index] = e;
                }
            }
        }
Пример #9
0
 /// <summary>Create a new entry with the given values.</summary>
 /// <param name="key">The key used to enter this in the table</param>
 /// <param name="value">The value for this key</param>
 /// <param name="next">A reference to the next entry in the table</param>
 internal Entry(int key, int value, IntHashtable.Entry next)
 {
     this.key   = key;
     this.value = value;
     this.next  = next;
 }