]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/libgnat/a-cborma.adb
[Ada] Efficiency improvement in bounded ordered containers
[thirdparty/gcc.git] / gcc / ada / libgnat / a-cborma.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 _ M A P S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2019, 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
38 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys);
39
40 with System; use type System.Address;
41
42 package body Ada.Containers.Bounded_Ordered_Maps is
43
44 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
45 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
46 -- See comment in Ada.Containers.Helpers
47
48 -----------------------------
49 -- Node Access Subprograms --
50 -----------------------------
51
52 -- These subprograms provide a functional interface to access fields
53 -- of a node, and a procedural interface for modifying these values.
54
55 function Color (Node : Node_Type) return Color_Type;
56 pragma Inline (Color);
57
58 function Left (Node : Node_Type) return Count_Type;
59 pragma Inline (Left);
60
61 function Parent (Node : Node_Type) return Count_Type;
62 pragma Inline (Parent);
63
64 function Right (Node : Node_Type) return Count_Type;
65 pragma Inline (Right);
66
67 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type);
68 pragma Inline (Set_Parent);
69
70 procedure Set_Left (Node : in out Node_Type; Left : Count_Type);
71 pragma Inline (Set_Left);
72
73 procedure Set_Right (Node : in out Node_Type; Right : Count_Type);
74 pragma Inline (Set_Right);
75
76 procedure Set_Color (Node : in out Node_Type; Color : Color_Type);
77 pragma Inline (Set_Color);
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
83 function Is_Greater_Key_Node
84 (Left : Key_Type;
85 Right : Node_Type) return Boolean;
86 pragma Inline (Is_Greater_Key_Node);
87
88 function Is_Less_Key_Node
89 (Left : Key_Type;
90 Right : Node_Type) return Boolean;
91 pragma Inline (Is_Less_Key_Node);
92
93 --------------------------
94 -- Local Instantiations --
95 --------------------------
96
97 package Tree_Operations is
98 new Red_Black_Trees.Generic_Bounded_Operations (Tree_Types);
99
100 use Tree_Operations;
101
102 package Key_Ops is
103 new Red_Black_Trees.Generic_Bounded_Keys
104 (Tree_Operations => Tree_Operations,
105 Key_Type => Key_Type,
106 Is_Less_Key_Node => Is_Less_Key_Node,
107 Is_Greater_Key_Node => Is_Greater_Key_Node);
108
109 ---------
110 -- "<" --
111 ---------
112
113 function "<" (Left, Right : Cursor) return Boolean is
114 begin
115 if Checks and then Left.Node = 0 then
116 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
117 end if;
118
119 if Checks and then Right.Node = 0 then
120 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
121 end if;
122
123 pragma Assert (Vet (Left.Container.all, Left.Node),
124 "Left cursor of ""<"" is bad");
125
126 pragma Assert (Vet (Right.Container.all, Right.Node),
127 "Right cursor of ""<"" is bad");
128
129 declare
130 LN : Node_Type renames Left.Container.Nodes (Left.Node);
131 RN : Node_Type renames Right.Container.Nodes (Right.Node);
132
133 begin
134 return LN.Key < RN.Key;
135 end;
136 end "<";
137
138 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
139 begin
140 if Checks and then Left.Node = 0 then
141 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
142 end if;
143
144 pragma Assert (Vet (Left.Container.all, Left.Node),
145 "Left cursor of ""<"" is bad");
146
147 declare
148 LN : Node_Type renames Left.Container.Nodes (Left.Node);
149
150 begin
151 return LN.Key < Right;
152 end;
153 end "<";
154
155 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
156 begin
157 if Checks and then Right.Node = 0 then
158 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
159 end if;
160
161 pragma Assert (Vet (Right.Container.all, Right.Node),
162 "Right cursor of ""<"" is bad");
163
164 declare
165 RN : Node_Type renames Right.Container.Nodes (Right.Node);
166
167 begin
168 return Left < RN.Key;
169 end;
170 end "<";
171
172 ---------
173 -- "=" --
174 ---------
175
176 function "=" (Left, Right : Map) return Boolean is
177 function Is_Equal_Node_Node (L, R : Node_Type) return Boolean;
178 pragma Inline (Is_Equal_Node_Node);
179
180 function Is_Equal is
181 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
182
183 ------------------------
184 -- Is_Equal_Node_Node --
185 ------------------------
186
187 function Is_Equal_Node_Node
188 (L, R : Node_Type) return Boolean is
189 begin
190 if L.Key < R.Key then
191 return False;
192
193 elsif R.Key < L.Key then
194 return False;
195
196 else
197 return L.Element = R.Element;
198 end if;
199 end Is_Equal_Node_Node;
200
201 -- Start of processing for "="
202
203 begin
204 return Is_Equal (Left, Right);
205 end "=";
206
207 ---------
208 -- ">" --
209 ---------
210
211 function ">" (Left, Right : Cursor) return Boolean is
212 begin
213 if Checks and then Left.Node = 0 then
214 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
215 end if;
216
217 if Checks and then Right.Node = 0 then
218 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
219 end if;
220
221 pragma Assert (Vet (Left.Container.all, Left.Node),
222 "Left cursor of "">"" is bad");
223
224 pragma Assert (Vet (Right.Container.all, Right.Node),
225 "Right cursor of "">"" is bad");
226
227 declare
228 LN : Node_Type renames Left.Container.Nodes (Left.Node);
229 RN : Node_Type renames Right.Container.Nodes (Right.Node);
230
231 begin
232 return RN.Key < LN.Key;
233 end;
234 end ">";
235
236 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
237 begin
238 if Checks and then Left.Node = 0 then
239 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
240 end if;
241
242 pragma Assert (Vet (Left.Container.all, Left.Node),
243 "Left cursor of "">"" is bad");
244
245 declare
246 LN : Node_Type renames Left.Container.Nodes (Left.Node);
247 begin
248 return Right < LN.Key;
249 end;
250 end ">";
251
252 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
253 begin
254 if Checks and then Right.Node = 0 then
255 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
256 end if;
257
258 pragma Assert (Vet (Right.Container.all, Right.Node),
259 "Right cursor of "">"" is bad");
260
261 declare
262 RN : Node_Type renames Right.Container.Nodes (Right.Node);
263
264 begin
265 return RN.Key < Left;
266 end;
267 end ">";
268
269 ------------
270 -- Assign --
271 ------------
272
273 procedure Assign (Target : in out Map; Source : Map) is
274 procedure Append_Element (Source_Node : Count_Type);
275
276 procedure Append_Elements is
277 new Tree_Operations.Generic_Iteration (Append_Element);
278
279 --------------------
280 -- Append_Element --
281 --------------------
282
283 procedure Append_Element (Source_Node : Count_Type) is
284 SN : Node_Type renames Source.Nodes (Source_Node);
285
286 procedure Set_Element (Node : in out Node_Type);
287 pragma Inline (Set_Element);
288
289 function New_Node return Count_Type;
290 pragma Inline (New_Node);
291
292 procedure Insert_Post is
293 new Key_Ops.Generic_Insert_Post (New_Node);
294
295 procedure Unconditional_Insert_Sans_Hint is
296 new Key_Ops.Generic_Unconditional_Insert (Insert_Post);
297
298 procedure Unconditional_Insert_Avec_Hint is
299 new Key_Ops.Generic_Unconditional_Insert_With_Hint
300 (Insert_Post,
301 Unconditional_Insert_Sans_Hint);
302
303 procedure Allocate is
304 new Tree_Operations.Generic_Allocate (Set_Element);
305
306 --------------
307 -- New_Node --
308 --------------
309
310 function New_Node return Count_Type is
311 Result : Count_Type;
312
313 begin
314 Allocate (Target, Result);
315 return Result;
316 end New_Node;
317
318 -----------------
319 -- Set_Element --
320 -----------------
321
322 procedure Set_Element (Node : in out Node_Type) is
323 begin
324 Node.Key := SN.Key;
325 Node.Element := SN.Element;
326 end Set_Element;
327
328 Target_Node : Count_Type;
329
330 -- Start of processing for Append_Element
331
332 begin
333 Unconditional_Insert_Avec_Hint
334 (Tree => Target,
335 Hint => 0,
336 Key => SN.Key,
337 Node => Target_Node);
338 end Append_Element;
339
340 -- Start of processing for Assign
341
342 begin
343 if Target'Address = Source'Address then
344 return;
345 end if;
346
347 if Checks and then Target.Capacity < Source.Length then
348 raise Capacity_Error
349 with "Target capacity is less than Source length";
350 end if;
351
352 Tree_Operations.Clear_Tree (Target);
353 Append_Elements (Source);
354 end Assign;
355
356 -------------
357 -- Ceiling --
358 -------------
359
360 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
361 Node : constant Count_Type := Key_Ops.Ceiling (Container, Key);
362
363 begin
364 if Node = 0 then
365 return No_Element;
366 end if;
367
368 return Cursor'(Container'Unrestricted_Access, Node);
369 end Ceiling;
370
371 -----------
372 -- Clear --
373 -----------
374
375 procedure Clear (Container : in out Map) is
376 begin
377 while not Container.Is_Empty loop
378 Container.Delete_Last;
379 end loop;
380 end Clear;
381
382 -----------
383 -- Color --
384 -----------
385
386 function Color (Node : Node_Type) return Color_Type is
387 begin
388 return Node.Color;
389 end Color;
390
391 ------------------------
392 -- Constant_Reference --
393 ------------------------
394
395 function Constant_Reference
396 (Container : aliased Map;
397 Position : Cursor) return Constant_Reference_Type
398 is
399 begin
400 if Checks and then Position.Container = null then
401 raise Constraint_Error with
402 "Position cursor has no element";
403 end if;
404
405 if Checks and then Position.Container /= Container'Unrestricted_Access
406 then
407 raise Program_Error with
408 "Position cursor designates wrong map";
409 end if;
410
411 pragma Assert (Vet (Container, Position.Node),
412 "Position cursor in Constant_Reference is bad");
413
414 declare
415 N : Node_Type renames Container.Nodes (Position.Node);
416 TC : constant Tamper_Counts_Access :=
417 Container.TC'Unrestricted_Access;
418 begin
419 return R : constant Constant_Reference_Type :=
420 (Element => N.Element'Access,
421 Control => (Controlled with TC))
422 do
423 Lock (TC.all);
424 end return;
425 end;
426 end Constant_Reference;
427
428 function Constant_Reference
429 (Container : aliased Map;
430 Key : Key_Type) return Constant_Reference_Type
431 is
432 Node : constant Count_Type := Key_Ops.Find (Container, Key);
433
434 begin
435 if Checks and then Node = 0 then
436 raise Constraint_Error with "key not in map";
437 end if;
438
439 declare
440 N : Node_Type renames Container.Nodes (Node);
441 TC : constant Tamper_Counts_Access :=
442 Container.TC'Unrestricted_Access;
443 begin
444 return R : constant Constant_Reference_Type :=
445 (Element => N.Element'Access,
446 Control => (Controlled with TC))
447 do
448 Lock (TC.all);
449 end return;
450 end;
451 end Constant_Reference;
452
453 --------------
454 -- Contains --
455 --------------
456
457 function Contains (Container : Map; Key : Key_Type) return Boolean is
458 begin
459 return Find (Container, Key) /= No_Element;
460 end Contains;
461
462 ----------
463 -- Copy --
464 ----------
465
466 function Copy (Source : Map; Capacity : Count_Type := 0) return Map is
467 C : Count_Type;
468
469 begin
470 if Capacity = 0 then
471 C := Source.Length;
472
473 elsif Capacity >= Source.Length then
474 C := Capacity;
475
476 elsif Checks then
477 raise Capacity_Error with "Capacity value too small";
478 end if;
479
480 return Target : Map (Capacity => C) do
481 Assign (Target => Target, Source => Source);
482 end return;
483 end Copy;
484
485 ------------
486 -- Delete --
487 ------------
488
489 procedure Delete (Container : in out Map; Position : in out Cursor) is
490 begin
491 if Checks and then Position.Node = 0 then
492 raise Constraint_Error with
493 "Position cursor of Delete equals No_Element";
494 end if;
495
496 if Checks and then Position.Container /= Container'Unrestricted_Access
497 then
498 raise Program_Error with
499 "Position cursor of Delete designates wrong map";
500 end if;
501
502 pragma Assert (Vet (Container, Position.Node),
503 "Position cursor of Delete is bad");
504
505 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
506 Tree_Operations.Free (Container, Position.Node);
507
508 Position := No_Element;
509 end Delete;
510
511 procedure Delete (Container : in out Map; Key : Key_Type) is
512 X : constant Count_Type := Key_Ops.Find (Container, Key);
513
514 begin
515 if Checks and then X = 0 then
516 raise Constraint_Error with "key not in map";
517 end if;
518
519 Tree_Operations.Delete_Node_Sans_Free (Container, X);
520 Tree_Operations.Free (Container, X);
521 end Delete;
522
523 ------------------
524 -- Delete_First --
525 ------------------
526
527 procedure Delete_First (Container : in out Map) is
528 X : constant Count_Type := Container.First;
529
530 begin
531 if X /= 0 then
532 Tree_Operations.Delete_Node_Sans_Free (Container, X);
533 Tree_Operations.Free (Container, X);
534 end if;
535 end Delete_First;
536
537 -----------------
538 -- Delete_Last --
539 -----------------
540
541 procedure Delete_Last (Container : in out Map) is
542 X : constant Count_Type := Container.Last;
543
544 begin
545 if X /= 0 then
546 Tree_Operations.Delete_Node_Sans_Free (Container, X);
547 Tree_Operations.Free (Container, X);
548 end if;
549 end Delete_Last;
550
551 -------------
552 -- Element --
553 -------------
554
555 function Element (Position : Cursor) return Element_Type is
556 begin
557 if Checks and then Position.Node = 0 then
558 raise Constraint_Error with
559 "Position cursor of function Element equals No_Element";
560 end if;
561
562 pragma Assert (Vet (Position.Container.all, Position.Node),
563 "Position cursor of function Element is bad");
564
565 return Position.Container.Nodes (Position.Node).Element;
566 end Element;
567
568 function Element (Container : Map; Key : Key_Type) return Element_Type is
569 Node : constant Count_Type := Key_Ops.Find (Container, Key);
570 begin
571 if Checks and then Node = 0 then
572 raise Constraint_Error with "key not in map";
573 end if;
574
575 return Container.Nodes (Node).Element;
576 end Element;
577
578 ---------------------
579 -- Equivalent_Keys --
580 ---------------------
581
582 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
583 begin
584 if Left < Right
585 or else Right < Left
586 then
587 return False;
588 else
589 return True;
590 end if;
591 end Equivalent_Keys;
592
593 -------------
594 -- Exclude --
595 -------------
596
597 procedure Exclude (Container : in out Map; Key : Key_Type) is
598 X : constant Count_Type := Key_Ops.Find (Container, Key);
599
600 begin
601 if X /= 0 then
602 Tree_Operations.Delete_Node_Sans_Free (Container, X);
603 Tree_Operations.Free (Container, X);
604 end if;
605 end Exclude;
606
607 --------------
608 -- Finalize --
609 --------------
610
611 procedure Finalize (Object : in out Iterator) is
612 begin
613 if Object.Container /= null then
614 Unbusy (Object.Container.TC);
615 end if;
616 end Finalize;
617
618 ----------
619 -- Find --
620 ----------
621
622 function Find (Container : Map; Key : Key_Type) return Cursor is
623 Node : constant Count_Type := Key_Ops.Find (Container, Key);
624 begin
625 if Node = 0 then
626 return No_Element;
627 else
628 return Cursor'(Container'Unrestricted_Access, Node);
629 end if;
630 end Find;
631
632 -----------
633 -- First --
634 -----------
635
636 function First (Container : Map) return Cursor is
637 begin
638 if Container.First = 0 then
639 return No_Element;
640 else
641 return Cursor'(Container'Unrestricted_Access, Container.First);
642 end if;
643 end First;
644
645 function First (Object : Iterator) return Cursor is
646 begin
647 -- The value of the iterator object's Node component influences the
648 -- behavior of the First (and Last) selector function.
649
650 -- When the Node component is 0, this means the iterator object was
651 -- constructed without a start expression, in which case the (forward)
652 -- iteration starts from the (logical) beginning of the entire sequence
653 -- of items (corresponding to Container.First, for a forward iterator).
654
655 -- Otherwise, this is iteration over a partial sequence of items. When
656 -- the Node component is positive, the iterator object was constructed
657 -- with a start expression, that specifies the position from which the
658 -- (forward) partial iteration begins.
659
660 if Object.Node = 0 then
661 return Bounded_Ordered_Maps.First (Object.Container.all);
662 else
663 return Cursor'(Object.Container, Object.Node);
664 end if;
665 end First;
666
667 -------------------
668 -- First_Element --
669 -------------------
670
671 function First_Element (Container : Map) return Element_Type is
672 begin
673 if Checks and then Container.First = 0 then
674 raise Constraint_Error with "map is empty";
675 end if;
676
677 return Container.Nodes (Container.First).Element;
678 end First_Element;
679
680 ---------------
681 -- First_Key --
682 ---------------
683
684 function First_Key (Container : Map) return Key_Type is
685 begin
686 if Checks and then Container.First = 0 then
687 raise Constraint_Error with "map is empty";
688 end if;
689
690 return Container.Nodes (Container.First).Key;
691 end First_Key;
692
693 -----------
694 -- Floor --
695 -----------
696
697 function Floor (Container : Map; Key : Key_Type) return Cursor is
698 Node : constant Count_Type := Key_Ops.Floor (Container, Key);
699 begin
700 if Node = 0 then
701 return No_Element;
702 else
703 return Cursor'(Container'Unrestricted_Access, Node);
704 end if;
705 end Floor;
706
707 ------------------------
708 -- Get_Element_Access --
709 ------------------------
710
711 function Get_Element_Access
712 (Position : Cursor) return not null Element_Access is
713 begin
714 return Position.Container.Nodes (Position.Node).Element'Access;
715 end Get_Element_Access;
716
717 -----------------
718 -- Has_Element --
719 -----------------
720
721 function Has_Element (Position : Cursor) return Boolean is
722 begin
723 return Position /= No_Element;
724 end Has_Element;
725
726 -------------
727 -- Include --
728 -------------
729
730 procedure Include
731 (Container : in out Map;
732 Key : Key_Type;
733 New_Item : Element_Type)
734 is
735 Position : Cursor;
736 Inserted : Boolean;
737
738 begin
739 Insert (Container, Key, New_Item, Position, Inserted);
740
741 if not Inserted then
742 TE_Check (Container.TC);
743
744 declare
745 N : Node_Type renames Container.Nodes (Position.Node);
746 begin
747 N.Key := Key;
748 N.Element := New_Item;
749 end;
750 end if;
751 end Include;
752
753 ------------
754 -- Insert --
755 ------------
756
757 procedure Insert
758 (Container : in out Map;
759 Key : Key_Type;
760 New_Item : Element_Type;
761 Position : out Cursor;
762 Inserted : out Boolean)
763 is
764 procedure Assign (Node : in out Node_Type);
765 pragma Inline (Assign);
766
767 function New_Node return Count_Type;
768 pragma Inline (New_Node);
769
770 procedure Insert_Post is
771 new Key_Ops.Generic_Insert_Post (New_Node);
772
773 procedure Insert_Sans_Hint is
774 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
775
776 procedure Allocate is
777 new Tree_Operations.Generic_Allocate (Assign);
778
779 ------------
780 -- Assign --
781 ------------
782
783 procedure Assign (Node : in out Node_Type) is
784 begin
785 Node.Key := Key;
786 Node.Element := New_Item;
787 end Assign;
788
789 --------------
790 -- New_Node --
791 --------------
792
793 function New_Node return Count_Type is
794 Result : Count_Type;
795 begin
796 Allocate (Container, Result);
797 return Result;
798 end New_Node;
799
800 -- Start of processing for Insert
801
802 begin
803 Insert_Sans_Hint
804 (Container,
805 Key,
806 Position.Node,
807 Inserted);
808
809 Position.Container := Container'Unrestricted_Access;
810 end Insert;
811
812 procedure Insert
813 (Container : in out Map;
814 Key : Key_Type;
815 New_Item : Element_Type)
816 is
817 Position : Cursor;
818 pragma Unreferenced (Position);
819
820 Inserted : Boolean;
821
822 begin
823 Insert (Container, Key, New_Item, Position, Inserted);
824
825 if Checks and then not Inserted then
826 raise Constraint_Error with "key already in map";
827 end if;
828 end Insert;
829
830 procedure Insert
831 (Container : in out Map;
832 Key : Key_Type;
833 Position : out Cursor;
834 Inserted : out Boolean)
835 is
836 procedure Assign (Node : in out Node_Type);
837 pragma Inline (Assign);
838
839 function New_Node return Count_Type;
840 pragma Inline (New_Node);
841
842 procedure Insert_Post is
843 new Key_Ops.Generic_Insert_Post (New_Node);
844
845 procedure Insert_Sans_Hint is
846 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
847
848 procedure Allocate is
849 new Tree_Operations.Generic_Allocate (Assign);
850
851 ------------
852 -- Assign --
853 ------------
854
855 procedure Assign (Node : in out Node_Type) is
856 pragma Warnings (Off);
857 Default_Initialized_Item : Element_Type;
858 pragma Unmodified (Default_Initialized_Item);
859 -- Default-initialized element (ok to reference, see below)
860
861 begin
862 Node.Key := Key;
863
864 -- There is no explicit element provided, but in an instance the element
865 -- type may be a scalar with a Default_Value aspect, or a composite type
866 -- with such a scalar component or with defaulted components, so insert
867 -- possibly initialized elements at the given position.
868
869 Node.Element := Default_Initialized_Item;
870 pragma Warnings (On);
871 end Assign;
872
873 --------------
874 -- New_Node --
875 --------------
876
877 function New_Node return Count_Type is
878 Result : Count_Type;
879 begin
880 Allocate (Container, Result);
881 return Result;
882 end New_Node;
883
884 -- Start of processing for Insert
885
886 begin
887 Insert_Sans_Hint
888 (Container,
889 Key,
890 Position.Node,
891 Inserted);
892
893 Position.Container := Container'Unrestricted_Access;
894 end Insert;
895
896 --------------
897 -- Is_Empty --
898 --------------
899
900 function Is_Empty (Container : Map) return Boolean is
901 begin
902 return Container.Length = 0;
903 end Is_Empty;
904
905 -------------------------
906 -- Is_Greater_Key_Node --
907 -------------------------
908
909 function Is_Greater_Key_Node
910 (Left : Key_Type;
911 Right : Node_Type) return Boolean
912 is
913 begin
914 -- Left > Right same as Right < Left
915
916 return Right.Key < Left;
917 end Is_Greater_Key_Node;
918
919 ----------------------
920 -- Is_Less_Key_Node --
921 ----------------------
922
923 function Is_Less_Key_Node
924 (Left : Key_Type;
925 Right : Node_Type) return Boolean
926 is
927 begin
928 return Left < Right.Key;
929 end Is_Less_Key_Node;
930
931 -------------
932 -- Iterate --
933 -------------
934
935 procedure Iterate
936 (Container : Map;
937 Process : not null access procedure (Position : Cursor))
938 is
939 procedure Process_Node (Node : Count_Type);
940 pragma Inline (Process_Node);
941
942 procedure Local_Iterate is
943 new Tree_Operations.Generic_Iteration (Process_Node);
944
945 ------------------
946 -- Process_Node --
947 ------------------
948
949 procedure Process_Node (Node : Count_Type) is
950 begin
951 Process (Cursor'(Container'Unrestricted_Access, Node));
952 end Process_Node;
953
954 Busy : With_Busy (Container.TC'Unrestricted_Access);
955
956 -- Start of processing for Iterate
957
958 begin
959 Local_Iterate (Container);
960 end Iterate;
961
962 function Iterate
963 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
964 is
965 begin
966 -- The value of the Node component influences the behavior of the First
967 -- and Last selector functions of the iterator object. When the Node
968 -- component is 0 (as is the case here), this means the iterator object
969 -- was constructed without a start expression. This is a complete
970 -- iterator, meaning that the iteration starts from the (logical)
971 -- beginning of the sequence of items.
972
973 -- Note: For a forward iterator, Container.First is the beginning, and
974 -- for a reverse iterator, Container.Last is the beginning.
975
976 return It : constant Iterator :=
977 (Limited_Controlled with
978 Container => Container'Unrestricted_Access,
979 Node => 0)
980 do
981 Busy (Container.TC'Unrestricted_Access.all);
982 end return;
983 end Iterate;
984
985 function Iterate
986 (Container : Map;
987 Start : Cursor)
988 return Map_Iterator_Interfaces.Reversible_Iterator'Class
989 is
990 begin
991 -- Iterator was defined to behave the same as for a complete iterator,
992 -- and iterate over the entire sequence of items. However, those
993 -- semantics were unintuitive and arguably error-prone (it is too easy
994 -- to accidentally create an endless loop), and so they were changed,
995 -- per the ARG meeting in Denver on 2011/11. However, there was no
996 -- consensus about what positive meaning this corner case should have,
997 -- and so it was decided to simply raise an exception. This does imply,
998 -- however, that it is not possible to use a partial iterator to specify
999 -- an empty sequence of items.
1000
1001 if Checks and then Start = No_Element then
1002 raise Constraint_Error with
1003 "Start position for iterator equals No_Element";
1004 end if;
1005
1006 if Checks and then Start.Container /= Container'Unrestricted_Access then
1007 raise Program_Error with
1008 "Start cursor of Iterate designates wrong map";
1009 end if;
1010
1011 pragma Assert (Vet (Container, Start.Node),
1012 "Start cursor of Iterate is bad");
1013
1014 -- The value of the Node component influences the behavior of the First
1015 -- and Last selector functions of the iterator object. When the Node
1016 -- component is positive (as is the case here), it means that this
1017 -- is a partial iteration, over a subset of the complete sequence of
1018 -- items. The iterator object was constructed with a start expression,
1019 -- indicating the position from which the iteration begins. (Note that
1020 -- the start position has the same value irrespective of whether this
1021 -- is a forward or reverse iteration.)
1022
1023 return It : constant Iterator :=
1024 (Limited_Controlled with
1025 Container => Container'Unrestricted_Access,
1026 Node => Start.Node)
1027 do
1028 Busy (Container.TC'Unrestricted_Access.all);
1029 end return;
1030 end Iterate;
1031
1032 ---------
1033 -- Key --
1034 ---------
1035
1036 function Key (Position : Cursor) return Key_Type is
1037 begin
1038 if Checks and then Position.Node = 0 then
1039 raise Constraint_Error with
1040 "Position cursor of function Key equals No_Element";
1041 end if;
1042
1043 pragma Assert (Vet (Position.Container.all, Position.Node),
1044 "Position cursor of function Key is bad");
1045
1046 return Position.Container.Nodes (Position.Node).Key;
1047 end Key;
1048
1049 ----------
1050 -- Last --
1051 ----------
1052
1053 function Last (Container : Map) return Cursor is
1054 begin
1055 if Container.Last = 0 then
1056 return No_Element;
1057 else
1058 return Cursor'(Container'Unrestricted_Access, Container.Last);
1059 end if;
1060 end Last;
1061
1062 function Last (Object : Iterator) return Cursor is
1063 begin
1064 -- The value of the iterator object's Node component influences the
1065 -- behavior of the Last (and First) selector function.
1066
1067 -- When the Node component is 0, this means the iterator object was
1068 -- constructed without a start expression, in which case the (reverse)
1069 -- iteration starts from the (logical) beginning of the entire sequence
1070 -- (corresponding to Container.Last, for a reverse iterator).
1071
1072 -- Otherwise, this is iteration over a partial sequence of items. When
1073 -- the Node component is positive, the iterator object was constructed
1074 -- with a start expression, that specifies the position from which the
1075 -- (reverse) partial iteration begins.
1076
1077 if Object.Node = 0 then
1078 return Bounded_Ordered_Maps.Last (Object.Container.all);
1079 else
1080 return Cursor'(Object.Container, Object.Node);
1081 end if;
1082 end Last;
1083
1084 ------------------
1085 -- Last_Element --
1086 ------------------
1087
1088 function Last_Element (Container : Map) return Element_Type is
1089 begin
1090 if Checks and then Container.Last = 0 then
1091 raise Constraint_Error with "map is empty";
1092 end if;
1093
1094 return Container.Nodes (Container.Last).Element;
1095 end Last_Element;
1096
1097 --------------
1098 -- Last_Key --
1099 --------------
1100
1101 function Last_Key (Container : Map) return Key_Type is
1102 begin
1103 if Checks and then Container.Last = 0 then
1104 raise Constraint_Error with "map is empty";
1105 end if;
1106
1107 return Container.Nodes (Container.Last).Key;
1108 end Last_Key;
1109
1110 ----------
1111 -- Left --
1112 ----------
1113
1114 function Left (Node : Node_Type) return Count_Type is
1115 begin
1116 return Node.Left;
1117 end Left;
1118
1119 ------------
1120 -- Length --
1121 ------------
1122
1123 function Length (Container : Map) return Count_Type is
1124 begin
1125 return Container.Length;
1126 end Length;
1127
1128 ----------
1129 -- Move --
1130 ----------
1131
1132 procedure Move (Target : in out Map; Source : in out Map) is
1133 begin
1134 if Target'Address = Source'Address then
1135 return;
1136 end if;
1137
1138 TC_Check (Source.TC);
1139
1140 Target.Assign (Source);
1141 Source.Clear;
1142 end Move;
1143
1144 ----------
1145 -- Next --
1146 ----------
1147
1148 procedure Next (Position : in out Cursor) is
1149 begin
1150 Position := Next (Position);
1151 end Next;
1152
1153 function Next (Position : Cursor) return Cursor is
1154 begin
1155 if Position = No_Element then
1156 return No_Element;
1157 end if;
1158
1159 pragma Assert (Vet (Position.Container.all, Position.Node),
1160 "Position cursor of Next is bad");
1161
1162 declare
1163 M : Map renames Position.Container.all;
1164
1165 Node : constant Count_Type :=
1166 Tree_Operations.Next (M, Position.Node);
1167
1168 begin
1169 if Node = 0 then
1170 return No_Element;
1171 end if;
1172
1173 return Cursor'(Position.Container, Node);
1174 end;
1175 end Next;
1176
1177 function Next
1178 (Object : Iterator;
1179 Position : Cursor) return Cursor
1180 is
1181 begin
1182 if Position.Container = null then
1183 return No_Element;
1184 end if;
1185
1186 if Checks and then Position.Container /= Object.Container then
1187 raise Program_Error with
1188 "Position cursor of Next designates wrong map";
1189 end if;
1190
1191 return Next (Position);
1192 end Next;
1193
1194 ------------
1195 -- Parent --
1196 ------------
1197
1198 function Parent (Node : Node_Type) return Count_Type is
1199 begin
1200 return Node.Parent;
1201 end Parent;
1202
1203 --------------
1204 -- Previous --
1205 --------------
1206
1207 procedure Previous (Position : in out Cursor) is
1208 begin
1209 Position := Previous (Position);
1210 end Previous;
1211
1212 function Previous (Position : Cursor) return Cursor is
1213 begin
1214 if Position = No_Element then
1215 return No_Element;
1216 end if;
1217
1218 pragma Assert (Vet (Position.Container.all, Position.Node),
1219 "Position cursor of Previous is bad");
1220
1221 declare
1222 M : Map renames Position.Container.all;
1223
1224 Node : constant Count_Type :=
1225 Tree_Operations.Previous (M, Position.Node);
1226
1227 begin
1228 if Node = 0 then
1229 return No_Element;
1230 end if;
1231
1232 return Cursor'(Position.Container, Node);
1233 end;
1234 end Previous;
1235
1236 function Previous
1237 (Object : Iterator;
1238 Position : Cursor) return Cursor
1239 is
1240 begin
1241 if Position.Container = null then
1242 return No_Element;
1243 end if;
1244
1245 if Checks and then Position.Container /= Object.Container then
1246 raise Program_Error with
1247 "Position cursor of Previous designates wrong map";
1248 end if;
1249
1250 return Previous (Position);
1251 end Previous;
1252
1253 ----------------------
1254 -- Pseudo_Reference --
1255 ----------------------
1256
1257 function Pseudo_Reference
1258 (Container : aliased Map'Class) return Reference_Control_Type
1259 is
1260 TC : constant Tamper_Counts_Access :=
1261 Container.TC'Unrestricted_Access;
1262 begin
1263 return R : constant Reference_Control_Type := (Controlled with TC) do
1264 Lock (TC.all);
1265 end return;
1266 end Pseudo_Reference;
1267
1268 -------------------
1269 -- Query_Element --
1270 -------------------
1271
1272 procedure Query_Element
1273 (Position : Cursor;
1274 Process : not null access procedure (Key : Key_Type;
1275 Element : Element_Type))
1276 is
1277 begin
1278 if Checks and then Position.Node = 0 then
1279 raise Constraint_Error with
1280 "Position cursor of Query_Element equals No_Element";
1281 end if;
1282
1283 pragma Assert (Vet (Position.Container.all, Position.Node),
1284 "Position cursor of Query_Element is bad");
1285
1286 declare
1287 M : Map renames Position.Container.all;
1288 N : Node_Type renames M.Nodes (Position.Node);
1289 Lock : With_Lock (M.TC'Unrestricted_Access);
1290 begin
1291 Process (N.Key, N.Element);
1292 end;
1293 end Query_Element;
1294
1295 ----------
1296 -- Read --
1297 ----------
1298
1299 procedure Read
1300 (Stream : not null access Root_Stream_Type'Class;
1301 Container : out Map)
1302 is
1303 procedure Read_Element (Node : in out Node_Type);
1304 pragma Inline (Read_Element);
1305
1306 procedure Allocate is
1307 new Tree_Operations.Generic_Allocate (Read_Element);
1308
1309 procedure Read_Elements is
1310 new Tree_Operations.Generic_Read (Allocate);
1311
1312 ------------------
1313 -- Read_Element --
1314 ------------------
1315
1316 procedure Read_Element (Node : in out Node_Type) is
1317 begin
1318 Key_Type'Read (Stream, Node.Key);
1319 Element_Type'Read (Stream, Node.Element);
1320 end Read_Element;
1321
1322 -- Start of processing for Read
1323
1324 begin
1325 Read_Elements (Stream, Container);
1326 end Read;
1327
1328 procedure Read
1329 (Stream : not null access Root_Stream_Type'Class;
1330 Item : out Cursor)
1331 is
1332 begin
1333 raise Program_Error with "attempt to stream map cursor";
1334 end Read;
1335
1336 procedure Read
1337 (Stream : not null access Root_Stream_Type'Class;
1338 Item : out Reference_Type)
1339 is
1340 begin
1341 raise Program_Error with "attempt to stream reference";
1342 end Read;
1343
1344 procedure Read
1345 (Stream : not null access Root_Stream_Type'Class;
1346 Item : out Constant_Reference_Type)
1347 is
1348 begin
1349 raise Program_Error with "attempt to stream reference";
1350 end Read;
1351
1352 ---------------
1353 -- Reference --
1354 ---------------
1355
1356 function Reference
1357 (Container : aliased in out Map;
1358 Position : Cursor) return Reference_Type
1359 is
1360 begin
1361 if Checks and then Position.Container = null then
1362 raise Constraint_Error with
1363 "Position cursor has no element";
1364 end if;
1365
1366 if Checks and then Position.Container /= Container'Unrestricted_Access
1367 then
1368 raise Program_Error with
1369 "Position cursor designates wrong map";
1370 end if;
1371
1372 pragma Assert (Vet (Container, Position.Node),
1373 "Position cursor in function Reference is bad");
1374
1375 declare
1376 N : Node_Type renames Container.Nodes (Position.Node);
1377 TC : constant Tamper_Counts_Access :=
1378 Container.TC'Unrestricted_Access;
1379 begin
1380 return R : constant Reference_Type :=
1381 (Element => N.Element'Access,
1382 Control => (Controlled with TC))
1383 do
1384 Lock (TC.all);
1385 end return;
1386 end;
1387 end Reference;
1388
1389 function Reference
1390 (Container : aliased in out Map;
1391 Key : Key_Type) return Reference_Type
1392 is
1393 Node : constant Count_Type := Key_Ops.Find (Container, Key);
1394
1395 begin
1396 if Checks and then Node = 0 then
1397 raise Constraint_Error with "key not in map";
1398 end if;
1399
1400 declare
1401 N : Node_Type renames Container.Nodes (Node);
1402 TC : constant Tamper_Counts_Access :=
1403 Container.TC'Unrestricted_Access;
1404 begin
1405 return R : constant Reference_Type :=
1406 (Element => N.Element'Access,
1407 Control => (Controlled with TC))
1408 do
1409 Lock (TC.all);
1410 end return;
1411 end;
1412 end Reference;
1413
1414 -------------
1415 -- Replace --
1416 -------------
1417
1418 procedure Replace
1419 (Container : in out Map;
1420 Key : Key_Type;
1421 New_Item : Element_Type)
1422 is
1423 Node : constant Count_Type := Key_Ops.Find (Container, Key);
1424
1425 begin
1426 if Checks and then Node = 0 then
1427 raise Constraint_Error with "key not in map";
1428 end if;
1429
1430 TE_Check (Container.TC);
1431
1432 declare
1433 N : Node_Type renames Container.Nodes (Node);
1434
1435 begin
1436 N.Key := Key;
1437 N.Element := New_Item;
1438 end;
1439 end Replace;
1440
1441 ---------------------
1442 -- Replace_Element --
1443 ---------------------
1444
1445 procedure Replace_Element
1446 (Container : in out Map;
1447 Position : Cursor;
1448 New_Item : Element_Type)
1449 is
1450 begin
1451 if Checks and then Position.Node = 0 then
1452 raise Constraint_Error with
1453 "Position cursor of Replace_Element equals No_Element";
1454 end if;
1455
1456 if Checks and then Position.Container /= Container'Unrestricted_Access
1457 then
1458 raise Program_Error with
1459 "Position cursor of Replace_Element designates wrong map";
1460 end if;
1461
1462 TE_Check (Container.TC);
1463
1464 pragma Assert (Vet (Container, Position.Node),
1465 "Position cursor of Replace_Element is bad");
1466
1467 Container.Nodes (Position.Node).Element := New_Item;
1468 end Replace_Element;
1469
1470 ---------------------
1471 -- Reverse_Iterate --
1472 ---------------------
1473
1474 procedure Reverse_Iterate
1475 (Container : Map;
1476 Process : not null access procedure (Position : Cursor))
1477 is
1478 procedure Process_Node (Node : Count_Type);
1479 pragma Inline (Process_Node);
1480
1481 procedure Local_Reverse_Iterate is
1482 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1483
1484 ------------------
1485 -- Process_Node --
1486 ------------------
1487
1488 procedure Process_Node (Node : Count_Type) is
1489 begin
1490 Process (Cursor'(Container'Unrestricted_Access, Node));
1491 end Process_Node;
1492
1493 Busy : With_Busy (Container.TC'Unrestricted_Access);
1494
1495 -- Start of processing for Reverse_Iterate
1496
1497 begin
1498 Local_Reverse_Iterate (Container);
1499 end Reverse_Iterate;
1500
1501 -----------
1502 -- Right --
1503 -----------
1504
1505 function Right (Node : Node_Type) return Count_Type is
1506 begin
1507 return Node.Right;
1508 end Right;
1509
1510 ---------------
1511 -- Set_Color --
1512 ---------------
1513
1514 procedure Set_Color
1515 (Node : in out Node_Type;
1516 Color : Color_Type)
1517 is
1518 begin
1519 Node.Color := Color;
1520 end Set_Color;
1521
1522 --------------
1523 -- Set_Left --
1524 --------------
1525
1526 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
1527 begin
1528 Node.Left := Left;
1529 end Set_Left;
1530
1531 ----------------
1532 -- Set_Parent --
1533 ----------------
1534
1535 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
1536 begin
1537 Node.Parent := Parent;
1538 end Set_Parent;
1539
1540 ---------------
1541 -- Set_Right --
1542 ---------------
1543
1544 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
1545 begin
1546 Node.Right := Right;
1547 end Set_Right;
1548
1549 --------------------
1550 -- Update_Element --
1551 --------------------
1552
1553 procedure Update_Element
1554 (Container : in out Map;
1555 Position : Cursor;
1556 Process : not null access procedure (Key : Key_Type;
1557 Element : in out Element_Type))
1558 is
1559 begin
1560 if Checks and then Position.Node = 0 then
1561 raise Constraint_Error with
1562 "Position cursor of Update_Element equals No_Element";
1563 end if;
1564
1565 if Checks and then Position.Container /= Container'Unrestricted_Access
1566 then
1567 raise Program_Error with
1568 "Position cursor of Update_Element designates wrong map";
1569 end if;
1570
1571 pragma Assert (Vet (Container, Position.Node),
1572 "Position cursor of Update_Element is bad");
1573
1574 declare
1575 N : Node_Type renames Container.Nodes (Position.Node);
1576 Lock : With_Lock (Container.TC'Unrestricted_Access);
1577 begin
1578 Process (N.Key, N.Element);
1579 end;
1580 end Update_Element;
1581
1582 -----------
1583 -- Write --
1584 -----------
1585
1586 procedure Write
1587 (Stream : not null access Root_Stream_Type'Class;
1588 Container : Map)
1589 is
1590 procedure Write_Node
1591 (Stream : not null access Root_Stream_Type'Class;
1592 Node : Node_Type);
1593 pragma Inline (Write_Node);
1594
1595 procedure Write_Nodes is
1596 new Tree_Operations.Generic_Write (Write_Node);
1597
1598 ----------------
1599 -- Write_Node --
1600 ----------------
1601
1602 procedure Write_Node
1603 (Stream : not null access Root_Stream_Type'Class;
1604 Node : Node_Type)
1605 is
1606 begin
1607 Key_Type'Write (Stream, Node.Key);
1608 Element_Type'Write (Stream, Node.Element);
1609 end Write_Node;
1610
1611 -- Start of processing for Write
1612
1613 begin
1614 Write_Nodes (Stream, Container);
1615 end Write;
1616
1617 procedure Write
1618 (Stream : not null access Root_Stream_Type'Class;
1619 Item : Cursor)
1620 is
1621 begin
1622 raise Program_Error with "attempt to stream map cursor";
1623 end Write;
1624
1625 procedure Write
1626 (Stream : not null access Root_Stream_Type'Class;
1627 Item : Reference_Type)
1628 is
1629 begin
1630 raise Program_Error with "attempt to stream reference";
1631 end Write;
1632
1633 procedure Write
1634 (Stream : not null access Root_Stream_Type'Class;
1635 Item : Constant_Reference_Type)
1636 is
1637 begin
1638 raise Program_Error with "attempt to stream reference";
1639 end Write;
1640
1641 end Ada.Containers.Bounded_Ordered_Maps;