2 |
7u83 |
1 |
/*
|
|
|
2 |
* Copyright 2019, The PLANIX Project
|
|
|
3 |
*
|
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
|
5 |
* modification, are permitted provided that the following conditions are
|
|
|
6 |
* met:
|
|
|
7 |
*
|
|
|
8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
9 |
* this list of conditions and the following disclaimer.
|
|
|
10 |
*
|
|
|
11 |
* 2. 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 |
*
|
|
|
15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
16 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
17 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
19 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
21 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
22 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
23 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
24 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
25 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
#ifndef _MAVL_H
|
|
|
29 |
#define _MAVL_H
|
|
|
30 |
|
|
|
31 |
#include <stdlib.h>
|
|
|
32 |
|
7 |
7u83 |
33 |
|
|
|
34 |
|
|
|
35 |
/** Maximum AVL Tree depth.
|
|
|
36 |
The number of nodes is calculated by 2^depth.
|
|
|
37 |
So a value of 32 should be enough for around 4
|
|
|
38 |
billion nodes. This value is only used within
|
|
|
39 |
struct mavliter.
|
|
|
40 |
*/
|
|
|
41 |
#ifndef MAVL_MAX_DEPTH
|
|
|
42 |
#define MAVL_MAX_DEPTH 32
|
|
|
43 |
#endif
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
2 |
7u83 |
48 |
/**
|
|
|
49 |
* Defines the structure of an AVL Node.
|
|
|
50 |
*/
|
|
|
51 |
struct mavlnode {
|
|
|
52 |
struct mavlnode *left; /* Pointer to left son */
|
|
|
53 |
struct mavlnode *right; /* Pointer to right son */
|
|
|
54 |
int bal:3; /* AVL balance factor */
|
|
|
55 |
};
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Definition of an AVL Tree
|
|
|
60 |
*/
|
|
|
61 |
struct mavl {
|
|
|
62 |
struct mavlnode *root; /* Pointer to root node */
|
|
|
63 |
int (*cmp) (const void *, const void *); /* Compare function */
|
|
|
64 |
void (*del) (void *); /* Delete element function */
|
|
|
65 |
int count; /* Number of elements
|
|
|
66 |
* currently stored in the
|
|
|
67 |
* tree */
|
|
|
68 |
void *(*malloc) (size_t n); /* used to allocate a new
|
|
|
69 |
* mavlnode */
|
|
|
70 |
void (*free) (void *ptr); /* used to free a mavlnode */
|
|
|
71 |
|
|
|
72 |
/*
|
|
|
73 |
* size of data appended to each mavlnode element. used to allocate
|
|
|
74 |
* space in mavl_add.
|
|
|
75 |
*/
|
|
|
76 |
size_t data_size;
|
|
|
77 |
};
|
|
|
78 |
|
|
|
79 |
void mavlnode_destroy(struct mavl *t, struct mavlnode *n);
|
|
|
80 |
|
|
|
81 |
#define mavlnode_dataptr(node) \
|
|
|
82 |
((void*)(((char*)((void*)(node))) + sizeof(struct mavlnode)))
|
|
|
83 |
|
|
|
84 |
struct mavl *mavl_create(int (*cmp) (const void *, const void *),
|
|
|
85 |
void (*del) (void *), size_t data_size);
|
|
|
86 |
|
|
|
87 |
void *mavl_insert(struct mavl *t, const void *data, int *exists);
|
5 |
7u83 |
88 |
int mavl_get_depth( struct mavl *t );
|
2 |
7u83 |
89 |
|
|
|
90 |
|
7 |
7u83 |
91 |
|
|
|
92 |
struct mavliter {
|
|
|
93 |
struct mavlnode *stack[MAVL_MAX_DEPTH * 2];
|
|
|
94 |
struct mavlnode *cur;
|
|
|
95 |
int stack_ptr;
|
|
|
96 |
struct mavlnode * root;
|
|
|
97 |
int ( *cmp ) ( const void *, const void * );
|
|
|
98 |
};
|
|
|
99 |
|
|
|
100 |
void * mavliter_get(struct mavliter *i);
|
|
|
101 |
void mavliter_init(struct mavliter * i, struct mavl * t);
|
|
|
102 |
void * mavliter_next ( struct mavliter *i );
|
|
|
103 |
void * mavliter_seek_set ( struct mavliter *i );
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
|
2 |
7u83 |
114 |
#define MAVL_E_NOTFOUND 2
|
|
|
115 |
#define MAVL_E_NOMEM 3
|
|
|
116 |
|
|
|
117 |
#endif
|