00001 #ifndef __LIB_KERNEL_HASH_H
00002 #define __LIB_KERNEL_HASH_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdbool.h>
00024 #include <stddef.h>
00025 #include <stdint.h>
00026 #include "list.h"
00027
00028
00029 struct hash_elem
00030 {
00031 struct list_elem list_elem;
00032 };
00033
00034
00035
00036
00037
00038
00039 #define hash_entry(HASH_ELEM, STRUCT, MEMBER) \
00040 ((STRUCT *) ((uint8_t *) &(HASH_ELEM)->list_elem \
00041 - offsetof (STRUCT, MEMBER.list_elem)))
00042
00043
00044
00045 typedef unsigned hash_hash_func (const struct hash_elem *e, void *aux);
00046
00047
00048
00049
00050 typedef bool hash_less_func (const struct hash_elem *a,
00051 const struct hash_elem *b,
00052 void *aux);
00053
00054
00055
00056 typedef void hash_action_func (struct hash_elem *e, void *aux);
00057
00058
00059 struct hash
00060 {
00061 size_t elem_cnt;
00062 size_t bucket_cnt;
00063 struct list *buckets;
00064 hash_hash_func *hash;
00065 hash_less_func *less;
00066 void *aux;
00067 };
00068
00069
00070 struct hash_iterator
00071 {
00072 struct hash *hash;
00073 struct list *bucket;
00074 struct hash_elem *elem;
00075 };
00076
00077
00078 bool hash_init (struct hash *, hash_hash_func *, hash_less_func *, void *aux);
00079 void hash_clear (struct hash *, hash_action_func *);
00080 void hash_destroy (struct hash *, hash_action_func *);
00081
00082
00083 struct hash_elem *hash_insert (struct hash *, struct hash_elem *);
00084 struct hash_elem *hash_replace (struct hash *, struct hash_elem *);
00085 struct hash_elem *hash_find (struct hash *, struct hash_elem *);
00086 struct hash_elem *hash_delete (struct hash *, struct hash_elem *);
00087
00088
00089 void hash_apply (struct hash *, hash_action_func *);
00090 void hash_first (struct hash_iterator *, struct hash *);
00091 struct hash_elem *hash_next (struct hash_iterator *);
00092 struct hash_elem *hash_cur (struct hash_iterator *);
00093
00094
00095 size_t hash_size (struct hash *);
00096 bool hash_empty (struct hash *);
00097
00098
00099 unsigned hash_bytes (const void *, size_t);
00100 unsigned hash_string (const char *);
00101 unsigned hash_int (int);
00102
00103 #endif