yading@11
|
1 /*
|
yading@11
|
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 /**
|
yading@11
|
22 * @file
|
yading@11
|
23 * A tree container.
|
yading@11
|
24 * @author Michael Niedermayer <michaelni@gmx.at>
|
yading@11
|
25 */
|
yading@11
|
26
|
yading@11
|
27 #ifndef AVUTIL_TREE_H
|
yading@11
|
28 #define AVUTIL_TREE_H
|
yading@11
|
29
|
yading@11
|
30 #include "attributes.h"
|
yading@11
|
31 #include "version.h"
|
yading@11
|
32
|
yading@11
|
33 /**
|
yading@11
|
34 * @addtogroup lavu_tree AVTree
|
yading@11
|
35 * @ingroup lavu_data
|
yading@11
|
36 *
|
yading@11
|
37 * Low complexity tree container
|
yading@11
|
38 *
|
yading@11
|
39 * Insertion, removal, finding equal, largest which is smaller than and
|
yading@11
|
40 * smallest which is larger than, all have O(log n) worst case complexity.
|
yading@11
|
41 * @{
|
yading@11
|
42 */
|
yading@11
|
43
|
yading@11
|
44
|
yading@11
|
45 struct AVTreeNode;
|
yading@11
|
46 extern const int av_tree_node_size;
|
yading@11
|
47
|
yading@11
|
48 /**
|
yading@11
|
49 * Allocate an AVTreeNode.
|
yading@11
|
50 */
|
yading@11
|
51 struct AVTreeNode *av_tree_node_alloc(void);
|
yading@11
|
52
|
yading@11
|
53 /**
|
yading@11
|
54 * Find an element.
|
yading@11
|
55 * @param root a pointer to the root node of the tree
|
yading@11
|
56 * @param next If next is not NULL, then next[0] will contain the previous
|
yading@11
|
57 * element and next[1] the next element. If either does not exist,
|
yading@11
|
58 * then the corresponding entry in next is unchanged.
|
yading@11
|
59 * @return An element with cmp(key, elem)==0 or NULL if no such element exists in
|
yading@11
|
60 * the tree.
|
yading@11
|
61 */
|
yading@11
|
62 void *av_tree_find(const struct AVTreeNode *root, void *key, int (*cmp)(void *key, const void *b), void *next[2]);
|
yading@11
|
63
|
yading@11
|
64 /**
|
yading@11
|
65 * Insert or remove an element.
|
yading@11
|
66 *
|
yading@11
|
67 * If *next is NULL, then the supplied element will be removed if it exists.
|
yading@11
|
68 * If *next is non-NULL, then the supplied element will be inserted, unless
|
yading@11
|
69 * it already exists in the tree.
|
yading@11
|
70 *
|
yading@11
|
71 * @param rootp A pointer to a pointer to the root node of the tree; note that
|
yading@11
|
72 * the root node can change during insertions, this is required
|
yading@11
|
73 * to keep the tree balanced.
|
yading@11
|
74 * @param key pointer to the element key to insert in the tree
|
yading@11
|
75 * @param next Used to allocate and free AVTreeNodes. For insertion the user
|
yading@11
|
76 * must set it to an allocated and zeroed object of at least
|
yading@11
|
77 * av_tree_node_size bytes size. av_tree_insert() will set it to
|
yading@11
|
78 * NULL if it has been consumed.
|
yading@11
|
79 * For deleting elements *next is set to NULL by the user and
|
yading@11
|
80 * av_tree_insert() will set it to the AVTreeNode which was
|
yading@11
|
81 * used for the removed element.
|
yading@11
|
82 * This allows the use of flat arrays, which have
|
yading@11
|
83 * lower overhead compared to many malloced elements.
|
yading@11
|
84 * You might want to define a function like:
|
yading@11
|
85 * @code
|
yading@11
|
86 * void *tree_insert(struct AVTreeNode **rootp, void *key, int (*cmp)(void *key, const void *b), AVTreeNode **next){
|
yading@11
|
87 * if(!*next) *next= av_mallocz(av_tree_node_size);
|
yading@11
|
88 * return av_tree_insert(rootp, key, cmp, next);
|
yading@11
|
89 * }
|
yading@11
|
90 * void *tree_remove(struct AVTreeNode **rootp, void *key, int (*cmp)(void *key, const void *b, AVTreeNode **next)){
|
yading@11
|
91 * av_freep(next);
|
yading@11
|
92 * return av_tree_insert(rootp, key, cmp, next);
|
yading@11
|
93 * }
|
yading@11
|
94 * @endcode
|
yading@11
|
95 * @param cmp compare function used to compare elements in the tree
|
yading@11
|
96 * @return If no insertion happened, the found element; if an insertion or
|
yading@11
|
97 * removal happened, then either key or NULL will be returned.
|
yading@11
|
98 * Which one it is depends on the tree state and the implementation. You
|
yading@11
|
99 * should make no assumptions that it's one or the other in the code.
|
yading@11
|
100 */
|
yading@11
|
101 void *av_tree_insert(struct AVTreeNode **rootp, void *key, int (*cmp)(void *key, const void *b), struct AVTreeNode **next);
|
yading@11
|
102 void av_tree_destroy(struct AVTreeNode *t);
|
yading@11
|
103
|
yading@11
|
104 /**
|
yading@11
|
105 * Apply enu(opaque, &elem) to all the elements in the tree in a given range.
|
yading@11
|
106 *
|
yading@11
|
107 * @param cmp a comparison function that returns < 0 for a element below the
|
yading@11
|
108 * range, > 0 for a element above the range and == 0 for a
|
yading@11
|
109 * element inside the range
|
yading@11
|
110 *
|
yading@11
|
111 * @note The cmp function should use the same ordering used to construct the
|
yading@11
|
112 * tree.
|
yading@11
|
113 */
|
yading@11
|
114 void av_tree_enumerate(struct AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, void *elem), int (*enu)(void *opaque, void *elem));
|
yading@11
|
115
|
yading@11
|
116 /**
|
yading@11
|
117 * @}
|
yading@11
|
118 */
|
yading@11
|
119
|
yading@11
|
120 #endif /* AVUTIL_TREE_H */
|