PKUOS - Pintos
Pintos source browser for PKU Operating System course
hash.h
Go to the documentation of this file.
1#ifndef __LIB_KERNEL_HASH_H
2#define __LIB_KERNEL_HASH_H
3
4/** Hash table.
5
6 This data structure is thoroughly documented in the Tour of
7 Pintos for Project 3.
8
9 This is a standard hash table with chaining. To locate an
10 element in the table, we compute a hash function over the
11 element's data and use that as an index into an array of
12 doubly linked lists, then linearly search the list.
13
14 The chain lists do not use dynamic allocation. Instead, each
15 structure that can potentially be in a hash must embed a
16 struct hash_elem member. All of the hash functions operate on
17 these `struct hash_elem's. The hash_entry macro allows
18 conversion from a struct hash_elem back to a structure object
19 that contains it. This is the same technique used in the
20 linked list implementation. Refer to lib/kernel/list.h for a
21 detailed explanation. */
22
23#include <stdbool.h>
24#include <stddef.h>
25#include <stdint.h>
26#include "list.h"
27
28/** Hash element. */
29struct hash_elem
30 {
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)->list_elem \
41 - offsetof (STRUCT, MEMBER.list_elem)))
42
43/** Computes and returns the hash value for hash element E, given
44 auxiliary data AUX. */
45typedef unsigned hash_hash_func (const struct hash_elem *e, void *aux);
46
47/** Compares the value of two hash elements A and B, given
48 auxiliary data AUX. Returns true if A is less than B, or
49 false if A is greater than or equal to B. */
50typedef bool hash_less_func (const struct hash_elem *a,
51 const struct hash_elem *b,
52 void *aux);
53
54/** Performs some operation on hash element E, given auxiliary
55 data AUX. */
56typedef void hash_action_func (struct hash_elem *e, void *aux);
57
58/** Hash table. */
59struct hash
60 {
61 size_t elem_cnt; /**< Number of elements in table. */
62 size_t bucket_cnt; /**< Number of buckets, a power of 2. */
63 struct list *buckets; /**< Array of `bucket_cnt' lists. */
64 hash_hash_func *hash; /**< Hash function. */
65 hash_less_func *less; /**< Comparison function. */
66 void *aux; /**< Auxiliary data for `hash' and `less'. */
67 };
68
69/** A hash table iterator. */
71 {
72 struct hash *hash; /**< The hash table. */
73 struct list *bucket; /**< Current bucket. */
74 struct hash_elem *elem; /**< Current hash element in current bucket. */
75 };
76
77/** Basic life cycle. */
78bool hash_init (struct hash *, hash_hash_func *, hash_less_func *, void *aux);
79void hash_clear (struct hash *, hash_action_func *);
80void hash_destroy (struct hash *, hash_action_func *);
81
82/** Search, insertion, deletion. */
83struct hash_elem *hash_insert (struct hash *, struct hash_elem *);
84struct hash_elem *hash_replace (struct hash *, struct hash_elem *);
85struct hash_elem *hash_find (struct hash *, struct hash_elem *);
86struct hash_elem *hash_delete (struct hash *, struct hash_elem *);
87
88/** Iteration. */
89void hash_apply (struct hash *, hash_action_func *);
90void hash_first (struct hash_iterator *, struct hash *);
91struct hash_elem *hash_next (struct hash_iterator *);
92struct hash_elem *hash_cur (struct hash_iterator *);
93
94/** Information. */
95size_t hash_size (struct hash *);
96bool hash_empty (struct hash *);
97
98/** Sample hash functions. */
99unsigned hash_bytes (const void *, size_t);
100unsigned hash_string (const char *);
101unsigned hash_int (int);
102
103#endif /**< lib/kernel/hash.h */
struct hash_elem * hash_find(struct hash *, struct hash_elem *)
Finds and returns an element equal to E in hash table H, or a null pointer if no equal element exists...
Definition: hash.c:132
unsigned hash_bytes(const void *, size_t)
Sample hash functions.
Definition: hash.c:266
struct hash_elem * hash_insert(struct hash *, struct hash_elem *)
Search, insertion, deletion.
Definition: hash.c:99
bool hash_less_func(const struct hash_elem *a, const struct hash_elem *b, void *aux)
Compares the value of two hash elements A and B, given auxiliary data AUX.
Definition: hash.h:50
void hash_destroy(struct hash *, hash_action_func *)
Destroys hash table H.
Definition: hash.c:87
size_t hash_size(struct hash *)
Information.
Definition: hash.c:248
void hash_first(struct hash_iterator *, struct hash *)
Initializes I for iterating hash table H.
Definition: hash.c:200
struct hash_elem * hash_delete(struct hash *, struct hash_elem *)
Finds, removes, and returns an element equal to E in hash table H.
Definition: hash.c:145
void hash_apply(struct hash *, hash_action_func *)
Iteration.
Definition: hash.c:163
bool hash_init(struct hash *, hash_hash_func *, hash_less_func *, void *aux)
Basic life cycle.
Definition: hash.c:25
unsigned hash_string(const char *)
Returns a hash of string S.
Definition: hash.c:283
struct hash_elem * hash_replace(struct hash *, struct hash_elem *)
Inserts NEW into hash table H, replacing any equal element already in the table, which is returned.
Definition: hash.c:115
struct hash_elem * hash_cur(struct hash_iterator *)
Returns the current element in the hash table iteration, or a null pointer at the end of the table.
Definition: hash.c:241
unsigned hash_hash_func(const struct hash_elem *e, void *aux)
Computes and returns the hash value for hash element E, given auxiliary data AUX.
Definition: hash.h:45
bool hash_empty(struct hash *)
Returns true if H contains no elements, false otherwise.
Definition: hash.c:255
unsigned hash_int(int)
lib/kernel/hash.h
Definition: hash.c:299
void hash_action_func(struct hash_elem *e, void *aux)
Performs some operation on hash element E, given auxiliary data AUX.
Definition: hash.h:56
struct hash_elem * hash_next(struct hash_iterator *)
Advances I to the next element in the hash table and returns it.
Definition: hash.c:219
void hash_clear(struct hash *, hash_action_func *)
Removes all the elements from H.
Definition: hash.c:54
Hash table.
Definition: hash.h:30
A hash table iterator.
Definition: hash.h:71
struct hash * hash
The hash table.
Definition: hash.h:72
struct list * bucket
Current bucket.
Definition: hash.h:73
struct hash_elem * elem
Current hash element in current bucket.
Definition: hash.h:74
Hash table.
Definition: hash.h:60
hash_less_func * less
Comparison function.
Definition: hash.h:65
hash_hash_func * hash
Hash function.
Definition: hash.h:64
void * aux
Auxiliary data for ‘hash’ and ‘less’.
Definition: hash.h:66
size_t bucket_cnt
Number of buckets, a power of 2.
Definition: hash.h:62
struct list * buckets
Array of ‘bucket_cnt’ lists.
Definition: hash.h:63
size_t elem_cnt
Number of elements in table.
Definition: hash.h:61
Doubly linked list.
Definition: list.h:91
List.
Definition: list.h:98