1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
32 -- Tree_Type is used to implement ordered containers. This package declares
33 -- the tree operations that depend on keys.
35 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
38 with package Tree_Operations
is new Generic_Operations
(<>);
40 use Tree_Operations
.Tree_Types
;
42 type Key_Type
(<>) is limited private;
44 with function Is_Less_Key_Node
46 R
: Node_Access
) return Boolean;
48 with function Is_Greater_Key_Node
50 R
: Node_Access
) return Boolean;
52 package Ada
.Containers
.Red_Black_Trees
.Generic_Keys
is
56 with function New_Node
return Node_Access
;
57 procedure Generic_Insert_Post
58 (Tree
: in out Tree_Type
;
62 -- Completes an insertion after the insertion position has been
63 -- determined. On output Z contains a pointer to the newly inserted
64 -- node, allocated using New_Node. If Tree is busy then
65 -- Program_Error is raised. If Y is null, then Tree must be empty.
66 -- Otherwise Y denotes the insertion position, and Before specifies
67 -- whether the new node is Y's left (True) or right (False) child.
70 with procedure Insert_Post
71 (T
: in out Tree_Type
;
76 procedure Generic_Conditional_Insert
77 (Tree
: in out Tree_Type
;
79 Node
: out Node_Access
;
80 Inserted
: out Boolean);
81 -- Inserts a new node in Tree, but only if the tree does not already
82 -- contain Key. Generic_Conditional_Insert first searches for a key
83 -- equivalent to Key in Tree. If an equivalent key is found, then on
84 -- output Node designates the node with that key and Inserted is
85 -- False; there is no allocation and Tree is not modified. Otherwise
86 -- Node designates a new node allocated using Insert_Post, and
90 with procedure Insert_Post
91 (T
: in out Tree_Type
;
96 procedure Generic_Unconditional_Insert
97 (Tree
: in out Tree_Type
;
99 Node
: out Node_Access
);
100 -- Inserts a new node in Tree. On output Node designates the new
101 -- node, which is allocated using Insert_Post. The node is inserted
102 -- immediately after already-existing equivalent keys.
105 with procedure Insert_Post
106 (T
: in out Tree_Type
;
109 Z
: out Node_Access
);
111 with procedure Unconditional_Insert_Sans_Hint
112 (Tree
: in out Tree_Type
;
114 Node
: out Node_Access
);
116 procedure Generic_Unconditional_Insert_With_Hint
117 (Tree
: in out Tree_Type
;
120 Node
: out Node_Access
);
121 -- Inserts a new node in Tree near position Hint, to avoid having to
122 -- search from the root for the insertion position. If Hint is null
123 -- then Generic_Unconditional_Insert_With_Hint attempts to insert
124 -- the new node after Tree.Last. If Hint is non-null then if Key is
125 -- less than Hint, it attempts to insert the new node immediately
126 -- prior to Hint. Otherwise it attempts to insert the node
127 -- immediately following Hint. We say "attempts" above to emphasize
128 -- that insertions always preserve invariants with respect to key
129 -- order, even when there's a hint. So if Key can't be inserted
130 -- immediately near Hint, then the new node is inserted in the
131 -- normal way, by searching for the correct position starting from
135 with procedure Insert_Post
136 (T
: in out Tree_Type
;
139 Z
: out Node_Access
);
141 with procedure Conditional_Insert_Sans_Hint
142 (Tree
: in out Tree_Type
;
144 Node
: out Node_Access
;
145 Inserted
: out Boolean);
147 procedure Generic_Conditional_Insert_With_Hint
148 (Tree
: in out Tree_Type
;
149 Position
: Node_Access
; -- the hint
151 Node
: out Node_Access
;
152 Inserted
: out Boolean);
153 -- Inserts a new node in Tree if the tree does not already contain
154 -- Key, using Position as a hint about where to insert the new node.
155 -- See Generic_Unconditional_Insert_With_Hint for more details about
160 Key
: Key_Type
) return Node_Access
;
161 -- Searches Tree for the smallest node equivalent to Key
165 Key
: Key_Type
) return Node_Access
;
166 -- Searches Tree for the smallest node equal to or greater than Key
170 Key
: Key_Type
) return Node_Access
;
171 -- Searches Tree for the largest node less than or equal to Key
175 Key
: Key_Type
) return Node_Access
;
176 -- Searches Tree for the smallest node greater than Key
179 with procedure Process
(Node
: Node_Access
);
180 procedure Generic_Iteration
183 -- Calls Process for each node in Tree equivalent to Key, in order
184 -- from earliest in range to latest.
187 with procedure Process
(Node
: Node_Access
);
188 procedure Generic_Reverse_Iteration
191 -- Calls Process for each node in Tree equivalent to Key, but in
192 -- order from largest in range to earliest.
194 end Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;