The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
trie.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/** Path-compressed prefix tries
19 *
20 * @file src/lib/util/trie.h
21 *
22 * @copyright 2017 Alan DeKok (aland@freeradius.org)
23 */
24RCSIDH(trie_h, "$Id: 6fa6302853993a6e9b10e29f4bf3344d584914ca $")
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <freeradius-devel/build.h>
31#include <freeradius-devel/missing.h>
32#include <freeradius-devel/util/misc.h>
33#include <freeradius-devel/util/talloc.h>
34
35#include <stdbool.h>
36#include <stdint.h>
37#include <stdio.h>
38
39typedef struct fr_trie_s fr_trie_t;
40
41/** Walk over a trie
42 *
43 */
44typedef int (*fr_trie_walk_t)(uint8_t const *key, size_t keylen, void *data, void *uctx);
45
46/* Get a key from data.
47 *
48 * @param[in,out] out - set to a small buffer on input. If the callback has more data
49 * than is available here, the callback can update "out" to point elsewhere
50 * @param[in,out] outlen The number of bits available in the initial buffer. On output,
51 * the number of bits available in the key
52 * @param[in] data the data which contains the key
53 * @return
54 * - <0 on error
55 * - 0 on success
56 */
57typedef int (*fr_trie_key_t)(uint8_t **out, size_t *outlen, void const *data);
58
59fr_trie_t *fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_node);
60
61int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data) CC_HINT(nonnull);
62
63void *fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen) CC_HINT(nonnull);
64
65void *fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen) CC_HINT(nonnull);
66
67void *fr_trie_remove_by_key(fr_trie_t *ft, void const *key, size_t keylen) CC_HINT(nonnull);
68
69int fr_trie_walk(fr_trie_t *ft, void *ctx, fr_trie_walk_t callback) CC_HINT(nonnull(1,3));
70
71/*
72 * Data oriented API.
73 */
74void *fr_trie_match(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
75
76void *fr_trie_find(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
77
78bool fr_trie_insert(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
79
80int fr_trie_replace(void **old, fr_trie_t *ft, void const *data) CC_HINT(nonnull(2, 3));
81
82void *fr_trie_remove(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
83
84bool fr_trie_delete(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
85
86unsigned int fr_trie_num_elements(fr_trie_t *ft) CC_HINT(nonnull); /* always returns 0 */
87
88#ifdef __cplusplus
89}
90#endif
#define RCSIDH(h, id)
Definition build.h:488
unsigned char uint8_t
void(* fr_free_t)(void *)
Definition misc.h:39
void * fr_trie_remove_by_key(fr_trie_t *ft, void const *key, size_t keylen)
Remove a key and return the associated user ctx.
Definition trie.c:2157
int(* fr_trie_key_t)(uint8_t **out, size_t *outlen, void const *data)
Definition trie.h:57
bool fr_trie_delete(fr_trie_t *ft, void const *data)
Remove node and free data (if a free function was specified)
Definition trie.c:2796
void * fr_trie_remove(fr_trie_t *ft, void const *data)
Remove an entry, without freeing the data.
Definition trie.c:2770
bool fr_trie_insert(fr_trie_t *ft, void const *data)
Insert data into a trie.
Definition trie.c:2697
fr_trie_t * fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_node)
Allocate a trie.
Definition trie.c:741
void * fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Lookup a key in a trie and return user ctx, if any.
Definition trie.c:1265
int fr_trie_walk(fr_trie_t *ft, void *ctx, fr_trie_walk_t callback))
Definition trie.c:2610
void * fr_trie_match(fr_trie_t *ft, void const *data)
Match an element exactly in the trie, returning the data.
Definition trie.c:2671
unsigned int fr_trie_num_elements(fr_trie_t *ft)
void * fr_trie_find(fr_trie_t *ft, void const *data)
Find an element in the trie, returning the data.
Definition trie.c:2645
void * fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Match a key and length in a trie and return user ctx, if any.
Definition trie.c:1289
int(* fr_trie_walk_t)(uint8_t const *key, size_t keylen, void *data, void *uctx)
Walk over a trie.
Definition trie.h:44
int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data)
Insert a key and user ctx into a trie.
Definition trie.c:1878
int fr_trie_replace(void **old, fr_trie_t *ft, void const *data))
Replace old data with new data, OR insert if there is no old.
Definition trie.c:2730
static fr_slen_t data
Definition value.h:1334
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1024