[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
hash.h File Reference
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "list.h"
Include dependency graph for hash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hash_elem
 
struct  hash
 
struct  hash_iterator
 

Macros

#define hash_entry(HASH_ELEM, STRUCT, MEMBER)
 

Typedefs

typedef uint64_t hash_hash_func(const struct hash_elem *e, void *aux)
 
typedef bool hash_less_func(const struct hash_elem *a, const struct hash_elem *b, void *aux)
 
typedef void hash_action_func(struct hash_elem *e, void *aux)
 

Functions

bool hash_init (struct hash *, hash_hash_func *, hash_less_func *, void *aux)
 
void hash_clear (struct hash *, hash_action_func *)
 
void hash_destroy (struct hash *, hash_action_func *)
 
struct hash_elemhash_insert (struct hash *, struct hash_elem *)
 
struct hash_elemhash_replace (struct hash *, struct hash_elem *)
 
struct hash_elemhash_find (struct hash *, struct hash_elem *)
 
struct hash_elemhash_delete (struct hash *, struct hash_elem *)
 
void hash_apply (struct hash *, hash_action_func *)
 
void hash_first (struct hash_iterator *, struct hash *)
 
struct hash_elemhash_next (struct hash_iterator *)
 
struct hash_elemhash_cur (struct hash_iterator *)
 
size_t hash_size (struct hash *)
 
bool hash_empty (struct hash *)
 
uint64_t hash_bytes (const void *, size_t)
 
uint64_t hash_string (const char *)
 
uint64_t hash_int (int)
 

Macro Definition Documentation

◆ hash_entry

#define hash_entry (   HASH_ELEM,
  STRUCT,
  MEMBER 
)
Value:
((STRUCT *) ((uint8_t *) &(HASH_ELEM)->list_elem \
- offsetof (STRUCT, MEMBER.list_elem)))
#define offsetof(TYPE, MEMBER)
Definition: stddef.h:5
unsigned char uint8_t
Definition: stdint.h:20
Definition: list.h:87

Typedef Documentation

◆ hash_action_func

typedef void hash_action_func(struct hash_elem *e, void *aux)

◆ hash_hash_func

typedef uint64_t hash_hash_func(const struct hash_elem *e, void *aux)

◆ hash_less_func

typedef bool hash_less_func(const struct hash_elem *a, const struct hash_elem *b, void *aux)

Function Documentation

◆ hash_apply()

void hash_apply ( struct hash h,
hash_action_func action 
)
153 {
154 size_t i;
155
156 ASSERT (action != NULL);
157
158 for (i = 0; i < h->bucket_cnt; i++) {
159 struct list *bucket = &h->buckets[i];
160 struct list_elem *elem, *next;
161
162 for (elem = list_begin (bucket); elem != list_end (bucket); elem = next) {
163 next = list_next (elem);
164 action (list_elem_to_hash_elem (elem), h->aux);
165 }
166 }
167}
#define ASSERT(CONDITION)
Definition: debug.h:30
#define list_elem_to_hash_elem(LIST_ELEM)
Definition: hash.c:12
static int next(int pos)
Definition: intq.c:74
struct list_elem * list_begin(struct list *)
Definition: list.c:68
struct list_elem * list_next(struct list_elem *)
Definition: list.c:77
struct list_elem * list_end(struct list *)
Definition: list.c:88
#define NULL
Definition: stddef.h:4
void * aux
Definition: hash.h:64
size_t bucket_cnt
Definition: hash.h:60
struct list * buckets
Definition: hash.h:61
Definition: list.h:93
Here is the call graph for this function:

◆ hash_bytes()

uint64_t hash_bytes ( const void *  buf_,
size_t  size 
)
246 {
247 /* Fowler-Noll-Vo 32-bit hash, for bytes. */
248 const unsigned char *buf = buf_;
250
251 ASSERT (buf != NULL);
252
254 while (size-- > 0)
255 hash = (hash * FNV_64_PRIME) ^ *buf++;
256
257 return hash;
258}
#define FNV_64_PRIME
Definition: hash.c:241
#define FNV_64_BASIS
Definition: hash.c:242
uint16_t size
Definition: mmu.h:0
unsigned long long int uint64_t
Definition: stdint.h:29
Definition: hash.h:58
Here is the caller graph for this function:

◆ hash_clear()

void hash_clear ( struct hash h,
hash_action_func destructor 
)
51 {
52 size_t i;
53
54 for (i = 0; i < h->bucket_cnt; i++) {
55 struct list *bucket = &h->buckets[i];
56
57 if (destructor != NULL)
58 while (!list_empty (bucket)) {
59 struct list_elem *list_elem = list_pop_front (bucket);
61 destructor (hash_elem, h->aux);
62 }
63
64 list_init (bucket);
65 }
66
67 h->elem_cnt = 0;
68}
struct list_elem * list_pop_front(struct list *)
Definition: list.c:251
void list_init(struct list *)
Definition: list.c:58
bool list_empty(struct list *)
Definition: list.c:296
Definition: hash.h:29
size_t elem_cnt
Definition: hash.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_cur()

struct hash_elem * hash_cur ( struct hash_iterator i)
224 {
225 return i->elem;
226}
struct hash_elem * elem
Definition: hash.h:71
Here is the caller graph for this function:

◆ hash_delete()

struct hash_elem * hash_delete ( struct hash h,
struct hash_elem e 
)
137 {
138 struct hash_elem *found = find_elem (h, find_bucket (h, e), e);
139 if (found != NULL) {
140 remove_elem (h, found);
141 rehash (h);
142 }
143 return found;
144}
static void remove_elem(struct hash *, struct hash_elem *)
Definition: hash.c:392
static struct hash_elem * find_elem(struct hash *, struct list *, struct hash_elem *)
Definition: hash.c:291
static struct list * find_bucket(struct hash *, struct hash_elem *)
Definition: hash.c:283
static void rehash(struct hash *)
Definition: hash.c:324
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_destroy()

void hash_destroy ( struct hash h,
hash_action_func destructor 
)
81 {
82 if (destructor != NULL)
83 hash_clear (h, destructor);
84 free (h->buckets);
85}
void hash_clear(struct hash *h, hash_action_func *destructor)
Definition: hash.c:51
void free(void *)
Definition: malloc.c:202
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_empty()

bool hash_empty ( struct hash h)
236 {
237 return h->elem_cnt == 0;
238}
Here is the caller graph for this function:

◆ hash_find()

struct hash_elem * hash_find ( struct hash h,
struct hash_elem e 
)
125 {
126 return find_elem (h, find_bucket (h, e), e);
127}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_first()

void hash_first ( struct hash_iterator i,
struct hash h 
)
187 {
188 ASSERT (i != NULL);
189 ASSERT (h != NULL);
190
191 i->hash = h;
192 i->bucket = i->hash->buckets;
194}
struct list_elem * list_head(struct list *)
Definition: list.c:141
struct hash * hash
Definition: hash.h:69
struct list * bucket
Definition: hash.h:70
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_init()

bool hash_init ( struct hash h,
hash_hash_func hash,
hash_less_func less,
void *  aux 
)
26 {
27 h->elem_cnt = 0;
28 h->bucket_cnt = 4;
29 h->buckets = malloc (sizeof *h->buckets * h->bucket_cnt);
30 h->hash = hash;
31 h->less = less;
32 h->aux = aux;
33
34 if (h->buckets != NULL) {
35 hash_clear (h, NULL);
36 return true;
37 } else
38 return false;
39}
void * malloc(size_t) __attribute__((malloc))
Definition: malloc.c:85
hash_less_func * less
Definition: hash.h:63
hash_hash_func * hash
Definition: hash.h:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_insert()

struct hash_elem * hash_insert ( struct hash h,
struct hash_elem new 
)
94 {
95 struct list *bucket = find_bucket (h, new);
96 struct hash_elem *old = find_elem (h, bucket, new);
97
98 if (old == NULL)
99 insert_elem (h, bucket, new);
100
101 rehash (h);
102
103 return old;
104}
static void insert_elem(struct hash *, struct list *, struct hash_elem *)
Definition: hash.c:385
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_int()

uint64_t hash_int ( int  i)
277 {
278 return hash_bytes (&i, sizeof i);
279}
uint64_t hash_bytes(const void *buf_, size_t size)
Definition: hash.c:246
Here is the call graph for this function:

◆ hash_next()

struct hash_elem * hash_next ( struct hash_iterator i)
205 {
206 ASSERT (i != NULL);
207
209 while (i->elem == list_elem_to_hash_elem (list_end (i->bucket))) {
210 if (++i->bucket >= i->hash->buckets + i->hash->bucket_cnt) {
211 i->elem = NULL;
212 break;
213 }
215 }
216
217 return i->elem;
218}
struct list_elem list_elem
Definition: hash.h:30
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_replace()

struct hash_elem * hash_replace ( struct hash h,
struct hash_elem new 
)
109 {
110 struct list *bucket = find_bucket (h, new);
111 struct hash_elem *old = find_elem (h, bucket, new);
112
113 if (old != NULL)
114 remove_elem (h, old);
115 insert_elem (h, bucket, new);
116
117 rehash (h);
118
119 return old;
120}
Here is the call graph for this function:

◆ hash_size()

size_t hash_size ( struct hash h)
230 {
231 return h->elem_cnt;
232}

◆ hash_string()

uint64_t hash_string ( const char *  s_)
262 {
263 const unsigned char *s = (const unsigned char *) s_;
265
266 ASSERT (s != NULL);
267
269 while (*s != '\0')
270 hash = (hash * FNV_64_PRIME) ^ *s++;
271
272 return hash;
273}
static uint8_t s[256]
Definition: random.c:17