summaryrefslogtreecommitdiff
path: root/src/hash.h
blob: 4f595bbb17e9417142c4e2e97be271e7767d16f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (c) 2007, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * hash.h
 * generic hash functions
 */

#ifndef LIBSOLV_HASH_H
#define LIBSOLV_HASH_H

#include "pooltypes.h"

#ifdef __cplusplus
extern "C" {
#endif

/* value of a hash */
typedef unsigned int Hashval;

/* inside the hash table, Ids are stored. Hash maps: string -> hash -> Id */
typedef Id *Hashtable;

/* hash chain */
#define HASHCHAIN_START 7
#define HASHCHAIN_NEXT(h, hh, mask) (((h) + (hh)++) & (mask))

/* very simple hash function
 * string -> hash
 */
static inline Hashval
strhash(const char *str)
{
  Hashval r = 0;
  unsigned int c;
  while ((c = *(const unsigned char *)str++) != 0)
    r += (r << 3) + c;
  return r;
}

static inline Hashval
strnhash(const char *str, unsigned len)
{
  Hashval r = 0;
  unsigned int c;
  while (len-- && (c = *(const unsigned char *)str++) != 0)
    r += (r << 3) + c;
  return r;
}

static inline Hashval
strhash_cont(const char *str, Hashval r)
{
  unsigned int c;
  while ((c = *(const unsigned char *)str++) != 0)
    r += (r << 3) + c;
  return r;
}


/* hash for rel
 * rel -> hash
 */
static inline Hashval
relhash(Id name, Id evr, int flags)
{
  return name + 7 * evr + 13 * flags;
}


/* compute bitmask for value
 * returns smallest (2^n-1) > 2 * num + 3
 *
 * used for Hashtable 'modulo' operation
 */
static inline Hashval
mkmask(unsigned int num)
{
  num = num * 2 + 3;
  while (num & (num - 1))
    num &= num - 1;
  return num * 2 - 1;
}

#ifdef __cplusplus
}
#endif

#endif /* LIBSOLV_HASH_H */