File: avl.h

package info (click to toggle)
getdp 3.0.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,856 kB
  • sloc: cpp: 63,020; fortran: 13,955; yacc: 9,350; f90: 1,640; lex: 799; makefile: 55; ansic: 34; awk: 33; sh: 23
file content (118 lines) | stat: -rw-r--r-- 3,867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// GetDP - Copyright (C) 1997-2018 P. Dular and C. Geuzaine, University of Liege
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/getdp/getdp/issues

#ifndef _AVL_H_
#define _AVL_H_

/*
 * avl package
 *
 * Copyright (c) 1988-1993, The Regents of the University of California.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the University of California not
 * be used in advertising or publicity pertaining to distribution of 
 * the software without specific, written prior permission.  The University
 * of California makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * THE UNIVERSITY OF CALIFORNIA DISCLAIMS ALL WARRANTIES WITH REGARD TO 
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR
 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

// Modified for Gmsh (C++, 64 bits, ...)

typedef struct avl_node_struct avl_node;
struct avl_node_struct {
  avl_node *left, *right;
  void *key;
  void *value;
  int height;
};


typedef struct avl_tree_struct avl_tree;
struct avl_tree_struct {
  avl_node *root;
  int (*compar)(const void *key1, const void *key2);
  int num_entries;
  int modified;
};


typedef struct avl_generator_struct avl_generator;
struct avl_generator_struct {
  avl_tree *tree;
  avl_node **nodelist;
  int count;
};


#define AVL_FORWARD     0
#define AVL_BACKWARD    1

#define AVL_MOST_LEFT   0
#define AVL_MOST_RIGHT  1

#define avl_is_member(tree, key)   avl_lookup(tree, key, (void **) 0)
#define NIL(type)            (type *) 0

#define avl_foreach_item(table, gen, dir, key_p, value_p)               \
    for(gen = avl_init_gen(table, dir);                                 \
            avl_gen(gen, key_p, value_p) || (avl_free_gen(gen),0);)


inline void avl_walk_forward(avl_node *node, void (*func)(void *key, void *value))
{
  if (node != NIL(avl_node)) {
    avl_walk_forward(node->left, func);
    (*func)(node->key, node->value);
    avl_walk_forward(node->right, func);
  }
}

inline void avl_walk_backward(avl_node *node, void (*func)(void *key, void *value))
{
  if (node != NIL(avl_node)) {
    avl_walk_backward(node->right, func);
    (*func)(node->key, node->value);
    avl_walk_backward(node->left, func);
  }
}

inline void avl_foreach(avl_tree *tree, void (*func)(void *key, void *value), int direction)
{
  if (direction == AVL_FORWARD) {
    avl_walk_forward(tree->root, func);
  } else {
    avl_walk_backward(tree->root, func);
  }
}

avl_tree *avl_init_table(int (*compar)(const void *key1, const void *key2));
int avl_lookup(avl_tree *tree, void *key, void **value_p);
int avl_insert(avl_tree *tree, void *key, void *value);
int avl_delete(avl_tree *tree, void **key_p, void **value_p);
void avl_free_table(avl_tree *tree, void (*key_free)(void *key), void (*value_free)(void *value));
int avl_count(avl_tree *tree);
int avl_check_tree(avl_tree *tree);
int avl_extremum(avl_tree *tree, int side, void **value_p);

avl_generator *avl_init_gen(avl_tree *tree, int dir);
int avl_gen(avl_generator *gen, void **key_p, void **value_p);
void avl_free_gen(avl_generator *gen);

int avl_numcmp(const void *x, const void*y);

#endif