]> git.sesse.net Git - ffmpeg/blob - libavutil/tree.h
9450d96653c2f9c625470884ce50906fb02db40d
[ffmpeg] / libavutil / tree.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file tree.h
23  * A tree container.
24  * @author Michael Niedermayer <michaelni@gmx.at>
25  */
26
27 #ifndef FFMPEG_TREE_H
28 #define FFMPEG_TREE_H
29
30 struct AVTreeNode;
31 extern const int av_tree_node_size;
32
33 /**
34  * Finds an element.
35  * @param root a pointer to the root node of the tree
36  * @param next If next is not NULL then next[0] will contain the previous
37  *             element and next[1] the next element if either does not exist
38  *             then the corresponding entry in next is unchanged.
39  * @return An element with cmp(key, elem)==0 or NULL if no such element exists in
40  *         the tree.
41  */
42 void *av_tree_find(const struct AVTreeNode *root, void *key, int (*cmp)(void *key, const void *b), void *next[2]);
43
44 /**
45  * Inserts or removes an element.
46  * If *next is NULL then the element supplied will be removed, if no such
47  * element exists behavior is undefined.
48  * If *next is not NULL then the element supplied will be inserted, unless
49  * it already exists in the tree.
50  * @param rootp A pointer to a pointer to the root node of the tree. Note that
51  *              the root node can change during insertions, this is required
52  *              to keep the tree balanced.
53  * @param next Used to allocate and free AVTreeNodes. For insertion the user
54  *             must set it to an allocated and zeroed object of at least
55  *             av_tree_node_size bytes size. av_tree_insert() will set it to
56  *             NULL if it has been consumed.
57  *             For deleting elements *next is set to NULL by the user and
58  *             av_tree_node_size() will set it to the AVTreeNode which was
59  *             used for the removed element.
60  *             This allows the use of flat arrays, which have
61  *             lower overhead compared to many malloced elements.
62  *             You might want to define a function like:
63  *             void *tree_insert(struct AVTreeNode **rootp, void *key, int (*cmp)(void *key, const void *b), AVTreeNode **next){
64  *                 if(!*next) *next= av_mallocz(av_tree_node_size);
65  *                 return av_tree_insert(rootp, key, cmp, next);
66  *             }
67  *             void *tree_remove(struct AVTreeNode **rootp, void *key, int (*cmp)(void *key, const void *b, AVTreeNode **next)){
68  *                 if(*next) av_freep(next);
69  *                 return av_tree_insert(rootp, key, cmp, next);
70  *             }
71  *
72  * @return If no insertion happened, the found element.
73  *         If an insertion or removial happened, then either key or NULL will be returned.
74  *         Which one it is depends on the tree state and the implementation. You
75  *         should make no assumptions that it's one or the other in the code.
76  */
77 void *av_tree_insert(struct AVTreeNode **rootp, void *key, int (*cmp)(void *key, const void *b), struct AVTreeNode **next);
78 void av_tree_destroy(struct AVTreeNode *t);
79
80 #endif /* FFMPEG_TREE_H */