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)
5  2004, 2005, 2006, Christian Thaeter <chth@gmx.net>
6  Copyright (C) Lumiera.org
7  2007, 2008, Christian Thaeter <ct@pipapo.org>
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of
12  the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 
24 
37 #ifndef LIB_PSPLAY_H
38 #define LIB_PSPLAY_H
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 
43 
48 typedef struct psplaynode_struct psplaynode;
49 typedef psplaynode* PSplaynode;
51 {
52  PSplaynode left;
53  PSplaynode right;
54 };
55 
56 #define PSPLAYNODE_INITIALIZER {NULL, NULL}
57 
64 typedef int (*psplay_cmp_fn)(const void* a, const void* b);
65 
66 
74 typedef void (*psplay_delete_fn)(PSplaynode node);
75 
76 
82 typedef const void* (*psplay_key_fn)(const PSplaynode node);
83 
84 
90 typedef struct psplay_struct psplay;
91 typedef psplay* PSplay;
92 
94 {
95  PSplaynode tree; /* the tree */
96  PSplaynode* found_parent; /* maybe direct parent of last found node, used for fast remove */
97  psplay_cmp_fn cmp;
98  psplay_key_fn key;
99  psplay_delete_fn del;
100 
101  size_t elem_cnt;
102  unsigned log2; /* roughly log2 of the elem_cnt*/
103 };
104 
105 
111 static inline size_t
112 psplay_nelements (PSplay self)
113 {
114  return self->elem_cnt;
115 };
116 
117 
126 PSplay
127 psplay_init (PSplay self, psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del);
128 
129 
136 PSplay
137 psplay_destroy (PSplay self);
138 
146 PSplay
148 
149 
155 void
156 psplay_delete (PSplay self);
157 
158 
166 PSplaynode
167 psplaynode_init (PSplaynode self);
168 
169 
179 PSplaynode
180 psplay_insert (PSplay self, PSplaynode node, int splayfactor);
181 
182 
191 PSplaynode
192 psplay_find (PSplay self, const void* key, int splayfactor);
193 
194 
203 PSplaynode
204 psplay_remove (PSplay self, PSplaynode node);
205 
206 
213 PSplaynode
214 psplay_remove_key (PSplay self, void* key);
215 
216 
223 void
224 psplay_delete_node (PSplay self, PSplaynode node);
225 
226 
233 void
234 psplay_delete_key (PSplay self, void* key);
235 
236 
237 enum psplay_order_enum
238  {
239  PSPLAY_PREORDER,
240  PSPLAY_INORDER,
241  PSPLAY_POSTORDER
242  };
243 
265 typedef psplay_delete_fn (*psplay_action_fn)(PSplaynode node, const enum psplay_order_enum which, int level, void* data);
266 
267 extern const psplay_delete_fn PSPLAY_CONT;
268 extern const psplay_delete_fn PSPLAY_STOP;
269 extern const psplay_delete_fn PSPLAY_REMOVE;
270 
281 int
282 psplay_walk (PSplay self, PSplaynode node, psplay_action_fn action, int level, void* data);
283 
284 
285 void
286 psplay_dump (PSplay self, FILE* dest);
287 
288 #endif /*LIB_PSPLAY_H*/
PSplaynode psplay_remove(PSplay self, PSplaynode node)
Remove a node from a splay tree.
Definition: psplay.c:349
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:93
PSplaynode psplay_remove_key(PSplay self, void *key)
Remove a node by key from a splay tree.
Definition: psplay.c:400
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:265
Type and handle for a psplay tree node This node have to be placed inside users data.
Definition: psplay.h:50
PSplaynode psplay_find(PSplay self, const void *key, int splayfactor)
Find a element in a splay tree.
Definition: psplay.c:309
PSplay psplay_init(PSplay self, psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del)
Initialize a splay tree.
Definition: psplay.c:75
const void *(* psplay_key_fn)(const PSplaynode node)
Retrieve the key from a user datastructure.
Definition: psplay.h:82
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:131
PSplay psplay_new(psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del)
Allocate a splay tree.
Definition: psplay.c:97
int psplay_walk(PSplay self, PSplaynode node, psplay_action_fn action, int level, void *data)
Start a tree traversal.
Definition: psplay.c:451
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:124
PSplay psplay_destroy(PSplay self)
Destroy a splay tree Frees all elements and associated resources of a splay tree. ...
Definition: psplay.c:110
static size_t psplay_nelements(PSplay self)
Number of elements in tree.
Definition: psplay.h:112
int(* psplay_cmp_fn)(const void *a, const void *b)
Function use to compare keys.
Definition: psplay.h:64
PSplaynode psplay_insert(PSplay self, PSplaynode node, int splayfactor)
Insert a element into a splay tree.
Definition: psplay.c:255
void psplay_delete_node(PSplay self, PSplaynode node)
Delete a node from a splay tree.
Definition: psplay.c:407
void psplay_delete_key(PSplay self, void *key)
Delete a node by key from a splay tree.
Definition: psplay.c:415
void(* psplay_delete_fn)(PSplaynode node)
Destructor for user defined data Called when an element got removed from a splay tree.
Definition: psplay.h:74