KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > sort > HashMap


1 package com.icl.saxon.sort;
2
3 // Copyright © International Computers Limited 1998
4
// See conditions of use
5

6 /**
7 * A simple class for testing membership of a fixed set of ASCII strings.
8 * The class must be initialised with enough space for all the strings,
9 * it will go into an infinite loop if it fills. The string matching is case-blind,
10 * using an algorithm that works only for ASCII
11 */

12
13 public class HashMap {
14
15     String JavaDoc[] strings;
16     int size;
17
18     public HashMap(int size) {
19         strings = new String JavaDoc[size];
20         this.size = size;
21     }
22
23     public void set(String JavaDoc s) {
24         int hash = (hashCode(s) & 0x7fffffff) % size;
25         while(true) {
26             if (strings[hash]==null) {
27                 strings[hash] = s;
28                 return;
29             }
30             if (strings[hash].equalsIgnoreCase(s)) {
31                 return;
32             }
33             hash = (hash + 1) % size;
34         }
35     }
36
37     public boolean get(String JavaDoc s) {
38         int hash = (hashCode(s) & 0x7fffffff) % size;
39         while(true) {
40             if (strings[hash]==null) {
41                 return false;
42             }
43             if (strings[hash].equalsIgnoreCase(s)) {
44                 return true;
45             }
46             hash = (hash + 1) % size;
47         }
48     }
49
50     private int hashCode(String JavaDoc s) {
51         // get a hashcode that doesn't depend on the case of characters.
52
// This relies on the fact that char & 0xDF is case-blind in ASCII
53
int hash = 0;
54         int limit = s.length();
55         if (limit>24) limit = 24;
56         for (int i=0; i<limit; i++) {
57             hash = (hash<<1) + (s.charAt(i) & 0xdf);
58         }
59         return hash;
60     }
61 }
62
63
Popular Tags