splay-tree.h 6.09 KB
Newer Older
1
/* A splay-tree datatype.  
2
   Copyright (C) 1998-2019 Free Software Foundation, Inc.
3 4
   Contributed by Mark Mitchell (mark@markmitchell.com).

5
   This file is part of GCC.
6
   
7 8 9 10
   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.
11

12 13 14 15
   GCC is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
16

17 18 19 20
   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING.  If not, write to
   the Free Software Foundation, 51 Franklin Street - Fifth Floor,
   Boston, MA 02110-1301, USA.  */
21 22

/* For an easily readable description of splay-trees, see:
23 24 25 26 27 28 29 30 31 32 33 34 35 36

     Lewis, Harry R. and Denenberg, Larry.  Data Structures and Their
     Algorithms.  Harper-Collins, Inc.  1991.  

   The major feature of splay trees is that all basic tree operations
   are amortized O(log n) time for a tree with n nodes.  */

#ifndef _SPLAY_TREE_H
#define _SPLAY_TREE_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

37
#include "ansidecl.h"
38

39 40
#ifdef HAVE_STDINT_H
#include <stdint.h>
41
#endif
42 43
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
44 45
#endif

46 47 48 49
/* Use typedefs for the key and data types to facilitate changing
   these types, if necessary.  These types should be sufficiently wide
   that any pointer or scalar can be cast to these types, and then
   cast back, without loss of precision.  */
50 51
typedef uintptr_t splay_tree_key;
typedef uintptr_t splay_tree_value;
52 53

/* Forward declaration for a node in the tree.  */
54
typedef struct splay_tree_node_s *splay_tree_node;
55 56 57

/* The type of a function which compares two splay-tree keys.  The
   function should return values as for qsort.  */
58
typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key);
59 60

/* The type of a function used to deallocate any resources associated
61 62 63 64 65 66
   with the key.  If you provide this function, the splay tree
   will take the ownership of the memory of the splay_tree_key arg
   of splay_tree_insert.  This function is called to release the keys
   present in the tree when calling splay_tree_delete or splay_tree_remove.
   If splay_tree_insert is called with a key equal to a key already
   present in the tree, the old key and old value will be released.  */
67
typedef void (*splay_tree_delete_key_fn) (splay_tree_key);
68 69

/* The type of a function used to deallocate any resources associated
70 71 72
   with the value.  If you provide this function, the memory of the
   splay_tree_value arg of splay_tree_insert is managed similarly to
   the splay_tree_key memory: see splay_tree_delete_key_fn.  */
73
typedef void (*splay_tree_delete_value_fn) (splay_tree_value);
74 75

/* The type of a function used to iterate over the tree.  */
76
typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*);
77

78 79 80 81
/* The type of a function used to allocate memory for tree root and
   node structures.  The first argument is the number of bytes needed;
   the second is a data pointer the splay tree functions pass through
   to the allocator.  This function must never return zero.  */
82
typedef void *(*splay_tree_allocate_fn) (int, void *);
83 84 85 86 87

/* The type of a function used to free memory allocated using the
   corresponding splay_tree_allocate_fn.  The first argument is the
   memory to be freed; the latter is a data pointer the splay tree
   functions pass through to the freer.  */
88
typedef void (*splay_tree_deallocate_fn) (void *, void *);
89

90
/* The nodes in the splay tree.  */
91
struct splay_tree_node_s {
92
  /* The key.  */
93
  splay_tree_key key;
94 95

  /* The value.  */
96
  splay_tree_value value;
97 98

  /* The left and right children, respectively.  */
99 100
  splay_tree_node left;
  splay_tree_node right;
101 102 103
};

/* The splay tree itself.  */
104
struct splay_tree_s {
105
  /* The root of the tree.  */
106
  splay_tree_node root;
107 108 109 110 111 112 113 114 115

  /* The comparision function.  */
  splay_tree_compare_fn comp;

  /* The deallocate-key function.  NULL if no cleanup is necessary.  */
  splay_tree_delete_key_fn delete_key;

  /* The deallocate-value function.  NULL if no cleanup is necessary.  */
  splay_tree_delete_value_fn delete_value;
116

117
  /* Node allocate function.  Takes allocate_data as a parameter. */
118
  splay_tree_allocate_fn allocate;
119 120

  /* Free function for nodes and trees.  Takes allocate_data as a parameter.  */
121
  splay_tree_deallocate_fn deallocate;
122 123

  /* Parameter for allocate/free functions.  */
124
  void *allocate_data;
125
};
126

127
typedef struct splay_tree_s *splay_tree;
128

129 130 131
extern splay_tree splay_tree_new (splay_tree_compare_fn,
				  splay_tree_delete_key_fn,
				  splay_tree_delete_value_fn);
132
extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn,
133 134 135 136 137
						 splay_tree_delete_key_fn,
						 splay_tree_delete_value_fn,
						 splay_tree_allocate_fn,
						 splay_tree_deallocate_fn,
						 void *);
138 139 140 141 142 143 144
extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn,
					      splay_tree_delete_key_fn,
					      splay_tree_delete_value_fn,
					      splay_tree_allocate_fn,
					      splay_tree_allocate_fn,
					      splay_tree_deallocate_fn,
					      void *);
145
extern void splay_tree_delete (splay_tree);
146
extern splay_tree_node splay_tree_insert (splay_tree,
147 148
					  splay_tree_key,
					  splay_tree_value);
149 150 151 152 153 154 155 156
extern void splay_tree_remove	(splay_tree, splay_tree_key);
extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key);
extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key);
extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key);
extern splay_tree_node splay_tree_max (splay_tree);
extern splay_tree_node splay_tree_min (splay_tree);
extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*);
extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key);
157 158 159
extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key);
extern int splay_tree_compare_strings (splay_tree_key, splay_tree_key);
extern void splay_tree_delete_pointers (splay_tree_value);
160

161 162 163 164 165
#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _SPLAY_TREE_H */