4393 /etc/rpc: 100133 and 100169 should be added
[unleashed.git] / usr / src / cmd / sh / hash.c
blob28456e2de64cb2faa0e6d820f55d3fba3cea6650
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 1990 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include "hash.h"
35 #include "defs.h"
37 #define STRCMP(A, B) (cf(A, B) != 0)
38 #define FACTOR 035761254233 /* Magic multiplication factor */
39 #define TABLENGTH 64 /* must be multiple of 2 */
40 #define LOG2LEN 6 /* log2 of TABLENGTH */
43 NOTE: The following algorithm only works on machines where
44 the results of multiplying two integers is the least
45 significant part of the double word integer required to hold
46 the result. It is adapted from Knuth, Volume 3, section 6.4.
49 #define hash(str) (int)(((unsigned)(crunch(str) * FACTOR)) >> shift)
50 struct node
52 ENTRY item;
53 struct node *next;
56 static struct node **last;
57 static struct node *next;
58 static struct node **table;
60 static unsigned int bitsper; /* Bits per byte */
61 static unsigned int shift;
63 static unsigned int crunch();
65 void
66 hcreate(void)
68 unsigned char c = (unsigned char)~0; /* A byte full of 1's */
69 int j;
71 table = (struct node **)alloc(TABLENGTH * sizeof(struct node *));
73 for (j=0; j < TABLENGTH; ++j)
75 table[j] = 0;
78 bitsper = 0;
80 while (c)
82 c = (unsigned int)c >> 1;
83 bitsper++;
86 shift = (bitsper * sizeof(int)) - LOG2LEN;
90 void hscan(uscan)
91 void (*uscan)();
93 struct node *p, *nxt;
94 int j;
96 for (j=0; j < TABLENGTH; ++j)
98 p = table[j];
99 while (p)
101 nxt = p->next;
102 (*uscan)(&p->item);
103 p = nxt;
110 ENTRY *
111 hfind(str)
112 unsigned char *str;
114 struct node *p;
115 struct node **q;
116 unsigned int i;
117 int res;
119 i = hash(str);
121 if(table[i] == 0)
123 last = &table[i];
124 next = 0;
125 return(0);
127 else
129 q = &table[i];
130 p = table[i];
131 while (p != 0 && (res = STRCMP(str, p->item.key)))
133 q = &(p->next);
134 p = p->next;
137 if (p != 0 && res == 0)
138 return(&(p->item));
139 else
141 last = q;
142 next = p;
143 return(0);
148 ENTRY *
149 henter(item)
150 ENTRY item;
152 struct node *p = (struct node *)alloc(sizeof(struct node));
154 p->item = item;
155 *last = p;
156 p->next = next;
157 return(&(p->item));
161 static unsigned int
162 crunch(key)
163 unsigned char *key;
165 unsigned int sum = 0;
166 int s;
168 for (s = 0; *key; s++) /* Simply add up the bytes */
169 sum += *key++;
171 return(sum + s);