01: package net.sf.saxon.event;
02:
03: /**
04: * A simple class for testing membership of a fixed set of casse-insensitive ASCII strings.
05: * The class must be initialised with enough space for all the strings,
06: * it will go into an infinite loop if it fills. The string matching is case-blind,
07: * using an algorithm that works only for ASCII.
08: *
09: * The class implements part of the java.util.Set interface; it could be replaced with
10: * an implementation of java.util.Set together with a class that implemented a customized
11: * equals() method.
12: */
13:
14: public class HTMLTagHashSet {
15:
16: String[] strings;
17: int size;
18:
19: public HTMLTagHashSet(int size) {
20: strings = new String[size];
21: this .size = size;
22: }
23:
24: public void add(String s) {
25: int hash = (hashCode(s) & 0x7fffffff) % size;
26: while (true) {
27: if (strings[hash] == null) {
28: strings[hash] = s;
29: return;
30: }
31: if (strings[hash].equalsIgnoreCase(s)) {
32: return;
33: }
34: hash = (hash + 1) % size;
35: }
36: }
37:
38: public boolean contains(String s) {
39: int hash = (hashCode(s) & 0x7fffffff) % size;
40: while (true) {
41: if (strings[hash] == null) {
42: return false;
43: }
44: if (strings[hash].equalsIgnoreCase(s)) {
45: return true;
46: }
47: hash = (hash + 1) % size;
48: }
49: }
50:
51: private int hashCode(String s) {
52: // get a hashcode that doesn't depend on the case of characters.
53: // This relies on the fact that char & 0xDF is case-blind in ASCII
54: int hash = 0;
55: int limit = s.length();
56: if (limit > 24)
57: limit = 24;
58: for (int i = 0; i < limit; i++) {
59: hash = (hash << 1) + (s.charAt(i) & 0xdf);
60: }
61: return hash;
62: }
63: }
64:
65: //
66: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
67: // you may not use this file except in compliance with the License. You may obtain a copy of the
68: // License at http://www.mozilla.org/MPL/
69: //
70: // Software distributed under the License is distributed on an "AS IS" basis,
71: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
72: // See the License for the specific language governing rights and limitations under the License.
73: //
74: // The Original Code is: all this file.
75: //
76: // The Initial Developer of this module is Michael H. Kay.
77: //
78: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
79: //
80: // Contributor(s): none.
81: //
|