1 | #ifndef lint |
---|
2 | static char Rcs_Id[] = |
---|
3 | "$Id: hash.c,v 1.1.1.1 1997-09-03 21:08:09 ghudson Exp $"; |
---|
4 | #endif |
---|
5 | |
---|
6 | /* |
---|
7 | * hash.c - a simple hash function for ispell |
---|
8 | * |
---|
9 | * Pace Willisson, 1983 |
---|
10 | * |
---|
11 | * Copyright 1992, 1993, Geoff Kuenning, Granada Hills, CA |
---|
12 | * All rights reserved. |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * 3. All modifications to the source code must be clearly marked as |
---|
24 | * such. Binary redistributions based on modified source code |
---|
25 | * must be clearly marked as modified versions in the documentation |
---|
26 | * and/or other materials provided with the distribution. |
---|
27 | * 4. All advertising materials mentioning features or use of this software |
---|
28 | * must display the following acknowledgment: |
---|
29 | * This product includes software developed by Geoff Kuenning and |
---|
30 | * other unpaid contributors. |
---|
31 | * 5. The name of Geoff Kuenning may not be used to endorse or promote |
---|
32 | * products derived from this software without specific prior |
---|
33 | * written permission. |
---|
34 | * |
---|
35 | * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND |
---|
36 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
38 | * ARE DISCLAIMED. IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE |
---|
39 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
40 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
41 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
42 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
43 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
44 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
45 | * SUCH DAMAGE. |
---|
46 | */ |
---|
47 | |
---|
48 | /* |
---|
49 | * $Log: not supported by cvs2svn $ |
---|
50 | * Revision 1.20 1994/01/25 07:11:34 geoff |
---|
51 | * Get rid of all old RCS log lines in preparation for the 3.1 release. |
---|
52 | * |
---|
53 | */ |
---|
54 | |
---|
55 | #include "config.h" |
---|
56 | #include "ispell.h" |
---|
57 | #include "proto.h" |
---|
58 | |
---|
59 | int hash P ((ichar_t * word, int hashtblsize)); |
---|
60 | |
---|
61 | /* |
---|
62 | * The following hash algorithm is due to Ian Dall, with slight modifications |
---|
63 | * by Geoff Kuenning to reflect the results of testing with the English |
---|
64 | * dictionaries actually distributed with ispell. |
---|
65 | */ |
---|
66 | #define HASHSHIFT 5 |
---|
67 | |
---|
68 | #ifdef NO_CAPITALIZATION_SUPPORT |
---|
69 | #define HASHUPPER(c) c |
---|
70 | #else /* NO_CAPITALIZATION_SUPPORT */ |
---|
71 | #define HASHUPPER(c) mytoupper(c) |
---|
72 | #endif /* NO_CAPITALIZATION_SUPPORT */ |
---|
73 | |
---|
74 | int hash (s, hashtblsize) |
---|
75 | register ichar_t * s; |
---|
76 | register int hashtblsize; |
---|
77 | { |
---|
78 | register long h = 0; |
---|
79 | register int i; |
---|
80 | |
---|
81 | #ifdef ICHAR_IS_CHAR |
---|
82 | for (i = 4; i-- && *s != 0; ) |
---|
83 | h = (h << 8) | HASHUPPER (*s++); |
---|
84 | #else /* ICHAR_IS_CHAR */ |
---|
85 | for (i = 2; i-- && *s != 0; ) |
---|
86 | h = (h << 16) | HASHUPPER (*s++); |
---|
87 | #endif /* ICHAR_IS_CHAR */ |
---|
88 | while (*s != 0) |
---|
89 | { |
---|
90 | /* |
---|
91 | * We have to do circular shifts the hard way, since C doesn't |
---|
92 | * have them even though the hardware probably does. Oh, well. |
---|
93 | */ |
---|
94 | h = (h << HASHSHIFT) |
---|
95 | | ((h >> (32 - HASHSHIFT)) & ((1 << HASHSHIFT) - 1)); |
---|
96 | h ^= HASHUPPER (*s++); |
---|
97 | } |
---|
98 | return (unsigned long) h % hashtblsize; |
---|
99 | } |
---|