KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > StringIntMap


1 /*
2
3    Copyright 2002 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.css.engine;
19
20 /**
21  * A simple hashtable, not synchronized, with fixed load factor.
22  * Keys are Strings and values are ints.
23  *
24  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
25  * @version $Id: StringIntMap.java,v 1.3 2004/08/18 07:12:48 vhardy Exp $
26  */

27 public class StringIntMap {
28
29     /**
30      * The underlying array
31      */

32     protected Entry[] table;
33         
34     /**
35      * The number of entries
36      */

37     protected int count;
38         
39     /**
40      * Creates a new table.
41      * @param c The capacity of the table.
42      */

43     public StringIntMap(int c) {
44     table = new Entry[((c * 3) >>> 2) + 1];
45     }
46
47     /**
48      * Gets the value corresponding to the given string.
49      * @return the value or -1.
50      */

51     public int get(String JavaDoc key) {
52     int hash = key.hashCode() & 0x7FFFFFFF;
53     int index = hash % table.length;
54     
55     for (Entry e = table[index]; e != null; e = e.next) {
56         if ((e.hash == hash) && e.key.equals(key)) {
57         return e.value;
58         }
59     }
60     return -1;
61     }
62     
63     /**
64      * Sets a new value for the given variable
65      */

66     public void put(String JavaDoc key, int value) {
67     int hash = key.hashCode() & 0x7FFFFFFF;
68     int index = hash % table.length;
69     
70     for (Entry e = table[index]; e != null; e = e.next) {
71         if ((e.hash == hash) && e.key.equals(key)) {
72         e.value = value;
73         return;
74         }
75     }
76     
77     // The key is not in the hash table
78
int len = table.length;
79     if (count++ >= (len * 3) >>> 2) {
80         rehash();
81         index = hash % table.length;
82     }
83     
84     Entry e = new Entry(hash, key, value, table[index]);
85     table[index] = e;
86     }
87
88     /**
89      * Rehash the table
90      */

91     protected void rehash () {
92     Entry[] oldTable = table;
93     
94     table = new Entry[oldTable.length * 2 + 1];
95     
96     for (int i = oldTable.length-1; i >= 0; i--) {
97         for (Entry old = oldTable[i]; old != null;) {
98         Entry e = old;
99         old = old.next;
100         
101         int index = e.hash % table.length;
102         e.next = table[index];
103         table[index] = e;
104         }
105     }
106     }
107
108     /**
109      * To manage collisions
110      */

111     protected static class Entry {
112     /**
113      * The hash code
114      */

115     public int hash;
116     
117     /**
118      * The key
119      */

120     public String JavaDoc key;
121     
122     /**
123      * The value
124      */

125     public int value;
126     
127     /**
128      * The next entry
129      */

130     public Entry next;
131     
132     /**
133      * Creates a new entry
134      */

135     public Entry(int hash, String JavaDoc key, int value, Entry next) {
136         this.hash = hash;
137         this.key = key;
138         this.value = value;
139         this.next = next;
140     }
141     }
142 }
143
Popular Tags