/usr/share/ada/adainclude/aunit/aunit-lists.adb is in libaunit2-dev 1.03-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | ------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- A U N I T . L I S T S --
-- --
-- B o d y --
-- --
-- $Revision: 1.2 $
-- --
-- Copyright (C) 2000 Ada Core Technologies, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
-- Simple linked lists. Adapted from EiffelBase LINKED_LIST
package body AUnit.Lists is
-- Local Specs:
function Last_Element (L : List) return Linkable_Access;
-- Last node
function Previous (L : List) return Linkable_Access;
-- Previous node
procedure Destroy is
new Ada.Unchecked_Deallocation (Element, Element_Access);
-- Cleanup discarded elements
procedure Destroy is
new Ada.Unchecked_Deallocation (Linkable, Linkable_Access);
-- Cleanup discarded nodes
-- Accessors:
function First (L : List) return Element is
-- First Item on list
begin
return L.First_Element.Item.all;
end First;
function Index (L : List) return Natural is
-- Current index
-- Doing it this way in case of list merging
P : Linkable_Access;
Iter : List := L;
Result : Natural := 0;
begin
if After (Iter) then
Result := Count (Iter) + 1;
elsif not Before (Iter) then
P := L.Active;
Start (Iter);
Result := 1;
while Iter.Active /= P loop
Result := Result + 1;
Forth (Iter);
end loop;
end if;
return Result;
end Index;
function Item (L : List) return Element is
-- Currrent item
begin
return L.Active.Item.all;
end Item;
function Last (L : List) return Element is
-- Last item
begin
return Last_Element (L).Item.all;
end Last;
-- Measurement:
function Count (L : List) return Natural is
-- Number of items in list
begin
return L.Count;
end Count;
-- Status report:
function Empty (L : List) return Boolean is
-- Empty list?
begin
return Count (L) = 0;
end Empty;
function After (L : List) return Boolean is
-- No valid cursor position to the right?
begin
return L.After;
end After;
function Before (L : List) return Boolean is
-- No valid cursor position to the left?
begin
return L.Before;
end Before;
function Is_First (L : List) return Boolean is
-- Cursor at first position?
begin
return not After (L) and not Before (L)
and L.Active = L.First_Element;
end Is_First;
function Is_Last (L : List) return Boolean is
-- Cursor at last position?
begin
return (not After (L) and not Before (L)
and (L.Active /= null)) and then (L.Active.Right = null);
end Is_Last;
function Off (L : List) return Boolean is
-- No current item?
begin
return L.After or else L.Before;
end Off;
-- Cursor movement:
procedure Back (L : in out List) is
-- Move to previous position
begin
if Empty (L) then
L.Before := True;
L.After := False;
elsif After (L) then
L.After := False;
elsif Is_First (L) then
L.Before := True;
else
L.Active := Previous (L);
end if;
end Back;
procedure Finish (L : in out List)is
-- Move to last position
P : Linkable_Access;
begin
if not Empty (L) then
P := L.Active;
while P.Right /= null loop
P := P.Right;
end loop;
L.Active := P;
L.After := False;
L.Before := False;
else
L.Before := True;
L.After := False;
end if;
end Finish;
procedure Forth (L : in out List) is
-- Move to next position
Old_Active : Linkable_Access;
begin
if Before (L) then
L.Before := False;
if Empty (L) then
L.After := True;
end if;
else
Old_Active := L.Active;
L.Active := L.Active.Right;
if L.Active = null then
L.Active := Old_Active;
L.After := True;
end if;
end if;
end Forth;
procedure Go_I_Th (L : in out List; I : Natural) is
-- Move to i'th position
begin
if I = 0 then
L.Before := True;
L.After := False;
L.Active := L.First_Element;
elsif I = Count (L) + 1 then
L.Before := False;
L.After := True;
L.Active := Last_Element (L);
else
Move (L, I - Index (L));
end if;
end Go_I_Th;
procedure Move (L : in out List; I : Integer) is
-- Move I positions
Counter : Natural := 0;
New_Index : Integer := 0;
P : Linkable_Access;
begin
if I > 0 then
if Before (L) then
L.Before := False;
Counter := 1;
end if;
P := L.Active;
while (Counter /= I) and then (P /= null) loop
L.Active := P;
P := P.Right;
Counter := Counter + 1;
end loop;
if P = null then
L.After := True;
else
L.Active := P;
end if;
elsif I < 0 then
New_Index := Index (L) + I;
L.Before := True;
L.After := False;
L.Active := L.First_Element;
if New_Index > 0 then
Move (L, New_Index);
end if;
end if;
end Move;
procedure Start (L : in out List) is
-- Move to first position
begin
if L.First_Element /= null then
L.Active := L.First_Element;
L.After := False;
else
L.After := True;
end if;
L.Before := False;
end Start;
-- Element change:
procedure Extend (L : in out List; E : Element) is
-- Add E to end. Do not move cursor
P : Linkable_Access := new Linkable'(new Element'(E), null);
begin
if Empty (L) then
L.First_Element := P;
L.Active := P;
else
Put_Right (Last_Element (L), P);
if After (L) then
L.Active := P;
end if;
end if;
L.Count := L.Count + 1;
end Extend;
procedure Put_Front (L : in out List; E : Element) is
-- Add E to start. Do not move cursor
P : Linkable_Access := new Linkable'(new Element'(E), null);
begin
Put_Right (P, L.First_Element);
L.First_Element := P;
if Before (L) or else Empty (L) then
L.Active := P;
end if;
L.Count := L.Count + 1;
end Put_Front;
procedure Put_Left (L : in out List; E : Element) is
-- Add E to left of cursor. Do not move cursor
P : Linkable_Access;
begin
if Empty (L) then
Put_Front (L, E);
elsif After (L) then
Back (L);
Put_Right (L, E);
Move (L, 2);
else
P := new Linkable'(L.Active.Item, null);
Put_Right (P, L.Active.Right);
L.Active.Item := new Element'(E);
Put_Right (L.Active, P);
L.Active := P;
L.Count := L.Count + 1;
end if;
end Put_Left;
procedure Put_Right (L : in out List; E : Element) is
-- Add E to right of cursor. Do not move cursor
P : Linkable_Access := new Linkable'(new Element'(E), null);
begin
if Before (L) then
Put_Right (P, L.First_Element);
L.First_Element := P;
L.Active := P;
else
Put_Right (P, L.Active.Right);
Put_Right (L.Active, P);
end if;
L.Count := L.Count + 1;
end Put_Right;
procedure Replace (L : in out List; E : Element) is
-- Replace current item with E
begin
L.Active.Item.all := E;
end Replace;
-- Removal:
procedure Remove (L : in out List) is
-- Remove current item. Move cursor to right
Removed, Succ : Linkable_Access;
begin
Removed := L.Active;
if Is_First (L) then
L.First_Element := L.First_Element.Right;
L.Active.Right := null;
L.Active := L.First_Element;
if Count (L) = 1 then
L.After := True;
end if;
elsif Is_Last (L) then
L.Active := Previous (L);
if L.Active /= null then
if L.Active /= null then
L.Active.Right := null;
end if;
end if;
L.After := True;
else
Succ := L.Active.Right;
Put_Right (Previous (L), Succ);
L.Active.Right := null;
L.Active := Succ;
end if;
Destroy (Removed.Item);
Destroy (Removed);
L.Count := L.Count - 1;
end Remove;
procedure Remove_Left (L : in out List) is
-- Remove item to left of cursor. Do not move cursor
begin
Move (L, -2);
Remove_Right (L);
Forth (L);
end Remove_Left;
procedure Remove_Right (L : in out List) is
-- Remove item to right of cursor. Do not move cursor
Removed, Succ : Linkable_Access;
begin
if Before (L) then
Removed := L.First_Element;
L.First_Element := L.First_Element.Right;
L.Active.Right := null;
L.Active := L.First_Element;
else
Succ := L.Active.Right;
Removed := Succ;
Put_Right (L.Active, Succ.Right);
Succ.Right := null;
end if;
L.Count := L.Count - 1;
Destroy (Removed.Item);
Destroy (Removed);
end Remove_Right;
procedure Wipe_Out (L : in out List) is
-- Remove all items.
begin
Start (L);
while not Off (L) loop
Remove (L);
end loop;
end Wipe_Out;
-- Local bodies:
function Last_Element (L : List) return Linkable_Access is
-- Last node
P, Result : Linkable_Access;
begin
if not Empty (L) then
Result := L.Active;
P := L.Active.Right;
while P /= null loop
Result := P;
P := P.Right;
end loop;
end if;
return Result;
end Last_Element;
function Previous (L : List) return Linkable_Access is
-- Previous node
P, Result : Linkable_Access;
begin
if After (L) then
Result := L.Active;
elsif not (Is_First (L) or Before (L)) then
P := L.First_Element;
while P.Right /= L.Active loop
P := P.Right;
end loop;
Result := P;
end if;
return Result;
end Previous;
procedure Put_Right (L : Linkable_Access; R : Linkable_Access) is
-- Add R to right of L
begin
L.Right := R;
end Put_Right;
end AUnit.Lists;
|