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-2013, 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
is
39 -----------------------------
40 -- Node Access Subprograms --
41 -----------------------------
43 -- These subprograms provide a functional interface to access fields
44 -- of a node, and a procedural interface for modifying these values.
47 (Node
: Node_Type
) return Ada
.Containers
.Red_Black_Trees
.Color_Type
;
48 pragma Inline
(Color
);
50 function Left_Son
(Node
: Node_Type
) return Count_Type
;
51 pragma Inline
(Left_Son
);
53 function Parent
(Node
: Node_Type
) return Count_Type
;
54 pragma Inline
(Parent
);
56 function Right_Son
(Node
: Node_Type
) return Count_Type
;
57 pragma Inline
(Right_Son
);
60 (Node
: in out Node_Type
;
61 Color
: Ada
.Containers
.Red_Black_Trees
.Color_Type
);
62 pragma Inline
(Set_Color
);
64 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
);
65 pragma Inline
(Set_Left
);
67 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
);
68 pragma Inline
(Set_Right
);
70 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
);
71 pragma Inline
(Set_Parent
);
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 -- All need comments ???
80 with procedure Set_Element
(Node
: in out Node_Type
);
81 procedure Generic_Allocate
82 (Tree
: in out Tree_Types
.Tree_Type
'Class;
83 Node
: out Count_Type
);
85 procedure Free
(Tree
: in out Map
; X
: Count_Type
);
87 function Is_Greater_Key_Node
89 Right
: Node_Type
) return Boolean;
90 pragma Inline
(Is_Greater_Key_Node
);
92 function Is_Less_Key_Node
94 Right
: Node_Type
) return Boolean;
95 pragma Inline
(Is_Less_Key_Node
);
97 --------------------------
98 -- Local Instantiations --
99 --------------------------
101 package Tree_Operations
is
102 new Red_Black_Trees
.Generic_Bounded_Operations
103 (Tree_Types
=> Tree_Types
,
110 new Red_Black_Trees
.Generic_Bounded_Keys
111 (Tree_Operations
=> Tree_Operations
,
112 Key_Type
=> Key_Type
,
113 Is_Less_Key_Node
=> Is_Less_Key_Node
,
114 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
120 function "=" (Left
, Right
: Map
) return Boolean is
126 if Length
(Left
) /= Length
(Right
) then
130 if Is_Empty
(Left
) then
134 Lst
:= Next
(Left
, Last
(Left
).Node
);
136 Node
:= First
(Left
).Node
;
137 while Node
/= Lst
loop
138 ENode
:= Find
(Right
, Left
.Nodes
(Node
).Key
).Node
;
141 Left
.Nodes
(Node
).Element
/= Right
.Nodes
(ENode
).Element
146 Node
:= Next
(Left
, Node
);
156 procedure Assign
(Target
: in out Map
; Source
: Map
) is
157 procedure Append_Element
(Source_Node
: Count_Type
);
159 procedure Append_Elements
is
160 new Tree_Operations
.Generic_Iteration
(Append_Element
);
166 procedure Append_Element
(Source_Node
: Count_Type
) is
167 SN
: Node_Type
renames Source
.Nodes
(Source_Node
);
169 procedure Set_Element
(Node
: in out Node_Type
);
170 pragma Inline
(Set_Element
);
172 function New_Node
return Count_Type
;
173 pragma Inline
(New_Node
);
175 procedure Insert_Post
is new Key_Ops
.Generic_Insert_Post
(New_Node
);
177 procedure Unconditional_Insert_Sans_Hint
is
178 new Key_Ops
.Generic_Unconditional_Insert
(Insert_Post
);
180 procedure Unconditional_Insert_Avec_Hint
is
181 new Key_Ops
.Generic_Unconditional_Insert_With_Hint
183 Unconditional_Insert_Sans_Hint
);
185 procedure Allocate
is new Generic_Allocate
(Set_Element
);
191 function New_Node
return Count_Type
is
194 Allocate
(Target
, Result
);
202 procedure Set_Element
(Node
: in out Node_Type
) is
205 Node
.Element
:= SN
.Element
;
208 Target_Node
: Count_Type
;
210 -- Start of processing for Append_Element
213 Unconditional_Insert_Avec_Hint
217 Node
=> Target_Node
);
220 -- Start of processing for Assign
223 if Target
'Address = Source
'Address then
227 if Target
.Capacity
< Length
(Source
) then
228 raise Storage_Error
with "not enough capacity"; -- SE or CE? ???
231 Tree_Operations
.Clear_Tree
(Target
);
232 Append_Elements
(Source
);
239 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
240 Node
: constant Count_Type
:= Key_Ops
.Ceiling
(Container
, Key
);
247 return (Node
=> Node
);
254 procedure Clear
(Container
: in out Map
) is
256 Tree_Operations
.Clear_Tree
(Container
);
263 function Color
(Node
: Node_Type
) return Color_Type
is
272 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean is
274 return Find
(Container
, Key
) /= No_Element
;
281 function Copy
(Source
: Map
; Capacity
: Count_Type
:= 0) return Map
is
282 Node
: Count_Type
:= 1;
286 if 0 < Capacity
and then Capacity
< Source
.Capacity
then
287 raise Capacity_Error
;
290 return Target
: Map
(Count_Type
'Max (Source
.Capacity
, Capacity
)) do
291 if Length
(Source
) > 0 then
292 Target
.Length
:= Source
.Length
;
293 Target
.Root
:= Source
.Root
;
294 Target
.First
:= Source
.First
;
295 Target
.Last
:= Source
.Last
;
296 Target
.Free
:= Source
.Free
;
298 while Node
<= Source
.Capacity
loop
299 Target
.Nodes
(Node
).Element
:=
300 Source
.Nodes
(Node
).Element
;
301 Target
.Nodes
(Node
).Key
:=
302 Source
.Nodes
(Node
).Key
;
303 Target
.Nodes
(Node
).Parent
:=
304 Source
.Nodes
(Node
).Parent
;
305 Target
.Nodes
(Node
).Left
:=
306 Source
.Nodes
(Node
).Left
;
307 Target
.Nodes
(Node
).Right
:=
308 Source
.Nodes
(Node
).Right
;
309 Target
.Nodes
(Node
).Color
:=
310 Source
.Nodes
(Node
).Color
;
311 Target
.Nodes
(Node
).Has_Element
:=
312 Source
.Nodes
(Node
).Has_Element
;
316 while Node
<= Target
.Capacity
loop
318 Formal_Ordered_Maps
.Free
(Tree
=> Target
, X
=> N
);
325 ---------------------
326 -- Current_To_Last --
327 ---------------------
329 function Current_To_Last
(Container
: Map
; Current
: Cursor
) return Map
is
330 Curs
: Cursor
:= First
(Container
);
331 C
: Map
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
335 if Curs
= No_Element
then
339 elsif Current
/= No_Element
and not Has_Element
(Container
, Current
) then
340 raise Constraint_Error
;
343 while Curs
.Node
/= Current
.Node
loop
346 Curs
:= Next
(Container
, (Node
=> Node
));
357 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) is
359 if not Has_Element
(Container
, Position
) then
360 raise Constraint_Error
with
361 "Position cursor of Delete has no element";
364 pragma Assert
(Vet
(Container
, Position
.Node
),
365 "Position cursor of Delete is bad");
367 Tree_Operations
.Delete_Node_Sans_Free
(Container
,
369 Formal_Ordered_Maps
.Free
(Container
, Position
.Node
);
372 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
373 X
: constant Node_Access
:= Key_Ops
.Find
(Container
, Key
);
377 raise Constraint_Error
with "key not in map";
380 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
381 Formal_Ordered_Maps
.Free
(Container
, X
);
388 procedure Delete_First
(Container
: in out Map
) is
389 X
: constant Node_Access
:= First
(Container
).Node
;
392 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
393 Formal_Ordered_Maps
.Free
(Container
, X
);
401 procedure Delete_Last
(Container
: in out Map
) is
402 X
: constant Node_Access
:= Last
(Container
).Node
;
405 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
406 Formal_Ordered_Maps
.Free
(Container
, X
);
414 function Element
(Container
: Map
; Position
: Cursor
) return Element_Type
is
416 if not Has_Element
(Container
, Position
) then
417 raise Constraint_Error
with
418 "Position cursor of function Element has no element";
421 pragma Assert
(Vet
(Container
, Position
.Node
),
422 "Position cursor of function Element is bad");
424 return Container
.Nodes
(Position
.Node
).Element
;
428 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
429 Node
: constant Node_Access
:= Find
(Container
, Key
).Node
;
433 raise Constraint_Error
with "key not in map";
436 return Container
.Nodes
(Node
).Element
;
439 ---------------------
440 -- Equivalent_Keys --
441 ---------------------
443 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
458 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
459 X
: constant Node_Access
:= Key_Ops
.Find
(Container
, Key
);
462 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
463 Formal_Ordered_Maps
.Free
(Container
, X
);
471 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
472 Node
: constant Count_Type
:= Key_Ops
.Find
(Container
, Key
);
479 return (Node
=> Node
);
486 function First
(Container
: Map
) return Cursor
is
488 if Length
(Container
) = 0 then
492 return (Node
=> Container
.First
);
499 function First_Element
(Container
: Map
) return Element_Type
is
501 if Is_Empty
(Container
) then
502 raise Constraint_Error
with "map is empty";
505 return Container
.Nodes
(First
(Container
).Node
).Element
;
512 function First_Key
(Container
: Map
) return Key_Type
is
514 if Is_Empty
(Container
) then
515 raise Constraint_Error
with "map is empty";
518 return Container
.Nodes
(First
(Container
).Node
).Key
;
521 -----------------------
522 -- First_To_Previous --
523 -----------------------
525 function First_To_Previous
527 Current
: Cursor
) return Map
529 Curs
: Cursor
:= Current
;
530 C
: Map
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
534 if Curs
= No_Element
then
537 elsif not Has_Element
(Container
, Curs
) then
538 raise Constraint_Error
;
541 while Curs
.Node
/= 0 loop
544 Curs
:= Next
(Container
, (Node
=> Node
));
549 end First_To_Previous
;
555 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
is
556 Node
: constant Count_Type
:= Key_Ops
.Floor
(Container
, Key
);
563 return (Node
=> Node
);
575 Tree
.Nodes
(X
).Has_Element
:= False;
576 Tree_Operations
.Free
(Tree
, X
);
579 ----------------------
580 -- Generic_Allocate --
581 ----------------------
583 procedure Generic_Allocate
584 (Tree
: in out Tree_Types
.Tree_Type
'Class;
585 Node
: out Count_Type
)
587 procedure Allocate
is
588 new Tree_Operations
.Generic_Allocate
(Set_Element
);
590 Allocate
(Tree
, Node
);
591 Tree
.Nodes
(Node
).Has_Element
:= True;
592 end Generic_Allocate
;
598 function Has_Element
(Container
: Map
; Position
: Cursor
) return Boolean is
600 if Position
.Node
= 0 then
604 return Container
.Nodes
(Position
.Node
).Has_Element
;
612 (Container
: in out Map
;
614 New_Item
: Element_Type
)
620 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
624 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
627 N
.Element
:= New_Item
;
633 (Container
: in out Map
;
635 New_Item
: Element_Type
;
636 Position
: out Cursor
;
637 Inserted
: out Boolean)
639 function New_Node
return Node_Access
;
642 procedure Insert_Post
is
643 new Key_Ops
.Generic_Insert_Post
(New_Node
);
645 procedure Insert_Sans_Hint
is
646 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
652 function New_Node
return Node_Access
is
653 procedure Initialize
(Node
: in out Node_Type
);
654 procedure Allocate_Node
is new Generic_Allocate
(Initialize
);
656 procedure Initialize
(Node
: in out Node_Type
) is
659 Node
.Element
:= New_Item
;
665 Allocate_Node
(Container
, X
);
669 -- Start of processing for Insert
680 (Container
: in out Map
;
682 New_Item
: Element_Type
)
688 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
691 raise Constraint_Error
with "key already in map";
699 function Is_Empty
(Container
: Map
) return Boolean is
701 return Length
(Container
) = 0;
704 -------------------------
705 -- Is_Greater_Key_Node --
706 -------------------------
708 function Is_Greater_Key_Node
710 Right
: Node_Type
) return Boolean
713 -- k > node same as node < k
715 return Right
.Key
< Left
;
716 end Is_Greater_Key_Node
;
718 ----------------------
719 -- Is_Less_Key_Node --
720 ----------------------
722 function Is_Less_Key_Node
724 Right
: Node_Type
) return Boolean
727 return Left
< Right
.Key
;
728 end Is_Less_Key_Node
;
734 function Key
(Container
: Map
; Position
: Cursor
) return Key_Type
is
736 if not Has_Element
(Container
, Position
) then
737 raise Constraint_Error
with
738 "Position cursor of function Key has no element";
741 pragma Assert
(Vet
(Container
, Position
.Node
),
742 "Position cursor of function Key is bad");
744 return Container
.Nodes
(Position
.Node
).Key
;
751 function Last
(Container
: Map
) return Cursor
is
753 if Length
(Container
) = 0 then
757 return (Node
=> Container
.Last
);
764 function Last_Element
(Container
: Map
) return Element_Type
is
766 if Is_Empty
(Container
) then
767 raise Constraint_Error
with "map is empty";
770 return Container
.Nodes
(Last
(Container
).Node
).Element
;
777 function Last_Key
(Container
: Map
) return Key_Type
is
779 if Is_Empty
(Container
) then
780 raise Constraint_Error
with "map is empty";
783 return Container
.Nodes
(Last
(Container
).Node
).Key
;
790 function Left_Son
(Node
: Node_Type
) return Count_Type
is
799 function Length
(Container
: Map
) return Count_Type
is
801 return Container
.Length
;
808 procedure Move
(Target
: in out Map
; Source
: in out Map
) is
809 NN
: Tree_Types
.Nodes_Type
renames Source
.Nodes
;
813 if Target
'Address = Source
'Address then
817 if Target
.Capacity
< Length
(Source
) then
818 raise Constraint_Error
with -- ???
819 "Source length exceeds Target capacity";
825 X
:= First
(Source
).Node
;
828 -- Here we insert a copy of the source element into the target, and
829 -- then delete the element from the source. Another possibility is
830 -- that delete it first (and hang onto its index), then insert it.
833 Insert
(Target
, NN
(X
).Key
, NN
(X
).Element
); -- optimize???
835 Tree_Operations
.Delete_Node_Sans_Free
(Source
, X
);
836 Formal_Ordered_Maps
.Free
(Source
, X
);
844 procedure Next
(Container
: Map
; Position
: in out Cursor
) is
846 Position
:= Next
(Container
, Position
);
849 function Next
(Container
: Map
; Position
: Cursor
) return Cursor
is
851 if Position
= No_Element
then
855 if not Has_Element
(Container
, Position
) then
856 raise Constraint_Error
;
859 pragma Assert
(Vet
(Container
, Position
.Node
),
860 "bad cursor in Next");
862 return (Node
=> Tree_Operations
.Next
(Container
, Position
.Node
));
869 function Overlap
(Left
, Right
: Map
) return Boolean is
871 if Length
(Left
) = 0 or Length
(Right
) = 0 then
876 L_Node
: Count_Type
:= First
(Left
).Node
;
877 R_Node
: Count_Type
:= First
(Right
).Node
;
878 L_Last
: constant Count_Type
:= Next
(Left
, Last
(Left
).Node
);
879 R_Last
: constant Count_Type
:= Next
(Right
, Last
(Right
).Node
);
882 if Left
'Address = Right
'Address then
888 or else R_Node
= R_Last
893 if Left
.Nodes
(L_Node
).Key
< Right
.Nodes
(R_Node
).Key
then
894 L_Node
:= Next
(Left
, L_Node
);
896 elsif Right
.Nodes
(R_Node
).Key
< Left
.Nodes
(L_Node
).Key
then
897 R_Node
:= Next
(Right
, R_Node
);
910 function Parent
(Node
: Node_Type
) return Count_Type
is
919 procedure Previous
(Container
: Map
; Position
: in out Cursor
) is
921 Position
:= Previous
(Container
, Position
);
924 function Previous
(Container
: Map
; Position
: Cursor
) return Cursor
is
926 if Position
= No_Element
then
930 if not Has_Element
(Container
, Position
) then
931 raise Constraint_Error
;
934 pragma Assert
(Vet
(Container
, Position
.Node
),
935 "bad cursor in Previous");
938 Node
: constant Count_Type
:=
939 Tree_Operations
.Previous
(Container
, Position
.Node
);
946 return (Node
=> Node
);
955 (Container
: in out Map
;
957 New_Item
: Element_Type
)
961 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
, Key
);
965 raise Constraint_Error
with "key not in map";
969 N
: Node_Type
renames Container
.Nodes
(Node
);
972 N
.Element
:= New_Item
;
977 ---------------------
978 -- Replace_Element --
979 ---------------------
981 procedure Replace_Element
982 (Container
: in out Map
;
984 New_Item
: Element_Type
)
987 if not Has_Element
(Container
, Position
) then
988 raise Constraint_Error
with
989 "Position cursor of Replace_Element has no element";
992 pragma Assert
(Vet
(Container
, Position
.Node
),
993 "Position cursor of Replace_Element is bad");
995 Container
.Nodes
(Position
.Node
).Element
:= New_Item
;
1002 function Right_Son
(Node
: Node_Type
) return Count_Type
is
1011 procedure Set_Color
(Node
: in out Node_Type
; Color
: Color_Type
) is
1013 Node
.Color
:= Color
;
1020 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
) is
1029 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
) is
1031 Node
.Parent
:= Parent
;
1038 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
) is
1040 Node
.Right
:= Right
;
1047 function Strict_Equal
(Left
, Right
: Map
) return Boolean is
1048 LNode
: Count_Type
:= First
(Left
).Node
;
1049 RNode
: Count_Type
:= First
(Right
).Node
;
1052 if Length
(Left
) /= Length
(Right
) then
1056 while LNode
= RNode
loop
1061 if Left
.Nodes
(LNode
).Element
/= Right
.Nodes
(RNode
).Element
1062 or else Left
.Nodes
(LNode
).Key
/= Right
.Nodes
(RNode
).Key
1067 LNode
:= Next
(Left
, LNode
);
1068 RNode
:= Next
(Right
, RNode
);
1074 end Ada
.Containers
.Formal_Ordered_Maps
;