[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
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 {
31};
32
33/* Converts pointer to hash element HASH_ELEM into a pointer to
34 * the structure that HASH_ELEM is embedded inside. Supply the
35 * name of the outer structure STRUCT and the member name MEMBER
36 * of the hash element. See the big comment at the top of the
37 * file for an example. */
38#define hash_entry(HASH_ELEM, STRUCT, MEMBER) \
39 ((STRUCT *) ((uint8_t *) &(HASH_ELEM)->list_elem \
40 - offsetof (STRUCT, MEMBER.list_elem)))
41
42/* Computes and returns the hash value for hash element E, given
43 * auxiliary data AUX. */
44typedef uint64_t 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. */
49typedef bool hash_less_func (const struct hash_elem *a,
50 const struct hash_elem *b,
51 void *aux);
52
53/* Performs some operation on hash element E, given auxiliary
54 * data AUX. */
55typedef void hash_action_func (struct hash_elem *e, void *aux);
56
57/* Hash table. */
58struct hash {
59 size_t elem_cnt; /* Number of elements in table. */
60 size_t bucket_cnt; /* Number of buckets, a power of 2. */
61 struct list *buckets; /* Array of `bucket_cnt' lists. */
62 hash_hash_func *hash; /* Hash function. */
63 hash_less_func *less; /* Comparison function. */
64 void *aux; /* Auxiliary data for `hash' and `less'. */
65};
66
67/* A hash table iterator. */
69 struct hash *hash; /* The hash table. */
70 struct list *bucket; /* Current bucket. */
71 struct hash_elem *elem; /* Current hash element in current bucket. */
72};
73
74/* Basic life cycle. */
75bool hash_init (struct hash *, hash_hash_func *, hash_less_func *, void *aux);
76void hash_clear (struct hash *, hash_action_func *);
77void hash_destroy (struct hash *, hash_action_func *);
78
79/* Search, insertion, deletion. */
80struct hash_elem *hash_insert (struct hash *, struct hash_elem *);
81struct hash_elem *hash_replace (struct hash *, struct hash_elem *);
82struct hash_elem *hash_find (struct hash *, struct hash_elem *);
83struct hash_elem *hash_delete (struct hash *, struct hash_elem *);
84
85/* Iteration. */
86void hash_apply (struct hash *, hash_action_func *);
87void hash_first (struct hash_iterator *, struct hash *);
88struct hash_elem *hash_next (struct hash_iterator *);
89struct hash_elem *hash_cur (struct hash_iterator *);
90
91/* Information. */
92size_t hash_size (struct hash *);
93bool hash_empty (struct hash *);
94
95/* Sample hash functions. */
96uint64_t hash_bytes (const void *, size_t);
97uint64_t hash_string (const char *);
98uint64_t hash_int (int);
99
100#endif /* lib/kernel/hash.h */
struct hash_elem * hash_find(struct hash *, struct hash_elem *)
Definition: hash.c:125
struct hash_elem * hash_insert(struct hash *, struct hash_elem *)
Definition: hash.c:94
bool hash_less_func(const struct hash_elem *a, const struct hash_elem *b, void *aux)
Definition: hash.h:49
void hash_destroy(struct hash *, hash_action_func *)
Definition: hash.c:81
size_t hash_size(struct hash *)
Definition: hash.c:230
void hash_first(struct hash_iterator *, struct hash *)
Definition: hash.c:187
struct hash_elem * hash_delete(struct hash *, struct hash_elem *)
Definition: hash.c:137
void hash_apply(struct hash *, hash_action_func *)
Definition: hash.c:153
uint64_t hash_hash_func(const struct hash_elem *e, void *aux)
Definition: hash.h:44
bool hash_init(struct hash *, hash_hash_func *, hash_less_func *, void *aux)
Definition: hash.c:25
uint64_t hash_int(int)
Definition: hash.c:277
struct hash_elem * hash_replace(struct hash *, struct hash_elem *)
Definition: hash.c:109
struct hash_elem * hash_cur(struct hash_iterator *)
Definition: hash.c:224
uint64_t hash_string(const char *)
Definition: hash.c:262
bool hash_empty(struct hash *)
Definition: hash.c:236
void hash_action_func(struct hash_elem *e, void *aux)
Definition: hash.h:55
struct hash_elem * hash_next(struct hash_iterator *)
Definition: hash.c:205
uint64_t hash_bytes(const void *, size_t)
Definition: hash.c:246
void hash_clear(struct hash *, hash_action_func *)
Definition: hash.c:51
unsigned long long int uint64_t
Definition: stdint.h:29
Definition: hash.h:29
Definition: hash.h:68
struct hash * hash
Definition: hash.h:69
struct list * bucket
Definition: hash.h:70
struct hash_elem * elem
Definition: hash.h:71
Definition: hash.h:58
hash_less_func * less
Definition: hash.h:63
hash_hash_func * hash
Definition: hash.h:62
void * aux
Definition: hash.h:64
size_t bucket_cnt
Definition: hash.h:60
struct list * buckets
Definition: hash.h:61
size_t elem_cnt
Definition: hash.h:59
Definition: list.h:87
Definition: list.h:93