vm: change NO_MEM to a more impossible value
[minix.git] / usr.bin / m4 / lib / ohash_lookup_interval.c
blobe49780168ba289aa2a1f2d87c5d31c43505dd3f0
1 /* $OpenBSD: ohash_lookup_interval.c,v 1.3 2006/01/16 15:52:25 espie Exp $ */
2 /* ex:ts=8 sw=4:
3 */
5 /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "ohash_int.h"
22 unsigned int
23 ohash_lookup_interval(struct ohash *h, const char *start, const char *end,
24 uint32_t hv)
26 unsigned int i, incr;
27 unsigned int empty;
29 #ifdef STATS_HASH
30 STAT_HASH_LOOKUP++;
31 #endif
32 empty = NONE;
33 i = hv % h->size;
34 incr = ((hv % (h->size-2)) & ~1) + 1;
35 while (h->t[i].p != NULL) {
36 #ifdef STATS_HASH
37 STAT_HASH_LENGTH++;
38 #endif
39 if (h->t[i].p == DELETED) {
40 if (empty == NONE)
41 empty = i;
42 } else if (h->t[i].hv == hv &&
43 strncmp(h->t[i].p+h->info.key_offset, start,
44 end - start) == 0 &&
45 (h->t[i].p+h->info.key_offset)[end-start] == '\0') {
46 if (empty != NONE) {
47 h->t[empty].hv = hv;
48 h->t[empty].p = h->t[i].p;
49 h->t[i].p = DELETED;
50 return empty;
51 } else {
52 #ifdef STATS_HASH
53 STAT_HASH_POSITIVE++;
54 #endif
55 return i;
58 i += incr;
59 if (i >= h->size)
60 i -= h->size;
63 /* Found an empty position. */
64 if (empty != NONE)
65 i = empty;
66 h->t[i].hv = hv;
67 return i;