Lumiera  0.pre.03
»edit your freedom«
psplay.h
Go to the documentation of this file.
1 /*
2  psplay.h - probabilistic splay tree
3 
4  Copyright (C) (contributed to CinelerraCV)
5  2004-2006, Christian Thaeter <chth@gmx.net>
6  Copyright (C)
7  2007, 2008, Christian Thaeter <ct@pipapo.org>
8 
9   **Lumiera** is free software; you can redistribute it and/or modify it
10   under the terms of the GNU General Public License as published by the
11   Free Software Foundation; either version 2 of the License, or (at your
12   option) any later version. See the file COPYING for further details.
13 */
14 
15 
28 #ifndef LIB_PSPLAY_H
29 #define LIB_PSPLAY_H
30 
31 #include <stdint.h>
32 #include <stdio.h>
33 
34 
39 typedef struct psplaynode_struct psplaynode;
40 typedef psplaynode* PSplaynode;
42 {
43  PSplaynode left;
44  PSplaynode right;
45 };
46 
47 #define PSPLAYNODE_INITIALIZER {NULL, NULL}
48 
55 typedef int (*psplay_cmp_fn)(const void* a, const void* b);
56 
57 
65 typedef void (*psplay_delete_fn)(PSplaynode node);
66 
67 
73 typedef const void* (*psplay_key_fn)(const PSplaynode node);
74 
75 
81 typedef struct psplay_struct psplay;
82 typedef psplay* PSplay;
83 
85 {
86  PSplaynode tree; /* the tree */
87  PSplaynode* found_parent; /* maybe direct parent of last found node, used for fast remove */
88  psplay_cmp_fn cmp;
89  psplay_key_fn key;
90  psplay_delete_fn del;
91 
92  size_t elem_cnt;
93  unsigned log2; /* roughly log2 of the elem_cnt*/
94 };
95 
96 
102 static inline size_t
103 psplay_nelements (PSplay self)
104 {
105  return self->elem_cnt;
106 };
107 
108 
117 PSplay
118 psplay_init (PSplay self, psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del);
119 
120 
127 PSplay
128 psplay_destroy (PSplay self);
129 
137 PSplay
139 
140 
146 void
147 psplay_delete (PSplay self);
148 
149 
157 PSplaynode
158 psplaynode_init (PSplaynode self);
159 
160 
170 PSplaynode
171 psplay_insert (PSplay self, PSplaynode node, int splayfactor);
172 
173 
182 PSplaynode
183 psplay_find (PSplay self, const void* key, int splayfactor);
184 
185 
194 PSplaynode
195 psplay_remove (PSplay self, PSplaynode node);
196 
197 
204 PSplaynode
205 psplay_remove_key (PSplay self, void* key);
206 
207 
214 void
215 psplay_delete_node (PSplay self, PSplaynode node);
216 
217 
224 void
225 psplay_delete_key (PSplay self, void* key);
226 
227 
228 enum psplay_order_enum
229  {
230  PSPLAY_PREORDER,
231  PSPLAY_INORDER,
232  PSPLAY_POSTORDER
233  };
234 
256 typedef psplay_delete_fn (*psplay_action_fn)(PSplaynode node, const enum psplay_order_enum which, int level, void* data);
257 
258 extern const psplay_delete_fn PSPLAY_CONT;
259 extern const psplay_delete_fn PSPLAY_STOP;
260 extern const psplay_delete_fn PSPLAY_REMOVE;
261 
272 int
273 psplay_walk (PSplay self, PSplaynode node, psplay_action_fn action, int level, void* data);
274 
275 
276 void
277 psplay_dump (PSplay self, FILE* dest);
278 
279 #endif /*LIB_PSPLAY_H*/
PSplaynode psplay_remove(PSplay self, PSplaynode node)
Remove a node from a splay tree.
Definition: psplay.c:340
Type and handle for a psplay root structure This structure shall be treated opaque, its only defined in the header to allow one to integrate it directly instead referencing it.
Definition: psplay.h:84
PSplaynode psplay_remove_key(PSplay self, void *key)
Remove a node by key from a splay tree.
Definition: psplay.c:391
psplay_delete_fn(* psplay_action_fn)(PSplaynode node, const enum psplay_order_enum which, int level, void *data)
Traverse a splay tree Traversing a tree calls a user supplied action three times An &#39;action&#39; must not...
Definition: psplay.h:256
Type and handle for a psplay tree node This node have to be placed inside users data.
Definition: psplay.h:41
PSplaynode psplay_find(PSplay self, const void *key, int splayfactor)
Find a element in a splay tree.
Definition: psplay.c:300
PSplay psplay_init(PSplay self, psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del)
Initialize a splay tree.
Definition: psplay.c:66
const void *(* psplay_key_fn)(const PSplaynode node)
Retrieve the key from a user datastructure.
Definition: psplay.h:73
PSplaynode psplaynode_init(PSplaynode self)
Initialise a splay tree node The user has to place this nodes within his datastructure and must Initi...
Definition: psplay.c:122
PSplay psplay_new(psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del)
Allocate a splay tree.
Definition: psplay.c:88
int psplay_walk(PSplay self, PSplaynode node, psplay_action_fn action, int level, void *data)
Start a tree traversal.
Definition: psplay.c:442
void psplay_delete(PSplay self)
Delete a splay tree Frees all elements and associated resources of a splay tree and then itseld...
Definition: psplay.c:115
PSplay psplay_destroy(PSplay self)
Destroy a splay tree Frees all elements and associated resources of a splay tree. ...
Definition: psplay.c:101
static size_t psplay_nelements(PSplay self)
Number of elements in tree.
Definition: psplay.h:103
int(* psplay_cmp_fn)(const void *a, const void *b)
Function use to compare keys.
Definition: psplay.h:55
PSplaynode psplay_insert(PSplay self, PSplaynode node, int splayfactor)
Insert a element into a splay tree.
Definition: psplay.c:246
void psplay_delete_node(PSplay self, PSplaynode node)
Delete a node from a splay tree.
Definition: psplay.c:398
void psplay_delete_key(PSplay self, void *key)
Delete a node by key from a splay tree.
Definition: psplay.c:406
void(* psplay_delete_fn)(PSplaynode node)
Destructor for user defined data Called when an element got removed from a splay tree.
Definition: psplay.h:65