home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / classes / com / ms / xml / util / StringHashtable.java < prev    next >
Encoding:
Java Source  |  1997-10-13  |  2.6 KB  |  138 lines

  1. /*
  2.  * @(#)StringHashtable.java 1.0 11/10/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7. package com.ms.xml.util;
  8.  
  9. class Entry
  10. {
  11.     int        hash;
  12.     String    key;
  13.     Object    value;
  14.     Entry    next;
  15.  
  16.     Entry(String key, Object value, int hash)
  17.     {
  18.         this.key = key;
  19.         this.value = value;
  20.         this.hash = hash;
  21.     }
  22. }
  23.  
  24. /**
  25.  * This simple hashtable uses strings as the keys.
  26.  *
  27.  * @version 1.0, 6/3/97
  28.  */
  29. public class StringHashtable 
  30. {
  31.     Entry[]    entries;
  32.  
  33.     /**
  34.      * Construct empty hashtable.
  35.      */
  36.     public StringHashtable()
  37.     {
  38.         this(13);
  39.     }
  40.  
  41.     /**
  42.      * Construct empty hashtable.
  43.      */
  44.     public StringHashtable(int size)
  45.     {
  46.         this.entries = new Entry[size];
  47.     }
  48.  
  49.     /**
  50.      * Add object to the hashtable.
  51.      */
  52.     public Object put(String key, Object value)
  53.     {
  54.         int hash = key.hashCode();
  55.         int index = ((hash & 0x7FFFFFFF) % entries.length);
  56.         for (Entry entry = entries[index]; entry != null; entry = entry.next)
  57.         {
  58.             if (entry.hash == hash && entry.key.equals(key))
  59.             {
  60.                 Object o = entry.value;
  61.                 entry.value = value;
  62.                 return o;
  63.             }
  64.         }
  65.         Entry entry = new Entry(key, value, hash);
  66.         entry.next = entries[index];
  67.         entries[index] = entry;
  68.         return null;
  69.     }
  70.  
  71.     /**
  72.      * Get a value from the hashtable.
  73.      */
  74.     public Object get(String key)
  75.     {
  76.         int hash = key.hashCode();
  77.         int index = ((hash & 0x7FFFFFFF) % entries.length);
  78.         for (Entry entry = entries[index]; entry != null; entry = entry.next)
  79.         {
  80.             if (entry.hash == hash && entry.key.equals(key))
  81.             {
  82.                 return entry.value;
  83.             }
  84.             
  85.         }
  86.         return null;
  87.     }
  88.  
  89.     /**
  90.      * Get a value from the hashtable.
  91.      */
  92.     public Object get(char chars[], int offset, int length)
  93.     {
  94.         // calculate hashcode the same way as the String class...
  95.         int hash = 0;
  96.         int off = offset;
  97.  
  98.         if (length < 16) 
  99.         {
  100.             for (int i = length; i > 0; i--) 
  101.             {
  102.                 hash = (hash * 37) + chars[off++];
  103.             }
  104.         } 
  105.         else 
  106.         {
  107.             // only sample some characters
  108.             int skip = length / 8;
  109.             for (int i = length; i > 0; i -= skip, off += skip) 
  110.             {
  111.                 hash = (hash * 39) + chars[off];
  112.             }
  113.         }
  114.         int index = ((hash & 0x7FFFFFFF) % entries.length);
  115.         for (Entry entry = entries[index]; entry != null; entry = entry.next)
  116.         {
  117. nextEntry:    {
  118.                 if (entry.hash == hash)
  119.                 {
  120.                     String key = entry.key;
  121.                     if (key.length() == length) 
  122.                     {
  123.                         while(--length >= 0)
  124.                         {
  125.                             if (chars[offset + length] != key.charAt(length))
  126.                                 break nextEntry;
  127.                         }
  128.                         return entry.value;
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.         return null;
  134.     }
  135. }    
  136.  
  137.  
  138.