1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.RED_BLACK_TREES.GENERIC_SET_OPERATIONS --
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 with System; use type System.Address;
34 package body Ada.Containers.Red_Black_Trees.Generic_Set_Operations is
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
40 procedure Clear (Tree : in out Tree_Type);
42 function Copy (Source : Tree_Type) return Tree_Type;
48 procedure Clear (Tree : in out Tree_Type) is
49 pragma Assert (Tree.Busy = 0);
50 pragma Assert (Tree.Lock = 0);
52 Root : Node_Access := Tree.Root;
53 pragma Warnings (Off, Root);
68 function Copy (Source : Tree_Type) return Tree_Type is
72 if Source.Length = 0 then
76 Target.Root := Copy_Tree (Source.Root);
77 Target.First := Tree_Operations.Min (Target.Root);
78 Target.Last := Tree_Operations.Max (Target.Root);
79 Target.Length := Source.Length;
88 procedure Difference (Target : in out Tree_Type; Source : Tree_Type) is
89 Tgt : Node_Access := Target.First;
90 Src : Node_Access := Source.First;
93 if Target'Address = Source'Address then
94 if Target.Busy > 0 then
95 raise Program_Error with
96 "attempt to tamper with cursors (container is busy)";
103 if Source.Length = 0 then
107 if Target.Busy > 0 then
108 raise Program_Error with
109 "attempt to tamper with cursors (container is busy)";
121 if Is_Less (Tgt, Src) then
122 Tgt := Tree_Operations.Next (Tgt);
124 elsif Is_Less (Src, Tgt) then
125 Src := Tree_Operations.Next (Src);
129 X : Node_Access := Tgt;
131 Tgt := Tree_Operations.Next (Tgt);
132 Tree_Operations.Delete_Node_Sans_Free (Target, X);
136 Src := Tree_Operations.Next (Src);
141 function Difference (Left, Right : Tree_Type) return Tree_Type is
144 L_Node : Node_Access := Left.First;
145 R_Node : Node_Access := Right.First;
147 Dst_Node : Node_Access;
148 pragma Warnings (Off, Dst_Node);
151 if Left'Address = Right'Address then
152 return Tree; -- Empty set
155 if Left.Length = 0 then
156 return Tree; -- Empty set
159 if Right.Length = 0 then
164 if L_Node = null then
168 if R_Node = null then
169 while L_Node /= null loop
174 Dst_Node => Dst_Node);
176 L_Node := Tree_Operations.Next (L_Node);
183 if Is_Less (L_Node, R_Node) then
188 Dst_Node => Dst_Node);
190 L_Node := Tree_Operations.Next (L_Node);
192 elsif Is_Less (R_Node, L_Node) then
193 R_Node := Tree_Operations.Next (R_Node);
196 L_Node := Tree_Operations.Next (L_Node);
197 R_Node := Tree_Operations.Next (R_Node);
203 Delete_Tree (Tree.Root);
211 procedure Intersection
212 (Target : in out Tree_Type;
215 Tgt : Node_Access := Target.First;
216 Src : Node_Access := Source.First;
219 if Target'Address = Source'Address then
223 if Target.Busy > 0 then
224 raise Program_Error with
225 "attempt to tamper with cursors (container is busy)";
228 if Source.Length = 0 then
236 if Is_Less (Tgt, Src) then
238 X : Node_Access := Tgt;
240 Tgt := Tree_Operations.Next (Tgt);
241 Tree_Operations.Delete_Node_Sans_Free (Target, X);
245 elsif Is_Less (Src, Tgt) then
246 Src := Tree_Operations.Next (Src);
249 Tgt := Tree_Operations.Next (Tgt);
250 Src := Tree_Operations.Next (Src);
254 while Tgt /= null loop
256 X : Node_Access := Tgt;
258 Tgt := Tree_Operations.Next (Tgt);
259 Tree_Operations.Delete_Node_Sans_Free (Target, X);
265 function Intersection (Left, Right : Tree_Type) return Tree_Type is
268 L_Node : Node_Access := Left.First;
269 R_Node : Node_Access := Right.First;
271 Dst_Node : Node_Access;
272 pragma Warnings (Off, Dst_Node);
275 if Left'Address = Right'Address then
280 if L_Node = null then
284 if R_Node = null then
288 if Is_Less (L_Node, R_Node) then
289 L_Node := Tree_Operations.Next (L_Node);
291 elsif Is_Less (R_Node, L_Node) then
292 R_Node := Tree_Operations.Next (R_Node);
299 Dst_Node => Dst_Node);
301 L_Node := Tree_Operations.Next (L_Node);
302 R_Node := Tree_Operations.Next (R_Node);
308 Delete_Tree (Tree.Root);
318 Of_Set : Tree_Type) return Boolean
321 if Subset'Address = Of_Set'Address then
325 if Subset.Length > Of_Set.Length then
330 Subset_Node : Node_Access := Subset.First;
331 Set_Node : Node_Access := Of_Set.First;
335 if Set_Node = null then
336 return Subset_Node = null;
339 if Subset_Node = null then
343 if Is_Less (Subset_Node, Set_Node) then
347 if Is_Less (Set_Node, Subset_Node) then
348 Set_Node := Tree_Operations.Next (Set_Node);
350 Set_Node := Tree_Operations.Next (Set_Node);
351 Subset_Node := Tree_Operations.Next (Subset_Node);
361 function Overlap (Left, Right : Tree_Type) return Boolean is
362 L_Node : Node_Access := Left.First;
363 R_Node : Node_Access := Right.First;
366 if Left'Address = Right'Address then
367 return Left.Length /= 0;
372 or else R_Node = null
377 if Is_Less (L_Node, R_Node) then
378 L_Node := Tree_Operations.Next (L_Node);
380 elsif Is_Less (R_Node, L_Node) then
381 R_Node := Tree_Operations.Next (R_Node);
389 --------------------------
390 -- Symmetric_Difference --
391 --------------------------
393 procedure Symmetric_Difference
394 (Target : in out Tree_Type;
397 Tgt : Node_Access := Target.First;
398 Src : Node_Access := Source.First;
400 New_Tgt_Node : Node_Access;
401 pragma Warnings (Off, New_Tgt_Node);
404 if Target.Busy > 0 then
405 raise Program_Error with
406 "attempt to tamper with cursors (container is busy)";
409 if Target'Address = Source'Address then
416 while Src /= null loop
421 Dst_Node => New_Tgt_Node);
423 Src := Tree_Operations.Next (Src);
433 if Is_Less (Tgt, Src) then
434 Tgt := Tree_Operations.Next (Tgt);
436 elsif Is_Less (Src, Tgt) then
441 Dst_Node => New_Tgt_Node);
443 Src := Tree_Operations.Next (Src);
447 X : Node_Access := Tgt;
449 Tgt := Tree_Operations.Next (Tgt);
450 Tree_Operations.Delete_Node_Sans_Free (Target, X);
454 Src := Tree_Operations.Next (Src);
457 end Symmetric_Difference;
459 function Symmetric_Difference (Left, Right : Tree_Type) return Tree_Type is
462 L_Node : Node_Access := Left.First;
463 R_Node : Node_Access := Right.First;
465 Dst_Node : Node_Access;
466 pragma Warnings (Off, Dst_Node);
469 if Left'Address = Right'Address then
470 return Tree; -- Empty set
473 if Right.Length = 0 then
477 if Left.Length = 0 then
482 if L_Node = null then
483 while R_Node /= null loop
488 Dst_Node => Dst_Node);
489 R_Node := Tree_Operations.Next (R_Node);
495 if R_Node = null then
496 while L_Node /= null loop
501 Dst_Node => Dst_Node);
503 L_Node := Tree_Operations.Next (L_Node);
509 if Is_Less (L_Node, R_Node) then
514 Dst_Node => Dst_Node);
516 L_Node := Tree_Operations.Next (L_Node);
518 elsif Is_Less (R_Node, L_Node) then
523 Dst_Node => Dst_Node);
525 R_Node := Tree_Operations.Next (R_Node);
528 L_Node := Tree_Operations.Next (L_Node);
529 R_Node := Tree_Operations.Next (R_Node);
535 Delete_Tree (Tree.Root);
537 end Symmetric_Difference;
543 procedure Union (Target : in out Tree_Type; Source : Tree_Type)
547 procedure Process (Node : Node_Access);
548 pragma Inline (Process);
550 procedure Iterate is new Tree_Operations.Generic_Iteration (Process);
556 procedure Process (Node : Node_Access) is
565 -- Start of processing for Union
568 if Target'Address = Source'Address then
572 if Target.Busy > 0 then
573 raise Program_Error with
574 "attempt to tamper with cursors (container is busy)";
580 function Union (Left, Right : Tree_Type) return Tree_Type is
582 if Left'Address = Right'Address then
586 if Left.Length = 0 then
590 if Right.Length = 0 then
595 Tree : Tree_Type := Copy (Left);
599 procedure Process (Node : Node_Access);
600 pragma Inline (Process);
603 new Tree_Operations.Generic_Iteration (Process);
609 procedure Process (Node : Node_Access) is
618 -- Start of processing for Union
626 Delete_Tree (Tree.Root);
632 end Ada.Containers.Red_Black_Trees.Generic_Set_Operations;