]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/libgnat/a-cborse.adb
Use DW_TAG_module for Ada
[thirdparty/gcc.git] / gcc / ada / libgnat / a-cborse.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ O R D E R E D _ S E T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2024, 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 with Ada.Containers.Helpers; use Ada.Containers.Helpers;
31
32 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations;
33 pragma Elaborate_All
34 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations);
35
36 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys;
37 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys);
38
39 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations;
40 pragma Elaborate_All
41 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations);
42
43 with Ada.Unchecked_Deallocation;
44
45 with System; use type System.Address;
46 with System.Put_Images;
47
48 package body Ada.Containers.Bounded_Ordered_Sets with
49 SPARK_Mode => Off
50 is
51
52 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
53 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
54 -- See comment in Ada.Containers.Helpers
55
56 ------------------------------
57 -- Access to Fields of Node --
58 ------------------------------
59
60 -- These subprograms provide functional notation for access to fields
61 -- of a node, and procedural notation for modifying these fields.
62
63 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type;
64 pragma Inline (Color);
65
66 function Left (Node : Node_Type) return Count_Type;
67 pragma Inline (Left);
68
69 function Parent (Node : Node_Type) return Count_Type;
70 pragma Inline (Parent);
71
72 function Right (Node : Node_Type) return Count_Type;
73 pragma Inline (Right);
74
75 procedure Set_Color
76 (Node : in out Node_Type;
77 Color : Red_Black_Trees.Color_Type);
78 pragma Inline (Set_Color);
79
80 procedure Set_Left (Node : in out Node_Type; Left : Count_Type);
81 pragma Inline (Set_Left);
82
83 procedure Set_Right (Node : in out Node_Type; Right : Count_Type);
84 pragma Inline (Set_Right);
85
86 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type);
87 pragma Inline (Set_Parent);
88
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
92
93 procedure Insert_Sans_Hint
94 (Container : in out Set;
95 New_Item : Element_Type;
96 Node : out Count_Type;
97 Inserted : out Boolean);
98
99 procedure Insert_With_Hint
100 (Dst_Set : in out Set;
101 Dst_Hint : Count_Type;
102 Src_Node : Node_Type;
103 Dst_Node : out Count_Type);
104
105 function Is_Greater_Element_Node
106 (Left : Element_Type;
107 Right : Node_Type) return Boolean;
108 pragma Inline (Is_Greater_Element_Node);
109
110 function Is_Less_Element_Node
111 (Left : Element_Type;
112 Right : Node_Type) return Boolean;
113 pragma Inline (Is_Less_Element_Node);
114
115 function Is_Less_Node_Node (L, R : Node_Type) return Boolean;
116 pragma Inline (Is_Less_Node_Node);
117
118 procedure Replace_Element
119 (Container : in out Set;
120 Index : Count_Type;
121 Item : Element_Type);
122
123 --------------------------
124 -- Local Instantiations --
125 --------------------------
126
127 package Tree_Operations is
128 new Red_Black_Trees.Generic_Bounded_Operations (Tree_Types);
129
130 use Tree_Operations;
131
132 package Element_Keys is
133 new Red_Black_Trees.Generic_Bounded_Keys
134 (Tree_Operations => Tree_Operations,
135 Key_Type => Element_Type,
136 Is_Less_Key_Node => Is_Less_Element_Node,
137 Is_Greater_Key_Node => Is_Greater_Element_Node);
138
139 package Set_Ops is
140 new Red_Black_Trees.Generic_Bounded_Set_Operations
141 (Tree_Operations => Tree_Operations,
142 Set_Type => Set,
143 Assign => Assign,
144 Insert_With_Hint => Insert_With_Hint,
145 Is_Less => Is_Less_Node_Node);
146
147 ---------
148 -- "<" --
149 ---------
150
151 function "<" (Left, Right : Cursor) return Boolean is
152 begin
153 if Checks and then Left.Node = 0 then
154 raise Constraint_Error with "Left cursor equals No_Element";
155 end if;
156
157 if Checks and then Right.Node = 0 then
158 raise Constraint_Error with "Right cursor equals No_Element";
159 end if;
160
161 pragma Assert (Vet (Left.Container.all, Left.Node),
162 "bad Left cursor in ""<""");
163
164 pragma Assert (Vet (Right.Container.all, Right.Node),
165 "bad Right cursor in ""<""");
166
167 declare
168 LN : Nodes_Type renames Left.Container.Nodes;
169 RN : Nodes_Type renames Right.Container.Nodes;
170 begin
171 return LN (Left.Node).Element < RN (Right.Node).Element;
172 end;
173 end "<";
174
175 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
176 begin
177 if Checks and then Left.Node = 0 then
178 raise Constraint_Error with "Left cursor equals No_Element";
179 end if;
180
181 pragma Assert (Vet (Left.Container.all, Left.Node),
182 "bad Left cursor in ""<""");
183
184 return Left.Container.Nodes (Left.Node).Element < Right;
185 end "<";
186
187 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
188 begin
189 if Checks and then Right.Node = 0 then
190 raise Constraint_Error with "Right cursor equals No_Element";
191 end if;
192
193 pragma Assert (Vet (Right.Container.all, Right.Node),
194 "bad Right cursor in ""<""");
195
196 return Left < Right.Container.Nodes (Right.Node).Element;
197 end "<";
198
199 ---------
200 -- "=" --
201 ---------
202
203 function "=" (Left, Right : Set) return Boolean is
204 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean;
205 pragma Inline (Is_Equal_Node_Node);
206
207 function Is_Equal is
208 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
209
210 ------------------------
211 -- Is_Equal_Node_Node --
212 ------------------------
213
214 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean is
215 begin
216 return L.Element = R.Element;
217 end Is_Equal_Node_Node;
218
219 -- Start of processing for Is_Equal
220
221 begin
222 return Is_Equal (Left, Right);
223 end "=";
224
225 ---------
226 -- ">" --
227 ---------
228
229 function ">" (Left, Right : Cursor) return Boolean is
230 begin
231 if Checks and then Left.Node = 0 then
232 raise Constraint_Error with "Left cursor equals No_Element";
233 end if;
234
235 if Checks and then Right.Node = 0 then
236 raise Constraint_Error with "Right cursor equals No_Element";
237 end if;
238
239 pragma Assert (Vet (Left.Container.all, Left.Node),
240 "bad Left cursor in "">""");
241
242 pragma Assert (Vet (Right.Container.all, Right.Node),
243 "bad Right cursor in "">""");
244
245 -- L > R same as R < L
246
247 declare
248 LN : Nodes_Type renames Left.Container.Nodes;
249 RN : Nodes_Type renames Right.Container.Nodes;
250 begin
251 return RN (Right.Node).Element < LN (Left.Node).Element;
252 end;
253 end ">";
254
255 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
256 begin
257 if Checks and then Right.Node = 0 then
258 raise Constraint_Error with "Right cursor equals No_Element";
259 end if;
260
261 pragma Assert (Vet (Right.Container.all, Right.Node),
262 "bad Right cursor in "">""");
263
264 return Right.Container.Nodes (Right.Node).Element < Left;
265 end ">";
266
267 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
268 begin
269 if Checks and then Left.Node = 0 then
270 raise Constraint_Error with "Left cursor equals No_Element";
271 end if;
272
273 pragma Assert (Vet (Left.Container.all, Left.Node),
274 "bad Left cursor in "">""");
275
276 return Right < Left.Container.Nodes (Left.Node).Element;
277 end ">";
278
279 ------------
280 -- Assign --
281 ------------
282
283 procedure Assign (Target : in out Set; Source : Set) is
284 procedure Append_Element (Source_Node : Count_Type);
285
286 procedure Append_Elements is
287 new Tree_Operations.Generic_Iteration (Append_Element);
288
289 --------------------
290 -- Append_Element --
291 --------------------
292
293 procedure Append_Element (Source_Node : Count_Type) is
294 SN : Node_Type renames Source.Nodes (Source_Node);
295
296 procedure Set_Element (Node : in out Node_Type);
297 pragma Inline (Set_Element);
298
299 function New_Node return Count_Type;
300 pragma Inline (New_Node);
301
302 procedure Insert_Post is
303 new Element_Keys.Generic_Insert_Post (New_Node);
304
305 procedure Unconditional_Insert_Sans_Hint is
306 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
307
308 procedure Unconditional_Insert_Avec_Hint is
309 new Element_Keys.Generic_Unconditional_Insert_With_Hint
310 (Insert_Post,
311 Unconditional_Insert_Sans_Hint);
312
313 procedure Allocate is
314 new Tree_Operations.Generic_Allocate (Set_Element);
315
316 --------------
317 -- New_Node --
318 --------------
319
320 function New_Node return Count_Type is
321 Result : Count_Type;
322 begin
323 Allocate (Target, Result);
324 return Result;
325 end New_Node;
326
327 -----------------
328 -- Set_Element --
329 -----------------
330
331 procedure Set_Element (Node : in out Node_Type) is
332 begin
333 Node.Element := SN.Element;
334 end Set_Element;
335
336 Target_Node : Count_Type;
337
338 -- Start of processing for Append_Element
339
340 begin
341 Unconditional_Insert_Avec_Hint
342 (Tree => Target,
343 Hint => 0,
344 Key => SN.Element,
345 Node => Target_Node);
346 end Append_Element;
347
348 -- Start of processing for Assign
349
350 begin
351 if Target'Address = Source'Address then
352 return;
353 end if;
354
355 if Checks and then Target.Capacity < Source.Length then
356 raise Capacity_Error
357 with "Target capacity is less than Source length";
358 end if;
359
360 Target.Clear;
361 Append_Elements (Source);
362 end Assign;
363
364 -------------
365 -- Ceiling --
366 -------------
367
368 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
369 Node : constant Count_Type :=
370 Element_Keys.Ceiling (Container, Item);
371 begin
372 return (if Node = 0 then No_Element
373 else Cursor'(Container'Unrestricted_Access, Node));
374 end Ceiling;
375
376 -----------
377 -- Clear --
378 -----------
379
380 procedure Clear (Container : in out Set) is
381 begin
382 while not Container.Is_Empty loop
383 Container.Delete_Last;
384 end loop;
385 end Clear;
386
387 -----------
388 -- Color --
389 -----------
390
391 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type is
392 begin
393 return Node.Color;
394 end Color;
395
396 ------------------------
397 -- Constant_Reference --
398 ------------------------
399
400 function Constant_Reference
401 (Container : aliased Set;
402 Position : Cursor) return Constant_Reference_Type
403 is
404 begin
405 if Checks and then Position.Container = null then
406 raise Constraint_Error with "Position cursor has no element";
407 end if;
408
409 if Checks and then Position.Container /= Container'Unrestricted_Access
410 then
411 raise Program_Error with
412 "Position cursor designates wrong container";
413 end if;
414
415 pragma Assert
416 (Vet (Container, Position.Node),
417 "bad cursor in Constant_Reference");
418
419 declare
420 N : Node_Type renames Container.Nodes (Position.Node);
421 TC : constant Tamper_Counts_Access :=
422 Container.TC'Unrestricted_Access;
423 begin
424 return R : constant Constant_Reference_Type :=
425 (Element => N.Element'Unchecked_Access,
426 Control => (Controlled with TC))
427 do
428 Busy (TC.all);
429 end return;
430 end;
431 end Constant_Reference;
432
433 --------------
434 -- Contains --
435 --------------
436
437 function Contains
438 (Container : Set;
439 Item : Element_Type) return Boolean
440 is
441 begin
442 return Find (Container, Item) /= No_Element;
443 end Contains;
444
445 ----------
446 -- Copy --
447 ----------
448
449 function Copy (Source : Set; Capacity : Count_Type := 0) return Set is
450 C : constant Count_Type :=
451 (if Capacity = 0 then Source.Length
452 else Capacity);
453 begin
454 if Checks and then C < Source.Length then
455 raise Capacity_Error with "Capacity too small";
456 end if;
457
458 return Target : Set (Capacity => C) do
459 Assign (Target => Target, Source => Source);
460 end return;
461 end Copy;
462
463 ------------
464 -- Delete --
465 ------------
466
467 procedure Delete (Container : in out Set; Position : in out Cursor) is
468 begin
469 TC_Check (Container.TC);
470
471 if Checks and then Position.Node = 0 then
472 raise Constraint_Error with "Position cursor equals No_Element";
473 end if;
474
475 if Checks and then Position.Container /= Container'Unrestricted_Access
476 then
477 raise Program_Error with "Position cursor designates wrong set";
478 end if;
479
480 pragma Assert (Vet (Container, Position.Node),
481 "bad cursor in Delete");
482
483 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
484 Tree_Operations.Free (Container, Position.Node);
485
486 Position := No_Element;
487 end Delete;
488
489 procedure Delete (Container : in out Set; Item : Element_Type) is
490 X : constant Count_Type := Element_Keys.Find (Container, Item);
491
492 begin
493 Tree_Operations.Delete_Node_Sans_Free (Container, X);
494
495 if Checks and then X = 0 then
496 raise Constraint_Error with "attempt to delete element not in set";
497 end if;
498
499 Tree_Operations.Free (Container, X);
500 end Delete;
501
502 ------------------
503 -- Delete_First --
504 ------------------
505
506 procedure Delete_First (Container : in out Set) is
507 X : constant Count_Type := Container.First;
508 begin
509 if X /= 0 then
510 Tree_Operations.Delete_Node_Sans_Free (Container, X);
511 Tree_Operations.Free (Container, X);
512 end if;
513 end Delete_First;
514
515 -----------------
516 -- Delete_Last --
517 -----------------
518
519 procedure Delete_Last (Container : in out Set) is
520 X : constant Count_Type := Container.Last;
521 begin
522 if X /= 0 then
523 Tree_Operations.Delete_Node_Sans_Free (Container, X);
524 Tree_Operations.Free (Container, X);
525 end if;
526 end Delete_Last;
527
528 ----------------
529 -- Difference --
530 ----------------
531
532 procedure Difference (Target : in out Set; Source : Set)
533 renames Set_Ops.Set_Difference;
534
535 function Difference (Left, Right : Set) return Set
536 renames Set_Ops.Set_Difference;
537
538 -------------
539 -- Element --
540 -------------
541
542 function Element (Position : Cursor) return Element_Type is
543 begin
544 if Checks and then Position.Node = 0 then
545 raise Constraint_Error with "Position cursor equals No_Element";
546 end if;
547
548 pragma Assert (Vet (Position.Container.all, Position.Node),
549 "bad cursor in Element");
550
551 return Position.Container.Nodes (Position.Node).Element;
552 end Element;
553
554 -----------
555 -- Empty --
556 -----------
557
558 function Empty (Capacity : Count_Type := 10) return Set is
559 begin
560 return Result : Set (Capacity) do
561 null;
562 end return;
563 end Empty;
564
565 -------------------------
566 -- Equivalent_Elements --
567 -------------------------
568
569 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
570 begin
571 return (if Left < Right or else Right < Left then False else True);
572 end Equivalent_Elements;
573
574 ---------------------
575 -- Equivalent_Sets --
576 ---------------------
577
578 function Equivalent_Sets (Left, Right : Set) return Boolean is
579 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean;
580 pragma Inline (Is_Equivalent_Node_Node);
581
582 function Is_Equivalent is
583 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
584
585 -----------------------------
586 -- Is_Equivalent_Node_Node --
587 -----------------------------
588
589 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean is
590 begin
591 return (if L.Element < R.Element then False
592 elsif R.Element < L.Element then False
593 else True);
594 end Is_Equivalent_Node_Node;
595
596 -- Start of processing for Equivalent_Sets
597
598 begin
599 return Is_Equivalent (Left, Right);
600 end Equivalent_Sets;
601
602 -------------
603 -- Exclude --
604 -------------
605
606 procedure Exclude (Container : in out Set; Item : Element_Type) is
607 X : constant Count_Type := Element_Keys.Find (Container, Item);
608 begin
609 if X /= 0 then
610 Tree_Operations.Delete_Node_Sans_Free (Container, X);
611 Tree_Operations.Free (Container, X);
612 end if;
613 end Exclude;
614
615 --------------
616 -- Finalize --
617 --------------
618
619 procedure Finalize (Object : in out Iterator) is
620 begin
621 if Object.Container /= null then
622 Unbusy (Object.Container.TC);
623 end if;
624 end Finalize;
625
626 ----------
627 -- Find --
628 ----------
629
630 function Find (Container : Set; Item : Element_Type) return Cursor is
631 Node : constant Count_Type := Element_Keys.Find (Container, Item);
632 begin
633 return (if Node = 0 then No_Element
634 else Cursor'(Container'Unrestricted_Access, Node));
635 end Find;
636
637 -----------
638 -- First --
639 -----------
640
641 function First (Container : Set) return Cursor is
642 begin
643 return (if Container.First = 0 then No_Element
644 else Cursor'(Container'Unrestricted_Access, Container.First));
645 end First;
646
647 function First (Object : Iterator) return Cursor is
648 begin
649 -- The value of the iterator object's Node component influences the
650 -- behavior of the First (and Last) selector function.
651
652 -- When the Node component is 0, this means the iterator object was
653 -- constructed without a start expression, in which case the (forward)
654 -- iteration starts from the (logical) beginning of the entire sequence
655 -- of items (corresponding to Container.First, for a forward iterator).
656
657 -- Otherwise, this is iteration over a partial sequence of items. When
658 -- the Node component is positive, the iterator object was constructed
659 -- with a start expression, that specifies the position from which the
660 -- (forward) partial iteration begins.
661
662 if Object.Node = 0 then
663 return Bounded_Ordered_Sets.First (Object.Container.all);
664 else
665 return Cursor'(Object.Container, Object.Node);
666 end if;
667 end First;
668
669 -------------------
670 -- First_Element --
671 -------------------
672
673 function First_Element (Container : Set) return Element_Type is
674 begin
675 if Checks and then Container.First = 0 then
676 raise Constraint_Error with "set is empty";
677 end if;
678
679 return Container.Nodes (Container.First).Element;
680 end First_Element;
681
682 -----------
683 -- Floor --
684 -----------
685
686 function Floor (Container : Set; Item : Element_Type) return Cursor is
687 Node : constant Count_Type := Element_Keys.Floor (Container, Item);
688 begin
689 return (if Node = 0 then No_Element
690 else Cursor'(Container'Unrestricted_Access, Node));
691 end Floor;
692
693 -- Ada 2022 features:
694
695 function Has_Element (Container : Set; Position : Cursor) return Boolean is
696 begin
697 pragma Assert
698 (Position.Container = null or else Vet (Container, Position.Node),
699 "bad cursor in Has_Element");
700 pragma Assert ((Position.Container = null) = (Position.Node = 0),
701 "bad nullity in Has_Element");
702 return Position.Container = Container'Unrestricted_Access;
703 end Has_Element;
704
705 function Tampering_With_Cursors_Prohibited
706 (Container : Set) return Boolean
707 is
708 begin
709 return Is_Busy (Container.TC);
710 end Tampering_With_Cursors_Prohibited;
711
712 function Element (Container : Set; Position : Cursor) return Element_Type is
713 begin
714 if Checks and then not Has_Element (Container, Position) then
715 raise Program_Error with "Position for wrong Container";
716 end if;
717
718 return Element (Position);
719 end Element;
720
721 procedure Query_Element
722 (Container : Set;
723 Position : Cursor;
724 Process : not null access procedure (Element : Element_Type)) is
725 begin
726 if Checks and then not Has_Element (Container, Position) then
727 raise Program_Error with "Position for wrong Container";
728 end if;
729
730 Query_Element (Position, Process);
731 end Query_Element;
732
733 function Next (Container : Set; Position : Cursor) return Cursor is
734 begin
735 if Checks and then
736 not (Position = No_Element or else Has_Element (Container, Position))
737 then
738 raise Program_Error with "Position for wrong Container";
739 end if;
740
741 return Next (Position);
742 end Next;
743
744 procedure Next (Container : Set; Position : in out Cursor) is
745 begin
746 Position := Next (Container, Position);
747 end Next;
748
749 ------------------
750 -- Generic_Keys --
751 ------------------
752
753 package body Generic_Keys is
754
755 -----------------------
756 -- Local Subprograms --
757 -----------------------
758
759 function Is_Greater_Key_Node
760 (Left : Key_Type;
761 Right : Node_Type) return Boolean;
762 pragma Inline (Is_Greater_Key_Node);
763
764 function Is_Less_Key_Node
765 (Left : Key_Type;
766 Right : Node_Type) return Boolean;
767 pragma Inline (Is_Less_Key_Node);
768
769 --------------------------
770 -- Local Instantiations --
771 --------------------------
772
773 package Key_Keys is
774 new Red_Black_Trees.Generic_Bounded_Keys
775 (Tree_Operations => Tree_Operations,
776 Key_Type => Key_Type,
777 Is_Less_Key_Node => Is_Less_Key_Node,
778 Is_Greater_Key_Node => Is_Greater_Key_Node);
779
780 ------------
781 -- Adjust --
782 ------------
783
784 procedure Adjust (Control : in out Reference_Control_Type) is
785 begin
786 Impl.Reference_Control_Type (Control).Adjust;
787 if Control.Old_Key /= null then
788 Control.Old_Key := new Key_Type'(Control.Old_Key.all);
789 end if;
790 end Adjust;
791
792 -------------
793 -- Ceiling --
794 -------------
795
796 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
797 Node : constant Count_Type :=
798 Key_Keys.Ceiling (Container, Key);
799 begin
800 return (if Node = 0 then No_Element
801 else Cursor'(Container'Unrestricted_Access, Node));
802 end Ceiling;
803
804 ------------------------
805 -- Constant_Reference --
806 ------------------------
807
808 function Constant_Reference
809 (Container : aliased Set;
810 Key : Key_Type) return Constant_Reference_Type
811 is
812 Position : constant Cursor := Find (Container, Key);
813
814 begin
815 if Checks and then Position = No_Element then
816 raise Constraint_Error with "key not in set";
817 end if;
818
819 return Constant_Reference (Container, Position);
820 end Constant_Reference;
821
822 --------------
823 -- Contains --
824 --------------
825
826 function Contains (Container : Set; Key : Key_Type) return Boolean is
827 begin
828 return Find (Container, Key) /= No_Element;
829 end Contains;
830
831 ------------
832 -- Delete --
833 ------------
834
835 procedure Delete (Container : in out Set; Key : Key_Type) is
836 X : constant Count_Type := Key_Keys.Find (Container, Key);
837
838 begin
839 if Checks and then X = 0 then
840 raise Constraint_Error with "attempt to delete key not in set";
841 end if;
842
843 Tree_Operations.Delete_Node_Sans_Free (Container, X);
844 Tree_Operations.Free (Container, X);
845 end Delete;
846
847 -------------
848 -- Element --
849 -------------
850
851 function Element (Container : Set; Key : Key_Type) return Element_Type is
852 Node : constant Count_Type := Key_Keys.Find (Container, Key);
853
854 begin
855 if Checks and then Node = 0 then
856 raise Constraint_Error with "key not in set";
857 end if;
858
859 return Container.Nodes (Node).Element;
860 end Element;
861
862 ---------------------
863 -- Equivalent_Keys --
864 ---------------------
865
866 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
867 begin
868 return (if Left < Right or else Right < Left then False else True);
869 end Equivalent_Keys;
870
871 -------------
872 -- Exclude --
873 -------------
874
875 procedure Exclude (Container : in out Set; Key : Key_Type) is
876 X : constant Count_Type := Key_Keys.Find (Container, Key);
877 begin
878 if X /= 0 then
879 Tree_Operations.Delete_Node_Sans_Free (Container, X);
880 Tree_Operations.Free (Container, X);
881 end if;
882 end Exclude;
883
884 --------------
885 -- Finalize --
886 --------------
887
888 procedure Finalize (Control : in out Reference_Control_Type) is
889 procedure Deallocate is
890 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
891 begin
892 if Control.Container /= null then
893 Impl.Reference_Control_Type (Control).Finalize;
894
895 if Checks and then not (Key (Control.Pos) = Control.Old_Key.all)
896 then
897 Delete (Control.Container.all, Key (Control.Pos));
898 raise Program_Error;
899 end if;
900
901 Control.Container := null;
902 Deallocate (Control.Old_Key);
903 end if;
904 end Finalize;
905
906 ----------
907 -- Find --
908 ----------
909
910 function Find (Container : Set; Key : Key_Type) return Cursor is
911 Node : constant Count_Type := Key_Keys.Find (Container, Key);
912 begin
913 return (if Node = 0 then No_Element
914 else Cursor'(Container'Unrestricted_Access, Node));
915 end Find;
916
917 -----------
918 -- Floor --
919 -----------
920
921 function Floor (Container : Set; Key : Key_Type) return Cursor is
922 Node : constant Count_Type := Key_Keys.Floor (Container, Key);
923 begin
924 return (if Node = 0 then No_Element
925 else Cursor'(Container'Unrestricted_Access, Node));
926 end Floor;
927
928 -------------------------
929 -- Is_Greater_Key_Node --
930 -------------------------
931
932 function Is_Greater_Key_Node
933 (Left : Key_Type;
934 Right : Node_Type) return Boolean
935 is
936 begin
937 return Key (Right.Element) < Left;
938 end Is_Greater_Key_Node;
939
940 ----------------------
941 -- Is_Less_Key_Node --
942 ----------------------
943
944 function Is_Less_Key_Node
945 (Left : Key_Type;
946 Right : Node_Type) return Boolean
947 is
948 begin
949 return Left < Key (Right.Element);
950 end Is_Less_Key_Node;
951
952 ---------
953 -- Key --
954 ---------
955
956 function Key (Position : Cursor) return Key_Type is
957 begin
958 if Checks and then Position.Node = 0 then
959 raise Constraint_Error with
960 "Position cursor equals No_Element";
961 end if;
962
963 pragma Assert (Vet (Position.Container.all, Position.Node),
964 "bad cursor in Key");
965
966 return Key (Position.Container.Nodes (Position.Node).Element);
967 end Key;
968
969 ----------
970 -- Read --
971 ----------
972
973 procedure Read
974 (Stream : not null access Root_Stream_Type'Class;
975 Item : out Reference_Type)
976 is
977 begin
978 raise Program_Error with "attempt to stream reference";
979 end Read;
980
981 ------------------------------
982 -- Reference_Preserving_Key --
983 ------------------------------
984
985 function Reference_Preserving_Key
986 (Container : aliased in out Set;
987 Position : Cursor) return Reference_Type
988 is
989 begin
990 if Checks and then Position.Container = null then
991 raise Constraint_Error with "Position cursor has no element";
992 end if;
993
994 if Checks and then Position.Container /= Container'Unrestricted_Access
995 then
996 raise Program_Error with
997 "Position cursor designates wrong container";
998 end if;
999
1000 pragma Assert
1001 (Vet (Container, Position.Node),
1002 "bad cursor in function Reference_Preserving_Key");
1003
1004 declare
1005 N : Node_Type renames Container.Nodes (Position.Node);
1006 begin
1007 return R : constant Reference_Type :=
1008 (Element => N.Element'Unchecked_Access,
1009 Control =>
1010 (Controlled with
1011 Container.TC'Unrestricted_Access,
1012 Container => Container'Unchecked_Access,
1013 Pos => Position,
1014 Old_Key => new Key_Type'(Key (Position))))
1015 do
1016 Busy (Container.TC);
1017 end return;
1018 end;
1019 end Reference_Preserving_Key;
1020
1021 function Reference_Preserving_Key
1022 (Container : aliased in out Set;
1023 Key : Key_Type) return Reference_Type
1024 is
1025 Position : constant Cursor := Find (Container, Key);
1026
1027 begin
1028 if Checks and then Position = No_Element then
1029 raise Constraint_Error with "key not in set";
1030 end if;
1031
1032 return Reference_Preserving_Key (Container, Position);
1033 end Reference_Preserving_Key;
1034
1035 -------------
1036 -- Replace --
1037 -------------
1038
1039 procedure Replace
1040 (Container : in out Set;
1041 Key : Key_Type;
1042 New_Item : Element_Type)
1043 is
1044 Node : constant Count_Type := Key_Keys.Find (Container, Key);
1045
1046 begin
1047 if Checks and then Node = 0 then
1048 raise Constraint_Error with
1049 "attempt to replace key not in set";
1050 end if;
1051
1052 Replace_Element (Container, Node, New_Item);
1053 end Replace;
1054
1055 -----------------------------------
1056 -- Update_Element_Preserving_Key --
1057 -----------------------------------
1058
1059 procedure Update_Element_Preserving_Key
1060 (Container : in out Set;
1061 Position : Cursor;
1062 Process : not null access procedure (Element : in out Element_Type))
1063 is
1064 begin
1065 if Checks and then Position.Node = 0 then
1066 raise Constraint_Error with
1067 "Position cursor equals No_Element";
1068 end if;
1069
1070 if Checks and then Position.Container /= Container'Unrestricted_Access
1071 then
1072 raise Program_Error with
1073 "Position cursor designates wrong set";
1074 end if;
1075
1076 pragma Assert (Vet (Container, Position.Node),
1077 "bad cursor in Update_Element_Preserving_Key");
1078
1079 -- Per AI05-0022, the container implementation is required to detect
1080 -- element tampering by a generic actual subprogram.
1081
1082 declare
1083 N : Node_Type renames Container.Nodes (Position.Node);
1084 E : Element_Type renames N.Element;
1085 K : constant Key_Type := Key (E);
1086 Lock : With_Lock (Container.TC'Unrestricted_Access);
1087 begin
1088 Process (E);
1089 if Equivalent_Keys (K, Key (E)) then
1090 return;
1091 end if;
1092 end;
1093
1094 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
1095 Tree_Operations.Free (Container, Position.Node);
1096
1097 raise Program_Error with "key was modified";
1098 end Update_Element_Preserving_Key;
1099
1100 -----------
1101 -- Write --
1102 -----------
1103
1104 procedure Write
1105 (Stream : not null access Root_Stream_Type'Class;
1106 Item : Reference_Type)
1107 is
1108 begin
1109 raise Program_Error with "attempt to stream reference";
1110 end Write;
1111 end Generic_Keys;
1112
1113 ------------------------
1114 -- Get_Element_Access --
1115 ------------------------
1116
1117 function Get_Element_Access
1118 (Position : Cursor) return not null Element_Access is
1119 begin
1120 return Position.Container.Nodes (Position.Node).Element'Access;
1121 end Get_Element_Access;
1122
1123 -----------------
1124 -- Has_Element --
1125 -----------------
1126
1127 function Has_Element (Position : Cursor) return Boolean is
1128 begin
1129 return Position /= No_Element;
1130 end Has_Element;
1131
1132 -------------
1133 -- Include --
1134 -------------
1135
1136 procedure Include (Container : in out Set; New_Item : Element_Type) is
1137 Position : Cursor;
1138 Inserted : Boolean;
1139
1140 begin
1141 Insert (Container, New_Item, Position, Inserted);
1142
1143 if not Inserted then
1144 TE_Check (Container.TC);
1145
1146 Container.Nodes (Position.Node).Element := New_Item;
1147 end if;
1148 end Include;
1149
1150 ------------
1151 -- Insert --
1152 ------------
1153
1154 procedure Insert
1155 (Container : in out Set;
1156 New_Item : Element_Type;
1157 Position : out Cursor;
1158 Inserted : out Boolean)
1159 is
1160 begin
1161 Insert_Sans_Hint
1162 (Container,
1163 New_Item,
1164 Position.Node,
1165 Inserted);
1166
1167 Position.Container := Container'Unrestricted_Access;
1168 end Insert;
1169
1170 procedure Insert
1171 (Container : in out Set;
1172 New_Item : Element_Type)
1173 is
1174 Position : Cursor;
1175 Inserted : Boolean;
1176
1177 begin
1178 Insert (Container, New_Item, Position, Inserted);
1179
1180 if Checks and then not Inserted then
1181 raise Constraint_Error with
1182 "attempt to insert element already in set";
1183 end if;
1184 end Insert;
1185
1186 ----------------------
1187 -- Insert_Sans_Hint --
1188 ----------------------
1189
1190 procedure Insert_Sans_Hint
1191 (Container : in out Set;
1192 New_Item : Element_Type;
1193 Node : out Count_Type;
1194 Inserted : out Boolean)
1195 is
1196 procedure Set_Element (Node : in out Node_Type);
1197 pragma Inline (Set_Element);
1198
1199 function New_Node return Count_Type;
1200 pragma Inline (New_Node);
1201
1202 procedure Insert_Post is
1203 new Element_Keys.Generic_Insert_Post (New_Node);
1204
1205 procedure Conditional_Insert_Sans_Hint is
1206 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1207
1208 procedure Allocate is
1209 new Tree_Operations.Generic_Allocate (Set_Element);
1210
1211 --------------
1212 -- New_Node --
1213 --------------
1214
1215 function New_Node return Count_Type is
1216 Result : Count_Type;
1217 begin
1218 Allocate (Container, Result);
1219 return Result;
1220 end New_Node;
1221
1222 -----------------
1223 -- Set_Element --
1224 -----------------
1225
1226 procedure Set_Element (Node : in out Node_Type) is
1227 begin
1228 Node.Element := New_Item;
1229 end Set_Element;
1230
1231 -- Start of processing for Insert_Sans_Hint
1232
1233 begin
1234 TC_Check (Container.TC);
1235
1236 Conditional_Insert_Sans_Hint
1237 (Container,
1238 New_Item,
1239 Node,
1240 Inserted);
1241 end Insert_Sans_Hint;
1242
1243 ----------------------
1244 -- Insert_With_Hint --
1245 ----------------------
1246
1247 procedure Insert_With_Hint
1248 (Dst_Set : in out Set;
1249 Dst_Hint : Count_Type;
1250 Src_Node : Node_Type;
1251 Dst_Node : out Count_Type)
1252 is
1253 Success : Boolean;
1254
1255 procedure Set_Element (Node : in out Node_Type);
1256 pragma Inline (Set_Element);
1257
1258 function New_Node return Count_Type;
1259 pragma Inline (New_Node);
1260
1261 procedure Insert_Post is
1262 new Element_Keys.Generic_Insert_Post (New_Node);
1263
1264 procedure Insert_Sans_Hint is
1265 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1266
1267 procedure Local_Insert_With_Hint is
1268 new Element_Keys.Generic_Conditional_Insert_With_Hint
1269 (Insert_Post,
1270 Insert_Sans_Hint);
1271
1272 procedure Allocate is
1273 new Tree_Operations.Generic_Allocate (Set_Element);
1274
1275 --------------
1276 -- New_Node --
1277 --------------
1278
1279 function New_Node return Count_Type is
1280 Result : Count_Type;
1281 begin
1282 Allocate (Dst_Set, Result);
1283 return Result;
1284 end New_Node;
1285
1286 -----------------
1287 -- Set_Element --
1288 -----------------
1289
1290 procedure Set_Element (Node : in out Node_Type) is
1291 begin
1292 Node.Element := Src_Node.Element;
1293 end Set_Element;
1294
1295 -- Start of processing for Insert_With_Hint
1296
1297 begin
1298 Local_Insert_With_Hint
1299 (Dst_Set,
1300 Dst_Hint,
1301 Src_Node.Element,
1302 Dst_Node,
1303 Success);
1304 end Insert_With_Hint;
1305
1306 ------------------
1307 -- Intersection --
1308 ------------------
1309
1310 procedure Intersection (Target : in out Set; Source : Set)
1311 renames Set_Ops.Set_Intersection;
1312
1313 function Intersection (Left, Right : Set) return Set
1314 renames Set_Ops.Set_Intersection;
1315
1316 --------------
1317 -- Is_Empty --
1318 --------------
1319
1320 function Is_Empty (Container : Set) return Boolean is
1321 begin
1322 return Container.Length = 0;
1323 end Is_Empty;
1324
1325 -----------------------------
1326 -- Is_Greater_Element_Node --
1327 -----------------------------
1328
1329 function Is_Greater_Element_Node
1330 (Left : Element_Type;
1331 Right : Node_Type) return Boolean
1332 is
1333 begin
1334 -- Compute e > node same as node < e
1335
1336 return Right.Element < Left;
1337 end Is_Greater_Element_Node;
1338
1339 --------------------------
1340 -- Is_Less_Element_Node --
1341 --------------------------
1342
1343 function Is_Less_Element_Node
1344 (Left : Element_Type;
1345 Right : Node_Type) return Boolean
1346 is
1347 begin
1348 return Left < Right.Element;
1349 end Is_Less_Element_Node;
1350
1351 -----------------------
1352 -- Is_Less_Node_Node --
1353 -----------------------
1354
1355 function Is_Less_Node_Node (L, R : Node_Type) return Boolean is
1356 begin
1357 return L.Element < R.Element;
1358 end Is_Less_Node_Node;
1359
1360 ---------------
1361 -- Is_Subset --
1362 ---------------
1363
1364 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean
1365 renames Set_Ops.Set_Subset;
1366
1367 -------------
1368 -- Iterate --
1369 -------------
1370
1371 procedure Iterate
1372 (Container : Set;
1373 Process : not null access procedure (Position : Cursor))
1374 is
1375 procedure Process_Node (Node : Count_Type);
1376 pragma Inline (Process_Node);
1377
1378 procedure Local_Iterate is
1379 new Tree_Operations.Generic_Iteration (Process_Node);
1380
1381 ------------------
1382 -- Process_Node --
1383 ------------------
1384
1385 procedure Process_Node (Node : Count_Type) is
1386 begin
1387 Process (Cursor'(Container'Unrestricted_Access, Node));
1388 end Process_Node;
1389
1390 S : Set renames Container'Unrestricted_Access.all;
1391 Busy : With_Busy (S.TC'Unrestricted_Access);
1392
1393 -- Start of processing for Iterate
1394
1395 begin
1396 Local_Iterate (S);
1397 end Iterate;
1398
1399 function Iterate (Container : Set)
1400 return Set_Iterator_Interfaces.Reversible_Iterator'class
1401 is
1402 begin
1403 -- The value of the Node component influences the behavior of the First
1404 -- and Last selector functions of the iterator object. When the Node
1405 -- component is 0 (as is the case here), this means the iterator object
1406 -- was constructed without a start expression. This is a complete
1407 -- iterator, meaning that the iteration starts from the (logical)
1408 -- beginning of the sequence of items.
1409
1410 -- Note: For a forward iterator, Container.First is the beginning, and
1411 -- for a reverse iterator, Container.Last is the beginning.
1412
1413 return It : constant Iterator :=
1414 Iterator'(Limited_Controlled with
1415 Container => Container'Unrestricted_Access,
1416 Node => 0)
1417 do
1418 Busy (Container.TC'Unrestricted_Access.all);
1419 end return;
1420 end Iterate;
1421
1422 function Iterate (Container : Set; Start : Cursor)
1423 return Set_Iterator_Interfaces.Reversible_Iterator'class
1424 is
1425 begin
1426 -- It was formerly the case that when Start = No_Element, the partial
1427 -- iterator was defined to behave the same as for a complete iterator,
1428 -- and iterate over the entire sequence of items. However, those
1429 -- semantics were unintuitive and arguably error-prone (it is too easy
1430 -- to accidentally create an endless loop), and so they were changed,
1431 -- per the ARG meeting in Denver on 2011/11. However, there was no
1432 -- consensus about what positive meaning this corner case should have,
1433 -- and so it was decided to simply raise an exception. This does imply,
1434 -- however, that it is not possible to use a partial iterator to specify
1435 -- an empty sequence of items.
1436
1437 if Checks and then Start = No_Element then
1438 raise Constraint_Error with
1439 "Start position for iterator equals No_Element";
1440 end if;
1441
1442 if Checks and then Start.Container /= Container'Unrestricted_Access then
1443 raise Program_Error with
1444 "Start cursor of Iterate designates wrong set";
1445 end if;
1446
1447 pragma Assert (Vet (Container, Start.Node),
1448 "Start cursor of Iterate is bad");
1449
1450 -- The value of the Node component influences the behavior of the First
1451 -- and Last selector functions of the iterator object. When the Node
1452 -- component is positive (as is the case here), it means that this
1453 -- is a partial iteration, over a subset of the complete sequence of
1454 -- items. The iterator object was constructed with a start expression,
1455 -- indicating the position from which the iteration begins. (Note that
1456 -- the start position has the same value irrespective of whether this
1457 -- is a forward or reverse iteration.)
1458
1459 return It : constant Iterator :=
1460 Iterator'(Limited_Controlled with
1461 Container => Container'Unrestricted_Access,
1462 Node => Start.Node)
1463 do
1464 Busy (Container.TC'Unrestricted_Access.all);
1465 end return;
1466 end Iterate;
1467
1468 ----------
1469 -- Last --
1470 ----------
1471
1472 function Last (Container : Set) return Cursor is
1473 begin
1474 return (if Container.Last = 0 then No_Element
1475 else Cursor'(Container'Unrestricted_Access, Container.Last));
1476 end Last;
1477
1478 function Last (Object : Iterator) return Cursor is
1479 begin
1480 -- The value of the iterator object's Node component influences the
1481 -- behavior of the Last (and First) selector function.
1482
1483 -- When the Node component is 0, this means the iterator object was
1484 -- constructed without a start expression, in which case the (reverse)
1485 -- iteration starts from the (logical) beginning of the entire sequence
1486 -- (corresponding to Container.Last, for a reverse iterator).
1487
1488 -- Otherwise, this is iteration over a partial sequence of items. When
1489 -- the Node component is positive, the iterator object was constructed
1490 -- with a start expression, that specifies the position from which the
1491 -- (reverse) partial iteration begins.
1492
1493 if Object.Node = 0 then
1494 return Bounded_Ordered_Sets.Last (Object.Container.all);
1495 else
1496 return Cursor'(Object.Container, Object.Node);
1497 end if;
1498 end Last;
1499
1500 ------------------
1501 -- Last_Element --
1502 ------------------
1503
1504 function Last_Element (Container : Set) return Element_Type is
1505 begin
1506 if Checks and then Container.Last = 0 then
1507 raise Constraint_Error with "set is empty";
1508 end if;
1509
1510 return Container.Nodes (Container.Last).Element;
1511 end Last_Element;
1512
1513 ----------
1514 -- Left --
1515 ----------
1516
1517 function Left (Node : Node_Type) return Count_Type is
1518 begin
1519 return Node.Left;
1520 end Left;
1521
1522 ------------
1523 -- Length --
1524 ------------
1525
1526 function Length (Container : Set) return Count_Type is
1527 begin
1528 return Container.Length;
1529 end Length;
1530
1531 ----------
1532 -- Move --
1533 ----------
1534
1535 procedure Move (Target : in out Set; Source : in out Set) is
1536 begin
1537 if Target'Address = Source'Address then
1538 return;
1539 end if;
1540
1541 TC_Check (Source.TC);
1542
1543 Target.Assign (Source);
1544 Source.Clear;
1545 end Move;
1546
1547 ----------
1548 -- Next --
1549 ----------
1550
1551 function Next (Position : Cursor) return Cursor is
1552 begin
1553 if Position = No_Element then
1554 return No_Element;
1555 end if;
1556
1557 pragma Assert (Vet (Position.Container.all, Position.Node),
1558 "bad cursor in Next");
1559
1560 declare
1561 Node : constant Count_Type :=
1562 Tree_Operations.Next (Position.Container.all, Position.Node);
1563
1564 begin
1565 if Node = 0 then
1566 return No_Element;
1567 end if;
1568
1569 return Cursor'(Position.Container, Node);
1570 end;
1571 end Next;
1572
1573 procedure Next (Position : in out Cursor) is
1574 begin
1575 Position := Next (Position);
1576 end Next;
1577
1578 function Next (Object : Iterator; Position : Cursor) return Cursor is
1579 begin
1580 if Position.Container = null then
1581 return No_Element;
1582 end if;
1583
1584 if Checks and then Position.Container /= Object.Container then
1585 raise Program_Error with
1586 "Position cursor of Next designates wrong set";
1587 end if;
1588
1589 return Next (Position);
1590 end Next;
1591
1592 -------------
1593 -- Overlap --
1594 -------------
1595
1596 function Overlap (Left, Right : Set) return Boolean
1597 renames Set_Ops.Set_Overlap;
1598
1599 ------------
1600 -- Parent --
1601 ------------
1602
1603 function Parent (Node : Node_Type) return Count_Type is
1604 begin
1605 return Node.Parent;
1606 end Parent;
1607
1608 --------------
1609 -- Previous --
1610 --------------
1611
1612 function Previous (Position : Cursor) return Cursor is
1613 begin
1614 if Position = No_Element then
1615 return No_Element;
1616 end if;
1617
1618 pragma Assert (Vet (Position.Container.all, Position.Node),
1619 "bad cursor in Previous");
1620
1621 declare
1622 Node : constant Count_Type :=
1623 Tree_Operations.Previous (Position.Container.all, Position.Node);
1624 begin
1625 return (if Node = 0 then No_Element
1626 else Cursor'(Position.Container, Node));
1627 end;
1628 end Previous;
1629
1630 procedure Previous (Position : in out Cursor) is
1631 begin
1632 Position := Previous (Position);
1633 end Previous;
1634
1635 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1636 begin
1637 if Position.Container = null then
1638 return No_Element;
1639 end if;
1640
1641 if Checks and then Position.Container /= Object.Container then
1642 raise Program_Error with
1643 "Position cursor of Previous designates wrong set";
1644 end if;
1645
1646 return Previous (Position);
1647 end Previous;
1648
1649 ----------------------
1650 -- Pseudo_Reference --
1651 ----------------------
1652
1653 function Pseudo_Reference
1654 (Container : aliased Set'Class) return Reference_Control_Type
1655 is
1656 TC : constant Tamper_Counts_Access :=
1657 Container.TC'Unrestricted_Access;
1658 begin
1659 return R : constant Reference_Control_Type := (Controlled with TC) do
1660 Busy (TC.all);
1661 end return;
1662 end Pseudo_Reference;
1663
1664 -------------------
1665 -- Query_Element --
1666 -------------------
1667
1668 procedure Query_Element
1669 (Position : Cursor;
1670 Process : not null access procedure (Element : Element_Type))
1671 is
1672 begin
1673 if Checks and then Position.Node = 0 then
1674 raise Constraint_Error with "Position cursor equals No_Element";
1675 end if;
1676
1677 pragma Assert (Vet (Position.Container.all, Position.Node),
1678 "bad cursor in Query_Element");
1679
1680 declare
1681 S : Set renames Position.Container.all;
1682 Lock : With_Lock (S.TC'Unrestricted_Access);
1683 begin
1684 Process (S.Nodes (Position.Node).Element);
1685 end;
1686 end Query_Element;
1687
1688 ---------------
1689 -- Put_Image --
1690 ---------------
1691
1692 procedure Put_Image
1693 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Set)
1694 is
1695 First_Time : Boolean := True;
1696 use System.Put_Images;
1697 begin
1698 Array_Before (S);
1699
1700 for X of V loop
1701 if First_Time then
1702 First_Time := False;
1703 else
1704 Simple_Array_Between (S);
1705 end if;
1706
1707 Element_Type'Put_Image (S, X);
1708 end loop;
1709
1710 Array_After (S);
1711 end Put_Image;
1712
1713 ----------
1714 -- Read --
1715 ----------
1716
1717 procedure Read
1718 (Stream : not null access Root_Stream_Type'Class;
1719 Container : out Set)
1720 is
1721 procedure Read_Element (Node : in out Node_Type);
1722 pragma Inline (Read_Element);
1723
1724 procedure Allocate is
1725 new Tree_Operations.Generic_Allocate (Read_Element);
1726
1727 procedure Read_Elements is
1728 new Tree_Operations.Generic_Read (Allocate);
1729
1730 ------------------
1731 -- Read_Element --
1732 ------------------
1733
1734 procedure Read_Element (Node : in out Node_Type) is
1735 begin
1736 Element_Type'Read (Stream, Node.Element);
1737 end Read_Element;
1738
1739 -- Start of processing for Read
1740
1741 begin
1742 Read_Elements (Stream, Container);
1743 end Read;
1744
1745 procedure Read
1746 (Stream : not null access Root_Stream_Type'Class;
1747 Item : out Cursor)
1748 is
1749 begin
1750 raise Program_Error with "attempt to stream set cursor";
1751 end Read;
1752
1753 procedure Read
1754 (Stream : not null access Root_Stream_Type'Class;
1755 Item : out Constant_Reference_Type)
1756 is
1757 begin
1758 raise Program_Error with "attempt to stream reference";
1759 end Read;
1760
1761 -------------
1762 -- Replace --
1763 -------------
1764
1765 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1766 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1767
1768 begin
1769 TE_Check (Container.TC);
1770
1771 if Checks and then Node = 0 then
1772 raise Constraint_Error with
1773 "attempt to replace element not in set";
1774 end if;
1775
1776 Container.Nodes (Node).Element := New_Item;
1777 end Replace;
1778
1779 ---------------------
1780 -- Replace_Element --
1781 ---------------------
1782
1783 procedure Replace_Element
1784 (Container : in out Set;
1785 Index : Count_Type;
1786 Item : Element_Type)
1787 is
1788 pragma Assert (Index /= 0);
1789
1790 function New_Node return Count_Type;
1791 pragma Inline (New_Node);
1792
1793 procedure Local_Insert_Post is
1794 new Element_Keys.Generic_Insert_Post (New_Node);
1795
1796 procedure Local_Insert_Sans_Hint is
1797 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1798
1799 procedure Local_Insert_With_Hint is
1800 new Element_Keys.Generic_Conditional_Insert_With_Hint
1801 (Local_Insert_Post,
1802 Local_Insert_Sans_Hint);
1803
1804 Nodes : Nodes_Type renames Container.Nodes;
1805 Node : Node_Type renames Nodes (Index);
1806
1807 --------------
1808 -- New_Node --
1809 --------------
1810
1811 function New_Node return Count_Type is
1812 begin
1813 Node.Element := Item;
1814 Node.Color := Red_Black_Trees.Red;
1815 Node.Parent := 0;
1816 Node.Right := 0;
1817 Node.Left := 0;
1818 return Index;
1819 end New_Node;
1820
1821 Hint : Count_Type;
1822 Result : Count_Type;
1823 Inserted : Boolean;
1824 Compare : Boolean;
1825
1826 -- Start of processing for Replace_Element
1827
1828 begin
1829 -- Replace_Element assigns value Item to the element designated by Node,
1830 -- per certain semantic constraints, described as follows.
1831
1832 -- If Item is equivalent to the element, then element is replaced and
1833 -- there's nothing else to do. This is the easy case.
1834
1835 -- If Item is not equivalent, then the node will (possibly) have to move
1836 -- to some other place in the tree. This is slighly more complicated,
1837 -- because we must ensure that Item is not equivalent to some other
1838 -- element in the tree (in which case, the replacement is not allowed).
1839
1840 -- Determine whether Item is equivalent to element on the specified
1841 -- node.
1842
1843 declare
1844 Lock : With_Lock (Container.TC'Unrestricted_Access);
1845 begin
1846 Compare := (if Item < Node.Element then False
1847 elsif Node.Element < Item then False
1848 else True);
1849 end;
1850
1851 if Compare then
1852
1853 -- Item is equivalent to the node's element, so we will not have to
1854 -- move the node.
1855
1856 TE_Check (Container.TC);
1857
1858 Node.Element := Item;
1859 return;
1860 end if;
1861
1862 -- The replacement Item is not equivalent to the element on the
1863 -- specified node, which means that it will need to be re-inserted in a
1864 -- different position in the tree. We must now determine whether Item is
1865 -- equivalent to some other element in the tree (which would prohibit
1866 -- the assignment and hence the move).
1867
1868 -- Ceiling returns the smallest element equivalent or greater than the
1869 -- specified Item; if there is no such element, then it returns 0.
1870
1871 Hint := Element_Keys.Ceiling (Container, Item);
1872
1873 if Hint /= 0 then -- Item <= Nodes (Hint).Element
1874 declare
1875 Lock : With_Lock (Container.TC'Unrestricted_Access);
1876 begin
1877 Compare := Item < Nodes (Hint).Element;
1878 end;
1879
1880 -- Item is equivalent to Nodes (Hint).Element
1881
1882 if Checks and then not Compare then
1883
1884 -- Ceiling returns an element that is equivalent or greater than
1885 -- Item. If Item is "not less than" the element, then by
1886 -- elimination we know that Item is equivalent to the element.
1887
1888 -- But this means that it is not possible to assign the value of
1889 -- Item to the specified element (on Node), because a different
1890 -- element (on Hint) equivalent to Item already exsits. (Were we
1891 -- to change Node's element value, we would have to move Node, but
1892 -- we would be unable to move the Node, because its new position
1893 -- in the tree is already occupied by an equivalent element.)
1894
1895 raise Program_Error with "attempt to replace existing element";
1896 end if;
1897
1898 -- Item is not equivalent to any other element in the tree
1899 -- (specifically, it is less than Nodes (Hint).Element), so it is
1900 -- safe to assign the value of Item to Node.Element. This means that
1901 -- the node will have to move to a different position in the tree
1902 -- (because its element will have a different value).
1903
1904 -- The nearest (greater) neighbor of Item is Hint. This will be the
1905 -- insertion position of Node (because its element will have Item as
1906 -- its new value).
1907
1908 -- If Node equals Hint, the relative position of Node does not
1909 -- change. This allows us to perform an optimization: we need not
1910 -- remove Node from the tree and then reinsert it with its new value,
1911 -- because it would only be placed in the exact same position.
1912
1913 if Hint = Index then
1914 TE_Check (Container.TC);
1915
1916 Node.Element := Item;
1917 return;
1918 end if;
1919 end if;
1920
1921 -- If we get here, it is because Item was greater than all elements in
1922 -- the tree (Hint = 0), or because Item was less than some element at a
1923 -- different place in the tree (Item < Nodes (Hint).Element and Hint /=
1924 -- Index). In either case, we remove Node from the tree and then insert
1925 -- Item into the tree, onto the same Node.
1926
1927 Tree_Operations.Delete_Node_Sans_Free (Container, Index);
1928
1929 Local_Insert_With_Hint
1930 (Tree => Container,
1931 Position => Hint,
1932 Key => Item,
1933 Node => Result,
1934 Inserted => Inserted);
1935
1936 pragma Assert (Inserted);
1937 pragma Assert (Result = Index);
1938 end Replace_Element;
1939
1940 procedure Replace_Element
1941 (Container : in out Set;
1942 Position : Cursor;
1943 New_Item : Element_Type)
1944 is
1945 begin
1946 if Checks and then Position.Node = 0 then
1947 raise Constraint_Error with
1948 "Position cursor equals No_Element";
1949 end if;
1950
1951 if Checks and then Position.Container /= Container'Unrestricted_Access
1952 then
1953 raise Program_Error with
1954 "Position cursor designates wrong set";
1955 end if;
1956
1957 pragma Assert (Vet (Container, Position.Node),
1958 "bad cursor in Replace_Element");
1959
1960 Replace_Element (Container, Position.Node, New_Item);
1961 end Replace_Element;
1962
1963 ---------------------
1964 -- Reverse_Iterate --
1965 ---------------------
1966
1967 procedure Reverse_Iterate
1968 (Container : Set;
1969 Process : not null access procedure (Position : Cursor))
1970 is
1971 procedure Process_Node (Node : Count_Type);
1972 pragma Inline (Process_Node);
1973
1974 procedure Local_Reverse_Iterate is
1975 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1976
1977 ------------------
1978 -- Process_Node --
1979 ------------------
1980
1981 procedure Process_Node (Node : Count_Type) is
1982 begin
1983 Process (Cursor'(Container'Unrestricted_Access, Node));
1984 end Process_Node;
1985
1986 S : Set renames Container'Unrestricted_Access.all;
1987 Busy : With_Busy (S.TC'Unrestricted_Access);
1988
1989 -- Start of processing for Reverse_Iterate
1990
1991 begin
1992 Local_Reverse_Iterate (S);
1993 end Reverse_Iterate;
1994
1995 -----------
1996 -- Right --
1997 -----------
1998
1999 function Right (Node : Node_Type) return Count_Type is
2000 begin
2001 return Node.Right;
2002 end Right;
2003
2004 ---------------
2005 -- Set_Color --
2006 ---------------
2007
2008 procedure Set_Color
2009 (Node : in out Node_Type;
2010 Color : Red_Black_Trees.Color_Type)
2011 is
2012 begin
2013 Node.Color := Color;
2014 end Set_Color;
2015
2016 --------------
2017 -- Set_Left --
2018 --------------
2019
2020 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
2021 begin
2022 Node.Left := Left;
2023 end Set_Left;
2024
2025 ----------------
2026 -- Set_Parent --
2027 ----------------
2028
2029 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
2030 begin
2031 Node.Parent := Parent;
2032 end Set_Parent;
2033
2034 ---------------
2035 -- Set_Right --
2036 ---------------
2037
2038 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
2039 begin
2040 Node.Right := Right;
2041 end Set_Right;
2042
2043 --------------------------
2044 -- Symmetric_Difference --
2045 --------------------------
2046
2047 procedure Symmetric_Difference (Target : in out Set; Source : Set)
2048 renames Set_Ops.Set_Symmetric_Difference;
2049
2050 function Symmetric_Difference (Left, Right : Set) return Set
2051 renames Set_Ops.Set_Symmetric_Difference;
2052
2053 ------------
2054 -- To_Set --
2055 ------------
2056
2057 function To_Set (New_Item : Element_Type) return Set is
2058 Node : Count_Type;
2059 Inserted : Boolean;
2060
2061 begin
2062 return S : Set (1) do
2063 Insert_Sans_Hint (S, New_Item, Node, Inserted);
2064 pragma Assert (Inserted);
2065 end return;
2066 end To_Set;
2067
2068 -----------
2069 -- Union --
2070 -----------
2071
2072 procedure Union (Target : in out Set; Source : Set)
2073 renames Set_Ops.Set_Union;
2074
2075 function Union (Left, Right : Set) return Set
2076 renames Set_Ops.Set_Union;
2077
2078 -----------
2079 -- Write --
2080 -----------
2081
2082 procedure Write
2083 (Stream : not null access Root_Stream_Type'Class;
2084 Container : Set)
2085 is
2086 procedure Write_Element
2087 (Stream : not null access Root_Stream_Type'Class;
2088 Node : Node_Type);
2089 pragma Inline (Write_Element);
2090
2091 procedure Write_Elements is
2092 new Tree_Operations.Generic_Write (Write_Element);
2093
2094 -------------------
2095 -- Write_Element --
2096 -------------------
2097
2098 procedure Write_Element
2099 (Stream : not null access Root_Stream_Type'Class;
2100 Node : Node_Type)
2101 is
2102 begin
2103 Element_Type'Write (Stream, Node.Element);
2104 end Write_Element;
2105
2106 -- Start of processing for Write
2107
2108 begin
2109 Write_Elements (Stream, Container);
2110 end Write;
2111
2112 procedure Write
2113 (Stream : not null access Root_Stream_Type'Class;
2114 Item : Cursor)
2115 is
2116 begin
2117 raise Program_Error with "attempt to stream set cursor";
2118 end Write;
2119
2120 procedure Write
2121 (Stream : not null access Root_Stream_Type'Class;
2122 Item : Constant_Reference_Type)
2123 is
2124 begin
2125 raise Program_Error with "attempt to stream reference";
2126 end Write;
2127
2128 end Ada.Containers.Bounded_Ordered_Sets;