use SATSOLVER_ prefix in include guards
[platform/upstream/libsolv.git] / src / hash.h
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * hash.h
10  * generic hash functions
11  */
12
13 #ifndef SATSOLVER_HASH_H
14 #define SATSOLVER_HASH_H
15
16 #include "pooltypes.h"
17
18 /* value of a hash */
19 typedef unsigned int Hashval;
20 /* mask for hash, used as modulo operator to ensure 'wrapping' of hash
21    values -> hash table */
22 typedef unsigned int Hashmask;
23
24 /* inside the hash table, Ids are stored. Hash maps: string -> hash -> Id */
25 typedef Id *Hashtable;
26
27 /* hash chain */
28 #define HASHCHAIN_START 7
29 #define HASHCHAIN_NEXT(h, hh, mask) (((h) + (hh)++) & (mask))
30
31 /* very simple hash function
32  * string -> hash
33  */
34 static inline Hashval
35 strhash(const char *str)
36 {
37   Hashval r = 0;
38   unsigned int c;
39   while ((c = *(const unsigned char *)str++) != 0)
40     r += (r << 3) + c;
41   return r;
42 }
43
44 /* hash for rel
45  * rel -> hash
46  */
47 static inline Hashval
48 relhash(Id name, Id evr, int flags)
49 {
50   return name + 7 * evr + 13 * flags;
51 }
52
53
54 /* compute bitmask for value
55  * returns smallest (2^n-1) > num
56  * 
57  * used for Hashtable 'modulo' operation
58  */ 
59 static inline Hashmask
60 mkmask(unsigned int num)
61 {
62   num *= 2;
63   while (num & (num - 1))
64     num &= num - 1;
65   return num * 2 - 1;
66 }
67
68 #endif /* SATSOLVER_HASH_H */