source: mainline/kernel/generic/include/adt/avl.h@ f8048d1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f8048d1 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[358ec13]1/*
2 * Copyright (C) 2007 Vojtech Mencl
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericadt
30 * @{
31 */
32/** @file
33 */
34
35#ifndef KERN_AVLTREE_H_
[7a0359b]36#define KERN_AVLTREE_H_
[358ec13]37
[525c5ac]38#include <stdbool.h>
[e29e44bf]39#include <stddef.h>
[83dab11]40#include <stdint.h>
[7a0359b]41#include <trace.h>
[358ec13]42
43/**
44 * Macro for getting a pointer to the structure which contains the avltree
45 * structure.
46 *
47 * @param link Pointer to the avltree structure.
48 * @param type Name of the outer structure.
49 * @param member Name of avltree attribute in the outer structure.
50 */
[d1e9321]51#define avltree_get_instance(node, type, member) \
52 ((type *)(((uint8_t *)(node)) - ((uint8_t *) &(((type *) NULL)->member))))
[358ec13]53
54typedef struct avltree_node avltree_node_t;
55typedef struct avltree avltree_t;
56
[d1e9321]57typedef uint64_t avltree_key_t;
58
[1433ecda]59typedef bool (*avltree_walker_t)(avltree_node_t *, void *);
[358ec13]60
61/** AVL tree node structure. */
[1433ecda]62struct avltree_node {
[1b20da0]63 /**
[358ec13]64 * Pointer to the left descendant of this node.
65 *
66 * All keys of nodes in the left subtree are less than the key of this
67 * node.
68 */
69 struct avltree_node *lft;
[a35b458]70
[1b20da0]71 /**
[358ec13]72 * Pointer to the right descendant of this node.
73 *
74 * All keys of nodes in the right subtree are greater than the key of
75 * this node.
76 */
77 struct avltree_node *rgt;
[a35b458]78
[358ec13]79 /** Pointer to the parent node. Root node has NULL parent. */
80 struct avltree_node *par;
[a35b458]81
[358ec13]82 /** Node's key. */
[1b20da0]83 avltree_key_t key;
[a35b458]84
[358ec13]85 /**
86 * Difference between the heights of the left and the right subtree of
87 * this node.
88 */
89 int8_t balance;
90};
91
92/** AVL tree structure. */
[1433ecda]93struct avltree {
[358ec13]94 /** AVL root node pointer */
95 struct avltree_node *root;
96
[1b20da0]97 /**
[358ec13]98 * Base of the tree is a value that is smaller or equal than every value
99 * in the tree (valid for positive keys otherwise ignore this atribute).
[1b20da0]100 *
[358ec13]101 * The base is added to the current key when a new node is inserted into
102 * the tree. The base is changed to the key of the node which is deleted
103 * with avltree_delete_min().
104 */
[1b20da0]105 avltree_key_t base;
[358ec13]106};
107
108
109/** Create empty AVL tree.
110 *
111 * @param t AVL tree.
112 */
[7a0359b]113NO_TRACE static inline void avltree_create(avltree_t *t)
[358ec13]114{
115 t->root = NULL;
116 t->base = 0;
117}
118
119/** Initialize node.
120 *
121 * @param node Node which is initialized.
122 */
[7a0359b]123NO_TRACE static inline void avltree_node_initialize(avltree_node_t *node)
[358ec13]124{
125 node->key = 0;
126 node->lft = NULL;
127 node->rgt = NULL;
128 node->par = NULL;
129 node->balance = 0;
130}
131
132extern avltree_node_t *avltree_find_min(avltree_t *t);
[d1e9321]133extern avltree_node_t *avltree_search(avltree_t *t, avltree_key_t key);
[358ec13]134extern void avltree_insert(avltree_t *t, avltree_node_t *newnode);
135extern void avltree_delete(avltree_t *t, avltree_node_t *node);
136extern bool avltree_delete_min(avltree_t *t);
[b76a2217]137extern void avltree_walk(avltree_t *t, avltree_walker_t walker, void *arg);
[358ec13]138
139#endif
140
141/** @}
142 */
Note: See TracBrowser for help on using the repository browser.