| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . C O N T A I N E R S . I N D E F I N I T E _ V E C T O R S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2004-2025, 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.Generic_Array_Sort; |
| 31 |
with Ada.Unchecked_Deallocation; |
| 32 |
|
| 33 |
with System; use type System.Address; |
| 34 |
with System.Put_Images; |
| 35 |
|
| 36 |
package body Ada.Containers.Indefinite_Vectors with |
| 37 |
SPARK_Mode => Off |
| 38 |
is |
| 39 |
|
| 40 |
pragma Warnings (Off, "variable ""Busy*"" is not referenced"); |
| 41 |
pragma Warnings (Off, "variable ""Lock*"" is not referenced"); |
| 42 |
-- See comment in Ada.Containers.Helpers |
| 43 |
|
| 44 |
procedure Free is |
| 45 |
new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access); |
| 46 |
|
| 47 |
procedure Free is |
| 48 |
new Ada.Unchecked_Deallocation (Element_Type, Element_Access); |
| 49 |
|
| 50 |
procedure Append_Slow_Path |
| 51 |
(Container : in out Vector; |
| 52 |
New_Item : Element_Type; |
| 53 |
Count : Count_Type); |
| 54 |
-- This is the slow path for Append. This is split out to minimize the size |
| 55 |
-- of Append, because we have Inline (Append). |
| 56 |
|
| 57 |
--------- |
| 58 |
-- "&" -- |
| 59 |
--------- |
| 60 |
|
| 61 |
-- We decide that the capacity of the result of "&" is the minimum needed |
| 62 |
-- -- the sum of the lengths of the vector parameters. We could decide to |
| 63 |
-- make it larger, but we have no basis for knowing how much larger, so we |
| 64 |
-- just allocate the minimum amount of storage. |
| 65 |
|
| 66 |
function "&" (Left, Right : Vector) return Vector is |
| 67 |
begin |
| 68 |
return Result : Vector do |
| 69 |
Reserve_Capacity (Result, Length (Left) + Length (Right)); |
| 70 |
Append_Vector (Result, Left); |
| 71 |
Append_Vector (Result, Right); |
| 72 |
end return; |
| 73 |
end "&"; |
| 74 |
|
| 75 |
function "&" (Left : Vector; Right : Element_Type) return Vector is |
| 76 |
begin |
| 77 |
return Result : Vector do |
| 78 |
Reserve_Capacity (Result, Length (Left) + 1); |
| 79 |
Append_Vector (Result, Left); |
| 80 |
Append (Result, Right); |
| 81 |
end return; |
| 82 |
end "&"; |
| 83 |
|
| 84 |
function "&" (Left : Element_Type; Right : Vector) return Vector is |
| 85 |
begin |
| 86 |
return Result : Vector do |
| 87 |
Reserve_Capacity (Result, 1 + Length (Right)); |
| 88 |
Append (Result, Left); |
| 89 |
Append_Vector (Result, Right); |
| 90 |
end return; |
| 91 |
end "&"; |
| 92 |
|
| 93 |
function "&" (Left, Right : Element_Type) return Vector is |
| 94 |
begin |
| 95 |
return Result : Vector do |
| 96 |
Reserve_Capacity (Result, 1 + 1); |
| 97 |
Append (Result, Left); |
| 98 |
Append (Result, Right); |
| 99 |
end return; |
| 100 |
end "&"; |
| 101 |
|
| 102 |
--------- |
| 103 |
-- "=" -- |
| 104 |
--------- |
| 105 |
|
| 106 |
overriding function "=" (Left, Right : Vector) return Boolean is |
| 107 |
begin |
| 108 |
if Left.Last /= Right.Last then |
| 109 |
return False; |
| 110 |
end if; |
| 111 |
|
| 112 |
if Left.Length = 0 then |
| 113 |
return True; |
| 114 |
end if; |
| 115 |
|
| 116 |
declare |
| 117 |
-- Per AI05-0022, the container implementation is required to detect |
| 118 |
-- element tampering by a generic actual subprogram. |
| 119 |
|
| 120 |
Lock_Left : With_Lock (Left.TC'Unrestricted_Access); |
| 121 |
Lock_Right : With_Lock (Right.TC'Unrestricted_Access); |
| 122 |
begin |
| 123 |
for J in Index_Type range Index_Type'First .. Left.Last loop |
| 124 |
if Left.Elements.EA (J) = null then |
| 125 |
if Right.Elements.EA (J) /= null then |
| 126 |
return False; |
| 127 |
end if; |
| 128 |
|
| 129 |
elsif Right.Elements.EA (J) = null then |
| 130 |
return False; |
| 131 |
|
| 132 |
elsif Left.Elements.EA (J).all /= Right.Elements.EA (J).all then |
| 133 |
return False; |
| 134 |
end if; |
| 135 |
end loop; |
| 136 |
end; |
| 137 |
|
| 138 |
return True; |
| 139 |
end "="; |
| 140 |
|
| 141 |
------------ |
| 142 |
-- Adjust -- |
| 143 |
------------ |
| 144 |
|
| 145 |
procedure Adjust (Container : in out Vector) is |
| 146 |
begin |
| 147 |
-- If the counts are nonzero, execution is technically erroneous, but |
| 148 |
-- it seems friendly to allow things like concurrent "=" on shared |
| 149 |
-- constants. |
| 150 |
|
| 151 |
Zero_Counts (Container.TC); |
| 152 |
|
| 153 |
if Container.Last = No_Index then |
| 154 |
Container.Elements := null; |
| 155 |
return; |
| 156 |
end if; |
| 157 |
|
| 158 |
declare |
| 159 |
L : constant Index_Type := Container.Last; |
| 160 |
E : Elements_Array renames |
| 161 |
Container.Elements.EA (Index_Type'First .. L); |
| 162 |
|
| 163 |
begin |
| 164 |
Container.Elements := null; |
| 165 |
Container.Last := No_Index; |
| 166 |
|
| 167 |
Container.Elements := new Elements_Type (L); |
| 168 |
|
| 169 |
for J in E'Range loop |
| 170 |
if E (J) /= null then |
| 171 |
Container.Elements.EA (J) := new Element_Type'(E (J).all); |
| 172 |
end if; |
| 173 |
|
| 174 |
Container.Last := J; |
| 175 |
end loop; |
| 176 |
end; |
| 177 |
end Adjust; |
| 178 |
|
| 179 |
------------------- |
| 180 |
-- Append_Vector -- |
| 181 |
------------------- |
| 182 |
|
| 183 |
procedure Append_Vector (Container : in out Vector; New_Item : Vector) is |
| 184 |
begin |
| 185 |
if Is_Empty (New_Item) then |
| 186 |
return; |
| 187 |
elsif Checks and then Container.Last = Index_Type'Last then |
| 188 |
raise Constraint_Error with "vector is already at its maximum length"; |
| 189 |
else |
| 190 |
Insert_Vector (Container, Container.Last + 1, New_Item); |
| 191 |
end if; |
| 192 |
end Append_Vector; |
| 193 |
|
| 194 |
procedure Append |
| 195 |
(Container : in out Vector; |
| 196 |
New_Item : Element_Type; |
| 197 |
Count : Count_Type) |
| 198 |
is |
| 199 |
begin |
| 200 |
-- In the general case, we take the slow path; for efficiency, |
| 201 |
-- we check for the common case where Count = 1 . |
| 202 |
|
| 203 |
if Count = 1 then |
| 204 |
Append (Container, New_Item); |
| 205 |
else |
| 206 |
Append_Slow_Path (Container, New_Item, Count); |
| 207 |
end if; |
| 208 |
end Append; |
| 209 |
|
| 210 |
------------ |
| 211 |
-- Append -- |
| 212 |
------------ |
| 213 |
|
| 214 |
procedure Append (Container : in out Vector; |
| 215 |
New_Item : Element_Type) |
| 216 |
is |
| 217 |
begin |
| 218 |
-- For performance, check for the common special case where the |
| 219 |
-- container already has room for at least one more element. |
| 220 |
-- In the general case, pass the buck to Insert. |
| 221 |
|
| 222 |
if Container.Elements /= null |
| 223 |
and then Container.Last /= Container.Elements.Last |
| 224 |
then |
| 225 |
TC_Check (Container.TC); |
| 226 |
|
| 227 |
-- Increment Container.Last after assigning the New_Item, so we |
| 228 |
-- leave the Container unmodified in case Finalize/Adjust raises |
| 229 |
-- an exception. |
| 230 |
|
| 231 |
declare |
| 232 |
New_Last : constant Index_Type := Container.Last + 1; |
| 233 |
|
| 234 |
-- The element allocator may need an accessibility check in the |
| 235 |
-- case actual type is class-wide or has access discriminants |
| 236 |
-- (see RM 4.8(10.1) and AI12-0035). |
| 237 |
|
| 238 |
pragma Unsuppress (Accessibility_Check); |
| 239 |
begin |
| 240 |
Container.Elements.EA (New_Last) := new Element_Type'(New_Item); |
| 241 |
Container.Last := New_Last; |
| 242 |
end; |
| 243 |
else |
| 244 |
Insert (Container, Last_Index (Container) + 1, New_Item, 1); |
| 245 |
end if; |
| 246 |
end Append; |
| 247 |
|
| 248 |
---------------------- |
| 249 |
-- Append_Slow_Path -- |
| 250 |
---------------------- |
| 251 |
|
| 252 |
procedure Append_Slow_Path |
| 253 |
(Container : in out Vector; |
| 254 |
New_Item : Element_Type; |
| 255 |
Count : Count_Type) |
| 256 |
is |
| 257 |
begin |
| 258 |
if Count = 0 then |
| 259 |
return; |
| 260 |
elsif Checks and then Container.Last = Index_Type'Last then |
| 261 |
raise Constraint_Error with "vector is already at its maximum length"; |
| 262 |
else |
| 263 |
Insert (Container, Container.Last + 1, New_Item, Count); |
| 264 |
end if; |
| 265 |
end Append_Slow_Path; |
| 266 |
|
| 267 |
------------ |
| 268 |
-- Assign -- |
| 269 |
------------ |
| 270 |
|
| 271 |
procedure Assign (Target : in out Vector; Source : Vector) is |
| 272 |
begin |
| 273 |
if Target'Address = Source'Address then |
| 274 |
return; |
| 275 |
else |
| 276 |
Target.Clear; |
| 277 |
Target.Append_Vector (Source); |
| 278 |
end if; |
| 279 |
end Assign; |
| 280 |
|
| 281 |
-------------- |
| 282 |
-- Capacity -- |
| 283 |
-------------- |
| 284 |
|
| 285 |
function Capacity (Container : Vector) return Count_Type is |
| 286 |
begin |
| 287 |
if Container.Elements = null then |
| 288 |
return 0; |
| 289 |
else |
| 290 |
return Container.Elements.EA'Length; |
| 291 |
end if; |
| 292 |
end Capacity; |
| 293 |
|
| 294 |
----------- |
| 295 |
-- Clear -- |
| 296 |
----------- |
| 297 |
|
| 298 |
procedure Clear (Container : in out Vector) is |
| 299 |
begin |
| 300 |
TC_Check (Container.TC); |
| 301 |
|
| 302 |
while Container.Last >= Index_Type'First loop |
| 303 |
declare |
| 304 |
X : Element_Access := Container.Elements.EA (Container.Last); |
| 305 |
begin |
| 306 |
Container.Elements.EA (Container.Last) := null; |
| 307 |
Container.Last := Container.Last - 1; |
| 308 |
Free (X); |
| 309 |
end; |
| 310 |
end loop; |
| 311 |
end Clear; |
| 312 |
|
| 313 |
------------------------ |
| 314 |
-- Constant_Reference -- |
| 315 |
------------------------ |
| 316 |
|
| 317 |
function Constant_Reference |
| 318 |
(Container : aliased Vector; |
| 319 |
Position : Cursor) return Constant_Reference_Type |
| 320 |
is |
| 321 |
begin |
| 322 |
if Checks then |
| 323 |
if Position.Container = null then |
| 324 |
raise Constraint_Error with "Position cursor has no element"; |
| 325 |
end if; |
| 326 |
|
| 327 |
if Position.Container /= Container'Unrestricted_Access then |
| 328 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 329 |
end if; |
| 330 |
|
| 331 |
if Position.Index > Position.Container.Last then |
| 332 |
raise Constraint_Error with "Position cursor is out of range"; |
| 333 |
end if; |
| 334 |
end if; |
| 335 |
|
| 336 |
declare |
| 337 |
TC : constant Tamper_Counts_Access := |
| 338 |
Container.TC'Unrestricted_Access; |
| 339 |
begin |
| 340 |
-- The following will raise Constraint_Error if Element is null |
| 341 |
|
| 342 |
return R : constant Constant_Reference_Type := |
| 343 |
(Element => Container.Elements.EA (Position.Index), |
| 344 |
Control => (Controlled with TC)) |
| 345 |
do |
| 346 |
Busy (TC.all); |
| 347 |
end return; |
| 348 |
end; |
| 349 |
end Constant_Reference; |
| 350 |
|
| 351 |
function Constant_Reference |
| 352 |
(Container : aliased Vector; |
| 353 |
Index : Index_Type) return Constant_Reference_Type |
| 354 |
is |
| 355 |
begin |
| 356 |
if Checks and then Index > Container.Last then |
| 357 |
raise Constraint_Error with "Index is out of range"; |
| 358 |
end if; |
| 359 |
|
| 360 |
declare |
| 361 |
TC : constant Tamper_Counts_Access := |
| 362 |
Container.TC'Unrestricted_Access; |
| 363 |
begin |
| 364 |
-- The following will raise Constraint_Error if Element is null |
| 365 |
|
| 366 |
return R : constant Constant_Reference_Type := |
| 367 |
(Element => Container.Elements.EA (Index), |
| 368 |
Control => (Controlled with TC)) |
| 369 |
do |
| 370 |
Busy (TC.all); |
| 371 |
end return; |
| 372 |
end; |
| 373 |
end Constant_Reference; |
| 374 |
|
| 375 |
-------------- |
| 376 |
-- Contains -- |
| 377 |
-------------- |
| 378 |
|
| 379 |
function Contains |
| 380 |
(Container : Vector; |
| 381 |
Item : Element_Type) return Boolean |
| 382 |
is |
| 383 |
begin |
| 384 |
return Find_Index (Container, Item) /= No_Index; |
| 385 |
end Contains; |
| 386 |
|
| 387 |
---------- |
| 388 |
-- Copy -- |
| 389 |
---------- |
| 390 |
|
| 391 |
function Copy |
| 392 |
(Source : Vector; |
| 393 |
Capacity : Count_Type := 0) return Vector |
| 394 |
is |
| 395 |
C : Count_Type; |
| 396 |
|
| 397 |
begin |
| 398 |
if Capacity < Source.Length then |
| 399 |
if Checks and then Capacity /= 0 then |
| 400 |
raise Capacity_Error |
| 401 |
with "Requested capacity is less than Source length"; |
| 402 |
end if; |
| 403 |
|
| 404 |
C := Source.Length; |
| 405 |
else |
| 406 |
C := Capacity; |
| 407 |
end if; |
| 408 |
|
| 409 |
return Target : Vector do |
| 410 |
Target.Reserve_Capacity (C); |
| 411 |
Target.Assign (Source); |
| 412 |
end return; |
| 413 |
end Copy; |
| 414 |
|
| 415 |
------------ |
| 416 |
-- Delete -- |
| 417 |
------------ |
| 418 |
|
| 419 |
procedure Delete |
| 420 |
(Container : in out Vector; |
| 421 |
Index : Extended_Index; |
| 422 |
Count : Count_Type := 1) |
| 423 |
is |
| 424 |
Old_Last : constant Index_Type'Base := Container.Last; |
| 425 |
New_Last : Index_Type'Base; |
| 426 |
Count2 : Count_Type'Base; -- count of items from Index to Old_Last |
| 427 |
J : Index_Type'Base; -- first index of items that slide down |
| 428 |
|
| 429 |
begin |
| 430 |
-- The tampering bits exist to prevent an item from being deleted (or |
| 431 |
-- otherwise harmfully manipulated) while it is being visited. Query, |
| 432 |
-- Update, and Iterate increment the busy count on entry, and decrement |
| 433 |
-- the count on exit. Delete checks the count to determine whether it is |
| 434 |
-- being called while the associated callback procedure is executing. |
| 435 |
|
| 436 |
TC_Check (Container.TC); |
| 437 |
|
| 438 |
-- Delete removes items from the vector, the number of which is the |
| 439 |
-- minimum of the specified Count and the items (if any) that exist from |
| 440 |
-- Index to Container.Last. There are no constraints on the specified |
| 441 |
-- value of Count (it can be larger than what's available at this |
| 442 |
-- position in the vector, for example), but there are constraints on |
| 443 |
-- the allowed values of the Index. |
| 444 |
|
| 445 |
-- As a precondition on the generic actual Index_Type, the base type |
| 446 |
-- must include Index_Type'Pred (Index_Type'First); this is the value |
| 447 |
-- that Container.Last assumes when the vector is empty. However, we do |
| 448 |
-- not allow that as the value for Index when specifying which items |
| 449 |
-- should be deleted, so we must manually check. (That the user is |
| 450 |
-- allowed to specify the value at all here is a consequence of the |
| 451 |
-- declaration of the Extended_Index subtype, which includes the values |
| 452 |
-- in the base range that immediately precede and immediately follow the |
| 453 |
-- values in the Index_Type.) |
| 454 |
|
| 455 |
if Checks and then Index < Index_Type'First then |
| 456 |
raise Constraint_Error with "Index is out of range (too small)"; |
| 457 |
end if; |
| 458 |
|
| 459 |
-- We do allow a value greater than Container.Last to be specified as |
| 460 |
-- the Index, but only if it's immediately greater. This allows the |
| 461 |
-- corner case of deleting no items from the back end of the vector to |
| 462 |
-- be treated as a no-op. (It is assumed that specifying an index value |
| 463 |
-- greater than Last + 1 indicates some deeper flaw in the caller's |
| 464 |
-- algorithm, so that case is treated as a proper error.) |
| 465 |
|
| 466 |
if Index > Old_Last then |
| 467 |
if Checks and then Index > Old_Last + 1 then |
| 468 |
raise Constraint_Error with "Index is out of range (too large)"; |
| 469 |
else |
| 470 |
return; |
| 471 |
end if; |
| 472 |
end if; |
| 473 |
|
| 474 |
-- Here and elsewhere we treat deleting 0 items from the container as a |
| 475 |
-- no-op, even when the container is busy, so we simply return. |
| 476 |
|
| 477 |
if Count = 0 then |
| 478 |
return; |
| 479 |
end if; |
| 480 |
|
| 481 |
-- The internal elements array isn't guaranteed to exist unless we have |
| 482 |
-- elements, so we handle that case here in order to avoid having to |
| 483 |
-- check it later. (Note that an empty vector can never be busy, so |
| 484 |
-- there's no semantic harm in returning early.) |
| 485 |
|
| 486 |
if Container.Is_Empty then |
| 487 |
return; |
| 488 |
end if; |
| 489 |
|
| 490 |
-- We first calculate what's available for deletion starting at |
| 491 |
-- Index. Here and elsewhere we use the wider of Index_Type'Base and |
| 492 |
-- Count_Type'Base as the type for intermediate values. (See function |
| 493 |
-- Length for more information.) |
| 494 |
|
| 495 |
if Count_Type'Base'Last >= Index_Type'Pos (Index_Type'Base'Last) then |
| 496 |
Count2 := Count_Type'Base (Old_Last) - Count_Type'Base (Index) + 1; |
| 497 |
else |
| 498 |
Count2 := Count_Type'Base (Old_Last - Index + 1); |
| 499 |
end if; |
| 500 |
|
| 501 |
-- If the number of elements requested (Count) for deletion is equal to |
| 502 |
-- (or greater than) the number of elements available (Count2) for |
| 503 |
-- deletion beginning at Index, then everything from Index to |
| 504 |
-- Container.Last is deleted (this is equivalent to Delete_Last). |
| 505 |
|
| 506 |
if Count >= Count2 then |
| 507 |
-- Elements in an indefinite vector are allocated, so we must iterate |
| 508 |
-- over the loop and deallocate elements one-at-a-time. We work from |
| 509 |
-- back to front, deleting the last element during each pass, in |
| 510 |
-- order to gracefully handle deallocation failures. |
| 511 |
|
| 512 |
declare |
| 513 |
EA : Elements_Array renames Container.Elements.EA; |
| 514 |
|
| 515 |
begin |
| 516 |
while Container.Last >= Index loop |
| 517 |
declare |
| 518 |
K : constant Index_Type := Container.Last; |
| 519 |
X : Element_Access := EA (K); |
| 520 |
|
| 521 |
begin |
| 522 |
-- We first isolate the element we're deleting, removing it |
| 523 |
-- from the vector before we attempt to deallocate it, in |
| 524 |
-- case the deallocation fails. |
| 525 |
|
| 526 |
EA (K) := null; |
| 527 |
Container.Last := K - 1; |
| 528 |
|
| 529 |
-- Container invariants have been restored, so it is now |
| 530 |
-- safe to attempt to deallocate the element. |
| 531 |
|
| 532 |
Free (X); |
| 533 |
end; |
| 534 |
end loop; |
| 535 |
end; |
| 536 |
|
| 537 |
return; |
| 538 |
end if; |
| 539 |
|
| 540 |
-- There are some elements that aren't being deleted (the requested |
| 541 |
-- count was less than the available count), so we must slide them down |
| 542 |
-- to Index. We first calculate the index values of the respective array |
| 543 |
-- slices, using the wider of Index_Type'Base and Count_Type'Base as the |
| 544 |
-- type for intermediate calculations. For the elements that slide down, |
| 545 |
-- index value New_Last is the last index value of their new home, and |
| 546 |
-- index value J is the first index of their old home. |
| 547 |
|
| 548 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 549 |
New_Last := Old_Last - Index_Type'Base (Count); |
| 550 |
J := Index + Index_Type'Base (Count); |
| 551 |
else |
| 552 |
New_Last := Index_Type'Base (Count_Type'Base (Old_Last) - Count); |
| 553 |
J := Index_Type'Base (Count_Type'Base (Index) + Count); |
| 554 |
end if; |
| 555 |
|
| 556 |
-- The internal elements array isn't guaranteed to exist unless we have |
| 557 |
-- elements, but we have that guarantee here because we know we have |
| 558 |
-- elements to slide. The array index values for each slice have |
| 559 |
-- already been determined, so what remains to be done is to first |
| 560 |
-- deallocate the elements that are being deleted, and then slide down |
| 561 |
-- to Index the elements that aren't being deleted. |
| 562 |
|
| 563 |
declare |
| 564 |
EA : Elements_Array renames Container.Elements.EA; |
| 565 |
|
| 566 |
begin |
| 567 |
-- Before we can slide down the elements that aren't being deleted, |
| 568 |
-- we need to deallocate the elements that are being deleted. |
| 569 |
|
| 570 |
for K in Index .. J - 1 loop |
| 571 |
declare |
| 572 |
X : Element_Access := EA (K); |
| 573 |
|
| 574 |
begin |
| 575 |
-- First we remove the element we're about to deallocate from |
| 576 |
-- the vector, in case the deallocation fails, in order to |
| 577 |
-- preserve representation invariants. |
| 578 |
|
| 579 |
EA (K) := null; |
| 580 |
|
| 581 |
-- The element has been removed from the vector, so it is now |
| 582 |
-- safe to attempt to deallocate it. |
| 583 |
|
| 584 |
Free (X); |
| 585 |
end; |
| 586 |
end loop; |
| 587 |
|
| 588 |
EA (Index .. New_Last) := EA (J .. Old_Last); |
| 589 |
Container.Last := New_Last; |
| 590 |
end; |
| 591 |
end Delete; |
| 592 |
|
| 593 |
procedure Delete |
| 594 |
(Container : in out Vector; |
| 595 |
Position : in out Cursor; |
| 596 |
Count : Count_Type := 1) |
| 597 |
is |
| 598 |
begin |
| 599 |
if Checks then |
| 600 |
if Position.Container = null then |
| 601 |
raise Constraint_Error with "Position cursor has no element"; |
| 602 |
|
| 603 |
elsif Position.Container /= Container'Unrestricted_Access then |
| 604 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 605 |
|
| 606 |
elsif Position.Index > Container.Last then |
| 607 |
raise Program_Error with "Position index is out of range"; |
| 608 |
end if; |
| 609 |
end if; |
| 610 |
|
| 611 |
Delete (Container, Position.Index, Count); |
| 612 |
Position := No_Element; |
| 613 |
end Delete; |
| 614 |
|
| 615 |
------------------ |
| 616 |
-- Delete_First -- |
| 617 |
------------------ |
| 618 |
|
| 619 |
procedure Delete_First |
| 620 |
(Container : in out Vector; |
| 621 |
Count : Count_Type := 1) |
| 622 |
is |
| 623 |
begin |
| 624 |
if Count = 0 then |
| 625 |
return; |
| 626 |
|
| 627 |
elsif Count >= Length (Container) then |
| 628 |
Clear (Container); |
| 629 |
return; |
| 630 |
|
| 631 |
else |
| 632 |
Delete (Container, Index_Type'First, Count); |
| 633 |
end if; |
| 634 |
end Delete_First; |
| 635 |
|
| 636 |
----------------- |
| 637 |
-- Delete_Last -- |
| 638 |
----------------- |
| 639 |
|
| 640 |
procedure Delete_Last |
| 641 |
(Container : in out Vector; |
| 642 |
Count : Count_Type := 1) |
| 643 |
is |
| 644 |
begin |
| 645 |
-- It is not permitted to delete items while the container is busy (for |
| 646 |
-- example, we're in the middle of a passive iteration). However, we |
| 647 |
-- always treat deleting 0 items as a no-op, even when we're busy, so we |
| 648 |
-- simply return without checking. |
| 649 |
|
| 650 |
if Count = 0 then |
| 651 |
return; |
| 652 |
end if; |
| 653 |
|
| 654 |
-- We cannot simply subsume the empty case into the loop below (the loop |
| 655 |
-- would iterate 0 times), because we rename the internal array object |
| 656 |
-- (which is allocated), but an empty vector isn't guaranteed to have |
| 657 |
-- actually allocated an array. (Note that an empty vector can never be |
| 658 |
-- busy, so there's no semantic harm in returning early here.) |
| 659 |
|
| 660 |
if Container.Is_Empty then |
| 661 |
return; |
| 662 |
end if; |
| 663 |
|
| 664 |
-- The tampering bits exist to prevent an item from being deleted (or |
| 665 |
-- otherwise harmfully manipulated) while it is being visited. Query, |
| 666 |
-- Update, and Iterate increment the busy count on entry, and decrement |
| 667 |
-- the count on exit. Delete_Last checks the count to determine whether |
| 668 |
-- it is being called while the associated callback procedure is |
| 669 |
-- executing. |
| 670 |
|
| 671 |
TC_Check (Container.TC); |
| 672 |
|
| 673 |
-- Elements in an indefinite vector are allocated, so we must iterate |
| 674 |
-- over the loop and deallocate elements one-at-a-time. We work from |
| 675 |
-- back to front, deleting the last element during each pass, in order |
| 676 |
-- to gracefully handle deallocation failures. |
| 677 |
|
| 678 |
declare |
| 679 |
E : Elements_Array renames Container.Elements.EA; |
| 680 |
|
| 681 |
begin |
| 682 |
for Indx in 1 .. Count_Type'Min (Count, Container.Length) loop |
| 683 |
declare |
| 684 |
J : constant Index_Type := Container.Last; |
| 685 |
X : Element_Access := E (J); |
| 686 |
|
| 687 |
begin |
| 688 |
-- Note that we first isolate the element we're deleting, |
| 689 |
-- removing it from the vector, before we actually deallocate |
| 690 |
-- it, in order to preserve representation invariants even if |
| 691 |
-- the deallocation fails. |
| 692 |
|
| 693 |
E (J) := null; |
| 694 |
Container.Last := J - 1; |
| 695 |
|
| 696 |
-- Container invariants have been restored, so it is now safe |
| 697 |
-- to deallocate the element. |
| 698 |
|
| 699 |
Free (X); |
| 700 |
end; |
| 701 |
end loop; |
| 702 |
end; |
| 703 |
end Delete_Last; |
| 704 |
|
| 705 |
------------- |
| 706 |
-- Element -- |
| 707 |
------------- |
| 708 |
|
| 709 |
function Element |
| 710 |
(Container : Vector; |
| 711 |
Index : Index_Type) return Element_Type |
| 712 |
is |
| 713 |
begin |
| 714 |
if Checks and then Index > Container.Last then |
| 715 |
raise Constraint_Error with "Index is out of range"; |
| 716 |
end if; |
| 717 |
|
| 718 |
declare |
| 719 |
EA : constant Element_Access := Container.Elements.EA (Index); |
| 720 |
begin |
| 721 |
if Checks and then EA = null then |
| 722 |
raise Constraint_Error with "element is empty"; |
| 723 |
else |
| 724 |
return EA.all; |
| 725 |
end if; |
| 726 |
end; |
| 727 |
end Element; |
| 728 |
|
| 729 |
function Element (Position : Cursor) return Element_Type is |
| 730 |
begin |
| 731 |
if Checks then |
| 732 |
if Position.Container = null then |
| 733 |
raise Constraint_Error with "Position cursor has no element"; |
| 734 |
end if; |
| 735 |
|
| 736 |
if Position.Index > Position.Container.Last then |
| 737 |
raise Constraint_Error with "Position cursor is out of range"; |
| 738 |
end if; |
| 739 |
end if; |
| 740 |
|
| 741 |
declare |
| 742 |
EA : constant Element_Access := |
| 743 |
Position.Container.Elements.EA (Position.Index); |
| 744 |
begin |
| 745 |
if Checks and then EA = null then |
| 746 |
raise Constraint_Error with "element is empty"; |
| 747 |
else |
| 748 |
return EA.all; |
| 749 |
end if; |
| 750 |
end; |
| 751 |
end Element; |
| 752 |
|
| 753 |
----------- |
| 754 |
-- Empty -- |
| 755 |
----------- |
| 756 |
|
| 757 |
function Empty (Capacity : Count_Type := 10) return Vector is |
| 758 |
begin |
| 759 |
return Result : Vector do |
| 760 |
Reserve_Capacity (Result, Capacity); |
| 761 |
end return; |
| 762 |
end Empty; |
| 763 |
|
| 764 |
-------------- |
| 765 |
-- Finalize -- |
| 766 |
-------------- |
| 767 |
|
| 768 |
procedure Finalize (Container : in out Vector) is |
| 769 |
begin |
| 770 |
Clear (Container); -- Checks busy-bit |
| 771 |
|
| 772 |
declare |
| 773 |
X : Elements_Access := Container.Elements; |
| 774 |
begin |
| 775 |
Container.Elements := null; |
| 776 |
Free (X); |
| 777 |
end; |
| 778 |
end Finalize; |
| 779 |
|
| 780 |
procedure Finalize (Object : in out Iterator) is |
| 781 |
begin |
| 782 |
Unbusy (Object.Container.TC); |
| 783 |
end Finalize; |
| 784 |
|
| 785 |
---------- |
| 786 |
-- Find -- |
| 787 |
---------- |
| 788 |
|
| 789 |
function Find |
| 790 |
(Container : Vector; |
| 791 |
Item : Element_Type; |
| 792 |
Position : Cursor := No_Element) return Cursor |
| 793 |
is |
| 794 |
begin |
| 795 |
if Checks and then Position.Container /= null then |
| 796 |
if Position.Container /= Container'Unrestricted_Access then |
| 797 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 798 |
end if; |
| 799 |
|
| 800 |
if Position.Index > Container.Last then |
| 801 |
raise Program_Error with "Position index is out of range"; |
| 802 |
end if; |
| 803 |
end if; |
| 804 |
|
| 805 |
-- Per AI05-0022, the container implementation is required to detect |
| 806 |
-- element tampering by a generic actual subprogram. |
| 807 |
|
| 808 |
declare |
| 809 |
Lock : With_Lock (Container.TC'Unrestricted_Access); |
| 810 |
begin |
| 811 |
for J in Position.Index .. Container.Last loop |
| 812 |
if Container.Elements.EA (J).all = Item then |
| 813 |
return Cursor'(Container'Unrestricted_Access, J); |
| 814 |
end if; |
| 815 |
end loop; |
| 816 |
|
| 817 |
return No_Element; |
| 818 |
end; |
| 819 |
end Find; |
| 820 |
|
| 821 |
---------------- |
| 822 |
-- Find_Index -- |
| 823 |
---------------- |
| 824 |
|
| 825 |
function Find_Index |
| 826 |
(Container : Vector; |
| 827 |
Item : Element_Type; |
| 828 |
Index : Index_Type := Index_Type'First) return Extended_Index |
| 829 |
is |
| 830 |
-- Per AI05-0022, the container implementation is required to detect |
| 831 |
-- element tampering by a generic actual subprogram. |
| 832 |
|
| 833 |
Lock : With_Lock (Container.TC'Unrestricted_Access); |
| 834 |
begin |
| 835 |
for Indx in Index .. Container.Last loop |
| 836 |
if Container.Elements.EA (Indx).all = Item then |
| 837 |
return Indx; |
| 838 |
end if; |
| 839 |
end loop; |
| 840 |
|
| 841 |
return No_Index; |
| 842 |
end Find_Index; |
| 843 |
|
| 844 |
----------- |
| 845 |
-- First -- |
| 846 |
----------- |
| 847 |
|
| 848 |
function First (Container : Vector) return Cursor is |
| 849 |
begin |
| 850 |
if Is_Empty (Container) then |
| 851 |
return No_Element; |
| 852 |
end if; |
| 853 |
|
| 854 |
return (Container'Unrestricted_Access, Index_Type'First); |
| 855 |
end First; |
| 856 |
|
| 857 |
function First (Object : Iterator) return Cursor is |
| 858 |
begin |
| 859 |
-- The value of the iterator object's Index component influences the |
| 860 |
-- behavior of the First (and Last) selector function. |
| 861 |
|
| 862 |
-- When the Index component is No_Index, this means the iterator |
| 863 |
-- object was constructed without a start expression, in which case the |
| 864 |
-- (forward) iteration starts from the (logical) beginning of the entire |
| 865 |
-- sequence of items (corresponding to Container.First, for a forward |
| 866 |
-- iterator). |
| 867 |
|
| 868 |
-- Otherwise, this is iteration over a partial sequence of items. |
| 869 |
-- When the Index component isn't No_Index, the iterator object was |
| 870 |
-- constructed with a start expression, that specifies the position |
| 871 |
-- from which the (forward) partial iteration begins. |
| 872 |
|
| 873 |
if Object.Index = No_Index then |
| 874 |
return First (Object.Container.all); |
| 875 |
else |
| 876 |
return Cursor'(Object.Container, Object.Index); |
| 877 |
end if; |
| 878 |
end First; |
| 879 |
|
| 880 |
------------------- |
| 881 |
-- First_Element -- |
| 882 |
------------------- |
| 883 |
|
| 884 |
function First_Element (Container : Vector) return Element_Type is |
| 885 |
begin |
| 886 |
if Checks and then Container.Last = No_Index then |
| 887 |
raise Constraint_Error with "Container is empty"; |
| 888 |
end if; |
| 889 |
|
| 890 |
declare |
| 891 |
EA : constant Element_Access := |
| 892 |
Container.Elements.EA (Index_Type'First); |
| 893 |
begin |
| 894 |
if Checks and then EA = null then |
| 895 |
raise Constraint_Error with "first element is empty"; |
| 896 |
else |
| 897 |
return EA.all; |
| 898 |
end if; |
| 899 |
end; |
| 900 |
end First_Element; |
| 901 |
|
| 902 |
----------------- |
| 903 |
-- New_Vector -- |
| 904 |
----------------- |
| 905 |
|
| 906 |
function New_Vector (First, Last : Index_Type) return Vector |
| 907 |
is |
| 908 |
begin |
| 909 |
return (To_Vector (Count_Type (Last - First + 1))); |
| 910 |
end New_Vector; |
| 911 |
|
| 912 |
----------------- |
| 913 |
-- First_Index -- |
| 914 |
----------------- |
| 915 |
|
| 916 |
function First_Index (Container : Vector) return Index_Type is |
| 917 |
pragma Unreferenced (Container); |
| 918 |
begin |
| 919 |
return Index_Type'First; |
| 920 |
end First_Index; |
| 921 |
|
| 922 |
--------------------- |
| 923 |
-- Generic_Sorting -- |
| 924 |
--------------------- |
| 925 |
|
| 926 |
package body Generic_Sorting is |
| 927 |
|
| 928 |
----------------------- |
| 929 |
-- Local Subprograms -- |
| 930 |
----------------------- |
| 931 |
|
| 932 |
function Is_Less (L, R : Element_Access) return Boolean; |
| 933 |
pragma Inline (Is_Less); |
| 934 |
|
| 935 |
------------- |
| 936 |
-- Is_Less -- |
| 937 |
------------- |
| 938 |
|
| 939 |
function Is_Less (L, R : Element_Access) return Boolean is |
| 940 |
begin |
| 941 |
if L = null then |
| 942 |
return R /= null; |
| 943 |
elsif R = null then |
| 944 |
return False; |
| 945 |
else |
| 946 |
return L.all < R.all; |
| 947 |
end if; |
| 948 |
end Is_Less; |
| 949 |
|
| 950 |
--------------- |
| 951 |
-- Is_Sorted -- |
| 952 |
--------------- |
| 953 |
|
| 954 |
function Is_Sorted (Container : Vector) return Boolean is |
| 955 |
begin |
| 956 |
if Container.Last <= Index_Type'First then |
| 957 |
return True; |
| 958 |
end if; |
| 959 |
|
| 960 |
-- Per AI05-0022, the container implementation is required to detect |
| 961 |
-- element tampering by a generic actual subprogram. |
| 962 |
|
| 963 |
declare |
| 964 |
Lock : With_Lock (Container.TC'Unrestricted_Access); |
| 965 |
E : Elements_Array renames Container.Elements.EA; |
| 966 |
begin |
| 967 |
for J in Index_Type'First .. Container.Last - 1 loop |
| 968 |
if Is_Less (E (J + 1), E (J)) then |
| 969 |
return False; |
| 970 |
end if; |
| 971 |
end loop; |
| 972 |
|
| 973 |
return True; |
| 974 |
end; |
| 975 |
end Is_Sorted; |
| 976 |
|
| 977 |
----------- |
| 978 |
-- Merge -- |
| 979 |
----------- |
| 980 |
|
| 981 |
procedure Merge (Target, Source : in out Vector) is |
| 982 |
I, J : Index_Type'Base; |
| 983 |
|
| 984 |
begin |
| 985 |
TC_Check (Source.TC); |
| 986 |
|
| 987 |
-- The semantics of Merge changed slightly per AI05-0021. It was |
| 988 |
-- originally the case that if Target and Source denoted the same |
| 989 |
-- container object, then the GNAT implementation of Merge did |
| 990 |
-- nothing. However, it was argued that RM05 did not precisely |
| 991 |
-- specify the semantics for this corner case. The decision of the |
| 992 |
-- ARG was that if Target and Source denote the same non-empty |
| 993 |
-- container object, then Program_Error is raised. |
| 994 |
|
| 995 |
if Source.Last < Index_Type'First then -- Source is empty |
| 996 |
return; |
| 997 |
end if; |
| 998 |
|
| 999 |
if Checks and then Target'Address = Source'Address then |
| 1000 |
raise Program_Error with |
| 1001 |
"Target and Source denote same non-empty container"; |
| 1002 |
end if; |
| 1003 |
|
| 1004 |
if Target.Last < Index_Type'First then -- Target is empty |
| 1005 |
Move (Target => Target, Source => Source); |
| 1006 |
return; |
| 1007 |
end if; |
| 1008 |
|
| 1009 |
I := Target.Last; -- original value (before Set_Length) |
| 1010 |
Target.Set_Length (Length (Target) + Length (Source)); |
| 1011 |
|
| 1012 |
-- Per AI05-0022, the container implementation is required to detect |
| 1013 |
-- element tampering by a generic actual subprogram. |
| 1014 |
|
| 1015 |
declare |
| 1016 |
TA : Elements_Array renames Target.Elements.EA; |
| 1017 |
SA : Elements_Array renames Source.Elements.EA; |
| 1018 |
|
| 1019 |
Lock_Target : With_Lock (Target.TC'Unchecked_Access); |
| 1020 |
Lock_Source : With_Lock (Source.TC'Unchecked_Access); |
| 1021 |
begin |
| 1022 |
J := Target.Last; -- new value (after Set_Length) |
| 1023 |
while Source.Last >= Index_Type'First loop |
| 1024 |
pragma Assert |
| 1025 |
(Source.Last <= Index_Type'First |
| 1026 |
or else not Is_Less (SA (Source.Last), |
| 1027 |
SA (Source.Last - 1))); |
| 1028 |
|
| 1029 |
if I < Index_Type'First then |
| 1030 |
declare |
| 1031 |
Src : Elements_Array renames |
| 1032 |
SA (Index_Type'First .. Source.Last); |
| 1033 |
begin |
| 1034 |
TA (Index_Type'First .. J) := Src; |
| 1035 |
Src := [others => null]; |
| 1036 |
end; |
| 1037 |
|
| 1038 |
Source.Last := No_Index; |
| 1039 |
exit; |
| 1040 |
end if; |
| 1041 |
|
| 1042 |
pragma Assert |
| 1043 |
(I <= Index_Type'First |
| 1044 |
or else not Is_Less (TA (I), TA (I - 1))); |
| 1045 |
|
| 1046 |
declare |
| 1047 |
Src : Element_Access renames SA (Source.Last); |
| 1048 |
Tgt : Element_Access renames TA (I); |
| 1049 |
|
| 1050 |
begin |
| 1051 |
if Is_Less (Src, Tgt) then |
| 1052 |
Target.Elements.EA (J) := Tgt; |
| 1053 |
Tgt := null; |
| 1054 |
I := I - 1; |
| 1055 |
|
| 1056 |
else |
| 1057 |
Target.Elements.EA (J) := Src; |
| 1058 |
Src := null; |
| 1059 |
Source.Last := Source.Last - 1; |
| 1060 |
end if; |
| 1061 |
end; |
| 1062 |
|
| 1063 |
J := J - 1; |
| 1064 |
end loop; |
| 1065 |
end; |
| 1066 |
end Merge; |
| 1067 |
|
| 1068 |
---------- |
| 1069 |
-- Sort -- |
| 1070 |
---------- |
| 1071 |
|
| 1072 |
procedure Sort (Container : in out Vector) is |
| 1073 |
procedure Sort is new Generic_Array_Sort |
| 1074 |
(Index_Type => Index_Type, |
| 1075 |
Element_Type => Element_Access, |
| 1076 |
Array_Type => Elements_Array, |
| 1077 |
"<" => Is_Less); |
| 1078 |
|
| 1079 |
-- Start of processing for Sort |
| 1080 |
|
| 1081 |
begin |
| 1082 |
if Container.Last <= Index_Type'First then |
| 1083 |
return; |
| 1084 |
end if; |
| 1085 |
|
| 1086 |
-- The exception behavior for the vector container must match that |
| 1087 |
-- for the list container, so we check for cursor tampering here |
| 1088 |
-- (which will catch more things) instead of for element tampering |
| 1089 |
-- (which will catch fewer things). It's true that the elements of |
| 1090 |
-- this vector container could be safely moved around while (say) an |
| 1091 |
-- iteration is taking place (iteration only increments the busy |
| 1092 |
-- counter), and so technically all we would need here is a test for |
| 1093 |
-- element tampering (indicated by the lock counter), that's simply |
| 1094 |
-- an artifact of our array-based implementation. Logically Sort |
| 1095 |
-- requires a check for cursor tampering. |
| 1096 |
|
| 1097 |
TC_Check (Container.TC); |
| 1098 |
|
| 1099 |
-- Per AI05-0022, the container implementation is required to detect |
| 1100 |
-- element tampering by a generic actual subprogram. |
| 1101 |
|
| 1102 |
declare |
| 1103 |
Lock : With_Lock (Container.TC'Unchecked_Access); |
| 1104 |
begin |
| 1105 |
Sort (Container.Elements.EA (Index_Type'First .. Container.Last)); |
| 1106 |
end; |
| 1107 |
end Sort; |
| 1108 |
|
| 1109 |
end Generic_Sorting; |
| 1110 |
|
| 1111 |
------------------------ |
| 1112 |
-- Get_Element_Access -- |
| 1113 |
------------------------ |
| 1114 |
|
| 1115 |
function Get_Element_Access |
| 1116 |
(Position : Cursor) return not null Element_Access |
| 1117 |
is |
| 1118 |
Ptr : constant Element_Access := |
| 1119 |
Position.Container.Elements.EA (Position.Index); |
| 1120 |
|
| 1121 |
begin |
| 1122 |
-- An indefinite vector may contain spaces that hold no elements. |
| 1123 |
-- Any iteration over an indefinite vector with spaces will raise |
| 1124 |
-- Constraint_Error. |
| 1125 |
|
| 1126 |
if Ptr = null then |
| 1127 |
raise Constraint_Error; |
| 1128 |
|
| 1129 |
else |
| 1130 |
return Ptr; |
| 1131 |
end if; |
| 1132 |
end Get_Element_Access; |
| 1133 |
|
| 1134 |
----------------- |
| 1135 |
-- Has_Element -- |
| 1136 |
----------------- |
| 1137 |
|
| 1138 |
function Has_Element (Position : Cursor) return Boolean is |
| 1139 |
begin |
| 1140 |
if Position.Container = null then |
| 1141 |
return False; |
| 1142 |
else |
| 1143 |
return Position.Index <= Position.Container.Last; |
| 1144 |
end if; |
| 1145 |
end Has_Element; |
| 1146 |
|
| 1147 |
------------ |
| 1148 |
-- Insert -- |
| 1149 |
------------ |
| 1150 |
|
| 1151 |
procedure Insert |
| 1152 |
(Container : in out Vector; |
| 1153 |
Before : Extended_Index; |
| 1154 |
New_Item : Element_Type; |
| 1155 |
Count : Count_Type := 1) |
| 1156 |
is |
| 1157 |
Old_Length : constant Count_Type := Container.Length; |
| 1158 |
|
| 1159 |
Max_Length : Count_Type'Base; -- determined from range of Index_Type |
| 1160 |
New_Length : Count_Type'Base; -- sum of current length and Count |
| 1161 |
New_Last : Index_Type'Base; -- last index of vector after insertion |
| 1162 |
|
| 1163 |
Index : Index_Type'Base; -- scratch for intermediate values |
| 1164 |
J : Count_Type'Base; -- scratch |
| 1165 |
|
| 1166 |
New_Capacity : Count_Type'Base; -- length of new, expanded array |
| 1167 |
Dst_Last : Index_Type'Base; -- last index of new, expanded array |
| 1168 |
Dst : Elements_Access; -- new, expanded internal array |
| 1169 |
|
| 1170 |
begin |
| 1171 |
-- The tampering bits exist to prevent an item from being harmfully |
| 1172 |
-- manipulated while it is being visited. Query, Update, and Iterate |
| 1173 |
-- increment the busy count on entry, and decrement the count on |
| 1174 |
-- exit. Insert checks the count to determine whether it is being called |
| 1175 |
-- while the associated callback procedure is executing. |
| 1176 |
|
| 1177 |
TC_Check (Container.TC); |
| 1178 |
|
| 1179 |
if Checks then |
| 1180 |
-- As a precondition on the generic actual Index_Type, the base type |
| 1181 |
-- must include Index_Type'Pred (Index_Type'First); this is the value |
| 1182 |
-- that Container.Last assumes when the vector is empty. However, we |
| 1183 |
-- do not allow that as the value for Index when specifying where the |
| 1184 |
-- new items should be inserted, so we must manually check. (That the |
| 1185 |
-- user is allowed to specify the value at all here is a consequence |
| 1186 |
-- of the declaration of the Extended_Index subtype, which includes |
| 1187 |
-- the values in the base range that immediately precede and |
| 1188 |
-- immediately follow the values in the Index_Type.) |
| 1189 |
|
| 1190 |
if Before < Index_Type'First then |
| 1191 |
raise Constraint_Error with |
| 1192 |
"Before index is out of range (too small)"; |
| 1193 |
end if; |
| 1194 |
|
| 1195 |
-- We do allow a value greater than Container.Last to be specified as |
| 1196 |
-- the Index, but only if it's immediately greater. This allows for |
| 1197 |
-- the case of appending items to the back end of the vector. (It is |
| 1198 |
-- assumed that specifying an index value greater than Last + 1 |
| 1199 |
-- indicates some deeper flaw in the caller's algorithm, so that case |
| 1200 |
-- is treated as a proper error.) |
| 1201 |
|
| 1202 |
if Before > Container.Last + 1 then |
| 1203 |
raise Constraint_Error with |
| 1204 |
"Before index is out of range (too large)"; |
| 1205 |
end if; |
| 1206 |
end if; |
| 1207 |
|
| 1208 |
-- We treat inserting 0 items into the container as a no-op, even when |
| 1209 |
-- the container is busy, so we simply return. |
| 1210 |
|
| 1211 |
if Count = 0 then |
| 1212 |
return; |
| 1213 |
end if; |
| 1214 |
|
| 1215 |
-- There are two constraints we need to satisfy. The first constraint is |
| 1216 |
-- that a container cannot have more than Count_Type'Last elements, so |
| 1217 |
-- we must check the sum of the current length and the insertion count. |
| 1218 |
-- Note: we cannot simply add these values, because of the possibility |
| 1219 |
-- of overflow. |
| 1220 |
|
| 1221 |
if Checks and then Old_Length > Count_Type'Last - Count then |
| 1222 |
raise Constraint_Error with "Count is out of range"; |
| 1223 |
end if; |
| 1224 |
|
| 1225 |
-- It is now safe compute the length of the new vector, without fear of |
| 1226 |
-- overflow. |
| 1227 |
|
| 1228 |
New_Length := Old_Length + Count; |
| 1229 |
|
| 1230 |
-- The second constraint is that the new Last index value cannot exceed |
| 1231 |
-- Index_Type'Last. In each branch below, we calculate the maximum |
| 1232 |
-- length (computed from the range of values in Index_Type), and then |
| 1233 |
-- compare the new length to the maximum length. If the new length is |
| 1234 |
-- acceptable, then we compute the new last index from that. |
| 1235 |
|
| 1236 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1237 |
|
| 1238 |
-- We have to handle the case when there might be more values in the |
| 1239 |
-- range of Index_Type than in the range of Count_Type. |
| 1240 |
|
| 1241 |
if Index_Type'First <= 0 then |
| 1242 |
|
| 1243 |
-- We know that No_Index (the same as Index_Type'First - 1) is |
| 1244 |
-- less than 0, so it is safe to compute the following sum without |
| 1245 |
-- fear of overflow. |
| 1246 |
|
| 1247 |
Index := No_Index + Index_Type'Base (Count_Type'Last); |
| 1248 |
|
| 1249 |
if Index <= Index_Type'Last then |
| 1250 |
|
| 1251 |
-- We have determined that range of Index_Type has at least as |
| 1252 |
-- many values as in Count_Type, so Count_Type'Last is the |
| 1253 |
-- maximum number of items that are allowed. |
| 1254 |
|
| 1255 |
Max_Length := Count_Type'Last; |
| 1256 |
|
| 1257 |
else |
| 1258 |
-- The range of Index_Type has fewer values than in Count_Type, |
| 1259 |
-- so the maximum number of items is computed from the range of |
| 1260 |
-- the Index_Type. |
| 1261 |
|
| 1262 |
Max_Length := Count_Type'Base (Index_Type'Last - No_Index); |
| 1263 |
end if; |
| 1264 |
|
| 1265 |
else |
| 1266 |
-- No_Index is equal or greater than 0, so we can safely compute |
| 1267 |
-- the difference without fear of overflow (which we would have to |
| 1268 |
-- worry about if No_Index were less than 0, but that case is |
| 1269 |
-- handled above). |
| 1270 |
|
| 1271 |
if Index_Type'Last - No_Index >= Count_Type_Last then |
| 1272 |
-- We have determined that range of Index_Type has at least as |
| 1273 |
-- many values as in Count_Type, so Count_Type'Last is the |
| 1274 |
-- maximum number of items that are allowed. |
| 1275 |
|
| 1276 |
Max_Length := Count_Type'Last; |
| 1277 |
|
| 1278 |
else |
| 1279 |
-- The range of Index_Type has fewer values than in Count_Type, |
| 1280 |
-- so the maximum number of items is computed from the range of |
| 1281 |
-- the Index_Type. |
| 1282 |
|
| 1283 |
Max_Length := Count_Type'Base (Index_Type'Last - No_Index); |
| 1284 |
end if; |
| 1285 |
end if; |
| 1286 |
|
| 1287 |
elsif Index_Type'First <= 0 then |
| 1288 |
|
| 1289 |
-- We know that No_Index (the same as Index_Type'First - 1) is less |
| 1290 |
-- than 0, so it is safe to compute the following sum without fear of |
| 1291 |
-- overflow. |
| 1292 |
|
| 1293 |
J := Count_Type'Base (No_Index) + Count_Type'Last; |
| 1294 |
|
| 1295 |
if J <= Count_Type'Base (Index_Type'Last) then |
| 1296 |
|
| 1297 |
-- We have determined that range of Index_Type has at least as |
| 1298 |
-- many values as in Count_Type, so Count_Type'Last is the maximum |
| 1299 |
-- number of items that are allowed. |
| 1300 |
|
| 1301 |
Max_Length := Count_Type'Last; |
| 1302 |
|
| 1303 |
else |
| 1304 |
-- The range of Index_Type has fewer values than Count_Type does, |
| 1305 |
-- so the maximum number of items is computed from the range of |
| 1306 |
-- the Index_Type. |
| 1307 |
|
| 1308 |
Max_Length := |
| 1309 |
Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index); |
| 1310 |
end if; |
| 1311 |
|
| 1312 |
else |
| 1313 |
-- No_Index is equal or greater than 0, so we can safely compute the |
| 1314 |
-- difference without fear of overflow (which we would have to worry |
| 1315 |
-- about if No_Index were less than 0, but that case is handled |
| 1316 |
-- above). |
| 1317 |
|
| 1318 |
Max_Length := |
| 1319 |
Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index); |
| 1320 |
end if; |
| 1321 |
|
| 1322 |
-- We have just computed the maximum length (number of items). We must |
| 1323 |
-- now compare the requested length to the maximum length, as we do not |
| 1324 |
-- allow a vector expand beyond the maximum (because that would create |
| 1325 |
-- an internal array with a last index value greater than |
| 1326 |
-- Index_Type'Last, with no way to index those elements). |
| 1327 |
|
| 1328 |
if Checks and then New_Length > Max_Length then |
| 1329 |
raise Constraint_Error with "Count is out of range"; |
| 1330 |
end if; |
| 1331 |
|
| 1332 |
-- New_Last is the last index value of the items in the container after |
| 1333 |
-- insertion. Use the wider of Index_Type'Base and Count_Type'Base to |
| 1334 |
-- compute its value from the New_Length. |
| 1335 |
|
| 1336 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1337 |
New_Last := No_Index + Index_Type'Base (New_Length); |
| 1338 |
else |
| 1339 |
New_Last := Index_Type'Base (Count_Type'Base (No_Index) + New_Length); |
| 1340 |
end if; |
| 1341 |
|
| 1342 |
if Container.Elements = null then |
| 1343 |
pragma Assert (Container.Last = No_Index); |
| 1344 |
|
| 1345 |
-- This is the simplest case, with which we must always begin: we're |
| 1346 |
-- inserting items into an empty vector that hasn't allocated an |
| 1347 |
-- internal array yet. Note that we don't need to check the busy bit |
| 1348 |
-- here, because an empty container cannot be busy. |
| 1349 |
|
| 1350 |
-- In an indefinite vector, elements are allocated individually, and |
| 1351 |
-- stored as access values on the internal array (the length of which |
| 1352 |
-- represents the vector "capacity"), which is separately allocated. |
| 1353 |
|
| 1354 |
Container.Elements := new Elements_Type (New_Last); |
| 1355 |
|
| 1356 |
-- The element backbone has been successfully allocated, so now we |
| 1357 |
-- allocate the elements. |
| 1358 |
|
| 1359 |
for Idx in Container.Elements.EA'Range loop |
| 1360 |
|
| 1361 |
-- In order to preserve container invariants, we always attempt |
| 1362 |
-- the element allocation first, before setting the Last index |
| 1363 |
-- value, in case the allocation fails (either because there is no |
| 1364 |
-- storage available, or because element initialization fails). |
| 1365 |
|
| 1366 |
declare |
| 1367 |
-- The element allocator may need an accessibility check in the |
| 1368 |
-- case actual type is class-wide or has access discriminants |
| 1369 |
-- (see RM 4.8(10.1) and AI12-0035). |
| 1370 |
|
| 1371 |
pragma Unsuppress (Accessibility_Check); |
| 1372 |
|
| 1373 |
begin |
| 1374 |
Container.Elements.EA (Idx) := new Element_Type'(New_Item); |
| 1375 |
end; |
| 1376 |
|
| 1377 |
-- The allocation of the element succeeded, so it is now safe to |
| 1378 |
-- update the Last index, restoring container invariants. |
| 1379 |
|
| 1380 |
Container.Last := Idx; |
| 1381 |
end loop; |
| 1382 |
|
| 1383 |
return; |
| 1384 |
end if; |
| 1385 |
|
| 1386 |
if New_Length <= Container.Elements.EA'Length then |
| 1387 |
|
| 1388 |
-- In this case, we're inserting elements into a vector that has |
| 1389 |
-- already allocated an internal array, and the existing array has |
| 1390 |
-- enough unused storage for the new items. |
| 1391 |
|
| 1392 |
declare |
| 1393 |
E : Elements_Array renames Container.Elements.EA; |
| 1394 |
K : Index_Type'Base; |
| 1395 |
|
| 1396 |
begin |
| 1397 |
if Before > Container.Last then |
| 1398 |
|
| 1399 |
-- The new items are being appended to the vector, so no |
| 1400 |
-- sliding of existing elements is required. |
| 1401 |
|
| 1402 |
for Idx in Before .. New_Last loop |
| 1403 |
|
| 1404 |
-- In order to preserve container invariants, we always |
| 1405 |
-- attempt the element allocation first, before setting the |
| 1406 |
-- Last index value, in case the allocation fails (either |
| 1407 |
-- because there is no storage available, or because element |
| 1408 |
-- initialization fails). |
| 1409 |
|
| 1410 |
declare |
| 1411 |
-- The element allocator may need an accessibility check |
| 1412 |
-- in case the actual type is class-wide or has access |
| 1413 |
-- discriminants (see RM 4.8(10.1) and AI12-0035). |
| 1414 |
|
| 1415 |
pragma Unsuppress (Accessibility_Check); |
| 1416 |
|
| 1417 |
begin |
| 1418 |
E (Idx) := new Element_Type'(New_Item); |
| 1419 |
end; |
| 1420 |
|
| 1421 |
-- The allocation of the element succeeded, so it is now |
| 1422 |
-- safe to update the Last index, restoring container |
| 1423 |
-- invariants. |
| 1424 |
|
| 1425 |
Container.Last := Idx; |
| 1426 |
end loop; |
| 1427 |
|
| 1428 |
else |
| 1429 |
-- The new items are being inserted before some existing |
| 1430 |
-- elements, so we must slide the existing elements up to their |
| 1431 |
-- new home. We use the wider of Index_Type'Base and |
| 1432 |
-- Count_Type'Base as the type for intermediate index values. |
| 1433 |
|
| 1434 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1435 |
Index := Before + Index_Type'Base (Count); |
| 1436 |
else |
| 1437 |
Index := Index_Type'Base (Count_Type'Base (Before) + Count); |
| 1438 |
end if; |
| 1439 |
|
| 1440 |
-- The new items are being inserted in the middle of the array, |
| 1441 |
-- in the range [Before, Index). Copy the existing elements to |
| 1442 |
-- the end of the array, to make room for the new items. |
| 1443 |
|
| 1444 |
E (Index .. New_Last) := E (Before .. Container.Last); |
| 1445 |
Container.Last := New_Last; |
| 1446 |
|
| 1447 |
-- We have copied the existing items up to the end of the |
| 1448 |
-- array, to make room for the new items in the middle of |
| 1449 |
-- the array. Now we actually allocate the new items. |
| 1450 |
|
| 1451 |
-- Note: initialize K outside loop to make it clear that |
| 1452 |
-- K always has a value if the exception handler triggers. |
| 1453 |
|
| 1454 |
K := Before; |
| 1455 |
|
| 1456 |
declare |
| 1457 |
-- The element allocator may need an accessibility check in |
| 1458 |
-- the case the actual type is class-wide or has access |
| 1459 |
-- discriminants (see RM 4.8(10.1) and AI12-0035). |
| 1460 |
|
| 1461 |
pragma Unsuppress (Accessibility_Check); |
| 1462 |
|
| 1463 |
begin |
| 1464 |
while K < Index loop |
| 1465 |
E (K) := new Element_Type'(New_Item); |
| 1466 |
K := K + 1; |
| 1467 |
end loop; |
| 1468 |
|
| 1469 |
exception |
| 1470 |
when others => |
| 1471 |
|
| 1472 |
-- Values in the range [Before, K) were successfully |
| 1473 |
-- allocated, but values in the range [K, Index) are |
| 1474 |
-- stale (these array positions contain copies of the |
| 1475 |
-- old items, that did not get assigned a new item, |
| 1476 |
-- because the allocation failed). We must finish what |
| 1477 |
-- we started by clearing out all of the stale values, |
| 1478 |
-- leaving a "hole" in the middle of the array. |
| 1479 |
|
| 1480 |
E (K .. Index - 1) := [others => null]; |
| 1481 |
raise; |
| 1482 |
end; |
| 1483 |
end if; |
| 1484 |
end; |
| 1485 |
|
| 1486 |
return; |
| 1487 |
end if; |
| 1488 |
|
| 1489 |
-- In this case, we're inserting elements into a vector that has already |
| 1490 |
-- allocated an internal array, but the existing array does not have |
| 1491 |
-- enough storage, so we must allocate a new, longer array. In order to |
| 1492 |
-- guarantee that the amortized insertion cost is O(1), we always |
| 1493 |
-- allocate an array whose length is some power-of-two factor of the |
| 1494 |
-- current array length. (The new array cannot have a length less than |
| 1495 |
-- the New_Length of the container, but its last index value cannot be |
| 1496 |
-- greater than Index_Type'Last.) |
| 1497 |
|
| 1498 |
New_Capacity := Count_Type'Max (1, Container.Elements.EA'Length); |
| 1499 |
while New_Capacity < New_Length loop |
| 1500 |
if New_Capacity > Count_Type'Last / 2 then |
| 1501 |
New_Capacity := Count_Type'Last; |
| 1502 |
exit; |
| 1503 |
end if; |
| 1504 |
|
| 1505 |
New_Capacity := 2 * New_Capacity; |
| 1506 |
end loop; |
| 1507 |
|
| 1508 |
if New_Capacity > Max_Length then |
| 1509 |
|
| 1510 |
-- We have reached the limit of capacity, so no further expansion |
| 1511 |
-- will occur. (This is not a problem, as there is never a need to |
| 1512 |
-- have more capacity than the maximum container length.) |
| 1513 |
|
| 1514 |
New_Capacity := Max_Length; |
| 1515 |
end if; |
| 1516 |
|
| 1517 |
-- We have computed the length of the new internal array (and this is |
| 1518 |
-- what "vector capacity" means), so use that to compute its last index. |
| 1519 |
|
| 1520 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1521 |
Dst_Last := No_Index + Index_Type'Base (New_Capacity); |
| 1522 |
else |
| 1523 |
Dst_Last := |
| 1524 |
Index_Type'Base (Count_Type'Base (No_Index) + New_Capacity); |
| 1525 |
end if; |
| 1526 |
|
| 1527 |
-- Now we allocate the new, longer internal array. If the allocation |
| 1528 |
-- fails, we have not changed any container state, so no side-effect |
| 1529 |
-- will occur as a result of propagating the exception. |
| 1530 |
|
| 1531 |
Dst := new Elements_Type (Dst_Last); |
| 1532 |
|
| 1533 |
-- We have our new internal array. All that needs to be done now is to |
| 1534 |
-- copy the existing items (if any) from the old array (the "source" |
| 1535 |
-- array) to the new array (the "destination" array), and then |
| 1536 |
-- deallocate the old array. |
| 1537 |
|
| 1538 |
declare |
| 1539 |
Src : Elements_Access := Container.Elements; |
| 1540 |
|
| 1541 |
begin |
| 1542 |
Dst.EA (Index_Type'First .. Before - 1) := |
| 1543 |
Src.EA (Index_Type'First .. Before - 1); |
| 1544 |
|
| 1545 |
if Before > Container.Last then |
| 1546 |
|
| 1547 |
-- The new items are being appended to the vector, so no |
| 1548 |
-- sliding of existing elements is required. |
| 1549 |
|
| 1550 |
-- We have copied the elements from to the old source array to the |
| 1551 |
-- new destination array, so we can now deallocate the old array. |
| 1552 |
|
| 1553 |
Container.Elements := Dst; |
| 1554 |
Free (Src); |
| 1555 |
|
| 1556 |
-- Now we append the new items. |
| 1557 |
|
| 1558 |
for Idx in Before .. New_Last loop |
| 1559 |
|
| 1560 |
-- In order to preserve container invariants, we always attempt |
| 1561 |
-- the element allocation first, before setting the Last index |
| 1562 |
-- value, in case the allocation fails (either because there |
| 1563 |
-- is no storage available, or because element initialization |
| 1564 |
-- fails). |
| 1565 |
|
| 1566 |
declare |
| 1567 |
-- The element allocator may need an accessibility check in |
| 1568 |
-- the case the actual type is class-wide or has access |
| 1569 |
-- discriminants (see RM 4.8(10.1) and AI12-0035). |
| 1570 |
|
| 1571 |
pragma Unsuppress (Accessibility_Check); |
| 1572 |
|
| 1573 |
begin |
| 1574 |
Dst.EA (Idx) := new Element_Type'(New_Item); |
| 1575 |
end; |
| 1576 |
|
| 1577 |
-- The allocation of the element succeeded, so it is now safe |
| 1578 |
-- to update the Last index, restoring container invariants. |
| 1579 |
|
| 1580 |
Container.Last := Idx; |
| 1581 |
end loop; |
| 1582 |
|
| 1583 |
else |
| 1584 |
-- The new items are being inserted before some existing elements, |
| 1585 |
-- so we must slide the existing elements up to their new home. |
| 1586 |
|
| 1587 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1588 |
Index := Before + Index_Type'Base (Count); |
| 1589 |
else |
| 1590 |
Index := Index_Type'Base (Count_Type'Base (Before) + Count); |
| 1591 |
end if; |
| 1592 |
|
| 1593 |
Dst.EA (Index .. New_Last) := Src.EA (Before .. Container.Last); |
| 1594 |
|
| 1595 |
-- We have copied the elements from to the old source array to the |
| 1596 |
-- new destination array, so we can now deallocate the old array. |
| 1597 |
|
| 1598 |
Container.Elements := Dst; |
| 1599 |
Container.Last := New_Last; |
| 1600 |
Free (Src); |
| 1601 |
|
| 1602 |
-- The new array has a range in the middle containing null access |
| 1603 |
-- values. Fill in that partition of the array with the new items. |
| 1604 |
|
| 1605 |
for Idx in Before .. Index - 1 loop |
| 1606 |
|
| 1607 |
-- Note that container invariants have already been satisfied |
| 1608 |
-- (in particular, the Last index value of the vector has |
| 1609 |
-- already been updated), so if this allocation fails we simply |
| 1610 |
-- let it propagate. |
| 1611 |
|
| 1612 |
declare |
| 1613 |
-- The element allocator may need an accessibility check in |
| 1614 |
-- the case the actual type is class-wide or has access |
| 1615 |
-- discriminants (see RM 4.8(10.1) and AI12-0035). |
| 1616 |
|
| 1617 |
pragma Unsuppress (Accessibility_Check); |
| 1618 |
|
| 1619 |
begin |
| 1620 |
Dst.EA (Idx) := new Element_Type'(New_Item); |
| 1621 |
end; |
| 1622 |
end loop; |
| 1623 |
end if; |
| 1624 |
end; |
| 1625 |
end Insert; |
| 1626 |
|
| 1627 |
procedure Insert_Vector |
| 1628 |
(Container : in out Vector; |
| 1629 |
Before : Extended_Index; |
| 1630 |
New_Item : Vector) |
| 1631 |
is |
| 1632 |
N : constant Count_Type := Length (New_Item); |
| 1633 |
J : Index_Type'Base; |
| 1634 |
|
| 1635 |
begin |
| 1636 |
-- Use Insert_Space to create the "hole" (the destination slice) into |
| 1637 |
-- which we copy the source items. |
| 1638 |
|
| 1639 |
Insert_Space (Container, Before, Count => N); |
| 1640 |
|
| 1641 |
if N = 0 then |
| 1642 |
|
| 1643 |
-- There's nothing else to do here (vetting of parameters was |
| 1644 |
-- performed already in Insert_Space), so we simply return. |
| 1645 |
|
| 1646 |
return; |
| 1647 |
end if; |
| 1648 |
|
| 1649 |
if Container'Address /= New_Item'Address then |
| 1650 |
|
| 1651 |
-- This is the simple case. New_Item denotes an object different |
| 1652 |
-- from Container, so there's nothing special we need to do to copy |
| 1653 |
-- the source items to their destination, because all of the source |
| 1654 |
-- items are contiguous. |
| 1655 |
|
| 1656 |
declare |
| 1657 |
subtype Src_Index_Subtype is Index_Type'Base range |
| 1658 |
Index_Type'First .. New_Item.Last; |
| 1659 |
|
| 1660 |
Src : Elements_Array renames |
| 1661 |
New_Item.Elements.EA (Src_Index_Subtype); |
| 1662 |
|
| 1663 |
Dst : Elements_Array renames Container.Elements.EA; |
| 1664 |
|
| 1665 |
Dst_Index : Index_Type'Base; |
| 1666 |
|
| 1667 |
begin |
| 1668 |
Dst_Index := Before - 1; |
| 1669 |
for Src_Index in Src'Range loop |
| 1670 |
Dst_Index := Dst_Index + 1; |
| 1671 |
|
| 1672 |
if Src (Src_Index) /= null then |
| 1673 |
Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all); |
| 1674 |
end if; |
| 1675 |
end loop; |
| 1676 |
end; |
| 1677 |
|
| 1678 |
return; |
| 1679 |
end if; |
| 1680 |
|
| 1681 |
-- New_Item denotes the same object as Container, so an insertion has |
| 1682 |
-- potentially split the source items. The first source slice is |
| 1683 |
-- [Index_Type'First, Before), and the second source slice is |
| 1684 |
-- [J, Container.Last], where index value J is the first index of the |
| 1685 |
-- second slice. (J gets computed below, but only after we have |
| 1686 |
-- determined that the second source slice is non-empty.) The |
| 1687 |
-- destination slice is always the range [Before, J). We perform the |
| 1688 |
-- copy in two steps, using each of the two slices of the source items. |
| 1689 |
|
| 1690 |
declare |
| 1691 |
L : constant Index_Type'Base := Before - 1; |
| 1692 |
|
| 1693 |
subtype Src_Index_Subtype is Index_Type'Base range |
| 1694 |
Index_Type'First .. L; |
| 1695 |
|
| 1696 |
Src : Elements_Array renames |
| 1697 |
Container.Elements.EA (Src_Index_Subtype); |
| 1698 |
|
| 1699 |
Dst : Elements_Array renames Container.Elements.EA; |
| 1700 |
|
| 1701 |
Dst_Index : Index_Type'Base; |
| 1702 |
|
| 1703 |
begin |
| 1704 |
-- We first copy the source items that precede the space we |
| 1705 |
-- inserted. (If Before equals Index_Type'First, then this first |
| 1706 |
-- source slice will be empty, which is harmless.) |
| 1707 |
|
| 1708 |
Dst_Index := Before - 1; |
| 1709 |
for Src_Index in Src'Range loop |
| 1710 |
Dst_Index := Dst_Index + 1; |
| 1711 |
|
| 1712 |
if Src (Src_Index) /= null then |
| 1713 |
Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all); |
| 1714 |
end if; |
| 1715 |
end loop; |
| 1716 |
|
| 1717 |
if Src'Length = N then |
| 1718 |
|
| 1719 |
-- The new items were effectively appended to the container, so we |
| 1720 |
-- have already copied all of the items that need to be copied. |
| 1721 |
-- We return early here, even though the source slice below is |
| 1722 |
-- empty (so the assignment would be harmless), because we want to |
| 1723 |
-- avoid computing J, which will overflow if J is greater than |
| 1724 |
-- Index_Type'Base'Last. |
| 1725 |
|
| 1726 |
return; |
| 1727 |
end if; |
| 1728 |
end; |
| 1729 |
|
| 1730 |
-- Index value J is the first index of the second source slice. (It is |
| 1731 |
-- also 1 greater than the last index of the destination slice.) Note: |
| 1732 |
-- avoid computing J if J is greater than Index_Type'Base'Last, in order |
| 1733 |
-- to avoid overflow. Prevent that by returning early above, immediately |
| 1734 |
-- after copying the first slice of the source, and determining that |
| 1735 |
-- this second slice of the source is empty. |
| 1736 |
|
| 1737 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1738 |
J := Before + Index_Type'Base (N); |
| 1739 |
else |
| 1740 |
J := Index_Type'Base (Count_Type'Base (Before) + N); |
| 1741 |
end if; |
| 1742 |
|
| 1743 |
declare |
| 1744 |
subtype Src_Index_Subtype is Index_Type'Base range |
| 1745 |
J .. Container.Last; |
| 1746 |
|
| 1747 |
Src : Elements_Array renames |
| 1748 |
Container.Elements.EA (Src_Index_Subtype); |
| 1749 |
|
| 1750 |
Dst : Elements_Array renames Container.Elements.EA; |
| 1751 |
|
| 1752 |
Dst_Index : Index_Type'Base; |
| 1753 |
|
| 1754 |
begin |
| 1755 |
-- We next copy the source items that follow the space we inserted. |
| 1756 |
-- Index value Dst_Index is the first index of that portion of the |
| 1757 |
-- destination that receives this slice of the source. (For the |
| 1758 |
-- reasons given above, this slice is guaranteed to be non-empty.) |
| 1759 |
|
| 1760 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 1761 |
Dst_Index := J - Index_Type'Base (Src'Length); |
| 1762 |
else |
| 1763 |
Dst_Index := Index_Type'Base (Count_Type'Base (J) - Src'Length); |
| 1764 |
end if; |
| 1765 |
|
| 1766 |
for Src_Index in Src'Range loop |
| 1767 |
if Src (Src_Index) /= null then |
| 1768 |
Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all); |
| 1769 |
end if; |
| 1770 |
|
| 1771 |
Dst_Index := Dst_Index + 1; |
| 1772 |
end loop; |
| 1773 |
end; |
| 1774 |
end Insert_Vector; |
| 1775 |
|
| 1776 |
procedure Insert_Vector |
| 1777 |
(Container : in out Vector; |
| 1778 |
Before : Cursor; |
| 1779 |
New_Item : Vector) |
| 1780 |
is |
| 1781 |
Index : Index_Type'Base; |
| 1782 |
|
| 1783 |
begin |
| 1784 |
if Checks and then Before.Container /= null |
| 1785 |
and then Before.Container /= Container'Unrestricted_Access |
| 1786 |
then |
| 1787 |
raise Program_Error with "Before cursor denotes wrong container"; |
| 1788 |
end if; |
| 1789 |
|
| 1790 |
if Is_Empty (New_Item) then |
| 1791 |
return; |
| 1792 |
end if; |
| 1793 |
|
| 1794 |
if Before.Container = null or else Before.Index > Container.Last then |
| 1795 |
if Checks and then Container.Last = Index_Type'Last then |
| 1796 |
raise Constraint_Error with |
| 1797 |
"vector is already at its maximum length"; |
| 1798 |
end if; |
| 1799 |
|
| 1800 |
Index := Container.Last + 1; |
| 1801 |
|
| 1802 |
else |
| 1803 |
Index := Before.Index; |
| 1804 |
end if; |
| 1805 |
|
| 1806 |
Insert_Vector (Container, Index, New_Item); |
| 1807 |
end Insert_Vector; |
| 1808 |
|
| 1809 |
procedure Insert_Vector |
| 1810 |
(Container : in out Vector; |
| 1811 |
Before : Cursor; |
| 1812 |
New_Item : Vector; |
| 1813 |
Position : out Cursor) |
| 1814 |
is |
| 1815 |
Index : Index_Type'Base; |
| 1816 |
|
| 1817 |
begin |
| 1818 |
if Checks and then Before.Container /= null |
| 1819 |
and then Before.Container /= Container'Unrestricted_Access |
| 1820 |
then |
| 1821 |
raise Program_Error with "Before cursor denotes wrong container"; |
| 1822 |
end if; |
| 1823 |
|
| 1824 |
if Is_Empty (New_Item) then |
| 1825 |
if Before.Container = null or else Before.Index > Container.Last then |
| 1826 |
Position := No_Element; |
| 1827 |
else |
| 1828 |
Position := (Container'Unrestricted_Access, Before.Index); |
| 1829 |
end if; |
| 1830 |
|
| 1831 |
return; |
| 1832 |
end if; |
| 1833 |
|
| 1834 |
if Before.Container = null or else Before.Index > Container.Last then |
| 1835 |
if Checks and then Container.Last = Index_Type'Last then |
| 1836 |
raise Constraint_Error with |
| 1837 |
"vector is already at its maximum length"; |
| 1838 |
end if; |
| 1839 |
|
| 1840 |
Index := Container.Last + 1; |
| 1841 |
|
| 1842 |
else |
| 1843 |
Index := Before.Index; |
| 1844 |
end if; |
| 1845 |
|
| 1846 |
Insert_Vector (Container, Index, New_Item); |
| 1847 |
|
| 1848 |
Position := (Container'Unrestricted_Access, Index); |
| 1849 |
end Insert_Vector; |
| 1850 |
|
| 1851 |
procedure Insert |
| 1852 |
(Container : in out Vector; |
| 1853 |
Before : Cursor; |
| 1854 |
New_Item : Element_Type; |
| 1855 |
Count : Count_Type := 1) |
| 1856 |
is |
| 1857 |
Index : Index_Type'Base; |
| 1858 |
|
| 1859 |
begin |
| 1860 |
if Checks and then Before.Container /= null |
| 1861 |
and then Before.Container /= Container'Unrestricted_Access |
| 1862 |
then |
| 1863 |
raise Program_Error with "Before cursor denotes wrong container"; |
| 1864 |
end if; |
| 1865 |
|
| 1866 |
if Count = 0 then |
| 1867 |
return; |
| 1868 |
end if; |
| 1869 |
|
| 1870 |
if Before.Container = null or else Before.Index > Container.Last then |
| 1871 |
if Checks and then Container.Last = Index_Type'Last then |
| 1872 |
raise Constraint_Error with |
| 1873 |
"vector is already at its maximum length"; |
| 1874 |
end if; |
| 1875 |
|
| 1876 |
Index := Container.Last + 1; |
| 1877 |
|
| 1878 |
else |
| 1879 |
Index := Before.Index; |
| 1880 |
end if; |
| 1881 |
|
| 1882 |
Insert (Container, Index, New_Item, Count); |
| 1883 |
end Insert; |
| 1884 |
|
| 1885 |
procedure Insert |
| 1886 |
(Container : in out Vector; |
| 1887 |
Before : Cursor; |
| 1888 |
New_Item : Element_Type; |
| 1889 |
Position : out Cursor; |
| 1890 |
Count : Count_Type := 1) |
| 1891 |
is |
| 1892 |
Index : Index_Type'Base; |
| 1893 |
|
| 1894 |
begin |
| 1895 |
if Checks and then Before.Container /= null |
| 1896 |
and then Before.Container /= Container'Unrestricted_Access |
| 1897 |
then |
| 1898 |
raise Program_Error with "Before cursor denotes wrong container"; |
| 1899 |
end if; |
| 1900 |
|
| 1901 |
if Count = 0 then |
| 1902 |
if Before.Container = null or else Before.Index > Container.Last then |
| 1903 |
Position := No_Element; |
| 1904 |
else |
| 1905 |
Position := (Container'Unrestricted_Access, Before.Index); |
| 1906 |
end if; |
| 1907 |
|
| 1908 |
return; |
| 1909 |
end if; |
| 1910 |
|
| 1911 |
if Before.Container = null or else Before.Index > Container.Last then |
| 1912 |
if Checks and then Container.Last = Index_Type'Last then |
| 1913 |
raise Constraint_Error with |
| 1914 |
"vector is already at its maximum length"; |
| 1915 |
end if; |
| 1916 |
|
| 1917 |
Index := Container.Last + 1; |
| 1918 |
|
| 1919 |
else |
| 1920 |
Index := Before.Index; |
| 1921 |
end if; |
| 1922 |
|
| 1923 |
Insert (Container, Index, New_Item, Count); |
| 1924 |
|
| 1925 |
Position := (Container'Unrestricted_Access, Index); |
| 1926 |
end Insert; |
| 1927 |
|
| 1928 |
------------------ |
| 1929 |
-- Insert_Space -- |
| 1930 |
------------------ |
| 1931 |
|
| 1932 |
procedure Insert_Space |
| 1933 |
(Container : in out Vector; |
| 1934 |
Before : Extended_Index; |
| 1935 |
Count : Count_Type := 1) |
| 1936 |
is |
| 1937 |
Old_Length : constant Count_Type := Container.Length; |
| 1938 |
|
| 1939 |
Max_Length : Count_Type'Base; -- determined from range of Index_Type |
| 1940 |
New_Length : Count_Type'Base; -- sum of current length and Count |
| 1941 |
New_Last : Index_Type'Base; -- last index of vector after insertion |
| 1942 |
|
| 1943 |
Index : Index_Type'Base; -- scratch for intermediate values |
| 1944 |
J : Count_Type'Base; -- scratch |
| 1945 |
|
| 1946 |
New_Capacity : Count_Type'Base; -- length of new, expanded array |
| 1947 |
Dst_Last : Index_Type'Base; -- last index of new, expanded array |
| 1948 |
Dst : Elements_Access; -- new, expanded internal array |
| 1949 |
|
| 1950 |
begin |
| 1951 |
-- The tampering bits exist to prevent an item from being harmfully |
| 1952 |
-- manipulated while it is being visited. Query, Update, and Iterate |
| 1953 |
-- increment the busy count on entry, and decrement the count on exit. |
| 1954 |
-- Insert checks the count to determine whether it is being called while |
| 1955 |
-- the associated callback procedure is executing. |
| 1956 |
|
| 1957 |
TC_Check (Container.TC); |
| 1958 |
|
| 1959 |
if Checks then |
| 1960 |
-- As a precondition on the generic actual Index_Type, the base type |
| 1961 |
-- must include Index_Type'Pred (Index_Type'First); this is the value |
| 1962 |
-- that Container.Last assumes when the vector is empty. However, we |
| 1963 |
-- do not allow that as the value for Index when specifying where the |
| 1964 |
-- new items should be inserted, so we must manually check. (That the |
| 1965 |
-- user is allowed to specify the value at all here is a consequence |
| 1966 |
-- of the declaration of the Extended_Index subtype, which includes |
| 1967 |
-- the values in the base range that immediately precede and |
| 1968 |
-- immediately follow the values in the Index_Type.) |
| 1969 |
|
| 1970 |
if Before < Index_Type'First then |
| 1971 |
raise Constraint_Error with |
| 1972 |
"Before index is out of range (too small)"; |
| 1973 |
end if; |
| 1974 |
|
| 1975 |
-- We do allow a value greater than Container.Last to be specified as |
| 1976 |
-- the Index, but only if it's immediately greater. This allows for |
| 1977 |
-- the case of appending items to the back end of the vector. (It is |
| 1978 |
-- assumed that specifying an index value greater than Last + 1 |
| 1979 |
-- indicates some deeper flaw in the caller's algorithm, so that case |
| 1980 |
-- is treated as a proper error.) |
| 1981 |
|
| 1982 |
if Before > Container.Last + 1 then |
| 1983 |
raise Constraint_Error with |
| 1984 |
"Before index is out of range (too large)"; |
| 1985 |
end if; |
| 1986 |
end if; |
| 1987 |
|
| 1988 |
-- We treat inserting 0 items into the container as a no-op, even when |
| 1989 |
-- the container is busy, so we simply return. |
| 1990 |
|
| 1991 |
if Count = 0 then |
| 1992 |
return; |
| 1993 |
end if; |
| 1994 |
|
| 1995 |
-- There are two constraints we need to satisfy. The first constraint is |
| 1996 |
-- that a container cannot have more than Count_Type'Last elements, so |
| 1997 |
-- we must check the sum of the current length and the insertion count. |
| 1998 |
-- Note: we cannot simply add these values, because of the possibility |
| 1999 |
-- of overflow. |
| 2000 |
|
| 2001 |
if Checks and then Old_Length > Count_Type'Last - Count then |
| 2002 |
raise Constraint_Error with "Count is out of range"; |
| 2003 |
end if; |
| 2004 |
|
| 2005 |
-- It is now safe compute the length of the new vector, without fear of |
| 2006 |
-- overflow. |
| 2007 |
|
| 2008 |
New_Length := Old_Length + Count; |
| 2009 |
|
| 2010 |
-- The second constraint is that the new Last index value cannot exceed |
| 2011 |
-- Index_Type'Last. In each branch below, we calculate the maximum |
| 2012 |
-- length (computed from the range of values in Index_Type), and then |
| 2013 |
-- compare the new length to the maximum length. If the new length is |
| 2014 |
-- acceptable, then we compute the new last index from that. |
| 2015 |
|
| 2016 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 2017 |
-- We have to handle the case when there might be more values in the |
| 2018 |
-- range of Index_Type than in the range of Count_Type. |
| 2019 |
|
| 2020 |
if Index_Type'First <= 0 then |
| 2021 |
|
| 2022 |
-- We know that No_Index (the same as Index_Type'First - 1) is |
| 2023 |
-- less than 0, so it is safe to compute the following sum without |
| 2024 |
-- fear of overflow. |
| 2025 |
|
| 2026 |
Index := No_Index + Index_Type'Base (Count_Type'Last); |
| 2027 |
|
| 2028 |
if Index <= Index_Type'Last then |
| 2029 |
|
| 2030 |
-- We have determined that range of Index_Type has at least as |
| 2031 |
-- many values as in Count_Type, so Count_Type'Last is the |
| 2032 |
-- maximum number of items that are allowed. |
| 2033 |
|
| 2034 |
Max_Length := Count_Type'Last; |
| 2035 |
|
| 2036 |
else |
| 2037 |
-- The range of Index_Type has fewer values than in Count_Type, |
| 2038 |
-- so the maximum number of items is computed from the range of |
| 2039 |
-- the Index_Type. |
| 2040 |
|
| 2041 |
Max_Length := Count_Type'Base (Index_Type'Last - No_Index); |
| 2042 |
end if; |
| 2043 |
|
| 2044 |
else |
| 2045 |
-- No_Index is equal or greater than 0, so we can safely compute |
| 2046 |
-- the difference without fear of overflow (which we would have to |
| 2047 |
-- worry about if No_Index were less than 0, but that case is |
| 2048 |
-- handled above). |
| 2049 |
|
| 2050 |
if Index_Type'Last - No_Index >= Count_Type_Last then |
| 2051 |
-- We have determined that range of Index_Type has at least as |
| 2052 |
-- many values as in Count_Type, so Count_Type'Last is the |
| 2053 |
-- maximum number of items that are allowed. |
| 2054 |
|
| 2055 |
Max_Length := Count_Type'Last; |
| 2056 |
|
| 2057 |
else |
| 2058 |
-- The range of Index_Type has fewer values than in Count_Type, |
| 2059 |
-- so the maximum number of items is computed from the range of |
| 2060 |
-- the Index_Type. |
| 2061 |
|
| 2062 |
Max_Length := Count_Type'Base (Index_Type'Last - No_Index); |
| 2063 |
end if; |
| 2064 |
end if; |
| 2065 |
|
| 2066 |
elsif Index_Type'First <= 0 then |
| 2067 |
|
| 2068 |
-- We know that No_Index (the same as Index_Type'First - 1) is less |
| 2069 |
-- than 0, so it is safe to compute the following sum without fear of |
| 2070 |
-- overflow. |
| 2071 |
|
| 2072 |
J := Count_Type'Base (No_Index) + Count_Type'Last; |
| 2073 |
|
| 2074 |
if J <= Count_Type'Base (Index_Type'Last) then |
| 2075 |
|
| 2076 |
-- We have determined that range of Index_Type has at least as |
| 2077 |
-- many values as in Count_Type, so Count_Type'Last is the maximum |
| 2078 |
-- number of items that are allowed. |
| 2079 |
|
| 2080 |
Max_Length := Count_Type'Last; |
| 2081 |
|
| 2082 |
else |
| 2083 |
-- The range of Index_Type has fewer values than Count_Type does, |
| 2084 |
-- so the maximum number of items is computed from the range of |
| 2085 |
-- the Index_Type. |
| 2086 |
|
| 2087 |
Max_Length := |
| 2088 |
Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index); |
| 2089 |
end if; |
| 2090 |
|
| 2091 |
else |
| 2092 |
-- No_Index is equal or greater than 0, so we can safely compute the |
| 2093 |
-- difference without fear of overflow (which we would have to worry |
| 2094 |
-- about if No_Index were less than 0, but that case is handled |
| 2095 |
-- above). |
| 2096 |
|
| 2097 |
Max_Length := |
| 2098 |
Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index); |
| 2099 |
end if; |
| 2100 |
|
| 2101 |
-- We have just computed the maximum length (number of items). We must |
| 2102 |
-- now compare the requested length to the maximum length, as we do not |
| 2103 |
-- allow a vector expand beyond the maximum (because that would create |
| 2104 |
-- an internal array with a last index value greater than |
| 2105 |
-- Index_Type'Last, with no way to index those elements). |
| 2106 |
|
| 2107 |
if Checks and then New_Length > Max_Length then |
| 2108 |
raise Constraint_Error with "Count is out of range"; |
| 2109 |
end if; |
| 2110 |
|
| 2111 |
-- New_Last is the last index value of the items in the container after |
| 2112 |
-- insertion. Use the wider of Index_Type'Base and Count_Type'Base to |
| 2113 |
-- compute its value from the New_Length. |
| 2114 |
|
| 2115 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 2116 |
New_Last := No_Index + Index_Type'Base (New_Length); |
| 2117 |
else |
| 2118 |
New_Last := Index_Type'Base (Count_Type'Base (No_Index) + New_Length); |
| 2119 |
end if; |
| 2120 |
|
| 2121 |
if Container.Elements = null then |
| 2122 |
pragma Assert (Container.Last = No_Index); |
| 2123 |
|
| 2124 |
-- This is the simplest case, with which we must always begin: we're |
| 2125 |
-- inserting items into an empty vector that hasn't allocated an |
| 2126 |
-- internal array yet. Note that we don't need to check the busy bit |
| 2127 |
-- here, because an empty container cannot be busy. |
| 2128 |
|
| 2129 |
-- In an indefinite vector, elements are allocated individually, and |
| 2130 |
-- stored as access values on the internal array (the length of which |
| 2131 |
-- represents the vector "capacity"), which is separately allocated. |
| 2132 |
-- We have no elements here (because we're inserting "space"), so all |
| 2133 |
-- we need to do is allocate the backbone. |
| 2134 |
|
| 2135 |
Container.Elements := new Elements_Type (New_Last); |
| 2136 |
Container.Last := New_Last; |
| 2137 |
|
| 2138 |
return; |
| 2139 |
end if; |
| 2140 |
|
| 2141 |
if New_Length <= Container.Elements.EA'Length then |
| 2142 |
|
| 2143 |
-- In this case, we are inserting elements into a vector that has |
| 2144 |
-- already allocated an internal array, and the existing array has |
| 2145 |
-- enough unused storage for the new items. |
| 2146 |
|
| 2147 |
declare |
| 2148 |
E : Elements_Array renames Container.Elements.EA; |
| 2149 |
|
| 2150 |
begin |
| 2151 |
if Before <= Container.Last then |
| 2152 |
|
| 2153 |
-- The new space is being inserted before some existing |
| 2154 |
-- elements, so we must slide the existing elements up to |
| 2155 |
-- their new home. We use the wider of Index_Type'Base and |
| 2156 |
-- Count_Type'Base as the type for intermediate index values. |
| 2157 |
|
| 2158 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 2159 |
Index := Before + Index_Type'Base (Count); |
| 2160 |
else |
| 2161 |
Index := Index_Type'Base (Count_Type'Base (Before) + Count); |
| 2162 |
end if; |
| 2163 |
|
| 2164 |
E (Index .. New_Last) := E (Before .. Container.Last); |
| 2165 |
E (Before .. Index - 1) := [others => null]; |
| 2166 |
end if; |
| 2167 |
end; |
| 2168 |
|
| 2169 |
Container.Last := New_Last; |
| 2170 |
return; |
| 2171 |
end if; |
| 2172 |
|
| 2173 |
-- In this case, we're inserting elements into a vector that has already |
| 2174 |
-- allocated an internal array, but the existing array does not have |
| 2175 |
-- enough storage, so we must allocate a new, longer array. In order to |
| 2176 |
-- guarantee that the amortized insertion cost is O(1), we always |
| 2177 |
-- allocate an array whose length is some power-of-two factor of the |
| 2178 |
-- current array length. (The new array cannot have a length less than |
| 2179 |
-- the New_Length of the container, but its last index value cannot be |
| 2180 |
-- greater than Index_Type'Last.) |
| 2181 |
|
| 2182 |
New_Capacity := Count_Type'Max (1, Container.Elements.EA'Length); |
| 2183 |
while New_Capacity < New_Length loop |
| 2184 |
if New_Capacity > Count_Type'Last / 2 then |
| 2185 |
New_Capacity := Count_Type'Last; |
| 2186 |
exit; |
| 2187 |
end if; |
| 2188 |
|
| 2189 |
New_Capacity := 2 * New_Capacity; |
| 2190 |
end loop; |
| 2191 |
|
| 2192 |
if New_Capacity > Max_Length then |
| 2193 |
|
| 2194 |
-- We have reached the limit of capacity, so no further expansion |
| 2195 |
-- will occur. (This is not a problem, as there is never a need to |
| 2196 |
-- have more capacity than the maximum container length.) |
| 2197 |
|
| 2198 |
New_Capacity := Max_Length; |
| 2199 |
end if; |
| 2200 |
|
| 2201 |
-- We have computed the length of the new internal array (and this is |
| 2202 |
-- what "vector capacity" means), so use that to compute its last index. |
| 2203 |
|
| 2204 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 2205 |
Dst_Last := No_Index + Index_Type'Base (New_Capacity); |
| 2206 |
else |
| 2207 |
Dst_Last := |
| 2208 |
Index_Type'Base (Count_Type'Base (No_Index) + New_Capacity); |
| 2209 |
end if; |
| 2210 |
|
| 2211 |
-- Now we allocate the new, longer internal array. If the allocation |
| 2212 |
-- fails, we have not changed any container state, so no side-effect |
| 2213 |
-- will occur as a result of propagating the exception. |
| 2214 |
|
| 2215 |
Dst := new Elements_Type (Dst_Last); |
| 2216 |
|
| 2217 |
-- We have our new internal array. All that needs to be done now is to |
| 2218 |
-- copy the existing items (if any) from the old array (the "source" |
| 2219 |
-- array) to the new array (the "destination" array), and then |
| 2220 |
-- deallocate the old array. |
| 2221 |
|
| 2222 |
declare |
| 2223 |
Src : Elements_Access := Container.Elements; |
| 2224 |
|
| 2225 |
begin |
| 2226 |
Dst.EA (Index_Type'First .. Before - 1) := |
| 2227 |
Src.EA (Index_Type'First .. Before - 1); |
| 2228 |
|
| 2229 |
if Before <= Container.Last then |
| 2230 |
|
| 2231 |
-- The new items are being inserted before some existing elements, |
| 2232 |
-- so we must slide the existing elements up to their new home. |
| 2233 |
|
| 2234 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 2235 |
Index := Before + Index_Type'Base (Count); |
| 2236 |
else |
| 2237 |
Index := Index_Type'Base (Count_Type'Base (Before) + Count); |
| 2238 |
end if; |
| 2239 |
|
| 2240 |
Dst.EA (Index .. New_Last) := Src.EA (Before .. Container.Last); |
| 2241 |
end if; |
| 2242 |
|
| 2243 |
-- We have copied the elements from to the old, source array to the |
| 2244 |
-- new, destination array, so we can now restore invariants, and |
| 2245 |
-- deallocate the old array. |
| 2246 |
|
| 2247 |
Container.Elements := Dst; |
| 2248 |
Container.Last := New_Last; |
| 2249 |
Free (Src); |
| 2250 |
end; |
| 2251 |
end Insert_Space; |
| 2252 |
|
| 2253 |
procedure Insert_Space |
| 2254 |
(Container : in out Vector; |
| 2255 |
Before : Cursor; |
| 2256 |
Position : out Cursor; |
| 2257 |
Count : Count_Type := 1) |
| 2258 |
is |
| 2259 |
Index : Index_Type'Base; |
| 2260 |
|
| 2261 |
begin |
| 2262 |
if Checks and then Before.Container /= null |
| 2263 |
and then Before.Container /= Container'Unrestricted_Access |
| 2264 |
then |
| 2265 |
raise Program_Error with "Before cursor denotes wrong container"; |
| 2266 |
end if; |
| 2267 |
|
| 2268 |
if Count = 0 then |
| 2269 |
if Before.Container = null or else Before.Index > Container.Last then |
| 2270 |
Position := No_Element; |
| 2271 |
else |
| 2272 |
Position := (Container'Unrestricted_Access, Before.Index); |
| 2273 |
end if; |
| 2274 |
|
| 2275 |
return; |
| 2276 |
end if; |
| 2277 |
|
| 2278 |
if Before.Container = null or else Before.Index > Container.Last then |
| 2279 |
if Checks and then Container.Last = Index_Type'Last then |
| 2280 |
raise Constraint_Error with |
| 2281 |
"vector is already at its maximum length"; |
| 2282 |
end if; |
| 2283 |
|
| 2284 |
Index := Container.Last + 1; |
| 2285 |
|
| 2286 |
else |
| 2287 |
Index := Before.Index; |
| 2288 |
end if; |
| 2289 |
|
| 2290 |
Insert_Space (Container, Index, Count); |
| 2291 |
|
| 2292 |
Position := (Container'Unrestricted_Access, Index); |
| 2293 |
end Insert_Space; |
| 2294 |
|
| 2295 |
-------------- |
| 2296 |
-- Is_Empty -- |
| 2297 |
-------------- |
| 2298 |
|
| 2299 |
function Is_Empty (Container : Vector) return Boolean is |
| 2300 |
begin |
| 2301 |
return Container.Last < Index_Type'First; |
| 2302 |
end Is_Empty; |
| 2303 |
|
| 2304 |
------------- |
| 2305 |
-- Iterate -- |
| 2306 |
------------- |
| 2307 |
|
| 2308 |
procedure Iterate |
| 2309 |
(Container : Vector; |
| 2310 |
Process : not null access procedure (Position : Cursor)) |
| 2311 |
is |
| 2312 |
Busy : With_Busy (Container.TC'Unrestricted_Access); |
| 2313 |
begin |
| 2314 |
for Indx in Index_Type'First .. Container.Last loop |
| 2315 |
Process (Cursor'(Container'Unrestricted_Access, Indx)); |
| 2316 |
end loop; |
| 2317 |
end Iterate; |
| 2318 |
|
| 2319 |
function Iterate |
| 2320 |
(Container : Vector) |
| 2321 |
return Vector_Iterator_Interfaces.Reversible_Iterator'Class |
| 2322 |
is |
| 2323 |
V : constant Vector_Access := Container'Unrestricted_Access; |
| 2324 |
begin |
| 2325 |
-- The value of its Index component influences the behavior of the First |
| 2326 |
-- and Last selector functions of the iterator object. When the Index |
| 2327 |
-- component is No_Index (as is the case here), this means the iterator |
| 2328 |
-- object was constructed without a start expression. This is a complete |
| 2329 |
-- iterator, meaning that the iteration starts from the (logical) |
| 2330 |
-- beginning of the sequence of items. |
| 2331 |
|
| 2332 |
-- Note: For a forward iterator, Container.First is the beginning, and |
| 2333 |
-- for a reverse iterator, Container.Last is the beginning. |
| 2334 |
|
| 2335 |
return It : constant Iterator := |
| 2336 |
(Limited_Controlled with |
| 2337 |
Container => V, |
| 2338 |
Index => No_Index) |
| 2339 |
do |
| 2340 |
Busy (Container.TC'Unrestricted_Access.all); |
| 2341 |
end return; |
| 2342 |
end Iterate; |
| 2343 |
|
| 2344 |
function Iterate |
| 2345 |
(Container : Vector; |
| 2346 |
Start : Cursor) |
| 2347 |
return Vector_Iterator_Interfaces.Reversible_Iterator'Class |
| 2348 |
is |
| 2349 |
V : constant Vector_Access := Container'Unrestricted_Access; |
| 2350 |
begin |
| 2351 |
-- It was formerly the case that when Start = No_Element, the partial |
| 2352 |
-- iterator was defined to behave the same as for a complete iterator, |
| 2353 |
-- and iterate over the entire sequence of items. However, those |
| 2354 |
-- semantics were unintuitive and arguably error-prone (it is too easy |
| 2355 |
-- to accidentally create an endless loop), and so they were changed, |
| 2356 |
-- per the ARG meeting in Denver on 2011/11. However, there was no |
| 2357 |
-- consensus about what positive meaning this corner case should have, |
| 2358 |
-- and so it was decided to simply raise an exception. This does imply, |
| 2359 |
-- however, that it is not possible to use a partial iterator to specify |
| 2360 |
-- an empty sequence of items. |
| 2361 |
|
| 2362 |
if Checks then |
| 2363 |
if Start.Container = null then |
| 2364 |
raise Constraint_Error with |
| 2365 |
"Start position for iterator equals No_Element"; |
| 2366 |
end if; |
| 2367 |
|
| 2368 |
if Start.Container /= V then |
| 2369 |
raise Program_Error with |
| 2370 |
"Start cursor of Iterate designates wrong vector"; |
| 2371 |
end if; |
| 2372 |
|
| 2373 |
if Start.Index > V.Last then |
| 2374 |
raise Constraint_Error with |
| 2375 |
"Start position for iterator equals No_Element"; |
| 2376 |
end if; |
| 2377 |
end if; |
| 2378 |
|
| 2379 |
-- The value of its Index component influences the behavior of the First |
| 2380 |
-- and Last selector functions of the iterator object. When the Index |
| 2381 |
-- component is not No_Index (as is the case here), it means that this |
| 2382 |
-- is a partial iteration, over a subset of the complete sequence of |
| 2383 |
-- items. The iterator object was constructed with a start expression, |
| 2384 |
-- indicating the position from which the iteration begins. Note that |
| 2385 |
-- the start position has the same value irrespective of whether this |
| 2386 |
-- is a forward or reverse iteration. |
| 2387 |
|
| 2388 |
return It : constant Iterator := |
| 2389 |
(Limited_Controlled with |
| 2390 |
Container => V, |
| 2391 |
Index => Start.Index) |
| 2392 |
do |
| 2393 |
Busy (Container.TC'Unrestricted_Access.all); |
| 2394 |
end return; |
| 2395 |
end Iterate; |
| 2396 |
|
| 2397 |
---------- |
| 2398 |
-- Last -- |
| 2399 |
---------- |
| 2400 |
|
| 2401 |
function Last (Container : Vector) return Cursor is |
| 2402 |
begin |
| 2403 |
if Is_Empty (Container) then |
| 2404 |
return No_Element; |
| 2405 |
end if; |
| 2406 |
|
| 2407 |
return (Container'Unrestricted_Access, Container.Last); |
| 2408 |
end Last; |
| 2409 |
|
| 2410 |
function Last (Object : Iterator) return Cursor is |
| 2411 |
begin |
| 2412 |
-- The value of the iterator object's Index component influences the |
| 2413 |
-- behavior of the Last (and First) selector function. |
| 2414 |
|
| 2415 |
-- When the Index component is No_Index, this means the iterator |
| 2416 |
-- object was constructed without a start expression, in which case the |
| 2417 |
-- (reverse) iteration starts from the (logical) beginning of the entire |
| 2418 |
-- sequence (corresponding to Container.Last, for a reverse iterator). |
| 2419 |
|
| 2420 |
-- Otherwise, this is iteration over a partial sequence of items. |
| 2421 |
-- When the Index component is not No_Index, the iterator object was |
| 2422 |
-- constructed with a start expression, that specifies the position |
| 2423 |
-- from which the (reverse) partial iteration begins. |
| 2424 |
|
| 2425 |
if Object.Index = No_Index then |
| 2426 |
return Last (Object.Container.all); |
| 2427 |
else |
| 2428 |
return Cursor'(Object.Container, Object.Index); |
| 2429 |
end if; |
| 2430 |
end Last; |
| 2431 |
|
| 2432 |
------------------ |
| 2433 |
-- Last_Element -- |
| 2434 |
------------------ |
| 2435 |
|
| 2436 |
function Last_Element (Container : Vector) return Element_Type is |
| 2437 |
begin |
| 2438 |
if Checks and then Container.Last = No_Index then |
| 2439 |
raise Constraint_Error with "Container is empty"; |
| 2440 |
end if; |
| 2441 |
|
| 2442 |
declare |
| 2443 |
EA : constant Element_Access := |
| 2444 |
Container.Elements.EA (Container.Last); |
| 2445 |
begin |
| 2446 |
if Checks and then EA = null then |
| 2447 |
raise Constraint_Error with "last element is empty"; |
| 2448 |
else |
| 2449 |
return EA.all; |
| 2450 |
end if; |
| 2451 |
end; |
| 2452 |
end Last_Element; |
| 2453 |
|
| 2454 |
---------------- |
| 2455 |
-- Last_Index -- |
| 2456 |
---------------- |
| 2457 |
|
| 2458 |
function Last_Index (Container : Vector) return Extended_Index is |
| 2459 |
begin |
| 2460 |
return Container.Last; |
| 2461 |
end Last_Index; |
| 2462 |
|
| 2463 |
------------ |
| 2464 |
-- Length -- |
| 2465 |
------------ |
| 2466 |
|
| 2467 |
function Length (Container : Vector) return Count_Type is |
| 2468 |
L : constant Index_Type'Base := Container.Last; |
| 2469 |
F : constant Index_Type := Index_Type'First; |
| 2470 |
|
| 2471 |
begin |
| 2472 |
-- The base range of the index type (Index_Type'Base) might not include |
| 2473 |
-- all values for length (Count_Type). Contrariwise, the index type |
| 2474 |
-- might include values outside the range of length. Hence we use |
| 2475 |
-- whatever type is wider for intermediate values when calculating |
| 2476 |
-- length. Note that no matter what the index type is, the maximum |
| 2477 |
-- length to which a vector is allowed to grow is always the minimum |
| 2478 |
-- of Count_Type'Last and (IT'Last - IT'First + 1). |
| 2479 |
|
| 2480 |
-- For example, an Index_Type with range -127 .. 127 is only guaranteed |
| 2481 |
-- to have a base range of -128 .. 127, but the corresponding vector |
| 2482 |
-- would have lengths in the range 0 .. 255. In this case we would need |
| 2483 |
-- to use Count_Type'Base for intermediate values. |
| 2484 |
|
| 2485 |
-- Another case would be the index range -2**63 + 1 .. -2**63 + 10. The |
| 2486 |
-- vector would have a maximum length of 10, but the index values lie |
| 2487 |
-- outside the range of Count_Type (which is only 32 bits). In this |
| 2488 |
-- case we would need to use Index_Type'Base for intermediate values. |
| 2489 |
|
| 2490 |
if Count_Type'Base'Last >= Index_Type'Pos (Index_Type'Base'Last) then |
| 2491 |
return Count_Type'Base (L) - Count_Type'Base (F) + 1; |
| 2492 |
else |
| 2493 |
return Count_Type (L - F + 1); |
| 2494 |
end if; |
| 2495 |
end Length; |
| 2496 |
|
| 2497 |
---------- |
| 2498 |
-- Move -- |
| 2499 |
---------- |
| 2500 |
|
| 2501 |
procedure Move |
| 2502 |
(Target : in out Vector; |
| 2503 |
Source : in out Vector) |
| 2504 |
is |
| 2505 |
begin |
| 2506 |
if Target'Address = Source'Address then |
| 2507 |
return; |
| 2508 |
end if; |
| 2509 |
|
| 2510 |
TC_Check (Source.TC); |
| 2511 |
|
| 2512 |
Clear (Target); -- Checks busy-bit |
| 2513 |
|
| 2514 |
declare |
| 2515 |
Target_Elements : constant Elements_Access := Target.Elements; |
| 2516 |
begin |
| 2517 |
Target.Elements := Source.Elements; |
| 2518 |
Source.Elements := Target_Elements; |
| 2519 |
end; |
| 2520 |
|
| 2521 |
Target.Last := Source.Last; |
| 2522 |
Source.Last := No_Index; |
| 2523 |
end Move; |
| 2524 |
|
| 2525 |
---------- |
| 2526 |
-- Next -- |
| 2527 |
---------- |
| 2528 |
|
| 2529 |
function Next (Position : Cursor) return Cursor is |
| 2530 |
begin |
| 2531 |
if Position.Container = null then |
| 2532 |
return No_Element; |
| 2533 |
elsif Position.Index < Position.Container.Last then |
| 2534 |
return (Position.Container, Position.Index + 1); |
| 2535 |
else |
| 2536 |
return No_Element; |
| 2537 |
end if; |
| 2538 |
end Next; |
| 2539 |
|
| 2540 |
function Next (Object : Iterator; Position : Cursor) return Cursor is |
| 2541 |
begin |
| 2542 |
if Position.Container = null then |
| 2543 |
return No_Element; |
| 2544 |
elsif Checks and then Position.Container /= Object.Container then |
| 2545 |
raise Program_Error with |
| 2546 |
"Position cursor of Next designates wrong vector"; |
| 2547 |
else |
| 2548 |
return Next (Position); |
| 2549 |
end if; |
| 2550 |
end Next; |
| 2551 |
|
| 2552 |
procedure Next (Position : in out Cursor) is |
| 2553 |
begin |
| 2554 |
if Position.Container = null then |
| 2555 |
return; |
| 2556 |
elsif Position.Index < Position.Container.Last then |
| 2557 |
Position.Index := Position.Index + 1; |
| 2558 |
else |
| 2559 |
Position := No_Element; |
| 2560 |
end if; |
| 2561 |
end Next; |
| 2562 |
|
| 2563 |
------------- |
| 2564 |
-- Prepend -- |
| 2565 |
------------- |
| 2566 |
|
| 2567 |
procedure Prepend |
| 2568 |
(Container : in out Vector; |
| 2569 |
New_Item : Element_Type; |
| 2570 |
Count : Count_Type := 1) |
| 2571 |
is |
| 2572 |
begin |
| 2573 |
Insert (Container, Index_Type'First, New_Item, Count); |
| 2574 |
end Prepend; |
| 2575 |
|
| 2576 |
------------- |
| 2577 |
-- Prepend_Vector -- |
| 2578 |
------------- |
| 2579 |
|
| 2580 |
procedure Prepend_Vector (Container : in out Vector; New_Item : Vector) is |
| 2581 |
begin |
| 2582 |
Insert_Vector (Container, Index_Type'First, New_Item); |
| 2583 |
end Prepend_Vector; |
| 2584 |
|
| 2585 |
-------------- |
| 2586 |
-- Previous -- |
| 2587 |
-------------- |
| 2588 |
|
| 2589 |
function Previous (Position : Cursor) return Cursor is |
| 2590 |
begin |
| 2591 |
if Position.Container = null then |
| 2592 |
return No_Element; |
| 2593 |
elsif Position.Index > Index_Type'First then |
| 2594 |
return (Position.Container, Position.Index - 1); |
| 2595 |
else |
| 2596 |
return No_Element; |
| 2597 |
end if; |
| 2598 |
end Previous; |
| 2599 |
|
| 2600 |
function Previous (Object : Iterator; Position : Cursor) return Cursor is |
| 2601 |
begin |
| 2602 |
if Position.Container = null then |
| 2603 |
return No_Element; |
| 2604 |
elsif Checks and then Position.Container /= Object.Container then |
| 2605 |
raise Program_Error with |
| 2606 |
"Position cursor of Previous designates wrong vector"; |
| 2607 |
else |
| 2608 |
return Previous (Position); |
| 2609 |
end if; |
| 2610 |
end Previous; |
| 2611 |
|
| 2612 |
procedure Previous (Position : in out Cursor) is |
| 2613 |
begin |
| 2614 |
if Position.Container = null then |
| 2615 |
return; |
| 2616 |
elsif Position.Index > Index_Type'First then |
| 2617 |
Position.Index := Position.Index - 1; |
| 2618 |
else |
| 2619 |
Position := No_Element; |
| 2620 |
end if; |
| 2621 |
end Previous; |
| 2622 |
|
| 2623 |
---------------------- |
| 2624 |
-- Pseudo_Reference -- |
| 2625 |
---------------------- |
| 2626 |
|
| 2627 |
function Pseudo_Reference |
| 2628 |
(Container : aliased Vector'Class) return Reference_Control_Type |
| 2629 |
is |
| 2630 |
TC : constant Tamper_Counts_Access := Container.TC'Unrestricted_Access; |
| 2631 |
begin |
| 2632 |
return R : constant Reference_Control_Type := (Controlled with TC) do |
| 2633 |
Busy (TC.all); |
| 2634 |
end return; |
| 2635 |
end Pseudo_Reference; |
| 2636 |
|
| 2637 |
------------------- |
| 2638 |
-- Query_Element -- |
| 2639 |
------------------- |
| 2640 |
|
| 2641 |
procedure Query_Element |
| 2642 |
(Container : Vector; |
| 2643 |
Index : Index_Type; |
| 2644 |
Process : not null access procedure (Element : Element_Type)) |
| 2645 |
is |
| 2646 |
Lock : With_Lock (Container.TC'Unrestricted_Access); |
| 2647 |
V : Vector renames Container'Unrestricted_Access.all; |
| 2648 |
|
| 2649 |
begin |
| 2650 |
if Checks and then Index > Container.Last then |
| 2651 |
raise Constraint_Error with "Index is out of range"; |
| 2652 |
end if; |
| 2653 |
|
| 2654 |
if Checks and then V.Elements.EA (Index) = null then |
| 2655 |
raise Constraint_Error with "element is null"; |
| 2656 |
end if; |
| 2657 |
|
| 2658 |
Process (V.Elements.EA (Index).all); |
| 2659 |
end Query_Element; |
| 2660 |
|
| 2661 |
procedure Query_Element |
| 2662 |
(Position : Cursor; |
| 2663 |
Process : not null access procedure (Element : Element_Type)) |
| 2664 |
is |
| 2665 |
begin |
| 2666 |
if Checks and then Position.Container = null then |
| 2667 |
raise Constraint_Error with "Position cursor has no element"; |
| 2668 |
else |
| 2669 |
Query_Element (Position.Container.all, Position.Index, Process); |
| 2670 |
end if; |
| 2671 |
end Query_Element; |
| 2672 |
|
| 2673 |
--------------- |
| 2674 |
-- Put_Image -- |
| 2675 |
--------------- |
| 2676 |
|
| 2677 |
procedure Put_Image |
| 2678 |
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Vector) |
| 2679 |
is |
| 2680 |
First_Time : Boolean := True; |
| 2681 |
use System.Put_Images; |
| 2682 |
begin |
| 2683 |
Array_Before (S); |
| 2684 |
|
| 2685 |
for X of V loop |
| 2686 |
if First_Time then |
| 2687 |
First_Time := False; |
| 2688 |
else |
| 2689 |
Simple_Array_Between (S); |
| 2690 |
end if; |
| 2691 |
|
| 2692 |
Element_Type'Put_Image (S, X); |
| 2693 |
end loop; |
| 2694 |
|
| 2695 |
Array_After (S); |
| 2696 |
end Put_Image; |
| 2697 |
|
| 2698 |
---------- |
| 2699 |
-- Read -- |
| 2700 |
---------- |
| 2701 |
|
| 2702 |
procedure Read |
| 2703 |
(Stream : not null access Root_Stream_Type'Class; |
| 2704 |
Container : out Vector) |
| 2705 |
is |
| 2706 |
Length : Count_Type'Base; |
| 2707 |
Last : Index_Type'Base := Index_Type'Pred (Index_Type'First); |
| 2708 |
B : Boolean; |
| 2709 |
|
| 2710 |
begin |
| 2711 |
Clear (Container); |
| 2712 |
|
| 2713 |
Count_Type'Base'Read (Stream, Length); |
| 2714 |
|
| 2715 |
if Length > Capacity (Container) then |
| 2716 |
Reserve_Capacity (Container, Capacity => Length); |
| 2717 |
end if; |
| 2718 |
|
| 2719 |
for J in Count_Type range 1 .. Length loop |
| 2720 |
Last := Last + 1; |
| 2721 |
|
| 2722 |
Boolean'Read (Stream, B); |
| 2723 |
|
| 2724 |
if B then |
| 2725 |
Container.Elements.EA (Last) := |
| 2726 |
new Element_Type'(Element_Type'Input (Stream)); |
| 2727 |
end if; |
| 2728 |
|
| 2729 |
Container.Last := Last; |
| 2730 |
end loop; |
| 2731 |
end Read; |
| 2732 |
|
| 2733 |
procedure Read |
| 2734 |
(Stream : not null access Root_Stream_Type'Class; |
| 2735 |
Position : out Cursor) |
| 2736 |
is |
| 2737 |
begin |
| 2738 |
raise Program_Error with "attempt to stream vector cursor"; |
| 2739 |
end Read; |
| 2740 |
|
| 2741 |
procedure Read |
| 2742 |
(Stream : not null access Root_Stream_Type'Class; |
| 2743 |
Item : out Reference_Type) |
| 2744 |
is |
| 2745 |
begin |
| 2746 |
raise Program_Error with "attempt to stream reference"; |
| 2747 |
end Read; |
| 2748 |
|
| 2749 |
procedure Read |
| 2750 |
(Stream : not null access Root_Stream_Type'Class; |
| 2751 |
Item : out Constant_Reference_Type) |
| 2752 |
is |
| 2753 |
begin |
| 2754 |
raise Program_Error with "attempt to stream reference"; |
| 2755 |
end Read; |
| 2756 |
|
| 2757 |
--------------- |
| 2758 |
-- Reference -- |
| 2759 |
--------------- |
| 2760 |
|
| 2761 |
function Reference |
| 2762 |
(Container : aliased in out Vector; |
| 2763 |
Position : Cursor) return Reference_Type |
| 2764 |
is |
| 2765 |
begin |
| 2766 |
if Checks then |
| 2767 |
if Position.Container = null then |
| 2768 |
raise Constraint_Error with "Position cursor has no element"; |
| 2769 |
end if; |
| 2770 |
|
| 2771 |
if Position.Container /= Container'Unrestricted_Access then |
| 2772 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 2773 |
end if; |
| 2774 |
|
| 2775 |
if Position.Index > Position.Container.Last then |
| 2776 |
raise Constraint_Error with "Position cursor is out of range"; |
| 2777 |
end if; |
| 2778 |
end if; |
| 2779 |
|
| 2780 |
declare |
| 2781 |
TC : constant Tamper_Counts_Access := |
| 2782 |
Container.TC'Unrestricted_Access; |
| 2783 |
begin |
| 2784 |
-- The following will raise Constraint_Error if Element is null |
| 2785 |
|
| 2786 |
return R : constant Reference_Type := |
| 2787 |
(Element => Container.Elements.EA (Position.Index), |
| 2788 |
Control => (Controlled with TC)) |
| 2789 |
do |
| 2790 |
Busy (TC.all); |
| 2791 |
end return; |
| 2792 |
end; |
| 2793 |
end Reference; |
| 2794 |
|
| 2795 |
function Reference |
| 2796 |
(Container : aliased in out Vector; |
| 2797 |
Index : Index_Type) return Reference_Type |
| 2798 |
is |
| 2799 |
begin |
| 2800 |
if Checks and then Index > Container.Last then |
| 2801 |
raise Constraint_Error with "Index is out of range"; |
| 2802 |
end if; |
| 2803 |
|
| 2804 |
declare |
| 2805 |
TC : constant Tamper_Counts_Access := |
| 2806 |
Container.TC'Unrestricted_Access; |
| 2807 |
begin |
| 2808 |
-- The following will raise Constraint_Error if Element is null |
| 2809 |
|
| 2810 |
return R : constant Reference_Type := |
| 2811 |
(Element => Container.Elements.EA (Index), |
| 2812 |
Control => (Controlled with TC)) |
| 2813 |
do |
| 2814 |
Busy (TC.all); |
| 2815 |
end return; |
| 2816 |
end; |
| 2817 |
end Reference; |
| 2818 |
|
| 2819 |
--------------------- |
| 2820 |
-- Replace_Element -- |
| 2821 |
--------------------- |
| 2822 |
|
| 2823 |
procedure Replace_Element |
| 2824 |
(Container : in out Vector; |
| 2825 |
Index : Index_Type; |
| 2826 |
New_Item : Element_Type) |
| 2827 |
is |
| 2828 |
begin |
| 2829 |
TE_Check (Container.TC); |
| 2830 |
|
| 2831 |
if Checks and then Index > Container.Last then |
| 2832 |
raise Constraint_Error with "Index is out of range"; |
| 2833 |
end if; |
| 2834 |
|
| 2835 |
declare |
| 2836 |
X : Element_Access := Container.Elements.EA (Index); |
| 2837 |
|
| 2838 |
-- The element allocator may need an accessibility check in the case |
| 2839 |
-- where the actual type is class-wide or has access discriminants |
| 2840 |
-- (see RM 4.8(10.1) and AI12-0035). |
| 2841 |
|
| 2842 |
pragma Unsuppress (Accessibility_Check); |
| 2843 |
|
| 2844 |
begin |
| 2845 |
Container.Elements.EA (Index) := new Element_Type'(New_Item); |
| 2846 |
Free (X); |
| 2847 |
end; |
| 2848 |
end Replace_Element; |
| 2849 |
|
| 2850 |
procedure Replace_Element |
| 2851 |
(Container : in out Vector; |
| 2852 |
Position : Cursor; |
| 2853 |
New_Item : Element_Type) |
| 2854 |
is |
| 2855 |
begin |
| 2856 |
TE_Check (Container.TC); |
| 2857 |
|
| 2858 |
if Checks then |
| 2859 |
if Position.Container = null then |
| 2860 |
raise Constraint_Error with "Position cursor has no element"; |
| 2861 |
end if; |
| 2862 |
|
| 2863 |
if Position.Container /= Container'Unrestricted_Access then |
| 2864 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 2865 |
end if; |
| 2866 |
|
| 2867 |
if Position.Index > Container.Last then |
| 2868 |
raise Constraint_Error with "Position cursor is out of range"; |
| 2869 |
end if; |
| 2870 |
end if; |
| 2871 |
|
| 2872 |
declare |
| 2873 |
X : Element_Access := Container.Elements.EA (Position.Index); |
| 2874 |
|
| 2875 |
-- The element allocator may need an accessibility check in the case |
| 2876 |
-- where the actual type is class-wide or has access discriminants |
| 2877 |
-- (see RM 4.8(10.1) and AI12-0035). |
| 2878 |
|
| 2879 |
pragma Unsuppress (Accessibility_Check); |
| 2880 |
|
| 2881 |
begin |
| 2882 |
Container.Elements.EA (Position.Index) := new Element_Type'(New_Item); |
| 2883 |
Free (X); |
| 2884 |
end; |
| 2885 |
end Replace_Element; |
| 2886 |
|
| 2887 |
---------------------- |
| 2888 |
-- Reserve_Capacity -- |
| 2889 |
---------------------- |
| 2890 |
|
| 2891 |
procedure Reserve_Capacity |
| 2892 |
(Container : in out Vector; |
| 2893 |
Capacity : Count_Type) |
| 2894 |
is |
| 2895 |
N : constant Count_Type := Length (Container); |
| 2896 |
|
| 2897 |
Index : Count_Type'Base; |
| 2898 |
Last : Index_Type'Base; |
| 2899 |
|
| 2900 |
begin |
| 2901 |
-- Reserve_Capacity can be used to either expand the storage available |
| 2902 |
-- for elements (this would be its typical use, in anticipation of |
| 2903 |
-- future insertion), or to trim back storage. In the latter case, |
| 2904 |
-- storage can only be trimmed back to the limit of the container |
| 2905 |
-- length. Note that Reserve_Capacity neither deletes (active) elements |
| 2906 |
-- nor inserts elements; it only affects container capacity, never |
| 2907 |
-- container length. |
| 2908 |
|
| 2909 |
if Capacity = 0 then |
| 2910 |
|
| 2911 |
-- This is a request to trim back storage, to the minimum amount |
| 2912 |
-- possible given the current state of the container. |
| 2913 |
|
| 2914 |
if N = 0 then |
| 2915 |
|
| 2916 |
-- The container is empty, so in this unique case we can |
| 2917 |
-- deallocate the entire internal array. Note that an empty |
| 2918 |
-- container can never be busy, so there's no need to check the |
| 2919 |
-- tampering bits. |
| 2920 |
|
| 2921 |
declare |
| 2922 |
X : Elements_Access := Container.Elements; |
| 2923 |
|
| 2924 |
begin |
| 2925 |
-- First we remove the internal array from the container, to |
| 2926 |
-- handle the case when the deallocation raises an exception |
| 2927 |
-- (although that's unlikely, since this is simply an array of |
| 2928 |
-- access values, all of which are null). |
| 2929 |
|
| 2930 |
Container.Elements := null; |
| 2931 |
|
| 2932 |
-- Container invariants have been restored, so it is now safe |
| 2933 |
-- to attempt to deallocate the internal array. |
| 2934 |
|
| 2935 |
Free (X); |
| 2936 |
end; |
| 2937 |
|
| 2938 |
elsif N < Container.Elements.EA'Length then |
| 2939 |
|
| 2940 |
-- The container is not empty, and the current length is less than |
| 2941 |
-- the current capacity, so there's storage available to trim. In |
| 2942 |
-- this case, we allocate a new internal array having a length |
| 2943 |
-- that exactly matches the number of items in the |
| 2944 |
-- container. (Reserve_Capacity does not delete active elements, |
| 2945 |
-- so this is the best we can do with respect to minimizing |
| 2946 |
-- storage). |
| 2947 |
|
| 2948 |
TC_Check (Container.TC); |
| 2949 |
|
| 2950 |
declare |
| 2951 |
subtype Array_Index_Subtype is Index_Type'Base range |
| 2952 |
Index_Type'First .. Container.Last; |
| 2953 |
|
| 2954 |
Src : Elements_Array renames |
| 2955 |
Container.Elements.EA (Array_Index_Subtype); |
| 2956 |
|
| 2957 |
X : Elements_Access := Container.Elements; |
| 2958 |
|
| 2959 |
begin |
| 2960 |
-- Although we have isolated the old internal array that we're |
| 2961 |
-- going to deallocate, we don't deallocate it until we have |
| 2962 |
-- successfully allocated a new one. If there is an exception |
| 2963 |
-- during allocation (because there is not enough storage), we |
| 2964 |
-- let it propagate without causing any side-effect. |
| 2965 |
|
| 2966 |
Container.Elements := new Elements_Type'(Container.Last, Src); |
| 2967 |
|
| 2968 |
-- We have successfully allocated a new internal array (with a |
| 2969 |
-- smaller length than the old one, and containing a copy of |
| 2970 |
-- just the active elements in the container), so we can |
| 2971 |
-- deallocate the old array. |
| 2972 |
|
| 2973 |
Free (X); |
| 2974 |
end; |
| 2975 |
end if; |
| 2976 |
|
| 2977 |
return; |
| 2978 |
end if; |
| 2979 |
|
| 2980 |
-- Reserve_Capacity can be used to expand the storage available for |
| 2981 |
-- elements, but we do not let the capacity grow beyond the number of |
| 2982 |
-- values in Index_Type'Range. (Were it otherwise, there would be no way |
| 2983 |
-- to refer to the elements with index values greater than |
| 2984 |
-- Index_Type'Last, so that storage would be wasted.) Here we compute |
| 2985 |
-- the Last index value of the new internal array, in a way that avoids |
| 2986 |
-- any possibility of overflow. |
| 2987 |
|
| 2988 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 2989 |
|
| 2990 |
-- We perform a two-part test. First we determine whether the |
| 2991 |
-- computed Last value lies in the base range of the type, and then |
| 2992 |
-- determine whether it lies in the range of the index (sub)type. |
| 2993 |
|
| 2994 |
-- Last must satisfy this relation: |
| 2995 |
-- First + Length - 1 <= Last |
| 2996 |
-- We regroup terms: |
| 2997 |
-- First - 1 <= Last - Length |
| 2998 |
-- Which can rewrite as: |
| 2999 |
-- No_Index <= Last - Length |
| 3000 |
|
| 3001 |
if Checks and then |
| 3002 |
Index_Type'Base'Last - Index_Type'Base (Capacity) < No_Index |
| 3003 |
then |
| 3004 |
raise Constraint_Error with "Capacity is out of range"; |
| 3005 |
end if; |
| 3006 |
|
| 3007 |
-- We now know that the computed value of Last is within the base |
| 3008 |
-- range of the type, so it is safe to compute its value: |
| 3009 |
|
| 3010 |
Last := No_Index + Index_Type'Base (Capacity); |
| 3011 |
|
| 3012 |
-- Finally we test whether the value is within the range of the |
| 3013 |
-- generic actual index subtype: |
| 3014 |
|
| 3015 |
if Checks and then Last > Index_Type'Last then |
| 3016 |
raise Constraint_Error with "Capacity is out of range"; |
| 3017 |
end if; |
| 3018 |
|
| 3019 |
elsif Index_Type'First <= 0 then |
| 3020 |
|
| 3021 |
-- Here we can compute Last directly, in the normal way. We know that |
| 3022 |
-- No_Index is less than 0, so there is no danger of overflow when |
| 3023 |
-- adding the (positive) value of Capacity. |
| 3024 |
|
| 3025 |
Index := Count_Type'Base (No_Index) + Capacity; -- Last |
| 3026 |
|
| 3027 |
if Checks and then Index > Count_Type'Base (Index_Type'Last) then |
| 3028 |
raise Constraint_Error with "Capacity is out of range"; |
| 3029 |
end if; |
| 3030 |
|
| 3031 |
-- We know that the computed value (having type Count_Type) of Last |
| 3032 |
-- is within the range of the generic actual index subtype, so it is |
| 3033 |
-- safe to convert to Index_Type: |
| 3034 |
|
| 3035 |
Last := Index_Type'Base (Index); |
| 3036 |
|
| 3037 |
else |
| 3038 |
-- Here Index_Type'First (and Index_Type'Last) is positive, so we |
| 3039 |
-- must test the length indirectly (by working backwards from the |
| 3040 |
-- largest possible value of Last), in order to prevent overflow. |
| 3041 |
|
| 3042 |
Index := Count_Type'Base (Index_Type'Last) - Capacity; -- No_Index |
| 3043 |
|
| 3044 |
if Checks and then Index < Count_Type'Base (No_Index) then |
| 3045 |
raise Constraint_Error with "Capacity is out of range"; |
| 3046 |
end if; |
| 3047 |
|
| 3048 |
-- We have determined that the value of Capacity would not create a |
| 3049 |
-- Last index value outside of the range of Index_Type, so we can now |
| 3050 |
-- safely compute its value. |
| 3051 |
|
| 3052 |
Last := Index_Type'Base (Count_Type'Base (No_Index) + Capacity); |
| 3053 |
end if; |
| 3054 |
|
| 3055 |
-- The requested capacity is non-zero, but we don't know yet whether |
| 3056 |
-- this is a request for expansion or contraction of storage. |
| 3057 |
|
| 3058 |
if Container.Elements = null then |
| 3059 |
|
| 3060 |
-- The container is empty (it doesn't even have an internal array), |
| 3061 |
-- so this represents a request to allocate storage having the given |
| 3062 |
-- capacity. |
| 3063 |
|
| 3064 |
Container.Elements := new Elements_Type (Last); |
| 3065 |
return; |
| 3066 |
end if; |
| 3067 |
|
| 3068 |
if Capacity <= N then |
| 3069 |
|
| 3070 |
-- This is a request to trim back storage, but only to the limit of |
| 3071 |
-- what's already in the container. (Reserve_Capacity never deletes |
| 3072 |
-- active elements, it only reclaims excess storage.) |
| 3073 |
|
| 3074 |
if N < Container.Elements.EA'Length then |
| 3075 |
|
| 3076 |
-- The container is not empty (because the requested capacity is |
| 3077 |
-- positive, and less than or equal to the container length), and |
| 3078 |
-- the current length is less than the current capacity, so there |
| 3079 |
-- is storage available to trim. In this case, we allocate a new |
| 3080 |
-- internal array having a length that exactly matches the number |
| 3081 |
-- of items in the container. |
| 3082 |
|
| 3083 |
TC_Check (Container.TC); |
| 3084 |
|
| 3085 |
declare |
| 3086 |
subtype Array_Index_Subtype is Index_Type'Base range |
| 3087 |
Index_Type'First .. Container.Last; |
| 3088 |
|
| 3089 |
Src : Elements_Array renames |
| 3090 |
Container.Elements.EA (Array_Index_Subtype); |
| 3091 |
|
| 3092 |
X : Elements_Access := Container.Elements; |
| 3093 |
|
| 3094 |
begin |
| 3095 |
-- Although we have isolated the old internal array that we're |
| 3096 |
-- going to deallocate, we don't deallocate it until we have |
| 3097 |
-- successfully allocated a new one. If there is an exception |
| 3098 |
-- during allocation (because there is not enough storage), we |
| 3099 |
-- let it propagate without causing any side-effect. |
| 3100 |
|
| 3101 |
Container.Elements := new Elements_Type'(Container.Last, Src); |
| 3102 |
|
| 3103 |
-- We have successfully allocated a new internal array (with a |
| 3104 |
-- smaller length than the old one, and containing a copy of |
| 3105 |
-- just the active elements in the container), so it is now |
| 3106 |
-- safe to deallocate the old array. |
| 3107 |
|
| 3108 |
Free (X); |
| 3109 |
end; |
| 3110 |
end if; |
| 3111 |
|
| 3112 |
return; |
| 3113 |
end if; |
| 3114 |
|
| 3115 |
-- The requested capacity is larger than the container length (the |
| 3116 |
-- number of active elements). Whether this represents a request for |
| 3117 |
-- expansion or contraction of the current capacity depends on what the |
| 3118 |
-- current capacity is. |
| 3119 |
|
| 3120 |
if Capacity = Container.Elements.EA'Length then |
| 3121 |
|
| 3122 |
-- The requested capacity matches the existing capacity, so there's |
| 3123 |
-- nothing to do here. We treat this case as a no-op, and simply |
| 3124 |
-- return without checking the busy bit. |
| 3125 |
|
| 3126 |
return; |
| 3127 |
end if; |
| 3128 |
|
| 3129 |
-- There is a change in the capacity of a non-empty container, so a new |
| 3130 |
-- internal array will be allocated. (The length of the new internal |
| 3131 |
-- array could be less or greater than the old internal array. We know |
| 3132 |
-- only that the length of the new internal array is greater than the |
| 3133 |
-- number of active elements in the container.) We must check whether |
| 3134 |
-- the container is busy before doing anything else. |
| 3135 |
|
| 3136 |
TC_Check (Container.TC); |
| 3137 |
|
| 3138 |
-- We now allocate a new internal array, having a length different from |
| 3139 |
-- its current value. |
| 3140 |
|
| 3141 |
declare |
| 3142 |
X : Elements_Access := Container.Elements; |
| 3143 |
|
| 3144 |
subtype Index_Subtype is Index_Type'Base range |
| 3145 |
Index_Type'First .. Container.Last; |
| 3146 |
|
| 3147 |
begin |
| 3148 |
-- We now allocate a new internal array, having a length different |
| 3149 |
-- from its current value. |
| 3150 |
|
| 3151 |
Container.Elements := new Elements_Type (Last); |
| 3152 |
|
| 3153 |
-- We have successfully allocated the new internal array, so now we |
| 3154 |
-- move the existing elements from the existing the old internal |
| 3155 |
-- array onto the new one. Note that we're just copying access |
| 3156 |
-- values, to this should not raise any exceptions. |
| 3157 |
|
| 3158 |
Container.Elements.EA (Index_Subtype) := X.EA (Index_Subtype); |
| 3159 |
|
| 3160 |
-- We have moved the elements from the old internal array, so now we |
| 3161 |
-- can deallocate it. |
| 3162 |
|
| 3163 |
Free (X); |
| 3164 |
end; |
| 3165 |
end Reserve_Capacity; |
| 3166 |
|
| 3167 |
---------------------- |
| 3168 |
-- Reverse_Elements -- |
| 3169 |
---------------------- |
| 3170 |
|
| 3171 |
procedure Reverse_Elements (Container : in out Vector) is |
| 3172 |
begin |
| 3173 |
if Container.Length <= 1 then |
| 3174 |
return; |
| 3175 |
end if; |
| 3176 |
|
| 3177 |
-- The exception behavior for the vector container must match that for |
| 3178 |
-- the list container, so we check for cursor tampering here (which will |
| 3179 |
-- catch more things) instead of for element tampering (which will catch |
| 3180 |
-- fewer things). It's true that the elements of this vector container |
| 3181 |
-- could be safely moved around while (say) an iteration is taking place |
| 3182 |
-- (iteration only increments the busy counter), and so technically all |
| 3183 |
-- we would need here is a test for element tampering (indicated by the |
| 3184 |
-- lock counter), that's simply an artifact of our array-based |
| 3185 |
-- implementation. Logically Reverse_Elements requires a check for |
| 3186 |
-- cursor tampering. |
| 3187 |
|
| 3188 |
TC_Check (Container.TC); |
| 3189 |
|
| 3190 |
declare |
| 3191 |
I : Index_Type; |
| 3192 |
J : Index_Type; |
| 3193 |
E : Elements_Array renames Container.Elements.EA; |
| 3194 |
|
| 3195 |
begin |
| 3196 |
I := Index_Type'First; |
| 3197 |
J := Container.Last; |
| 3198 |
while I < J loop |
| 3199 |
declare |
| 3200 |
EI : constant Element_Access := E (I); |
| 3201 |
|
| 3202 |
begin |
| 3203 |
E (I) := E (J); |
| 3204 |
E (J) := EI; |
| 3205 |
end; |
| 3206 |
|
| 3207 |
I := I + 1; |
| 3208 |
J := J - 1; |
| 3209 |
end loop; |
| 3210 |
end; |
| 3211 |
end Reverse_Elements; |
| 3212 |
|
| 3213 |
------------------ |
| 3214 |
-- Reverse_Find -- |
| 3215 |
------------------ |
| 3216 |
|
| 3217 |
function Reverse_Find |
| 3218 |
(Container : Vector; |
| 3219 |
Item : Element_Type; |
| 3220 |
Position : Cursor := No_Element) return Cursor |
| 3221 |
is |
| 3222 |
Last : Index_Type'Base; |
| 3223 |
|
| 3224 |
begin |
| 3225 |
if Checks and then Position.Container /= null |
| 3226 |
and then Position.Container /= Container'Unrestricted_Access |
| 3227 |
then |
| 3228 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 3229 |
end if; |
| 3230 |
|
| 3231 |
Last := |
| 3232 |
(if Position.Container = null or else Position.Index > Container.Last |
| 3233 |
then Container.Last |
| 3234 |
else Position.Index); |
| 3235 |
|
| 3236 |
-- Per AI05-0022, the container implementation is required to detect |
| 3237 |
-- element tampering by a generic actual subprogram. |
| 3238 |
|
| 3239 |
declare |
| 3240 |
Lock : With_Lock (Container.TC'Unrestricted_Access); |
| 3241 |
begin |
| 3242 |
for Indx in reverse Index_Type'First .. Last loop |
| 3243 |
if Container.Elements.EA (Indx) /= null |
| 3244 |
and then Container.Elements.EA (Indx).all = Item |
| 3245 |
then |
| 3246 |
return Cursor'(Container'Unrestricted_Access, Indx); |
| 3247 |
end if; |
| 3248 |
end loop; |
| 3249 |
|
| 3250 |
return No_Element; |
| 3251 |
end; |
| 3252 |
end Reverse_Find; |
| 3253 |
|
| 3254 |
------------------------ |
| 3255 |
-- Reverse_Find_Index -- |
| 3256 |
------------------------ |
| 3257 |
|
| 3258 |
function Reverse_Find_Index |
| 3259 |
(Container : Vector; |
| 3260 |
Item : Element_Type; |
| 3261 |
Index : Index_Type := Index_Type'Last) return Extended_Index |
| 3262 |
is |
| 3263 |
-- Per AI05-0022, the container implementation is required to detect |
| 3264 |
-- element tampering by a generic actual subprogram. |
| 3265 |
|
| 3266 |
Lock : With_Lock (Container.TC'Unrestricted_Access); |
| 3267 |
|
| 3268 |
Last : constant Index_Type'Base := |
| 3269 |
Index_Type'Min (Container.Last, Index); |
| 3270 |
|
| 3271 |
begin |
| 3272 |
for Indx in reverse Index_Type'First .. Last loop |
| 3273 |
if Container.Elements.EA (Indx) /= null |
| 3274 |
and then Container.Elements.EA (Indx).all = Item |
| 3275 |
then |
| 3276 |
return Indx; |
| 3277 |
end if; |
| 3278 |
end loop; |
| 3279 |
|
| 3280 |
return No_Index; |
| 3281 |
end Reverse_Find_Index; |
| 3282 |
|
| 3283 |
--------------------- |
| 3284 |
-- Reverse_Iterate -- |
| 3285 |
--------------------- |
| 3286 |
|
| 3287 |
procedure Reverse_Iterate |
| 3288 |
(Container : Vector; |
| 3289 |
Process : not null access procedure (Position : Cursor)) |
| 3290 |
is |
| 3291 |
Busy : With_Busy (Container.TC'Unrestricted_Access); |
| 3292 |
begin |
| 3293 |
for Indx in reverse Index_Type'First .. Container.Last loop |
| 3294 |
Process (Cursor'(Container'Unrestricted_Access, Indx)); |
| 3295 |
end loop; |
| 3296 |
end Reverse_Iterate; |
| 3297 |
|
| 3298 |
---------------- |
| 3299 |
-- Set_Length -- |
| 3300 |
---------------- |
| 3301 |
|
| 3302 |
procedure Set_Length (Container : in out Vector; Length : Count_Type) is |
| 3303 |
Count : constant Count_Type'Base := Container.Length - Length; |
| 3304 |
|
| 3305 |
begin |
| 3306 |
-- Set_Length allows the user to set the length explicitly, instead of |
| 3307 |
-- implicitly as a side-effect of deletion or insertion. If the |
| 3308 |
-- requested length is less than the current length, this is equivalent |
| 3309 |
-- to deleting items from the back end of the vector. If the requested |
| 3310 |
-- length is greater than the current length, then this is equivalent to |
| 3311 |
-- inserting "space" (nonce items) at the end. |
| 3312 |
|
| 3313 |
if Count >= 0 then |
| 3314 |
Container.Delete_Last (Count); |
| 3315 |
|
| 3316 |
elsif Checks and then Container.Last >= Index_Type'Last then |
| 3317 |
raise Constraint_Error with "vector is already at its maximum length"; |
| 3318 |
|
| 3319 |
else |
| 3320 |
Container.Insert_Space (Container.Last + 1, -Count); |
| 3321 |
end if; |
| 3322 |
end Set_Length; |
| 3323 |
|
| 3324 |
---------- |
| 3325 |
-- Swap -- |
| 3326 |
---------- |
| 3327 |
|
| 3328 |
procedure Swap (Container : in out Vector; I, J : Index_Type) is |
| 3329 |
begin |
| 3330 |
TE_Check (Container.TC); |
| 3331 |
|
| 3332 |
if Checks then |
| 3333 |
if I > Container.Last then |
| 3334 |
raise Constraint_Error with "I index is out of range"; |
| 3335 |
end if; |
| 3336 |
|
| 3337 |
if J > Container.Last then |
| 3338 |
raise Constraint_Error with "J index is out of range"; |
| 3339 |
end if; |
| 3340 |
end if; |
| 3341 |
|
| 3342 |
if I = J then |
| 3343 |
return; |
| 3344 |
end if; |
| 3345 |
|
| 3346 |
declare |
| 3347 |
EI : Element_Access renames Container.Elements.EA (I); |
| 3348 |
EJ : Element_Access renames Container.Elements.EA (J); |
| 3349 |
|
| 3350 |
EI_Copy : constant Element_Access := EI; |
| 3351 |
|
| 3352 |
begin |
| 3353 |
EI := EJ; |
| 3354 |
EJ := EI_Copy; |
| 3355 |
end; |
| 3356 |
end Swap; |
| 3357 |
|
| 3358 |
procedure Swap |
| 3359 |
(Container : in out Vector; |
| 3360 |
I, J : Cursor) |
| 3361 |
is |
| 3362 |
begin |
| 3363 |
if Checks then |
| 3364 |
if I.Container = null then |
| 3365 |
raise Constraint_Error with "I cursor has no element"; |
| 3366 |
end if; |
| 3367 |
|
| 3368 |
if J.Container = null then |
| 3369 |
raise Constraint_Error with "J cursor has no element"; |
| 3370 |
end if; |
| 3371 |
|
| 3372 |
if I.Container /= Container'Unrestricted_Access then |
| 3373 |
raise Program_Error with "I cursor denotes wrong container"; |
| 3374 |
end if; |
| 3375 |
|
| 3376 |
if J.Container /= Container'Unrestricted_Access then |
| 3377 |
raise Program_Error with "J cursor denotes wrong container"; |
| 3378 |
end if; |
| 3379 |
end if; |
| 3380 |
|
| 3381 |
Swap (Container, I.Index, J.Index); |
| 3382 |
end Swap; |
| 3383 |
|
| 3384 |
--------------- |
| 3385 |
-- To_Cursor -- |
| 3386 |
--------------- |
| 3387 |
|
| 3388 |
function To_Cursor |
| 3389 |
(Container : Vector; |
| 3390 |
Index : Extended_Index) return Cursor |
| 3391 |
is |
| 3392 |
begin |
| 3393 |
if Index not in Index_Type'First .. Container.Last then |
| 3394 |
return No_Element; |
| 3395 |
end if; |
| 3396 |
|
| 3397 |
return Cursor'(Container'Unrestricted_Access, Index); |
| 3398 |
end To_Cursor; |
| 3399 |
|
| 3400 |
-------------- |
| 3401 |
-- To_Index -- |
| 3402 |
-------------- |
| 3403 |
|
| 3404 |
function To_Index (Position : Cursor) return Extended_Index is |
| 3405 |
begin |
| 3406 |
if Position.Container = null then |
| 3407 |
return No_Index; |
| 3408 |
elsif Position.Index <= Position.Container.Last then |
| 3409 |
return Position.Index; |
| 3410 |
else |
| 3411 |
return No_Index; |
| 3412 |
end if; |
| 3413 |
end To_Index; |
| 3414 |
|
| 3415 |
--------------- |
| 3416 |
-- To_Vector -- |
| 3417 |
--------------- |
| 3418 |
|
| 3419 |
function To_Vector (Length : Count_Type) return Vector is |
| 3420 |
Index : Count_Type'Base; |
| 3421 |
Last : Index_Type'Base; |
| 3422 |
Elements : Elements_Access; |
| 3423 |
|
| 3424 |
begin |
| 3425 |
if Length = 0 then |
| 3426 |
return Empty_Vector; |
| 3427 |
end if; |
| 3428 |
|
| 3429 |
-- We create a vector object with a capacity that matches the specified |
| 3430 |
-- Length, but we do not allow the vector capacity (the length of the |
| 3431 |
-- internal array) to exceed the number of values in Index_Type'Range |
| 3432 |
-- (otherwise, there would be no way to refer to those components via an |
| 3433 |
-- index). We must therefore check whether the specified Length would |
| 3434 |
-- create a Last index value greater than Index_Type'Last. |
| 3435 |
|
| 3436 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 3437 |
|
| 3438 |
-- We perform a two-part test. First we determine whether the |
| 3439 |
-- computed Last value lies in the base range of the type, and then |
| 3440 |
-- determine whether it lies in the range of the index (sub)type. |
| 3441 |
|
| 3442 |
-- Last must satisfy this relation: |
| 3443 |
-- First + Length - 1 <= Last |
| 3444 |
-- We regroup terms: |
| 3445 |
-- First - 1 <= Last - Length |
| 3446 |
-- Which can rewrite as: |
| 3447 |
-- No_Index <= Last - Length |
| 3448 |
|
| 3449 |
if Checks and then |
| 3450 |
Index_Type'Base'Last - Index_Type'Base (Length) < No_Index |
| 3451 |
then |
| 3452 |
raise Constraint_Error with "Length is out of range"; |
| 3453 |
end if; |
| 3454 |
|
| 3455 |
-- We now know that the computed value of Last is within the base |
| 3456 |
-- range of the type, so it is safe to compute its value: |
| 3457 |
|
| 3458 |
Last := No_Index + Index_Type'Base (Length); |
| 3459 |
|
| 3460 |
-- Finally we test whether the value is within the range of the |
| 3461 |
-- generic actual index subtype: |
| 3462 |
|
| 3463 |
if Checks and then Last > Index_Type'Last then |
| 3464 |
raise Constraint_Error with "Length is out of range"; |
| 3465 |
end if; |
| 3466 |
|
| 3467 |
elsif Index_Type'First <= 0 then |
| 3468 |
|
| 3469 |
-- Here we can compute Last directly, in the normal way. We know that |
| 3470 |
-- No_Index is less than 0, so there is no danger of overflow when |
| 3471 |
-- adding the (positive) value of Length. |
| 3472 |
|
| 3473 |
Index := Count_Type'Base (No_Index) + Length; -- Last |
| 3474 |
|
| 3475 |
if Checks and then Index > Count_Type'Base (Index_Type'Last) then |
| 3476 |
raise Constraint_Error with "Length is out of range"; |
| 3477 |
end if; |
| 3478 |
|
| 3479 |
-- We know that the computed value (having type Count_Type) of Last |
| 3480 |
-- is within the range of the generic actual index subtype, so it is |
| 3481 |
-- safe to convert to Index_Type: |
| 3482 |
|
| 3483 |
Last := Index_Type'Base (Index); |
| 3484 |
|
| 3485 |
else |
| 3486 |
-- Here Index_Type'First (and Index_Type'Last) is positive, so we |
| 3487 |
-- must test the length indirectly (by working backwards from the |
| 3488 |
-- largest possible value of Last), in order to prevent overflow. |
| 3489 |
|
| 3490 |
Index := Count_Type'Base (Index_Type'Last) - Length; -- No_Index |
| 3491 |
|
| 3492 |
if Checks and then Index < Count_Type'Base (No_Index) then |
| 3493 |
raise Constraint_Error with "Length is out of range"; |
| 3494 |
end if; |
| 3495 |
|
| 3496 |
-- We have determined that the value of Length would not create a |
| 3497 |
-- Last index value outside of the range of Index_Type, so we can now |
| 3498 |
-- safely compute its value. |
| 3499 |
|
| 3500 |
Last := Index_Type'Base (Count_Type'Base (No_Index) + Length); |
| 3501 |
end if; |
| 3502 |
|
| 3503 |
Elements := new Elements_Type (Last); |
| 3504 |
|
| 3505 |
return Vector'(Controlled with Elements, Last, TC => <>); |
| 3506 |
end To_Vector; |
| 3507 |
|
| 3508 |
function To_Vector |
| 3509 |
(New_Item : Element_Type; |
| 3510 |
Length : Count_Type) return Vector |
| 3511 |
is |
| 3512 |
Index : Count_Type'Base; |
| 3513 |
Last : Index_Type'Base; |
| 3514 |
Elements : Elements_Access; |
| 3515 |
|
| 3516 |
begin |
| 3517 |
if Length = 0 then |
| 3518 |
return Empty_Vector; |
| 3519 |
end if; |
| 3520 |
|
| 3521 |
-- We create a vector object with a capacity that matches the specified |
| 3522 |
-- Length, but we do not allow the vector capacity (the length of the |
| 3523 |
-- internal array) to exceed the number of values in Index_Type'Range |
| 3524 |
-- (otherwise, there would be no way to refer to those components via an |
| 3525 |
-- index). We must therefore check whether the specified Length would |
| 3526 |
-- create a Last index value greater than Index_Type'Last. |
| 3527 |
|
| 3528 |
if Index_Type'Base'Last >= Count_Type_Last then |
| 3529 |
|
| 3530 |
-- We perform a two-part test. First we determine whether the |
| 3531 |
-- computed Last value lies in the base range of the type, and then |
| 3532 |
-- determine whether it lies in the range of the index (sub)type. |
| 3533 |
|
| 3534 |
-- Last must satisfy this relation: |
| 3535 |
-- First + Length - 1 <= Last |
| 3536 |
-- We regroup terms: |
| 3537 |
-- First - 1 <= Last - Length |
| 3538 |
-- Which can rewrite as: |
| 3539 |
-- No_Index <= Last - Length |
| 3540 |
|
| 3541 |
if Checks and then |
| 3542 |
Index_Type'Base'Last - Index_Type'Base (Length) < No_Index |
| 3543 |
then |
| 3544 |
raise Constraint_Error with "Length is out of range"; |
| 3545 |
end if; |
| 3546 |
|
| 3547 |
-- We now know that the computed value of Last is within the base |
| 3548 |
-- range of the type, so it is safe to compute its value: |
| 3549 |
|
| 3550 |
Last := No_Index + Index_Type'Base (Length); |
| 3551 |
|
| 3552 |
-- Finally we test whether the value is within the range of the |
| 3553 |
-- generic actual index subtype: |
| 3554 |
|
| 3555 |
if Checks and then Last > Index_Type'Last then |
| 3556 |
raise Constraint_Error with "Length is out of range"; |
| 3557 |
end if; |
| 3558 |
|
| 3559 |
elsif Index_Type'First <= 0 then |
| 3560 |
|
| 3561 |
-- Here we can compute Last directly, in the normal way. We know that |
| 3562 |
-- No_Index is less than 0, so there is no danger of overflow when |
| 3563 |
-- adding the (positive) value of Length. |
| 3564 |
|
| 3565 |
Index := Count_Type'Base (No_Index) + Length; -- Last |
| 3566 |
|
| 3567 |
if Checks and then Index > Count_Type'Base (Index_Type'Last) then |
| 3568 |
raise Constraint_Error with "Length is out of range"; |
| 3569 |
end if; |
| 3570 |
|
| 3571 |
-- We know that the computed value (having type Count_Type) of Last |
| 3572 |
-- is within the range of the generic actual index subtype, so it is |
| 3573 |
-- safe to convert to Index_Type: |
| 3574 |
|
| 3575 |
Last := Index_Type'Base (Index); |
| 3576 |
|
| 3577 |
else |
| 3578 |
-- Here Index_Type'First (and Index_Type'Last) is positive, so we |
| 3579 |
-- must test the length indirectly (by working backwards from the |
| 3580 |
-- largest possible value of Last), in order to prevent overflow. |
| 3581 |
|
| 3582 |
Index := Count_Type'Base (Index_Type'Last) - Length; -- No_Index |
| 3583 |
|
| 3584 |
if Checks and then Index < Count_Type'Base (No_Index) then |
| 3585 |
raise Constraint_Error with "Length is out of range"; |
| 3586 |
end if; |
| 3587 |
|
| 3588 |
-- We have determined that the value of Length would not create a |
| 3589 |
-- Last index value outside of the range of Index_Type, so we can now |
| 3590 |
-- safely compute its value. |
| 3591 |
|
| 3592 |
Last := Index_Type'Base (Count_Type'Base (No_Index) + Length); |
| 3593 |
end if; |
| 3594 |
|
| 3595 |
Elements := new Elements_Type (Last); |
| 3596 |
|
| 3597 |
-- We use Last as the index of the loop used to populate the internal |
| 3598 |
-- array with items. In general, we prefer to initialize the loop index |
| 3599 |
-- immediately prior to entering the loop. However, Last is also used in |
| 3600 |
-- the exception handler (to reclaim elements that have been allocated, |
| 3601 |
-- before propagating the exception), and the initialization of Last |
| 3602 |
-- after entering the block containing the handler confuses some static |
| 3603 |
-- analysis tools, with respect to whether Last has been properly |
| 3604 |
-- initialized when the handler executes. So here we initialize our loop |
| 3605 |
-- variable earlier than we prefer, before entering the block, so there |
| 3606 |
-- is no ambiguity. |
| 3607 |
|
| 3608 |
Last := Index_Type'First; |
| 3609 |
|
| 3610 |
declare |
| 3611 |
-- The element allocator may need an accessibility check in the case |
| 3612 |
-- where the actual type is class-wide or has access discriminants |
| 3613 |
-- (see RM 4.8(10.1) and AI12-0035). |
| 3614 |
|
| 3615 |
pragma Unsuppress (Accessibility_Check); |
| 3616 |
|
| 3617 |
begin |
| 3618 |
loop |
| 3619 |
Elements.EA (Last) := new Element_Type'(New_Item); |
| 3620 |
exit when Last = Elements.Last; |
| 3621 |
Last := Last + 1; |
| 3622 |
end loop; |
| 3623 |
|
| 3624 |
exception |
| 3625 |
when others => |
| 3626 |
for J in Index_Type'First .. Last - 1 loop |
| 3627 |
Free (Elements.EA (J)); |
| 3628 |
end loop; |
| 3629 |
|
| 3630 |
Free (Elements); |
| 3631 |
raise; |
| 3632 |
end; |
| 3633 |
|
| 3634 |
return (Controlled with Elements, Last, TC => <>); |
| 3635 |
end To_Vector; |
| 3636 |
|
| 3637 |
-------------------- |
| 3638 |
-- Update_Element -- |
| 3639 |
-------------------- |
| 3640 |
|
| 3641 |
procedure Update_Element |
| 3642 |
(Container : in out Vector; |
| 3643 |
Index : Index_Type; |
| 3644 |
Process : not null access procedure (Element : in out Element_Type)) |
| 3645 |
is |
| 3646 |
Lock : With_Lock (Container.TC'Unchecked_Access); |
| 3647 |
begin |
| 3648 |
if Checks and then Index > Container.Last then |
| 3649 |
raise Constraint_Error with "Index is out of range"; |
| 3650 |
end if; |
| 3651 |
|
| 3652 |
if Checks and then Container.Elements.EA (Index) = null then |
| 3653 |
raise Constraint_Error with "element is null"; |
| 3654 |
end if; |
| 3655 |
|
| 3656 |
Process (Container.Elements.EA (Index).all); |
| 3657 |
end Update_Element; |
| 3658 |
|
| 3659 |
procedure Update_Element |
| 3660 |
(Container : in out Vector; |
| 3661 |
Position : Cursor; |
| 3662 |
Process : not null access procedure (Element : in out Element_Type)) |
| 3663 |
is |
| 3664 |
begin |
| 3665 |
if Checks then |
| 3666 |
if Position.Container = null then |
| 3667 |
raise Constraint_Error with "Position cursor has no element"; |
| 3668 |
elsif Position.Container /= Container'Unrestricted_Access then |
| 3669 |
raise Program_Error with "Position cursor denotes wrong container"; |
| 3670 |
end if; |
| 3671 |
end if; |
| 3672 |
|
| 3673 |
Update_Element (Container, Position.Index, Process); |
| 3674 |
end Update_Element; |
| 3675 |
|
| 3676 |
----------- |
| 3677 |
-- Write -- |
| 3678 |
----------- |
| 3679 |
|
| 3680 |
procedure Write |
| 3681 |
(Stream : not null access Root_Stream_Type'Class; |
| 3682 |
Container : Vector) |
| 3683 |
is |
| 3684 |
N : constant Count_Type := Length (Container); |
| 3685 |
|
| 3686 |
begin |
| 3687 |
Count_Type'Base'Write (Stream, N); |
| 3688 |
|
| 3689 |
if N = 0 then |
| 3690 |
return; |
| 3691 |
end if; |
| 3692 |
|
| 3693 |
declare |
| 3694 |
E : Elements_Array renames Container.Elements.EA; |
| 3695 |
|
| 3696 |
begin |
| 3697 |
for Indx in Index_Type'First .. Container.Last loop |
| 3698 |
if E (Indx) = null then |
| 3699 |
Boolean'Write (Stream, False); |
| 3700 |
else |
| 3701 |
Boolean'Write (Stream, True); |
| 3702 |
Element_Type'Output (Stream, E (Indx).all); |
| 3703 |
end if; |
| 3704 |
end loop; |
| 3705 |
end; |
| 3706 |
end Write; |
| 3707 |
|
| 3708 |
procedure Write |
| 3709 |
(Stream : not null access Root_Stream_Type'Class; |
| 3710 |
Position : Cursor) |
| 3711 |
is |
| 3712 |
begin |
| 3713 |
raise Program_Error with "attempt to stream vector cursor"; |
| 3714 |
end Write; |
| 3715 |
|
| 3716 |
procedure Write |
| 3717 |
(Stream : not null access Root_Stream_Type'Class; |
| 3718 |
Item : Reference_Type) |
| 3719 |
is |
| 3720 |
begin |
| 3721 |
raise Program_Error with "attempt to stream reference"; |
| 3722 |
end Write; |
| 3723 |
|
| 3724 |
procedure Write |
| 3725 |
(Stream : not null access Root_Stream_Type'Class; |
| 3726 |
Item : Constant_Reference_Type) |
| 3727 |
is |
| 3728 |
begin |
| 3729 |
raise Program_Error with "attempt to stream reference"; |
| 3730 |
end Write; |
| 3731 |
|
| 3732 |
end Ada.Containers.Indefinite_Vectors; |