libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
htbl.h
Go to the documentation of this file.
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license in the file COPYING
10 * or http://www.opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file COPYING.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2023 Saso Kiselkov. All rights reserved.
24 */
34#ifndef _ACFUTILS_HTBL_H_
35#define _ACFUTILS_HTBL_H_
36
37#include <stdbool.h>
38#include <stdlib.h>
39#include <stdint.h>
40
41#include "sysmacros.h"
42#include "types.h"
43#include "list.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
62typedef struct {
63 size_t tbl_sz;
64 size_t key_sz;
65 list_t *buckets;
66 size_t num_values;
67 bool_t multi_value;
68} htbl_t;
69
70typedef struct {
71 htbl_t h;
72 size_t value_sz;
73} htbl2_t;
74
75typedef struct htbl_multi_value_s htbl_multi_value_t;
76#ifndef __INCLUDED_FROM_HTBL_C__
77typedef struct htbl2_multi_value_s htbl2_multi_value_t;
78#else
79typedef struct htbl_multi_value_s htbl2_multi_value_t;
80#endif
81
82API_EXPORT void htbl_create(htbl_t REQ_PTR(htbl), size_t tbl_sz,
83 size_t key_sz, bool_t multi_value);
84void htbl_destroy(htbl_t REQ_PTR(htbl));
85
86API_EXPORT void htbl2_create(htbl2_t REQ_PTR(htbl), size_t tbl_sz,
87 size_t key_sz, size_t value_sz, bool multi_value);
88void htbl2_destroy(htbl2_t REQ_PTR(htbl));
89
90API_EXPORT void htbl_empty(htbl_t REQ_PTR(htbl),
91 void (*func)(void *value, void *userinfo), void *userinfo);
92API_EXPORT void htbl2_empty(htbl2_t REQ_PTR(htbl), size_t value_sz,
93 void (*func)(void *value, void *userinfo), void *userinfo);
94
95API_EXPORT size_t htbl_count(const htbl_t *htbl);
96API_EXPORT size_t htbl2_count(const htbl2_t *htbl);
97
98API_EXPORT void htbl_set(htbl_t REQ_PTR(htbl), const void *key, void *value);
99API_EXPORT void htbl2_set(htbl2_t REQ_PTR(htbl), const void *key,
100 size_t key_sz, void *value, size_t value_sz);
101
102API_EXPORT void htbl_remove(htbl_t REQ_PTR(htbl),
103 const void *key, bool_t nil_ok);
104API_EXPORT void htbl2_remove(htbl2_t REQ_PTR(htbl),
105 const void *key, size_t key_sz, bool nil_ok);
106
107API_EXPORT void htbl_remove_multi(htbl_t REQ_PTR(htbl),
108 const void *key, htbl_multi_value_t *list_item);
109API_EXPORT void htbl2_remove_multi(htbl2_t REQ_PTR(htbl),
110 const void *key, size_t key_sz, htbl2_multi_value_t *list_item);
111
112API_EXPORT void *htbl_lookup(const htbl_t REQ_PTR(htbl), const void *key);
113API_EXPORT void *htbl2_lookup(const htbl2_t REQ_PTR(htbl), const void *key,
114 size_t key_sz, size_t value_sz);
115
116API_EXPORT const list_t *htbl_lookup_multi(const htbl_t REQ_PTR(htbl),
117 const void *key);
118API_EXPORT const list_t *htbl2_lookup_multi(const htbl2_t REQ_PTR(htbl),
119 const void *key, size_t key_sz);
120
121API_EXPORT void *htbl_value_multi(const htbl_multi_value_t *mv);
122API_EXPORT void *htbl2_value_multi(const htbl2_multi_value_t *mv,
123 size_t value_sz);
127#define HTBL_VALUE_MULTI(x) htbl_value_multi(x)
128
129API_EXPORT void htbl_foreach(const htbl_t *htbl,
130 void (*func)(const void *key, void *value, void *userinfo),
131 void *userinfo);
132API_EXPORT void htbl2_foreach(const htbl2_t REQ_PTR(htbl),
133 size_t key_sz, size_t value_sz,
134 void (*func)(const void *key, void *value, void *userinfo), void *userinfo);
135
136API_EXPORT char *htbl_dump(const htbl_t *htbl, bool_t printable_keys);
137
148UNUSED_ATTR static void
149htbl_free(void *obj, void *unused)
150{
151 LACF_UNUSED(unused);
152 free(obj);
153}
154
155#ifdef __cplusplus
156}
157#endif
158
159#endif /* _ACFUTILS_HTBL_H_ */
#define UNUSED_ATTR
Definition core.h:95
static void htbl_free(void *obj, void *unused)
Definition htbl.h:149
void htbl_foreach(const htbl_t *htbl, void(*func)(const void *key, void *value, void *userinfo), void *userinfo)
Definition htbl.c:521
void htbl_empty(htbl_t *htbl, void(*func)(void *value, void *userinfo), void *userinfo)
Definition htbl.c:203
void htbl_remove_multi(htbl_t *htbl, const void *key, htbl_multi_value_t *list_item)
Definition htbl.c:402
void htbl_set(htbl_t *htbl, const void *key, void *value)
Definition htbl.c:313
const list_t * htbl_lookup_multi(const htbl_t *htbl, const void *key)
Definition htbl.c:492
void htbl_create(htbl_t *htbl, size_t tbl_sz, size_t key_sz, bool_t multi_value)
Definition htbl.c:127
size_t htbl_count(const htbl_t *htbl)
Definition htbl.c:234
void htbl_remove(htbl_t *htbl, const void *key, bool_t nil_ok)
Definition htbl.c:341
void * htbl_value_multi(const htbl_multi_value_t *mv)
Definition htbl.c:578
char * htbl_dump(const htbl_t *htbl, bool_t printable_keys)
Definition htbl.c:602
void * htbl_lookup(const htbl_t *htbl, const void *key)
Definition htbl.c:462
void htbl_destroy(htbl_t *htbl)
Definition htbl.c:154
Definition htbl.h:70
Definition htbl.h:62
#define REQ_PTR(x)
Definition sysmacros.h:334