OSDN Git Service

2009-08-17 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-crbtgk.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_KEYS               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2009, Free Software Foundation, Inc.         --
10 --                                                                          --
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 3,  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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- This unit was originally developed by Matthew J Heaney.                  --
28 ------------------------------------------------------------------------------
29
30 package body Ada.Containers.Red_Black_Trees.Generic_Keys is
31
32    package Ops renames Tree_Operations;
33
34    -------------
35    -- Ceiling --
36    -------------
37
38    --  AKA Lower_Bound
39
40    function Ceiling (Tree : Tree_Type; Key : Key_Type) return Node_Access is
41       Y : Node_Access;
42       X : Node_Access;
43
44    begin
45       X := Tree.Root;
46       while X /= null loop
47          if Is_Greater_Key_Node (Key, X) then
48             X := Ops.Right (X);
49          else
50             Y := X;
51             X := Ops.Left (X);
52          end if;
53       end loop;
54
55       return Y;
56    end Ceiling;
57
58    ----------
59    -- Find --
60    ----------
61
62    function Find (Tree : Tree_Type; Key  : Key_Type) return Node_Access is
63       Y : Node_Access;
64       X : Node_Access;
65
66    begin
67       X := Tree.Root;
68       while X /= null loop
69          if Is_Greater_Key_Node (Key, X) then
70             X := Ops.Right (X);
71          else
72             Y := X;
73             X := Ops.Left (X);
74          end if;
75       end loop;
76
77       if Y = null then
78          return null;
79       end if;
80
81       if Is_Less_Key_Node (Key, Y) then
82          return null;
83       end if;
84
85       return Y;
86    end Find;
87
88    -----------
89    -- Floor --
90    -----------
91
92    function Floor (Tree : Tree_Type; Key  : Key_Type) return Node_Access is
93       Y : Node_Access;
94       X : Node_Access;
95
96    begin
97       X := Tree.Root;
98       while X /= null loop
99          if Is_Less_Key_Node (Key, X) then
100             X := Ops.Left (X);
101          else
102             Y := X;
103             X := Ops.Right (X);
104          end if;
105       end loop;
106
107       return Y;
108    end Floor;
109
110    --------------------------------
111    -- Generic_Conditional_Insert --
112    --------------------------------
113
114    procedure Generic_Conditional_Insert
115      (Tree     : in out Tree_Type;
116       Key      : Key_Type;
117       Node     : out Node_Access;
118       Inserted : out Boolean)
119    is
120       Y : Node_Access := null;
121       X : Node_Access := Tree.Root;
122
123    begin
124       Inserted := True;
125       while X /= null loop
126          Y := X;
127          Inserted := Is_Less_Key_Node (Key, X);
128
129          if Inserted then
130             X := Ops.Left (X);
131          else
132             X := Ops.Right (X);
133          end if;
134       end loop;
135
136       --  If Inserted is True, then this means either that Tree is
137       --  empty, or there was a least one node (strictly) greater than
138       --  Key. Otherwise, it means that Key is equal to or greater than
139       --  every node.
140
141       if Inserted then
142          if Y = Tree.First then
143             Insert_Post (Tree, Y, True, Node);
144             return;
145          end if;
146
147          Node := Ops.Previous (Y);
148
149       else
150          Node := Y;
151       end if;
152
153       --  Here Node has a value that is less than or equal to Key. We
154       --  now have to resolve whether Key is equal to or greater than
155       --  Node, which determines whether the insertion succeeds.
156
157       if Is_Greater_Key_Node (Key, Node) then
158          Insert_Post (Tree, Y, Inserted, Node);
159          Inserted := True;
160          return;
161       end if;
162
163       Inserted := False;
164    end Generic_Conditional_Insert;
165
166    ------------------------------------------
167    -- Generic_Conditional_Insert_With_Hint --
168    ------------------------------------------
169
170    procedure Generic_Conditional_Insert_With_Hint
171      (Tree      : in out Tree_Type;
172       Position  : Node_Access;
173       Key       : Key_Type;
174       Node      : out Node_Access;
175       Inserted  : out Boolean)
176    is
177    begin
178       --  The purpose of a hint is to avoid a search from the root of
179       --  tree. If we have it hint it means we only need to traverse the
180       --  subtree rooted at the hint to find the nearest neighbor. Note
181       --  that finding the neighbor means merely walking the tree; this
182       --  is not a search and the only comparisons that occur are with
183       --  the hint and its neighbor.
184
185       --  If Position is null, this is interpreted to mean that Key is
186       --  large relative to the nodes in the tree. If the tree is empty,
187       --  or Key is greater than the last node in the tree, then we're
188       --  done; otherwise the hint was "wrong" and we must search.
189
190       if Position = null then  -- largest
191          if Tree.Last = null
192            or else Is_Greater_Key_Node (Key, Tree.Last)
193          then
194             Insert_Post (Tree, Tree.Last, False, Node);
195             Inserted := True;
196          else
197             Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
198          end if;
199
200          return;
201       end if;
202
203       pragma Assert (Tree.Length > 0);
204
205       --  A hint can either name the node that immediately follows Key,
206       --  or immediately precedes Key. We first test whether Key is
207       --  less than the hint, and if so we compare Key to the node that
208       --  precedes the hint. If Key is both less than the hint and
209       --  greater than the hint's preceding neighbor, then we're done;
210       --  otherwise we must search.
211
212       --  Note also that a hint can either be an anterior node or a leaf
213       --  node. A new node is always inserted at the bottom of the tree
214       --  (at least prior to rebalancing), becoming the new left or
215       --  right child of leaf node (which prior to the insertion must
216       --  necessarily be null, since this is a leaf). If the hint names
217       --  an anterior node then its neighbor must be a leaf, and so
218       --  (here) we insert after the neighbor. If the hint names a leaf
219       --  then its neighbor must be anterior and so we insert before the
220       --  hint.
221
222       if Is_Less_Key_Node (Key, Position) then
223          declare
224             Before : constant Node_Access := Ops.Previous (Position);
225
226          begin
227             if Before = null then
228                Insert_Post (Tree, Tree.First, True, Node);
229                Inserted := True;
230
231             elsif Is_Greater_Key_Node (Key, Before) then
232                if Ops.Right (Before) = null then
233                   Insert_Post (Tree, Before, False, Node);
234                else
235                   Insert_Post (Tree, Position, True, Node);
236                end if;
237
238                Inserted := True;
239
240             else
241                Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
242             end if;
243          end;
244
245          return;
246       end if;
247
248       --  We know that Key isn't less than the hint so we try again,
249       --  this time to see if it's greater than the hint. If so we
250       --  compare Key to the node that follows the hint. If Key is both
251       --  greater than the hint and less than the hint's next neighbor,
252       --  then we're done; otherwise we must search.
253
254       if Is_Greater_Key_Node (Key, Position) then
255          declare
256             After : constant Node_Access := Ops.Next (Position);
257
258          begin
259             if After = null then
260                Insert_Post (Tree, Tree.Last, False, Node);
261                Inserted := True;
262
263             elsif Is_Less_Key_Node (Key, After) then
264                if Ops.Right (Position) = null then
265                   Insert_Post (Tree, Position, False, Node);
266                else
267                   Insert_Post (Tree, After, True, Node);
268                end if;
269
270                Inserted := True;
271
272             else
273                Conditional_Insert_Sans_Hint (Tree, Key, Node, Inserted);
274             end if;
275          end;
276
277          return;
278       end if;
279
280       --  We know that Key is neither less than the hint nor greater
281       --  than the hint, and that's the definition of equivalence.
282       --  There's nothing else we need to do, since a search would just
283       --  reach the same conclusion.
284
285       Node := Position;
286       Inserted := False;
287    end Generic_Conditional_Insert_With_Hint;
288
289    -------------------------
290    -- Generic_Insert_Post --
291    -------------------------
292
293    procedure Generic_Insert_Post
294      (Tree   : in out Tree_Type;
295       Y      : Node_Access;
296       Before : Boolean;
297       Z      : out Node_Access)
298    is
299    begin
300       if Tree.Length = Count_Type'Last then
301          raise Constraint_Error with "too many elements";
302       end if;
303
304       if Tree.Busy > 0 then
305          raise Program_Error with
306            "attempt to tamper with cursors (container is busy)";
307       end if;
308
309       Z := New_Node;
310       pragma Assert (Z /= null);
311       pragma Assert (Ops.Color (Z) = Red);
312
313       if Y = null then
314          pragma Assert (Tree.Length = 0);
315          pragma Assert (Tree.Root = null);
316          pragma Assert (Tree.First = null);
317          pragma Assert (Tree.Last = null);
318
319          Tree.Root := Z;
320          Tree.First := Z;
321          Tree.Last := Z;
322
323       elsif Before then
324          pragma Assert (Ops.Left (Y) = null);
325
326          Ops.Set_Left (Y, Z);
327
328          if Y = Tree.First then
329             Tree.First := Z;
330          end if;
331
332       else
333          pragma Assert (Ops.Right (Y) = null);
334
335          Ops.Set_Right (Y, Z);
336
337          if Y = Tree.Last then
338             Tree.Last := Z;
339          end if;
340       end if;
341
342       Ops.Set_Parent (Z, Y);
343       Ops.Rebalance_For_Insert (Tree, Z);
344       Tree.Length := Tree.Length + 1;
345    end Generic_Insert_Post;
346
347    -----------------------
348    -- Generic_Iteration --
349    -----------------------
350
351    procedure Generic_Iteration
352      (Tree : Tree_Type;
353       Key  : Key_Type)
354    is
355       procedure Iterate (Node : Node_Access);
356
357       -------------
358       -- Iterate --
359       -------------
360
361       procedure Iterate (Node : Node_Access) is
362          N : Node_Access;
363       begin
364          N := Node;
365          while N /= null loop
366             if Is_Less_Key_Node (Key, N) then
367                N := Ops.Left (N);
368             elsif Is_Greater_Key_Node (Key, N) then
369                N := Ops.Right (N);
370             else
371                Iterate (Ops.Left (N));
372                Process (N);
373                N := Ops.Right (N);
374             end if;
375          end loop;
376       end Iterate;
377
378    --  Start of processing for Generic_Iteration
379
380    begin
381       Iterate (Tree.Root);
382    end Generic_Iteration;
383
384    -------------------------------
385    -- Generic_Reverse_Iteration --
386    -------------------------------
387
388    procedure Generic_Reverse_Iteration
389      (Tree : Tree_Type;
390       Key  : Key_Type)
391    is
392       procedure Iterate (Node : Node_Access);
393
394       -------------
395       -- Iterate --
396       -------------
397
398       procedure Iterate (Node : Node_Access) is
399          N : Node_Access;
400       begin
401          N := Node;
402          while N /= null loop
403             if Is_Less_Key_Node (Key, N) then
404                N := Ops.Left (N);
405             elsif Is_Greater_Key_Node (Key, N) then
406                N := Ops.Right (N);
407             else
408                Iterate (Ops.Right (N));
409                Process (N);
410                N := Ops.Left (N);
411             end if;
412          end loop;
413       end Iterate;
414
415    --  Start of processing for Generic_Reverse_Iteration
416
417    begin
418       Iterate (Tree.Root);
419    end Generic_Reverse_Iteration;
420
421    ----------------------------------
422    -- Generic_Unconditional_Insert --
423    ----------------------------------
424
425    procedure Generic_Unconditional_Insert
426      (Tree : in out Tree_Type;
427       Key  : Key_Type;
428       Node : out Node_Access)
429    is
430       Y : Node_Access;
431       X : Node_Access;
432
433       Before : Boolean;
434
435    begin
436       Y := null;
437       Before := False;
438
439       X := Tree.Root;
440       while X /= null loop
441          Y := X;
442          Before := Is_Less_Key_Node (Key, X);
443
444          if Before then
445             X := Ops.Left (X);
446          else
447             X := Ops.Right (X);
448          end if;
449       end loop;
450
451       Insert_Post (Tree, Y, Before, Node);
452    end Generic_Unconditional_Insert;
453
454    --------------------------------------------
455    -- Generic_Unconditional_Insert_With_Hint --
456    --------------------------------------------
457
458    procedure Generic_Unconditional_Insert_With_Hint
459      (Tree : in out Tree_Type;
460       Hint : Node_Access;
461       Key  : Key_Type;
462       Node : out Node_Access)
463    is
464    begin
465       --  There are fewer constraints for an unconditional insertion
466       --  than for a conditional insertion, since we allow duplicate
467       --  keys. So instead of having to check (say) whether Key is
468       --  (strictly) greater than the hint's previous neighbor, here we
469       --  allow Key to be equal to or greater than the previous node.
470
471       --  There is the issue of what to do if Key is equivalent to the
472       --  hint. Does the new node get inserted before or after the hint?
473       --  We decide that it gets inserted after the hint, reasoning that
474       --  this is consistent with behavior for non-hint insertion, which
475       --  inserts a new node after existing nodes with equivalent keys.
476
477       --  First we check whether the hint is null, which is interpreted
478       --  to mean that Key is large relative to existing nodes.
479       --  Following our rule above, if Key is equal to or greater than
480       --  the last node, then we insert the new node immediately after
481       --  last. (We don't have an operation for testing whether a key is
482       --  "equal to or greater than" a node, so we must say instead "not
483       --  less than", which is equivalent.)
484
485       if Hint = null then  -- largest
486          if Tree.Last = null then
487             Insert_Post (Tree, null, False, Node);
488          elsif Is_Less_Key_Node (Key, Tree.Last) then
489             Unconditional_Insert_Sans_Hint (Tree, Key, Node);
490          else
491             Insert_Post (Tree, Tree.Last, False, Node);
492          end if;
493
494          return;
495       end if;
496
497       pragma Assert (Tree.Length > 0);
498
499       --  We decide here whether to insert the new node prior to the
500       --  hint. Key could be equivalent to the hint, so in theory we
501       --  could write the following test as "not greater than" (same as
502       --  "less than or equal to"). If Key were equivalent to the hint,
503       --  that would mean that the new node gets inserted before an
504       --  equivalent node. That wouldn't break any container invariants,
505       --  but our rule above says that new nodes always get inserted
506       --  after equivalent nodes. So here we test whether Key is both
507       --  less than the hint and equal to or greater than the hint's
508       --  previous neighbor, and if so insert it before the hint.
509
510       if Is_Less_Key_Node (Key, Hint) then
511          declare
512             Before : constant Node_Access := Ops.Previous (Hint);
513          begin
514             if Before = null then
515                Insert_Post (Tree, Hint, True, Node);
516             elsif Is_Less_Key_Node (Key, Before) then
517                Unconditional_Insert_Sans_Hint (Tree, Key, Node);
518             elsif Ops.Right (Before) = null then
519                Insert_Post (Tree, Before, False, Node);
520             else
521                Insert_Post (Tree, Hint, True, Node);
522             end if;
523          end;
524
525          return;
526       end if;
527
528       --  We know that Key isn't less than the hint, so it must be equal
529       --  or greater. So we just test whether Key is less than or equal
530       --  to (same as "not greater than") the hint's next neighbor, and
531       --  if so insert it after the hint.
532
533       declare
534          After : constant Node_Access := Ops.Next (Hint);
535       begin
536          if After = null then
537             Insert_Post (Tree, Hint, False, Node);
538          elsif Is_Greater_Key_Node (Key, After) then
539             Unconditional_Insert_Sans_Hint (Tree, Key, Node);
540          elsif Ops.Right (Hint) = null then
541             Insert_Post (Tree, Hint, False, Node);
542          else
543             Insert_Post (Tree, After, True, Node);
544          end if;
545       end;
546    end Generic_Unconditional_Insert_With_Hint;
547
548    -----------------
549    -- Upper_Bound --
550    -----------------
551
552    function Upper_Bound
553      (Tree : Tree_Type;
554       Key  : Key_Type) return Node_Access
555    is
556       Y : Node_Access;
557       X : Node_Access;
558
559    begin
560       X := Tree.Root;
561       while X /= null loop
562          if Is_Less_Key_Node (Key, X) then
563             Y := X;
564             X := Ops.Left (X);
565          else
566             X := Ops.Right (X);
567          end if;
568       end loop;
569
570       return Y;
571    end Upper_Bound;
572
573 end Ada.Containers.Red_Black_Trees.Generic_Keys;