1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FUNCTIONAL_BASE --
9 -- Copyright (C) 2016-2020, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
30 ------------------------------------------------------------------------------
33 with Ada.Unchecked_Deallocation;
35 package body Ada.Containers.Functional_Base with SPARK_Mode => Off is
37 function To_Count (Idx : Extended_Index) return Count_Type is
39 (Extended_Index'Pos (Idx) -
40 Extended_Index'Pos (Extended_Index'First)));
42 function To_Index (Position : Count_Type) return Extended_Index is
44 (Position + Extended_Index'Pos (Extended_Index'First)));
45 -- Conversion functions between Index_Type and Count_Type
47 function Find (C : Container; E : access Element_Type) return Count_Type;
48 -- Search a container C for an element equal to E.all, returning the
49 -- position in the underlying array.
51 procedure Resize (Base : Array_Base_Access);
52 -- Resize the underlying array if needed so that it can contain one more
59 function "=" (C1 : Container; C2 : Container) return Boolean is
61 if C1.Length /= C2.Length then
65 for I in 1 .. C1.Length loop
66 if C1.Base.Elements (I).all /= C2.Base.Elements (I).all then
78 function "<=" (C1 : Container; C2 : Container) return Boolean is
80 for I in 1 .. C1.Length loop
81 if Find (C2, C1.Base.Elements (I)) = 0 then
96 E : Element_Type) return Container
99 if To_Count (I) = C.Length + 1 and then C.Length = C.Base.Max_Length then
101 C.Base.Max_Length := C.Base.Max_Length + 1;
102 C.Base.Elements (C.Base.Max_Length) := new Element_Type'(E);
104 return Container'(Length => C.Base.Max_Length, Base => C.Base);
107 A : constant Array_Base_Access := Content_Init (C.Length);
110 A.Max_Length := C.Length + 1;
111 for J in 1 .. C.Length + 1 loop
112 if J /= To_Count (I) then
114 A.Elements (J) := C.Base.Elements (P);
116 A.Elements (J) := new Element_Type'(E);
120 return Container'(Length => A.Max_Length,
130 function Content_Init (L : Count_Type := 0) return Array_Base_Access
132 Max_Init : constant Count_Type := 100;
133 Size : constant Count_Type :=
134 (if L < Count_Type'Last - Max_Init then L + Max_Init
135 else Count_Type'Last);
136 Elements : constant Element_Array_Access :=
137 new Element_Array'(1 .. Size => <>);
139 return new Array_Base'(Max_Length => 0, Elements => Elements);
146 function Find (C : Container; E : access Element_Type) return Count_Type is
148 for I in 1 .. C.Length loop
149 if C.Base.Elements (I).all = E.all then
157 function Find (C : Container; E : Element_Type) return Extended_Index is
158 (To_Index (Find (C, E'Unrestricted_Access)));
164 function Get (C : Container; I : Index_Type) return Element_Type is
165 (C.Base.Elements (To_Count (I)).all);
171 function Intersection (C1 : Container; C2 : Container) return Container is
172 L : constant Count_Type := Num_Overlaps (C1, C2);
173 A : constant Array_Base_Access := Content_Init (L);
178 for I in 1 .. C1.Length loop
179 if Find (C2, C1.Base.Elements (I)) > 0 then
181 A.Elements (P) := C1.Base.Elements (I);
185 return Container'(Length => P, Base => A);
192 function Length (C : Container) return Count_Type is (C.Length);
193 ---------------------
195 ---------------------
197 function Num_Overlaps (C1 : Container; C2 : Container) return Count_Type is
201 for I in 1 .. C1.Length loop
202 if Find (C2, C1.Base.Elements (I)) > 0 then
214 function Remove (C : Container; I : Index_Type) return Container is
216 if To_Count (I) = C.Length then
217 return Container'(Length => C.Length - 1, Base => C.Base);
220 A : constant Array_Base_Access := Content_Init (C.Length - 1);
223 A.Max_Length := C.Length - 1;
224 for J in 1 .. C.Length loop
225 if J /= To_Count (I) then
227 A.Elements (P) := C.Base.Elements (J);
231 return Container'(Length => C.Length - 1, Base => A);
240 procedure Resize (Base : Array_Base_Access) is
242 if Base.Max_Length < Base.Elements'Length then
246 pragma Assert (Base.Max_Length = Base.Elements'Length);
248 if Base.Max_Length = Count_Type'Last then
249 raise Constraint_Error;
253 procedure Finalize is new Ada.Unchecked_Deallocation
254 (Object => Element_Array,
255 Name => Element_Array_Access_Base);
257 New_Length : constant Positive_Count_Type :=
258 (if Base.Max_Length > Count_Type'Last / 2 then Count_Type'Last
259 else 2 * Base.Max_Length);
260 Elements : constant Element_Array_Access :=
261 new Element_Array (1 .. New_Length);
262 Old_Elmts : Element_Array_Access_Base := Base.Elements;
264 Elements (1 .. Base.Max_Length) := Base.Elements.all;
265 Base.Elements := Elements;
266 Finalize (Old_Elmts);
277 E : Element_Type) return Container
279 Result : constant Container :=
280 Container'(Length => C.Length,
281 Base => Content_Init (C.Length));
284 Result.Base.Max_Length := C.Length;
285 Result.Base.Elements (1 .. C.Length) := C.Base.Elements (1 .. C.Length);
286 Result.Base.Elements (To_Count (I)) := new Element_Type'(E);
294 function Union (C1 : Container; C2 : Container) return Container is
295 N : constant Count_Type := Num_Overlaps (C1, C2);
298 -- if C2 is completely included in C1 then return C1
300 if N = Length (C2) then
304 -- else loop through C2 to find the remaining elements
307 L : constant Count_Type := Length (C1) - N + Length (C2);
308 A : constant Array_Base_Access := Content_Init (L);
309 P : Count_Type := Length (C1);
313 A.Elements (1 .. C1.Length) := C1.Base.Elements (1 .. C1.Length);
314 for I in 1 .. C2.Length loop
315 if Find (C1, C2.Base.Elements (I)) = 0 then
317 A.Elements (P) := C2.Base.Elements (I);
321 return Container'(Length => L, Base => A);
325 end Ada.Containers.Functional_Base;