]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.bin/m4/lib/ohash_lookup_interval.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.bin / m4 / lib / ohash_lookup_interval.c
1 /* $OpenBSD: ohash_lookup_interval.c,v 1.3 2006/01/16 15:52:25 espie Exp $ */
2 /* ex:ts=8 sw=4: 
3  */
4
5 /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
6  *
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.
10  *
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.
18  */
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21
22 #include "ohash_int.h"
23         
24 unsigned int
25 ohash_lookup_interval(struct ohash *h, const char *start, const char *end, 
26     uint32_t hv)
27 {
28         unsigned int    i, incr;
29         unsigned int    empty;
30         
31 #ifdef STATS_HASH
32         STAT_HASH_LOOKUP++;
33 #endif
34         empty = NONE;
35         i = hv % h->size;
36         incr = ((hv % (h->size-2)) & ~1) + 1;
37         while (h->t[i].p != NULL) {
38 #ifdef STATS_HASH
39                 STAT_HASH_LENGTH++;
40 #endif
41                 if (h->t[i].p == DELETED) {
42                         if (empty == NONE)
43                                 empty = i;
44                 } else if (h->t[i].hv == hv && 
45                     strncmp(h->t[i].p+h->info.key_offset, start, 
46                         end - start) == 0 &&
47                     (h->t[i].p+h->info.key_offset)[end-start] == '\0') {
48                         if (empty != NONE) {
49                                 h->t[empty].hv = hv;
50                                 h->t[empty].p = h->t[i].p;
51                                 h->t[i].p = DELETED;
52                                 return empty;
53                         } else {
54 #ifdef STATS_HASH
55                                 STAT_HASH_POSITIVE++;
56 #endif
57                                 return i;
58                         }
59                 }
60                 i += incr;
61                 if (i >= h->size) 
62                         i -= h->size;
63         }
64
65         /* Found an empty position.  */
66         if (empty != NONE) 
67                 i = empty;
68         h->t[i].hv = hv;
69         return i;
70 }