The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
search.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file search.h
5 * @brief search trees (tsearch: a balanced tree of pointers the caller
6 * owns) and linear search in an array.
7 */
8
9#include <stddef.h>
10
11typedef enum
12{
13 preorder, // a node is seen before its children
14 postorder, // between its children
15 endorder, // after its children
16 leaf // a node with no children (seen once)
17} VISIT;
18
19typedef int (*__compar_fn_t)(const void *, const void *);
20
21/**
22 * find key in the tree at *root, or add it.
23 * @return a pointer to the node's key pointer (the found one, or key), or
24 * NULL when there is no memory.
25 */
26void *tsearch(const void *key, void **root, int (*compare)(const void *, const void *));
27/** find key; NULL when it is not in the tree */
28void *tfind(const void *key, void *const *root, int (*compare)(const void *, const void *));
29/**
30 * remove key from the tree.
31 * @return a pointer to the parent's key pointer (any non-NULL pointer when
32 * the root was removed), or NULL when key is not in the tree.
33 */
34void *tdelete(const void *restrict key, void **restrict root,
35 int (*compare)(const void *, const void *));
36/** call action for each node, in the order of the tree, with its depth */
37void twalk(const void *root, void (*action)(const void *node, VISIT which, int depth));
38/** free every node, calling free_key with each key (GNU) */
39void tdestroy(void *root, void (*free_key)(void *key));
40
41/** find key in the array of *count items of size bytes; NULL if it is not there */
42void *lfind(const void *key, const void *base, size_t *count, size_t size,
43 int (*compare)(const void *, const void *));
44/** as lfind, but add key at the end (and count it) when it is not there */
45void *lsearch(const void *key, void *base, size_t *count, size_t size,
46 int (*compare)(const void *, const void *));
uint32_t base
Definition gdt.c:1
NULL, size_t, ptrdiff_t, offsetof.
void * tdelete(const void *restrict key, void **restrict root, int(*compare)(const void *, const void *))
remove key from the tree.
void twalk(const void *root, void(*action)(const void *node, VISIT which, int depth))
call action for each node, in the order of the tree, with its depth
int(*) __compar_fn_t(const void *, const void *)
Definition search.h:19
void tdestroy(void *root, void(*free_key)(void *key))
free every node, calling free_key with each key (GNU)
void * tfind(const void *key, void *const *root, int(*compare)(const void *, const void *))
find key; NULL when it is not in the tree
VISIT
Definition search.h:12
@ postorder
Definition search.h:14
@ leaf
Definition search.h:16
@ endorder
Definition search.h:15
@ preorder
Definition search.h:13
void * lfind(const void *key, const void *base, size_t *count, size_t size, int(*compare)(const void *, const void *))
find key in the array of *count items of size bytes; NULL if it is not there
void * tsearch(const void *key, void **root, int(*compare)(const void *, const void *))
find key in the tree at *root, or add it.
void * lsearch(const void *key, void *base, size_t *count, size_t size, int(*compare)(const void *, const void *))
as lfind, but add key at the end (and count it) when it is not there