| 1 |
/* Copyright 2022 Jeffrey Kegler */ |
| 2 |
|
| 3 |
/* This is a modification of avl.h, from Ben Pfaff's libavl, |
| 4 |
which was under the LGPL 3. Here is the copyright notice |
| 5 |
from that file: |
| 6 |
|
| 7 |
libavl - library for manipulation of binary trees. |
| 8 |
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software |
| 9 |
Foundation, Inc. |
| 10 |
|
| 11 |
This library is free software; you can redistribute it and/or |
| 12 |
modify it under the terms of the GNU Lesser General Public |
| 13 |
License as published by the Free Software Foundation; either |
| 14 |
version 3 of the License, or (at your option) any later version. |
| 15 |
|
| 16 |
This library is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
Lesser General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU Lesser General Public |
| 22 |
License along with this library; if not, write to the Free Software |
| 23 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 24 |
02110-1301 USA. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef _MARPA_AVL_H__ |
| 28 |
#define _MARPA_AVL_H__ 1 |
| 29 |
|
| 30 |
#include <stddef.h> |
| 31 |
|
| 32 |
/* Function types. */ |
| 33 |
typedef int marpa_avl_comparison_func (const void *avl_a, const void *avl_b, |
| 34 |
void *avl_param); |
| 35 |
typedef void marpa_avl_item_func (void *avl_item, void *avl_param); |
| 36 |
typedef void *marpa_avl_copy_func (void *avl_item, void *avl_param); |
| 37 |
|
| 38 |
/* An AVL tree node. */ |
| 39 |
struct marpa_avl_node |
| 40 |
{ |
| 41 |
struct marpa_avl_node *avl_link[2]; /* Subtrees. */ |
| 42 |
void *avl_data; /* Pointer to data. */ |
| 43 |
signed char avl_balance; /* Balance factor. */ |
| 44 |
}; |
| 45 |
|
| 46 |
/* Tree data structure. */ |
| 47 |
struct marpa_avl_table |
| 48 |
{ |
| 49 |
struct marpa_avl_node *avl_root; /* Tree's root. */ |
| 50 |
marpa_avl_comparison_func *avl_compare; /* Comparison function. */ |
| 51 |
void *avl_param; /* Extra argument to |avl_compare|. */ |
| 52 |
struct marpa_obstack *avl_obstack; |
| 53 |
size_t avl_count; /* Number of items in tree. */ |
| 54 |
unsigned long avl_generation; /* Generation number. */ |
| 55 |
}; |
| 56 |
typedef struct marpa_avl_table* MARPA_AVL_TREE; |
| 57 |
|
| 58 |
/* Maximum AVL tree height. */ |
| 59 |
#ifndef MARPA_AVL_MAX_HEIGHT |
| 60 |
#define MARPA_AVL_MAX_HEIGHT 92 |
| 61 |
#endif |
| 62 |
|
| 63 |
/* AVL traverser structure. */ |
| 64 |
struct marpa_avl_traverser |
| 65 |
{ |
| 66 |
MARPA_AVL_TREE avl_table; /* Tree being traversed. */ |
| 67 |
struct marpa_avl_node *avl_node; /* Current node in tree. */ |
| 68 |
struct marpa_avl_node *avl_stack[MARPA_AVL_MAX_HEIGHT]; |
| 69 |
/* All the nodes above |avl_node|. */ |
| 70 |
size_t avl_height; /* Number of nodes in |avl_parent|. */ |
| 71 |
unsigned long avl_generation; /* Generation number. */ |
| 72 |
}; |
| 73 |
typedef struct marpa_avl_traverser* MARPA_AVL_TRAV; |
| 74 |
|
| 75 |
#define MARPA_TREE_OF_AVL_TRAV(trav) ((trav)->avl_table) |
| 76 |
#define MARPA_DATA_OF_AVL_TRAV(trav) ((trav)->avl_node ? (trav)->avl_node->avl_data : NULL) |
| 77 |
#define MARPA_AVL_OBSTACK(table) ((table)->avl_obstack) |
| 78 |
|
| 79 |
/* Table functions. */ |
| 80 |
MARPA_AVL_TREE _marpa_avl_create (marpa_avl_comparison_func *, void *); |
| 81 |
MARPA_AVL_TREE _marpa_avl_copy (const MARPA_AVL_TREE , marpa_avl_copy_func *, |
| 82 |
marpa_avl_item_func *, int alignment); |
| 83 |
void _marpa_avl_destroy (MARPA_AVL_TREE ); |
| 84 |
void **_marpa_avl_probe (MARPA_AVL_TREE , void *); |
| 85 |
void *_marpa_avl_insert (MARPA_AVL_TREE , void *); |
| 86 |
void *_marpa_avl_replace (MARPA_AVL_TREE , void *); |
| 87 |
void *_marpa_avl_find (const MARPA_AVL_TREE , const void *); |
| 88 |
void *_marpa_avl_at_or_after (const MARPA_AVL_TREE , const void *); |
| 89 |
|
| 90 |
#define marpa_avl_count(table) ((size_t) (table)->avl_count) |
| 91 |
|
| 92 |
/* Table traverser functions. */ |
| 93 |
MARPA_AVL_TRAV _marpa_avl_t_init (MARPA_AVL_TREE ); |
| 94 |
MARPA_AVL_TRAV _marpa_avl_t_reset (MARPA_AVL_TRAV ); |
| 95 |
void *_marpa_avl_t_first (MARPA_AVL_TRAV ); |
| 96 |
void *_marpa_avl_t_last ( MARPA_AVL_TRAV ); |
| 97 |
void *_marpa_avl_t_find ( MARPA_AVL_TRAV , void *); |
| 98 |
void *_marpa_avl_t_copy (struct marpa_avl_traverser *, const struct marpa_avl_traverser *); |
| 99 |
void *_marpa_avl_t_next (MARPA_AVL_TRAV); |
| 100 |
void *_marpa_avl_t_prev (MARPA_AVL_TRAV); |
| 101 |
void *_marpa_avl_t_cur (MARPA_AVL_TRAV); |
| 102 |
void *_marpa_avl_t_insert (MARPA_AVL_TRAV, void *); |
| 103 |
void *_marpa_avl_t_replace (MARPA_AVL_TRAV, void *); |
| 104 |
void *_marpa_avl_t_at_or_after (MARPA_AVL_TRAV, void*); |
| 105 |
|
| 106 |
#endif /* marpa_avl.h */ |