5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2008 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
24 #error "Do not include this header directly"
27 /* Tree operatins header */
28 struct SilcTreeOpsStruct {
29 SilcBool (*add)(SilcTree *tree, void *entry);
30 SilcBool (*del)(SilcTree *tree, void *entry);
31 void *(*find)(SilcTree *tree, void *entry, SilcCompare compare,
35 /* Generic tree header, present in each entry in tree */
36 struct SilcTreeHeaderStruct {
37 struct SilcTreeHeaderStruct *parent; /* Parent of this node */
38 struct SilcTreeHeaderStruct *left; /* Left leaft */
39 struct SilcTreeHeaderStruct *right; /* Right leaft */
40 struct SilcTreeHeaderStruct *dup; /* Duplicates, middle leaf, etc. */
42 unsigned int duplicate : 1;
46 struct SilcTreeStruct {
47 const struct SilcTreeOpsStruct *ops;
52 unsigned int offset : 31;
53 unsigned int duplicates : 1;
56 /* Get tree header from entry */
57 #define SILC_TREE_GET_HEADER(tree, pos) \
58 ((void *)((unsigned char *)(pos) + tree->offset))
60 /* Get entry from tree header */
61 #define SILC_TREE_GET_ENTRY(tree, pos) \
62 ((void *)((unsigned char *)(pos) - tree->offset))
64 /* Low level undocumented macro API for creating three-leaf binary trees. */
66 #define silc_tree_set_root(tree, root) \
67 (tree).root = SILC_TREE_GET_HEADER(&(tree), (root))
68 #define silc_tree_set_parent(tree, at, parent) \
69 (at)->parent = SILC_TREE_GET_HEADER(&(tree), (parent))
70 #define silc_tree_set_left(tree, at, left) \
71 (at)->left = SILC_TREE_GET_HEADER(&(tree), (left))
72 #define silc_tree_set_middle(tree, at, middle) \
73 (at)->dup = SILC_TREE_GET_HEADER(&(tree), (middle))
74 #define silc_tree_set_right(tree, at, right) \
75 (at)->right = SILC_TREE_GET_HEADER(&(tree), (right))
77 #define silc_tree_get_root(tree) \
78 SILC_TREE_GET_ENTRY(&(tree), (tree).root)
79 #define silc_tree_get_parent(tree, at) \
80 SILC_TREE_GET_ENTRY(&(tree), (at)->parent)
81 #define silc_tree_get_left(tree, at) \
82 SILC_TREE_GET_ENTRY(&(tree), (at)->left)
83 #define silc_tree_get_middle(tree, at) \
84 SILC_TREE_GET_ENTRY(&(tree), (at)->middle)
85 #define silc_tree_get_right(tree, at) \
86 SILC_TREE_GET_ENTRY(&(tree), (at)->right)
88 #endif /* SILCTREE_I_H */