12#define list_elem_to_hash_elem(LIST_ELEM) \
13 list_entry(LIST_ELEM, struct hash_elem, list_elem)
62 if (destructor !=
NULL)
89 if (destructor !=
NULL)
261#define FNV_32_PRIME 16777619u
262#define FNV_32_BASIS 2166136261u
269 const unsigned char *
buf = buf_;
285 const unsigned char *
s = (
const unsigned char *) s_;
309 return &h->
buckets[bucket_idx];
343#define MIN_ELEMS_PER_BUCKET 1
344#define BEST_ELEMS_PER_BUCKET 2
345#define MAX_ELEMS_PER_BUCKET 4
354 size_t old_bucket_cnt, new_bucket_cnt;
355 struct list *new_buckets, *old_buckets;
369 if (new_bucket_cnt < 4)
375 if (new_bucket_cnt == old_bucket_cnt)
379 new_buckets =
malloc (
sizeof *new_buckets * new_bucket_cnt);
380 if (new_buckets ==
NULL)
387 for (i = 0; i < new_bucket_cnt; i++)
395 for (i = 0; i < old_bucket_cnt; i++)
397 struct list *old_bucket;
400 old_bucket = &old_buckets[i];
404 struct list *new_bucket
static char buf[BUF_SIZE]
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
struct hash_elem * hash_cur(struct hash_iterator *i)
Returns the current element in the hash table iteration, or a null pointer at the end of the table.
bool hash_empty(struct hash *h)
Returns true if H contains no elements, false otherwise.
struct hash_elem * hash_insert(struct hash *h, struct hash_elem *new)
Inserts NEW into hash table H and returns a null pointer, if no equal element is already in the table...
static size_t is_power_of_2(size_t x)
Returns true if X is a power of 2, otherwise false.
static size_t turn_off_least_1bit(size_t x)
Returns X with its lowest-order bit set to 1 turned off.
#define list_elem_to_hash_elem(LIST_ELEM)
Hash table.
unsigned hash_bytes(const void *buf_, size_t size)
Returns a hash of the SIZE bytes in BUF.
static void remove_elem(struct hash *, struct hash_elem *)
Removes E from hash table H.
unsigned hash_int(int i)
Returns a hash of integer I.
#define BEST_ELEMS_PER_BUCKET
Ideal elems/bucket.
void hash_apply(struct hash *h, hash_action_func *action)
Calls ACTION for each element in hash table H in arbitrary order.
struct hash_elem * hash_next(struct hash_iterator *i)
Advances I to the next element in the hash table and returns it.
void hash_first(struct hash_iterator *i, struct hash *h)
Initializes I for iterating hash table H.
bool hash_init(struct hash *h, hash_hash_func *hash, hash_less_func *less, void *aux)
Initializes hash table H to compute hash values using HASH and compare hash elements using LESS,...
unsigned hash_string(const char *s_)
Returns a hash of string S.
static struct list * find_bucket(struct hash *, struct hash_elem *)
Returns the bucket in H that E belongs in.
size_t hash_size(struct hash *h)
Returns the number of elements in H.
struct hash_elem * hash_find(struct hash *h, struct hash_elem *e)
Finds and returns an element equal to E in hash table H, or a null pointer if no equal element exists...
void hash_clear(struct hash *h, hash_action_func *destructor)
Removes all the elements from H.
static struct hash_elem * find_elem(struct hash *, struct list *, struct hash_elem *)
Searches BUCKET in H for a hash element equal to E.
void hash_destroy(struct hash *h, hash_action_func *destructor)
Destroys hash table H.
struct hash_elem * hash_replace(struct hash *h, struct hash_elem *new)
Inserts NEW into hash table H, replacing any equal element already in the table, which is returned.
#define FNV_32_PRIME
Fowler-Noll-Vo hash constants, for 32-bit word sizes.
static void insert_elem(struct hash *, struct list *, struct hash_elem *)
Inserts E into BUCKET (in hash table H).
struct hash_elem * hash_delete(struct hash *h, struct hash_elem *e)
Finds, removes, and returns an element equal to E in hash table H.
static void rehash(struct hash *)
Changes the number of buckets in hash table H to match the ideal.
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.
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.
void hash_action_func(struct hash_elem *e, void *aux)
Performs some operation on hash element E, given auxiliary data AUX.
static int next(int pos)
Returns the position after POS within an intq.
struct list_elem * list_begin(struct list *list)
Returns the beginning of LIST.
struct list_elem * list_remove(struct list_elem *elem)
Removes ELEM from its list and returns the element that followed it.
struct list_elem * list_pop_front(struct list *list)
Removes the front element from LIST and returns it.
void list_push_front(struct list *list, struct list_elem *elem)
Inserts ELEM at the beginning of LIST, so that it becomes the front in LIST.
struct list_elem * list_end(struct list *list)
Returns LIST's tail.
bool list_empty(struct list *list)
Returns true if LIST is empty, false otherwise.
struct list_elem * list_head(struct list *list)
Return's LIST's head.
void list_init(struct list *list)
Initializes LIST as an empty list.
struct list_elem * list_next(struct list_elem *elem)
Returns the element after ELEM in its list.
void * malloc(size_t size)
Obtains and returns a new block of at least SIZE bytes.
void free(void *p)
Frees block P, which must have been previously allocated with malloc(), calloc(), or realloc().
static char x
Verifies that mapping over the data segment is disallowed.
static uint8_t s[256]
RC4-based pseudo-random number generator (PRNG).
struct list_elem list_elem
struct hash * hash
The hash table.
struct list * bucket
Current bucket.
struct hash_elem * elem
Current hash element in current bucket.
hash_less_func * less
Comparison function.
hash_hash_func * hash
Hash function.
void * aux
Auxiliary data for ‘hash’ and ‘less’.
size_t bucket_cnt
Number of buckets, a power of 2.
struct list * buckets
Array of ‘bucket_cnt’ lists.
size_t elem_cnt
Number of elements in table.