FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hashtable.h
Go to the documentation of this file.
1 /*
2  * Generic hashtable
3  * Copyright (C) 2024 Emma Worley <emma@emma.gg>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef AVCODEC_HASHTABLE_H
23 #define AVCODEC_HASHTABLE_H
24 
25 #include <stddef.h>
26 
27 /* Implements a hash table using Robin Hood open addressing.
28  * See: https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf
29  *
30  * Keys are placed in the table based on their CRC value and are considered
31  * equal when they are bytewise-identical.
32  */
33 
35 
36 /**
37  * Create a fixed-sized Robin Hood hash table.
38  *
39  * @param ctx context to allocate and initialize
40  * @param key_size size of key type in bytes
41  * @param val_size size of value type in bytes
42  * @param max_entries maximum number of key-value pairs to store
43  *
44  * @return zero on success, nonzero on error
45  */
46 int ff_hashtable_alloc(struct FFHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries);
47 
48 /**
49  * Look up a value from a hash table given a key.
50  *
51  * @param ctx hash table context
52  * @param key pointer to key data
53  * @param val destination pointer for value data
54  *
55  * @return 1 if the key is found, zero if the key is not found
56  */
57 int ff_hashtable_get(const struct FFHashtableContext *ctx, const void *key, void *val);
58 
59 /**
60  * Store a value in a hash table given a key.
61  *
62  * @param ctx hash table context
63  * @param key pointer to key data
64  * @param val pointer for value data
65  *
66  * @return 1 if the key is written, zero if the key is not written due to the hash table reaching max capacity
67  */
68 int ff_hashtable_set(struct FFHashtableContext *ctx, const void *key, const void *val);
69 
70 /**
71  * Delete a value from a hash table given a key.
72  *
73  * @param ctx hash table context
74  * @param key pointer to key data
75  *
76  * @return 1 if the key is deleted, zero if the key is not deleted due to not being found
77  */
78 int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key);
79 
80 /**
81  * Delete all values from a hash table.
82  *
83  * @param ctx hash table context
84  */
86 
87 /**
88  * Free a hash table.
89  *
90  * @param ctx hash table context
91  */
93 
94 #endif
val
static double val(void *priv, double ch)
Definition: aeval.c:77
FFHashtableContext
Definition: hashtable.c:34
ff_hashtable_freep
void ff_hashtable_freep(struct FFHashtableContext **ctx)
Free a hash table.
Definition: hashtable.c:206
FFHashtableContext::max_entries
size_t max_entries
Definition: hashtable.c:38
FFHashtableContext::key_size
size_t key_size
Definition: hashtable.c:35
ctx
AVFormatContext * ctx
Definition: movenc.c:49
key
const char * key
Definition: hwcontext_opencl.c:189
FFHashtableContext::val_size
size_t val_size
Definition: hashtable.c:36
ff_hashtable_clear
void ff_hashtable_clear(struct FFHashtableContext *ctx)
Delete all values from a hash table.
Definition: hashtable.c:201
ff_hashtable_get
int ff_hashtable_get(const struct FFHashtableContext *ctx, const void *key, void *val)
Look up a value from a hash table given a key.
Definition: hashtable.c:97
ff_hashtable_set
int ff_hashtable_set(struct FFHashtableContext *ctx, const void *key, const void *val)
Store a value in a hash table given a key.
Definition: hashtable.c:119
ff_hashtable_alloc
int ff_hashtable_alloc(struct FFHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries)
Create a fixed-sized Robin Hood hash table.
Definition: hashtable.c:59
ff_hashtable_delete
int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key)
Delete a value from a hash table given a key.
Definition: hashtable.c:163