01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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 java.util.regex;
19:
20: /**
21: * Hashtable implementation for int values.
22: */
23: class IntHash {
24: int[] table;
25:
26: int[] values;
27:
28: int mask;
29:
30: int size; // maximum shift
31:
32: public IntHash(int size) {
33: while (size >= mask) {
34: mask = (mask << 1) | 1;
35: }
36: mask = (mask << 1) | 1;
37: table = new int[mask + 1];
38: values = new int[mask + 1];
39: this .size = size;
40: }
41:
42: public void put(int key, int value) {
43: int i = 0;
44: int hashCode = key & mask;
45:
46: for (;;) {
47: if (table[hashCode] == 0 // empty
48: || table[hashCode] == key) { // rewrite
49: table[hashCode] = key;
50: values[hashCode] = value;
51: return;
52: }
53: i++;
54: i &= mask;
55:
56: hashCode += i;
57: hashCode &= mask;
58: }
59: }
60:
61: public int get(int key) {
62: int hashCode = key & mask;
63: int i = 0;
64: int storedKey;
65:
66: for (;;) {
67: storedKey = table[hashCode];
68:
69: if (storedKey == 0) { // empty
70: return size;
71: }
72:
73: if (storedKey == key) {
74: return values[hashCode];
75: }
76:
77: i++;
78: i &= mask;
79:
80: hashCode += i;
81: hashCode &= mask;
82: }
83: }
84: }
|