1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . F O R M A L _ O R D E R E D _ M A P S --
9 -- Copyright (C) 2010-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
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. --
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 ------------------------------------------------------------------------------
28 with Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Operations
;
30 (Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Operations
);
32 with Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Keys
;
33 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Keys
);
35 with System
; use type System
.Address
;
37 package body Ada
.Containers
.Formal_Ordered_Maps
with
41 -----------------------------
42 -- Node Access Subprograms --
43 -----------------------------
45 -- These subprograms provide a functional interface to access fields
46 -- of a node, and a procedural interface for modifying these values.
49 (Node
: Node_Type
) return Ada
.Containers
.Red_Black_Trees
.Color_Type
;
50 pragma Inline
(Color
);
52 function Left_Son
(Node
: Node_Type
) return Count_Type
;
53 pragma Inline
(Left_Son
);
55 function Parent
(Node
: Node_Type
) return Count_Type
;
56 pragma Inline
(Parent
);
58 function Right_Son
(Node
: Node_Type
) return Count_Type
;
59 pragma Inline
(Right_Son
);
62 (Node
: in out Node_Type
;
63 Color
: Ada
.Containers
.Red_Black_Trees
.Color_Type
);
64 pragma Inline
(Set_Color
);
66 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
);
67 pragma Inline
(Set_Left
);
69 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
);
70 pragma Inline
(Set_Right
);
72 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
);
73 pragma Inline
(Set_Parent
);
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 -- All need comments ???
82 with procedure Set_Element
(Node
: in out Node_Type
);
83 procedure Generic_Allocate
84 (Tree
: in out Tree_Types
.Tree_Type
'Class;
85 Node
: out Count_Type
);
87 procedure Free
(Tree
: in out Map
; X
: Count_Type
);
89 function Is_Greater_Key_Node
91 Right
: Node_Type
) return Boolean;
92 pragma Inline
(Is_Greater_Key_Node
);
94 function Is_Less_Key_Node
96 Right
: Node_Type
) return Boolean;
97 pragma Inline
(Is_Less_Key_Node
);
99 --------------------------
100 -- Local Instantiations --
101 --------------------------
103 package Tree_Operations
is
104 new Red_Black_Trees
.Generic_Bounded_Operations
105 (Tree_Types
=> Tree_Types
,
112 new Red_Black_Trees
.Generic_Bounded_Keys
113 (Tree_Operations
=> Tree_Operations
,
114 Key_Type
=> Key_Type
,
115 Is_Less_Key_Node
=> Is_Less_Key_Node
,
116 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
122 function "=" (Left
, Right
: Map
) return Boolean is
128 if Length
(Left
) /= Length
(Right
) then
132 if Is_Empty
(Left
) then
136 Lst
:= Next
(Left
, Last
(Left
).Node
);
138 Node
:= First
(Left
).Node
;
139 while Node
/= Lst
loop
140 ENode
:= Find
(Right
, Left
.Nodes
(Node
).Key
).Node
;
143 Left
.Nodes
(Node
).Element
/= Right
.Nodes
(ENode
).Element
148 Node
:= Next
(Left
, Node
);
158 procedure Assign
(Target
: in out Map
; Source
: Map
) is
159 procedure Append_Element
(Source_Node
: Count_Type
);
161 procedure Append_Elements
is
162 new Tree_Operations
.Generic_Iteration
(Append_Element
);
168 procedure Append_Element
(Source_Node
: Count_Type
) is
169 SN
: Node_Type
renames Source
.Nodes
(Source_Node
);
171 procedure Set_Element
(Node
: in out Node_Type
);
172 pragma Inline
(Set_Element
);
174 function New_Node
return Count_Type
;
175 pragma Inline
(New_Node
);
177 procedure Insert_Post
is new Key_Ops
.Generic_Insert_Post
(New_Node
);
179 procedure Unconditional_Insert_Sans_Hint
is
180 new Key_Ops
.Generic_Unconditional_Insert
(Insert_Post
);
182 procedure Unconditional_Insert_Avec_Hint
is
183 new Key_Ops
.Generic_Unconditional_Insert_With_Hint
185 Unconditional_Insert_Sans_Hint
);
187 procedure Allocate
is new Generic_Allocate
(Set_Element
);
193 function New_Node
return Count_Type
is
196 Allocate
(Target
, Result
);
204 procedure Set_Element
(Node
: in out Node_Type
) is
207 Node
.Element
:= SN
.Element
;
210 Target_Node
: Count_Type
;
212 -- Start of processing for Append_Element
215 Unconditional_Insert_Avec_Hint
219 Node
=> Target_Node
);
222 -- Start of processing for Assign
225 if Target
'Address = Source
'Address then
229 if Target
.Capacity
< Length
(Source
) then
230 raise Storage_Error
with "not enough capacity"; -- SE or CE? ???
233 Tree_Operations
.Clear_Tree
(Target
);
234 Append_Elements
(Source
);
241 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
242 Node
: constant Count_Type
:= Key_Ops
.Ceiling
(Container
, Key
);
249 return (Node
=> Node
);
256 procedure Clear
(Container
: in out Map
) is
258 Tree_Operations
.Clear_Tree
(Container
);
265 function Color
(Node
: Node_Type
) return Color_Type
is
274 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
276 return Find
(Container
, Key
) /= No_Element
;
283 function Copy
(Source
: Map
; Capacity
: Count_Type
:= 0) return Map
is
284 Node
: Count_Type
:= 1;
288 if 0 < Capacity
and then Capacity
< Source
.Capacity
then
289 raise Capacity_Error
;
292 return Target
: Map
(Count_Type
'Max (Source
.Capacity
, Capacity
)) do
293 if Length
(Source
) > 0 then
294 Target
.Length
:= Source
.Length
;
295 Target
.Root
:= Source
.Root
;
296 Target
.First
:= Source
.First
;
297 Target
.Last
:= Source
.Last
;
298 Target
.Free
:= Source
.Free
;
300 while Node
<= Source
.Capacity
loop
301 Target
.Nodes
(Node
).Element
:=
302 Source
.Nodes
(Node
).Element
;
303 Target
.Nodes
(Node
).Key
:=
304 Source
.Nodes
(Node
).Key
;
305 Target
.Nodes
(Node
).Parent
:=
306 Source
.Nodes
(Node
).Parent
;
307 Target
.Nodes
(Node
).Left
:=
308 Source
.Nodes
(Node
).Left
;
309 Target
.Nodes
(Node
).Right
:=
310 Source
.Nodes
(Node
).Right
;
311 Target
.Nodes
(Node
).Color
:=
312 Source
.Nodes
(Node
).Color
;
313 Target
.Nodes
(Node
).Has_Element
:=
314 Source
.Nodes
(Node
).Has_Element
;
318 while Node
<= Target
.Capacity
loop
320 Formal_Ordered_Maps
.Free
(Tree
=> Target
, X
=> N
);
327 ---------------------
328 -- Current_To_Last --
329 ---------------------
331 function Current_To_Last
(Container
: Map
; Current
: Cursor
) return Map
is
332 Curs
: Cursor
:= First
(Container
);
333 C
: Map
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
337 if Curs
= No_Element
then
341 elsif Current
/= No_Element
and not Has_Element
(Container
, Current
) then
342 raise Constraint_Error
;
345 while Curs
.Node
/= Current
.Node
loop
348 Curs
:= Next
(Container
, (Node
=> Node
));
359 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) is
361 if not Has_Element
(Container
, Position
) then
362 raise Constraint_Error
with
363 "Position cursor of Delete has no element";
366 pragma Assert
(Vet
(Container
, Position
.Node
),
367 "Position cursor of Delete is bad");
369 Tree_Operations
.Delete_Node_Sans_Free
(Container
,
371 Formal_Ordered_Maps
.Free
(Container
, Position
.Node
);
374 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
375 X
: constant Node_Access
:= Key_Ops
.Find
(Container
, Key
);
379 raise Constraint_Error
with "key not in map";
382 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
383 Formal_Ordered_Maps
.Free
(Container
, X
);
390 procedure Delete_First
(Container
: in out Map
) is
391 X
: constant Node_Access
:= First
(Container
).Node
;
394 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
395 Formal_Ordered_Maps
.Free
(Container
, X
);
403 procedure Delete_Last
(Container
: in out Map
) is
404 X
: constant Node_Access
:= Last
(Container
).Node
;
407 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
408 Formal_Ordered_Maps
.Free
(Container
, X
);
416 function Element
(Container
: Map
; Position
: Cursor
) return Element_Type
is
418 if not Has_Element
(Container
, Position
) then
419 raise Constraint_Error
with
420 "Position cursor of function Element has no element";
423 pragma Assert
(Vet
(Container
, Position
.Node
),
424 "Position cursor of function Element is bad");
426 return Container
.Nodes
(Position
.Node
).Element
;
430 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
431 Node
: constant Node_Access
:= Find
(Container
, Key
).Node
;
435 raise Constraint_Error
with "key not in map";
438 return Container
.Nodes
(Node
).Element
;
441 ---------------------
442 -- Equivalent_Keys --
443 ---------------------
445 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
460 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
461 X
: constant Node_Access
:= Key_Ops
.Find
(Container
, Key
);
464 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
465 Formal_Ordered_Maps
.Free
(Container
, X
);
473 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
474 Node
: constant Count_Type
:= Key_Ops
.Find
(Container
, Key
);
481 return (Node
=> Node
);
488 function First
(Container
: Map
) return Cursor
is
490 if Length
(Container
) = 0 then
494 return (Node
=> Container
.First
);
501 function First_Element
(Container
: Map
) return Element_Type
is
503 if Is_Empty
(Container
) then
504 raise Constraint_Error
with "map is empty";
507 return Container
.Nodes
(First
(Container
).Node
).Element
;
514 function First_Key
(Container
: Map
) return Key_Type
is
516 if Is_Empty
(Container
) then
517 raise Constraint_Error
with "map is empty";
520 return Container
.Nodes
(First
(Container
).Node
).Key
;
523 -----------------------
524 -- First_To_Previous --
525 -----------------------
527 function First_To_Previous
529 Current
: Cursor
) return Map
531 Curs
: Cursor
:= Current
;
532 C
: Map
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
536 if Curs
= No_Element
then
539 elsif not Has_Element
(Container
, Curs
) then
540 raise Constraint_Error
;
543 while Curs
.Node
/= 0 loop
546 Curs
:= Next
(Container
, (Node
=> Node
));
551 end First_To_Previous
;
557 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
is
558 Node
: constant Count_Type
:= Key_Ops
.Floor
(Container
, Key
);
565 return (Node
=> Node
);
577 Tree
.Nodes
(X
).Has_Element
:= False;
578 Tree_Operations
.Free
(Tree
, X
);
581 ----------------------
582 -- Generic_Allocate --
583 ----------------------
585 procedure Generic_Allocate
586 (Tree
: in out Tree_Types
.Tree_Type
'Class;
587 Node
: out Count_Type
)
589 procedure Allocate
is
590 new Tree_Operations
.Generic_Allocate
(Set_Element
);
592 Allocate
(Tree
, Node
);
593 Tree
.Nodes
(Node
).Has_Element
:= True;
594 end Generic_Allocate
;
600 function Has_Element
(Container
: Map
; Position
: Cursor
) return Boolean is
602 if Position
.Node
= 0 then
606 return Container
.Nodes
(Position
.Node
).Has_Element
;
614 (Container
: in out Map
;
616 New_Item
: Element_Type
)
622 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
626 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
629 N
.Element
:= New_Item
;
635 (Container
: in out Map
;
637 New_Item
: Element_Type
;
638 Position
: out Cursor
;
639 Inserted
: out Boolean)
641 function New_Node
return Node_Access
;
644 procedure Insert_Post
is
645 new Key_Ops
.Generic_Insert_Post
(New_Node
);
647 procedure Insert_Sans_Hint
is
648 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
654 function New_Node
return Node_Access
is
655 procedure Initialize
(Node
: in out Node_Type
);
656 procedure Allocate_Node
is new Generic_Allocate
(Initialize
);
658 procedure Initialize
(Node
: in out Node_Type
) is
661 Node
.Element
:= New_Item
;
667 Allocate_Node
(Container
, X
);
671 -- Start of processing for Insert
682 (Container
: in out Map
;
684 New_Item
: Element_Type
)
690 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
693 raise Constraint_Error
with "key already in map";
701 function Is_Empty
(Container
: Map
) return Boolean is
703 return Length
(Container
) = 0;
706 -------------------------
707 -- Is_Greater_Key_Node --
708 -------------------------
710 function Is_Greater_Key_Node
712 Right
: Node_Type
) return Boolean
715 -- k > node same as node < k
717 return Right
.Key
< Left
;
718 end Is_Greater_Key_Node
;
720 ----------------------
721 -- Is_Less_Key_Node --
722 ----------------------
724 function Is_Less_Key_Node
726 Right
: Node_Type
) return Boolean
729 return Left
< Right
.Key
;
730 end Is_Less_Key_Node
;
736 function Key
(Container
: Map
; Position
: Cursor
) return Key_Type
is
738 if not Has_Element
(Container
, Position
) then
739 raise Constraint_Error
with
740 "Position cursor of function Key has no element";
743 pragma Assert
(Vet
(Container
, Position
.Node
),
744 "Position cursor of function Key is bad");
746 return Container
.Nodes
(Position
.Node
).Key
;
753 function Last
(Container
: Map
) return Cursor
is
755 if Length
(Container
) = 0 then
759 return (Node
=> Container
.Last
);
766 function Last_Element
(Container
: Map
) return Element_Type
is
768 if Is_Empty
(Container
) then
769 raise Constraint_Error
with "map is empty";
772 return Container
.Nodes
(Last
(Container
).Node
).Element
;
779 function Last_Key
(Container
: Map
) return Key_Type
is
781 if Is_Empty
(Container
) then
782 raise Constraint_Error
with "map is empty";
785 return Container
.Nodes
(Last
(Container
).Node
).Key
;
792 function Left_Son
(Node
: Node_Type
) return Count_Type
is
801 function Length
(Container
: Map
) return Count_Type
is
803 return Container
.Length
;
810 procedure Move
(Target
: in out Map
; Source
: in out Map
) is
811 NN
: Tree_Types
.Nodes_Type
renames Source
.Nodes
;
815 if Target
'Address = Source
'Address then
819 if Target
.Capacity
< Length
(Source
) then
820 raise Constraint_Error
with -- ???
821 "Source length exceeds Target capacity";
827 X
:= First
(Source
).Node
;
830 -- Here we insert a copy of the source element into the target, and
831 -- then delete the element from the source. Another possibility is
832 -- that delete it first (and hang onto its index), then insert it.
835 Insert
(Target
, NN
(X
).Key
, NN
(X
).Element
); -- optimize???
837 Tree_Operations
.Delete_Node_Sans_Free
(Source
, X
);
838 Formal_Ordered_Maps
.Free
(Source
, X
);
846 procedure Next
(Container
: Map
; Position
: in out Cursor
) is
848 Position
:= Next
(Container
, Position
);
851 function Next
(Container
: Map
; Position
: Cursor
) return Cursor
is
853 if Position
= No_Element
then
857 if not Has_Element
(Container
, Position
) then
858 raise Constraint_Error
;
861 pragma Assert
(Vet
(Container
, Position
.Node
),
862 "bad cursor in Next");
864 return (Node
=> Tree_Operations
.Next
(Container
, Position
.Node
));
871 function Overlap
(Left
, Right
: Map
) return Boolean is
873 if Length
(Left
) = 0 or Length
(Right
) = 0 then
878 L_Node
: Count_Type
:= First
(Left
).Node
;
879 R_Node
: Count_Type
:= First
(Right
).Node
;
880 L_Last
: constant Count_Type
:= Next
(Left
, Last
(Left
).Node
);
881 R_Last
: constant Count_Type
:= Next
(Right
, Last
(Right
).Node
);
884 if Left
'Address = Right
'Address then
890 or else R_Node
= R_Last
895 if Left
.Nodes
(L_Node
).Key
< Right
.Nodes
(R_Node
).Key
then
896 L_Node
:= Next
(Left
, L_Node
);
898 elsif Right
.Nodes
(R_Node
).Key
< Left
.Nodes
(L_Node
).Key
then
899 R_Node
:= Next
(Right
, R_Node
);
912 function Parent
(Node
: Node_Type
) return Count_Type
is
921 procedure Previous
(Container
: Map
; Position
: in out Cursor
) is
923 Position
:= Previous
(Container
, Position
);
926 function Previous
(Container
: Map
; Position
: Cursor
) return Cursor
is
928 if Position
= No_Element
then
932 if not Has_Element
(Container
, Position
) then
933 raise Constraint_Error
;
936 pragma Assert
(Vet
(Container
, Position
.Node
),
937 "bad cursor in Previous");
940 Node
: constant Count_Type
:=
941 Tree_Operations
.Previous
(Container
, Position
.Node
);
948 return (Node
=> Node
);
957 (Container
: in out Map
;
959 New_Item
: Element_Type
)
963 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
, Key
);
967 raise Constraint_Error
with "key not in map";
971 N
: Node_Type
renames Container
.Nodes
(Node
);
974 N
.Element
:= New_Item
;
979 ---------------------
980 -- Replace_Element --
981 ---------------------
983 procedure Replace_Element
984 (Container
: in out Map
;
986 New_Item
: Element_Type
)
989 if not Has_Element
(Container
, Position
) then
990 raise Constraint_Error
with
991 "Position cursor of Replace_Element has no element";
994 pragma Assert
(Vet
(Container
, Position
.Node
),
995 "Position cursor of Replace_Element is bad");
997 Container
.Nodes
(Position
.Node
).Element
:= New_Item
;
1004 function Right_Son
(Node
: Node_Type
) return Count_Type
is
1013 procedure Set_Color
(Node
: in out Node_Type
; Color
: Color_Type
) is
1015 Node
.Color
:= Color
;
1022 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
) is
1031 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
) is
1033 Node
.Parent
:= Parent
;
1040 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
) is
1042 Node
.Right
:= Right
;
1049 function Strict_Equal
(Left
, Right
: Map
) return Boolean is
1050 LNode
: Count_Type
:= First
(Left
).Node
;
1051 RNode
: Count_Type
:= First
(Right
).Node
;
1054 if Length
(Left
) /= Length
(Right
) then
1058 while LNode
= RNode
loop
1063 if Left
.Nodes
(LNode
).Element
/= Right
.Nodes
(RNode
).Element
1064 or else Left
.Nodes
(LNode
).Key
/= Right
.Nodes
(RNode
).Key
1069 LNode
:= Next
(Left
, LNode
);
1070 RNode
:= Next
(Right
, RNode
);
1076 end Ada
.Containers
.Formal_Ordered_Maps
;