GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_htable.h
Go to the documentation of this file.
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2021 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
14#ifndef GP_HTABLE_H
15#define GP_HTABLE_H
16
17#include <stddef.h>
18#include <string.h>
19#include <utils/gp_types.h>
20
32
35 void *key;
36 void *val;
37};
38
40struct gp_htable {
44 size_t size;
46 size_t used;
49};
50
52#define GP_HTABLE_FOREACH(table, var) \
53 for (struct gp_htable_rec *var = (table)->recs; var < &((table)->recs[(table)->size]); var++) \
54 if (var->key)
55
67gp_htable *gp_htable_new(unsigned int order, int flags);
68
78int gp_htable_init(gp_htable *self, unsigned int order, int flags);
79
87static inline size_t gp_htable_keys(gp_htable *self)
88{
89 return self->used;
90}
91
98
105static inline size_t gp_htable_strhash(const void *key, size_t htable_size)
106{
107 unsigned int h = 0;
108 const char *str = key;
109
110 while (*str)
111 h = (h * 151 + *str++) % htable_size;
112
113 return h;
114}
115
124static inline int gp_htable_strcmp(const void *key1, const void *key2)
125{
126 return !strcmp(key1, key2);
127}
128
136void gp_htable_put(gp_htable *self, void *val, char *key);
137
146void *gp_htable_get(gp_htable *self, const char *key);
147
156void *gp_htable_rem(gp_htable *self, const char *key);
157
158#endif /* GP_HTABLE_H */
gp_htable * gp_htable_new(unsigned int order, int flags)
Allocates a hash table.
static size_t gp_htable_keys(gp_htable *self)
Returns the number of keys in hash table.
Definition gp_htable.h:87
static size_t gp_htable_strhash(const void *key, size_t htable_size)
A string hashing function.
Definition gp_htable.h:105
void * gp_htable_rem(gp_htable *self, const char *key)
Removes an entry from a hash table.
static int gp_htable_strcmp(const void *key1, const void *key2)
A string matching function.
Definition gp_htable.h:124
void gp_htable_free(gp_htable *self)
Frees a hash table.
int gp_htable_init(gp_htable *self, unsigned int order, int flags)
Initializes an hash table embedded in a different structure.
gp_htable_flags
Flags to change how to deal with the hash table string keys.
Definition gp_htable.h:24
@ GP_HTABLE_FREE_SELF
Definition gp_htable.h:30
@ GP_HTABLE_COPY_KEY
Definition gp_htable.h:26
@ GP_HTABLE_FREE_KEY
Definition gp_htable.h:28
void gp_htable_put(gp_htable *self, void *val, char *key)
Adds a pointer to a hash table.
void * gp_htable_get(gp_htable *self, const char *key)
Search for an element given a string key.
A hash table record.
Definition gp_htable.h:34
A hash table.
Definition gp_htable.h:40
size_t used
Number of used slots in the hash table.
Definition gp_htable.h:46
enum gp_htable_flags flags
Flags.
Definition gp_htable.h:48
size_t size
Hash table record array size.
Definition gp_htable.h:44
struct gp_htable_rec * recs
Array for the hash table records.
Definition gp_htable.h:42
Common header for types.