Hashtable.java 10.4 KB
Newer Older
Tom Tromey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.util;

import java.io.Serializable;

/**
 * @author Warren Levy <warrenl@cygnus.com>
 * @date September 24, 1998.
 */
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * "The Java Language Specification", ISBN 0-201-63451-1
 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
 * Status:  Believed complete and correct
 */

class HashtableEntry
{
  public Object key;
  public Object value;
  public HashtableEntry nextEntry = null;

  public HashtableEntry(Object key, Object value)
  {
    this.key = key;
    this.value = value;
  }
}

class HashtableEnumeration implements Enumeration
{
  // TBD: Enumeration is not safe if new elements are put in the table as
  // this could cause a rehash and we'd completely lose our place.  Even
  // without a rehash, it is undetermined if a new element added would
  // appear in the enumeration.  The spec says nothing about this, but
  // the "Java Class Libraries" book infers that modifications to the
  // hashtable during enumeration causes indeterminate results.  Don't do it!
  // A safer way would be to make a copy of the table (e.g. into a vector)
  // but this is a fair bit  more expensive.
  private HashtableEntry[] bucket;
  private int bucketIndex;
  private HashtableEntry elem;
  private int enumCount;
  private int size;
  private boolean values;

  public HashtableEnumeration(HashtableEntry[] bkt, int sz, boolean isValues)
  {
    bucket = bkt;
    bucketIndex = -1;
    enumCount = 0;
    elem = null;
    size = sz;
    values = isValues;
  }

  public boolean hasMoreElements()
  {
    return enumCount < size;
  }

  public Object nextElement()
  {
    if (!hasMoreElements())
      throw new NoSuchElementException();

    // Find next element
    if (elem != null)		// In the middle of a bucket
      elem = elem.nextEntry;
    while (elem == null)	// Find the next non-empty bucket
      elem = bucket[++bucketIndex];

    enumCount++;
    return values ? elem.value : elem.key;
  }
}

// TBD: The algorithm used here closely reflects what is described in
// the "Java Class Libraries" book.  The "Java Language Spec" is much
// less specific about the implementation.  Because of this freedom
// provided by the actual spec, hash table algorithms should be
// investigated to see if there is a better alternative to this one.

// TODO12:
// public class Hashtable extends Dictionary
//			implements Map, Cloneable, Serializable

public class Hashtable extends Dictionary implements Cloneable, Serializable
{
  private HashtableEntry bucket[];
  private float loadFactor;
  private int hsize = 0;

  public Hashtable()
  {
    // The "Java Class Libraries" book (p. 919) says that initial size in this
    // case is 101 (a prime number to increase the odds of even distribution).
    this(101, 0.75F);
  }

  public Hashtable(int initialSize)
  {
    this(initialSize, 0.75F);
  }

  public Hashtable(int initialSize, float loadFactor)
  {
    if (initialSize < 0 || loadFactor <= 0.0 || loadFactor > 1.0)
      throw new IllegalArgumentException();

    bucket = new HashtableEntry[initialSize];
    this.loadFactor = loadFactor;
  }

  // TODO12:
  // public Hashtable(Map t)
  // {
  // }

  public synchronized void clear()
  {
    // Aid the GC by nulling out the entries in the hash table.
    for (int i = 0; i < bucket.length; i++)
      {
        HashtableEntry elem = bucket[i];
	bucket[i] = null;			// May already be null.
	while (elem != null)
	  {
	    HashtableEntry next = elem.nextEntry;
	    elem.nextEntry = null;		// May already be null.
	    elem = next;
	  }
      }
    hsize = 0;
  }

  public synchronized Object clone()
  {
    // New hashtable will have same initialCapacity and loadFactor.
    Hashtable newTable = new Hashtable(bucket.length, loadFactor);

    HashtableEntry newElem, prev = null;
    for (int i = 0; i < bucket.length; i++)
      for (HashtableEntry elem = bucket[i]; elem != null; elem = elem.nextEntry)
	{
	  // An easy but expensive method is newTable.put(elem.key, elem.value);
	  // Since the hash tables are the same size, the buckets and collisions
	  // will be the same in the new one, so we can just clone directly.
	  // This is much cheaper than using put.
	  newElem = new HashtableEntry(elem.key, elem.value);
	  if (newTable.bucket[i] == null)
	    prev = newTable.bucket[i] = newElem;
	  else
	    prev = prev.nextEntry = newElem;
	}

    newTable.hsize = this.hsize;
    return newTable;
  }

  public synchronized boolean contains(Object value) throws NullPointerException
  {
    // An exception is thrown here according to the JDK 1.2 doc.
    if (value == null)
      throw new NullPointerException();

    for (int i = 0; i < bucket.length; i++)
      for (HashtableEntry elem = bucket[i]; elem != null; elem = elem.nextEntry)
	if (elem.value.equals(value))
	  return true;

    return false;
  }

  public synchronized boolean containsKey(Object key)
  {
    // The Map interface mandates that we throw this.
    if (key == null)
      throw new NullPointerException ();

    for (HashtableEntry elem = bucket[Math.abs(key.hashCode()
					       % bucket.length)];
	 elem != null; elem = elem.nextEntry)
      if (elem.key.equals(key))
	return true;

    return false;
  }

  public synchronized Enumeration elements()
  {
    return new HashtableEnumeration(bucket, hsize, true);
  }

  public synchronized Object get(Object key)
  {
    // The Dictionary interface mandates that get() throw a
    // NullPointerException if key is null.
    if (key == null)
      throw new NullPointerException ();

    for (HashtableEntry elem = bucket[Math.abs (key.hashCode()
						% bucket.length)];
	 elem != null; elem = elem.nextEntry)
      if (elem.key.equals(key))
	return elem.value;

    return null;
  }

  public boolean isEmpty()
  {
    return this.hsize <= 0;
  }

  public synchronized Enumeration keys()
  {
    return new HashtableEnumeration(bucket, hsize, false);
  }

  public synchronized Object put(Object key, Object value)
				throws NullPointerException
  {
    if (key == null || value == null)
      throw new NullPointerException();

    HashtableEntry prevElem = null;
    final int index = Math.abs(key.hashCode() % bucket.length);

    for (HashtableEntry elem = bucket[index]; elem != null;
	 prevElem = elem, elem = elem.nextEntry)
      if (elem.key.equals(key))
	{
	  // Update with the new value and then return the old one.
	  Object oldVal = elem.value;
	  elem.value = value;
	  return oldVal;
	}

    // At this point, we know we need to add a new element.
    HashtableEntry newElem = new HashtableEntry(key, value);
    if (bucket[index] == null)
      bucket[index] = newElem;
    else
      prevElem.nextEntry = newElem;

    if (++hsize > loadFactor * bucket.length)
      rehash();

    return null;
  }

  protected void rehash()
  {
    // Create a new table which is twice the size (plus one) of the old.
    // One is added to make the new array length odd so it thus has at least
    // a (small) possibility of being a prime number.
    HashtableEntry oldBucket[] = bucket;
    bucket = new HashtableEntry[bucket.length * 2 + 1];

    // Copy over each entry into the new table
    HashtableEntry elem;
    for (int i = 0; i < oldBucket.length; i++)
      for (elem = oldBucket[i]; elem != null; elem = elem.nextEntry)
	{
	  // Calling put(elem.key, elem.value); would seem like the easy way
	  // but it is dangerous since put increases 'hsize' and calls rehash!
	  // This could become infinite recursion under the right
	  // circumstances.  Instead, we'll add the element directly; this is a
	  // bit more efficient than put since the data is already verified.
    	  final int index = Math.abs(elem.key.hashCode() % bucket.length);
	  HashtableEntry newElem = new HashtableEntry(elem.key, elem.value);
	  if (bucket[index] == null)
	    bucket[index] = newElem;
	  else
	    {
	      // Since this key can't already be in the table, just add this
	      // in at the top of the bucket.
	      newElem.nextEntry = bucket[index];
	      bucket[index] = newElem;
	    }
	}
  }

  public synchronized Object remove(Object key)
  {
    // TBD: Hmm, none of the various docs say to throw an exception here.
    if (key == null)
      return null;

    Object retval;
    HashtableEntry prevElem = null;
    final int index = Math.abs(key.hashCode() % bucket.length);

    for (HashtableEntry elem = bucket[index]; elem != null;
	 prevElem = elem, elem = elem.nextEntry)
      if (elem.key.equals(key))
	{
	  retval = elem.value;
	  if (prevElem == null)
	    bucket[index] = elem.nextEntry;
	  else
	    prevElem.nextEntry = elem.nextEntry;
	  --hsize;
	  return retval;
	}

    return null;
  }

  public int size()
  {
    return this.hsize;
  }

  public synchronized String toString()
  {
    // Following the Java Lang Spec 21.5.4 (p. 636).

    Enumeration keys = keys();
    Enumeration values = elements();

    // Prepend first element with open bracket
    StringBuffer result = new StringBuffer("{");

    // add first element if one exists
    // TBD: Seems like it is more efficient to catch the exception than
    // to call hasMoreElements each time around.
    try
    {
      result.append(keys.nextElement().toString() + "=" +
		values.nextElement().toString());
    }
    catch (NoSuchElementException ex)
    {
    }

    // Prepend subsequent elements with ", "
    try
    {
      while (true)
        result.append(", " + keys.nextElement().toString() + "=" +
		values.nextElement().toString());
    }
    catch (NoSuchElementException ex)
    {
    }

    // Append last element with closing bracket
    result.append("}");
    return result.toString();
  }

  // TODO12:
  // public Set entrySet()
  // {
  // }

  // TODO12:
  // public Set keySet()
  // {
  // }

  // Since JDK 1.2:
  // This method is identical to contains but is part of the 1.2 Map interface.
  // TBD: Should contains return containsValue instead?  Depends on which
  // will be called more typically.
  public synchronized boolean containsValue(Object value)
  {
    return this.contains(value);
  }

  // TODO12:
  // public boolean equals(Object o)
  // {
  // }

  // TODO12:
  // public boolean hashCode()
  // {
  // }

  // TODO12:
  // public void putAll(Map t)
  // {
  // }

  // TODO12:
  // public Collection values()
  // {
  // }
}