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