~brenns10/funlisp

ref: 7347d0eca6d0a50799d3048c21f376277e827224 funlisp/src/hashtable.h -rw-r--r-- 4.0 KiB
7347d0ecStephen Brennan Updates for readthedocs 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * hashtable.h: hash table implementation
 *
 * Stephen Brennan <stephen@brennan.io>
 */
#include <stdio.h>

#include "iter.h"

#ifndef HASHTABLE_H
#define HASHTABLE_H

typedef unsigned int (*hash_t)(void *to_hash);

typedef int (*comp_t)(void *left, void *right);

typedef int (*print_t)(FILE *f, void *data);

struct hashtable
{
	unsigned int length;    /* number of items currently in the table */
	unsigned int allocated; /* number of items allocated */

	unsigned int key_size;
	unsigned int value_size;

	hash_t hash;
	comp_t equal;

	void *table;
};

/**
 * @brief Initialize a hash table in memory already allocated.
 * @param table A pointer to the table to initialize.
 * @param hash_func A hash function for the table.
 * @param equal A comparison function for void pointers
 * @param key_size Size of keys.
 * @param value_size Size of values.
 */
void ht_init(struct hashtable *table, hash_t hash_func, comp_t equal,
             unsigned int key_size, unsigned int value_size);
/**
 * @brief Allocate and initialize a hash table.
 * @param hash_func A function that takes one void* and returns a hash value
 * generated from it.  It should be a good hash function.
 * @param equal A comparison function for void pointers.
 * @param key_size Size of keys.
 * @param value_size Size of values.
 * @returns A pointer to the new hash table.
 */
struct hashtable *ht_create(hash_t hash_func, comp_t equal,
                            unsigned int key_size, unsigned int value_size);
/**
 * @brief Free any resources used by the hash table, but doesn't free the
 * pointer.  Doesn't perform any actions on the data as it is deleted.
 *
 * If pointers are contained within the hash table, they are not freed. Use to
 * specify a deletion action on the hash table.
 * @param table The table to destroy.
 */
void ht_destroy(struct hashtable *table);
/**
 * @brief Free the hash table and its resources.	No pointers contained in the
 * table will be freed.
 * @param table The table to free.
 */
void ht_delete(struct hashtable *table);

/**
 * @brief Insert data into the hash table.
 *
 * Expands the hash table if the load factor is below a threshold.	If the key
 * already exists in the table, then the function will overwrite it with the new
 * data provided.
 * @param table A pointer to the hash table.
 * @param key The key to insert.
 * @param value The value to insert at the key.
 */
void ht_insert(struct hashtable *table, void *key, void *value);
void ht_insert_ptr(struct hashtable *table, void *key, void *value);
/**
 * @brief Remove the key, value pair stored in the hash table.
 *
 * This function does not call a deleter on the stored data.
 * @param table A pointer to the hash table.
 * @param key The key to delete.
 * @return -1 on failure, 0 otherwise
 */
int ht_remove(struct hashtable *table, void *key);
int ht_remove_ptr(struct hashtable *table, void *key);
/**
 * @brief Return the value associated with the key provided.
 * @param table A pointer to the hash table.
 * @param key The key whose value to retrieve.
 * @returns The value associated the key, NULL if not found
 */
void *ht_get(struct hashtable const *table, void *key);
void *ht_get_ptr(struct hashtable const *table, void *key);
/**
 * @brief Return true when a key is contained in the table.
 * @param table A pointer to the hash table.
 * @param key The key to search for.
 * @returns Whether the key is present.
 */
bool ht_contains(struct hashtable const *table, void *key);
bool ht_contains_ptr(struct hashtable const *table, void *key);
/**
 * @brief Return the hash of the data, interpreting it as a string.
 * @param data The string to hash, assuming that the value contained is a char*.
 * @returns The hash value of the string.
 */
unsigned int ht_string_hash(void *data);

int ht_string_comp(void *left, void *right);
int ht_int_comp(void *left, void *right);

struct iterator ht_iter_keys(struct hashtable *table);
struct iterator ht_iter_keys_ptr(struct hashtable *table);
struct iterator ht_iter_values(struct hashtable *table);
struct iterator ht_iter_values_ptr(struct hashtable *table);

#endif