Subversion Repositories HelenOS

Rev

Rev 2501 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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