2007-03-12 00:18:49 +03:00
|
|
|
/*
|
2008-01-21 22:57:40 +03:00
|
|
|
* Copyright 2002-2008, Haiku Inc. All rights reserved.
|
2007-03-12 00:18:49 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
#ifndef _KERNEL_UTIL_KHASH_H
|
|
|
|
#define _KERNEL_UTIL_KHASH_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2009-02-01 23:48:02 +03:00
|
|
|
// The use of offsetof() on non-PODs is invalid. Since many structs use
|
|
|
|
// templated members (i.e. DoublyLinkedList) which makes them non-PODs we
|
|
|
|
// can't use offsetof() anymore. This macro does the same, but requires an
|
|
|
|
// instance of the object in question.
|
|
|
|
#define offset_of_member(OBJECT, MEMBER) \
|
|
|
|
((size_t)((char*)&OBJECT.MEMBER - (char*)&OBJECT))
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:25:08 +03:00
|
|
|
// can be allocated on the stack
|
|
|
|
typedef struct hash_iterator {
|
|
|
|
void *current;
|
2002-07-09 16:24:59 +04:00
|
|
|
int bucket;
|
2002-11-29 11:25:08 +03:00
|
|
|
} hash_iterator;
|
|
|
|
|
|
|
|
typedef struct hash_table hash_table;
|
|
|
|
|
2003-06-27 06:27:43 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2002-11-29 11:25:08 +03:00
|
|
|
struct hash_table *hash_init(uint32 table_size, int next_ptr_offset,
|
|
|
|
int compare_func(void *element, const void *key),
|
|
|
|
uint32 hash_func(void *element, const void *key, uint32 range));
|
|
|
|
int hash_uninit(struct hash_table *table);
|
|
|
|
status_t hash_insert(struct hash_table *table, void *_element);
|
2008-01-21 22:57:40 +03:00
|
|
|
status_t hash_insert_grow(struct hash_table *table, void *_element);
|
2002-11-29 11:25:08 +03:00
|
|
|
status_t hash_remove(struct hash_table *table, void *_element);
|
2007-03-12 00:18:49 +03:00
|
|
|
void hash_remove_current(struct hash_table *table, struct hash_iterator *iterator);
|
2004-11-10 04:59:09 +03:00
|
|
|
void *hash_remove_first(struct hash_table *table, uint32 *_cookie);
|
2002-11-29 11:25:08 +03:00
|
|
|
void *hash_find(struct hash_table *table, void *e);
|
|
|
|
void *hash_lookup(struct hash_table *table, const void *key);
|
|
|
|
struct hash_iterator *hash_open(struct hash_table *table, struct hash_iterator *i);
|
|
|
|
void hash_close(struct hash_table *table, struct hash_iterator *i, bool free_iterator);
|
|
|
|
void *hash_next(struct hash_table *table, struct hash_iterator *i);
|
|
|
|
void hash_rewind(struct hash_table *table, struct hash_iterator *i);
|
2007-09-09 21:19:52 +04:00
|
|
|
uint32 hash_count_elements(struct hash_table *table);
|
|
|
|
uint32 hash_count_used_slots(struct hash_table *table);
|
2008-09-22 18:58:40 +04:00
|
|
|
void hash_dump_table(struct hash_table* table);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2007-03-12 00:18:49 +03:00
|
|
|
/* function pointers must look like this:
|
2002-11-29 11:25:08 +03:00
|
|
|
*
|
|
|
|
* uint32 hash_func(void *e, const void *key, uint32 range);
|
|
|
|
* hash function should calculate hash on either e or key,
|
2007-03-12 00:18:49 +03:00
|
|
|
* depending on which one is not NULL - they also need
|
|
|
|
* to make sure the returned value is within range.
|
2002-11-29 11:25:08 +03:00
|
|
|
* int compare_func(void *e, const void *key);
|
|
|
|
* compare function should compare the element with
|
|
|
|
* the key, returning 0 if equal, other if not
|
|
|
|
*/
|
|
|
|
|
2013-05-12 01:17:55 +04:00
|
|
|
uint32 hash_hash_string(const char *string);
|
|
|
|
uint32 hash_hash_string_part(const char *string, size_t maxLength);
|
2002-11-29 11:25:08 +03:00
|
|
|
|
2003-06-27 06:27:43 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-12 00:18:49 +03:00
|
|
|
#endif /* _KERNEL_UTIL_KHASH_H */
|