Change list_elem from typedef to struct.
[pintos-anon] / src / lib / kernel / hash.h
1 #ifndef __LIB_KERNEL_HASH_H
2 #define __LIB_KERNEL_HASH_H
3
4 /* Hash table.
5
6    This is a standard hash table with chaining.  To locate an
7    element in the table, we compute a hash function over the
8    element's data and use that as an index into an array of
9    doubly linked lists, then linearly search the list.
10
11    The chain lists do not use dynamic allocation.  Instead, each
12    structure that can potentially be in a hash must embed a
13    struct hash_elem member.  All of the hash functions operate on
14    these `struct hash_elem's.  The hash_entry macro allows
15    conversion from a struct hash_elem back to a structure object
16    that contains it.  This is the same technique used in the
17    linked list implementation.  Refer to lib/kernel/list.h for a
18    detailed explanation.
19
20    The FAQ for the VM project contains a detailed example of how
21    to use the hash table. */
22
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include "list.h"
27
28 /* Hash element. */
29 struct hash_elem 
30   {
31     struct list_elem list_elem;
32   };
33
34 /* Converts pointer to hash element HASH_ELEM into a pointer to
35    the structure that HASH_ELEM is embedded inside.  Supply the
36    name of the outer structure STRUCT and the member name MEMBER
37    of the hash element.  See the big comment at the top of the
38    file for an example. */
39 #define hash_entry(HASH_ELEM, STRUCT, MEMBER)                              \
40         ((STRUCT *) ((uint8_t *) (HASH_ELEM) - offsetof (STRUCT, MEMBER)))
41
42 /* Computes and returns the hash value for hash element E, given
43    auxiliary data AUX. */
44 typedef unsigned hash_hash_func (const struct hash_elem *e, void *aux);
45
46 /* Compares the value of two hash elements A and B, given
47    auxiliary data AUX.  Returns true if A is less than B, or
48    false if A is greater than or equal to B. */
49 typedef bool hash_less_func (const struct hash_elem *a,
50                              const struct hash_elem *b,
51                              void *aux);
52
53 /* Hash table. */
54 struct hash 
55   {
56     size_t elem_cnt;            /* Number of elements in table. */
57     size_t bucket_cnt;          /* Number of buckets, a power of 2. */
58     struct list *buckets;       /* Array of `bucket_cnt' lists. */
59     hash_hash_func *hash;       /* Hash function. */
60     hash_less_func *less;       /* Comparison function. */
61     void *aux;                  /* Auxiliary data for `hash' and `less'. */
62   };
63
64 /* A hash table iterator. */
65 struct hash_iterator 
66   {
67     struct hash *hash;          /* The hash table. */
68     struct list *bucket;        /* Current bucket. */
69     struct hash_elem *elem;     /* Current hash element in current bucket. */
70   };
71
72 /* Basic life cycle. */
73 bool hash_init (struct hash *, hash_hash_func *, hash_less_func *, void *aux);
74 void hash_clear (struct hash *);
75 void hash_destroy (struct hash *);
76
77 /* Search, insertion, deletion. */
78 struct hash_elem *hash_insert (struct hash *, struct hash_elem *);
79 struct hash_elem *hash_replace (struct hash *, struct hash_elem *);
80 struct hash_elem *hash_find (struct hash *, struct hash_elem *);
81 struct hash_elem *hash_delete (struct hash *, struct hash_elem *);
82
83 /* Iteration. */
84 void hash_first (struct hash_iterator *, struct hash *);
85 struct hash_elem *hash_next (struct hash_iterator *);
86 struct hash_elem *hash_cur (struct hash_iterator *);
87
88 /* Information. */
89 size_t hash_size (struct hash *);
90 bool hash_empty (struct hash *);
91
92 /* Sample hash functions. */
93 unsigned hash_bytes (const void *, size_t);
94 unsigned hash_string (const char *);
95 unsigned hash_int (int);
96
97 #endif /* lib/kernel/hash.h */