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
23  * A tree container.
24  * @author Michael Niedermayer <michaelni@gmx.at>
25  */
26 
27 module ffmpeg.libavutil.tree;
28 
29 extern (C):
30 import ffmpeg; @nogc nothrow:
31 
32 /**
33  * @addtogroup lavu_tree AVTree
34  * @ingroup lavu_data
35  *
36  * Low-complexity tree container
37  *
38  * Insertion, removal, finding equal, largest which is smaller than and
39  * smallest which is larger than, all have O(log n) worst-case complexity.
40  * @{
41  */
42 
43 struct AVTreeNode;
44 extern __gshared const int av_tree_node_size;
45 
46 /**
47  * Allocate an AVTreeNode.
48  */
49 AVTreeNode* av_tree_node_alloc ();
50 
51 /**
52  * Find an element.
53  * @param root a pointer to the root node of the tree
54  * @param next If next is not NULL, then next[0] will contain the previous
55  *             element and next[1] the next element. If either does not exist,
56  *             then the corresponding entry in next is unchanged.
57  * @param cmp compare function used to compare elements in the tree,
58  *            API identical to that of Standard C's qsort
59  *            It is guaranteed that the first and only the first argument to cmp()
60  *            will be the key parameter to av_tree_find(), thus it could if the
61  *            user wants, be a different type (like an opaque context).
62  * @return An element with cmp(key, elem) == 0 or NULL if no such element
63  *         exists in the tree.
64  */
65 void* av_tree_find (
66     const(AVTreeNode)* root,
67     void* key,
68     int function (const(void)* key, const(void)* b) cmp,
69     ref void*[2] next);
70 
71 /**
72  * Insert or remove an element.
73  *
74  * If *next is NULL, then the supplied element will be removed if it exists.
75  * If *next is non-NULL, then the supplied element will be inserted, unless
76  * it already exists in the tree.
77  *
78  * @param rootp A pointer to a pointer to the root node of the tree; note that
79  *              the root node can change during insertions, this is required
80  *              to keep the tree balanced.
81  * @param key  pointer to the element key to insert in the tree
82  * @param next Used to allocate and free AVTreeNodes. For insertion the user
83  *             must set it to an allocated and zeroed object of at least
84  *             av_tree_node_size bytes size. av_tree_insert() will set it to
85  *             NULL if it has been consumed.
86  *             For deleting elements *next is set to NULL by the user and
87  *             av_tree_insert() will set it to the AVTreeNode which was
88  *             used for the removed element.
89  *             This allows the use of flat arrays, which have
90  *             lower overhead compared to many malloced elements.
91  *             You might want to define a function like:
92  *             @code
93  *             void *tree_insert(struct AVTreeNode **rootp, void *key,
94  *                               int (*cmp)(void *key, const void *b),
95  *                               AVTreeNode **next)
96  *             {
97  *                 if (!*next)
98  *                     *next = av_mallocz(av_tree_node_size);
99  *                 return av_tree_insert(rootp, key, cmp, next);
100  *             }
101  *             void *tree_remove(struct AVTreeNode **rootp, void *key,
102  *                               int (*cmp)(void *key, const void *b, AVTreeNode **next))
103  *             {
104  *                 av_freep(next);
105  *                 return av_tree_insert(rootp, key, cmp, next);
106  *             }
107  *             @endcode
108  * @param cmp compare function used to compare elements in the tree, API identical
109  *            to that of Standard C's qsort
110  * @return If no insertion happened, the found element; if an insertion or
111  *         removal happened, then either key or NULL will be returned.
112  *         Which one it is depends on the tree state and the implementation. You
113  *         should make no assumptions that it's one or the other in the code.
114  */
115 void* av_tree_insert (
116     AVTreeNode** rootp,
117     void* key,
118     int function (const(void)* key, const(void)* b) cmp,
119     AVTreeNode** next);
120 
121 void av_tree_destroy (AVTreeNode* t);
122 
123 /**
124  * Apply enu(opaque, &elem) to all the elements in the tree in a given range.
125  *
126  * @param cmp a comparison function that returns < 0 for an element below the
127  *            range, > 0 for an element above the range and == 0 for an
128  *            element inside the range
129  *
130  * @note The cmp function should use the same ordering used to construct the
131  *       tree.
132  */
133 void av_tree_enumerate (
134     AVTreeNode* t,
135     void* opaque,
136     int function (void* opaque, void* elem) cmp,
137     int function (void* opaque, void* elem) enu);
138 
139 /**
140  * @}
141  */
142 
143 /* AVUTIL_TREE_H */