/usr/share/ada/adainclude/xmlada/sax-htable.adb is in libxmlada4.1-dev 4.1-4.
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 | -----------------------------------------------------------------------
-- XML/Ada - An XML suite for Ada95 --
-- --
-- Copyright (C) 2004-2010, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public --
-- License as published by the Free Software Foundation; either --
-- version 2 of the License, or (at your option) any later version. --
-- --
-- This library is distributed in the hope that it will be useful, --
-- but WITHOUT 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 along with this library; if not, write to the --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330, --
-- Boston, MA 02111-1307, USA. --
-- --
-----------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
with Interfaces; use Interfaces;
package body Sax.HTable is
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
(Htable_Item, Item_Ptr);
-----------
-- Reset --
-----------
procedure Reset (Hash_Table : in out HTable) is
Item, Tmp : Item_Ptr;
begin
for Index in Hash_Table.Table'Range loop
if Hash_Table.Table (Index).Set then
Free (Hash_Table.Table (Index).Elem);
Item := Hash_Table.Table (Index).Next;
while Item /= null loop
Free (Item.Elem);
Tmp := Item;
Item := Item.Next;
Unchecked_Free (Tmp);
end loop;
Hash_Table.Table (Index).Set := False;
end if;
end loop;
end Reset;
-------------------
-- Set_With_Hash --
-------------------
procedure Set_With_Hash
(Hash_Table : in out HTable;
E : Element;
Hashed : Interfaces.Unsigned_32)
is
Index : constant Unsigned_32 := Hashed mod Hash_Table.Size + 1;
Item : Item_Ptr;
begin
if Hash_Table.Table (Index).Set then
-- Check whether we already have the item
if Equal (Get_Key (Hash_Table.Table (Index).Elem), Get_Key (E)) then
Hash_Table.Table (Index).Elem := E;
return;
else
Item := Hash_Table.Table (Index).Next;
while Item /= null loop
if Equal (Get_Key (Item.Elem), Get_Key (E)) then
Item.Elem := E;
return;
end if;
Item := Item.Next;
end loop;
end if;
Hash_Table.Table (Index).Next := new Htable_Item'
(Elem => E,
Next => Hash_Table.Table (Index).Next);
else
Hash_Table.Table (Index) :=
(Elem => E,
Next => null,
Set => True);
end if;
end Set_With_Hash;
---------
-- Set --
---------
procedure Set (Hash_Table : in out HTable; E : Element) is
begin
Set_With_Hash (Hash_Table, E, Hash (Get_Key (E)));
end Set;
---------
-- Get --
---------
function Get (Hash_Table : HTable; K : Key) return Element is
Tmp : constant Element_Ptr := Get_Ptr (Hash_Table, K);
begin
if Tmp = null then
return Empty_Element;
else
return Tmp.all;
end if;
end Get;
-------------
-- Get_Ptr --
-------------
function Get_Ptr (Hash_Table : HTable; K : Key) return Element_Ptr is
begin
return Get_Ptr_With_Hash (Hash_Table, K, Hash (K));
end Get_Ptr;
-----------------------
-- Get_Ptr_With_Hash --
-----------------------
function Get_Ptr_With_Hash
(Hash_Table : HTable;
K : Key;
Hashed : Interfaces.Unsigned_32) return Element_Ptr
is
H : constant Unsigned_32 := Hashed mod Hash_Table.Size + 1;
Elmt : Item_Ptr;
begin
if Hash_Table.Table (H).Set then
if Equal (Get_Key (Hash_Table.Table (H).Elem), K) then
return Hash_Table.Table (H).Elem'Unrestricted_Access;
else
Elmt := Hash_Table.Table (H).Next;
while Elmt /= null loop
if Equal (Get_Key (Elmt.Elem), K) then
return Elmt.Elem'Access;
end if;
Elmt := Elmt.Next;
end loop;
end if;
end if;
return null;
end Get_Ptr_With_Hash;
------------
-- Remove --
------------
procedure Remove (Hash_Table : in out HTable; K : Key) is
Index : constant Unsigned_32 :=
Hash (K) mod Hash_Table.Size + 1;
Elmt : Item_Ptr;
Next_Elmt : Item_Ptr;
begin
if not Hash_Table.Table (Index).Set then
return;
elsif Equal (Get_Key (Hash_Table.Table (Index).Elem), K) then
Free (Hash_Table.Table (Index).Elem);
Elmt := Hash_Table.Table (Index).Next; -- second element in list
if Elmt = null then
Hash_Table.Table (Index).Set := False;
else
Hash_Table.Table (Index).Elem := Elmt.Elem;
Hash_Table.Table (Index).Next := Elmt.Next; -- to third element
Unchecked_Free (Elmt); -- no longer needed, was copied to first
end if;
else
Next_Elmt := Hash_Table.Table (Index).Next;
loop
if Next_Elmt = null then
return;
elsif Equal (Get_Key (Next_Elmt.Elem), K) then
if Elmt = null then
Hash_Table.Table (Index).Next := Next_Elmt.Next;
else
Elmt.Next := Next_Elmt.Next;
end if;
Free (Next_Elmt.Elem);
Unchecked_Free (Next_Elmt);
return;
end if;
Elmt := Next_Elmt;
Next_Elmt := Elmt.Next;
end loop;
end if;
end Remove;
----------------
-- Remove_All --
----------------
procedure Remove_All (Hash_Table : in out HTable) is
Item, Item2 : Item_Ptr;
Prev : Item_Ptr;
begin
for T in Hash_Table.Table'Range loop
if Hash_Table.Table (T).Set then
-- First examine the remaining of the list in that bucket
Prev := null;
Item := Hash_Table.Table (T).Next;
while Item /= null loop
if not Preserve (Item.Elem) then
if Prev = null then
Hash_Table.Table (T).Next := Item.Next;
else
Prev.Next := Item.Next;
end if;
Item2 := Item;
Item := Item.Next; -- Prev not changed
Free (Item2.Elem);
Unchecked_Free (Item2);
else
Prev := Item;
Item := Item.Next;
end if;
end loop;
-- Then examine the bucket itself
if not Preserve (Hash_Table.Table (T).Elem) then
Free (Hash_Table.Table (T).Elem);
if Hash_Table.Table (T).Next = null then
Hash_Table.Table (T).Set := False;
else
Item := Hash_Table.Table (T).Next;
Hash_Table.Table (T).Elem := Item.Elem;
Hash_Table.Table (T).Next := Item.Next;
Unchecked_Free (Item);
end if;
end if;
end if;
end loop;
end Remove_All;
-----------
-- First --
-----------
function First (Hash_Table : HTable) return Iterator is
begin
for Index in Hash_Table.Table'Range loop
if Hash_Table.Table (Index).Set then
return (Index => Index,
Elem => Hash_Table.Table (Index).Elem'Unrestricted_Access,
Item => null);
end if;
end loop;
return No_Iterator;
end First;
----------
-- Next --
----------
procedure Next (Hash_Table : HTable; Iter : in out Iterator) is
begin
pragma Assert (Iter /= No_Iterator);
if Iter.Item = null then
Iter.Item := Hash_Table.Table (Iter.Index).Next;
else
Iter.Item := Iter.Item.Next;
end if;
if Iter.Item /= null then
Iter.Elem := Iter.Item.Elem'Unrestricted_Access;
return;
end if;
loop
Iter.Index := Unsigned_32'Succ (Iter.Index);
exit when Iter.Index > Hash_Table.Table'Last
or else Hash_Table.Table (Iter.Index).Set;
end loop;
if Iter.Index > Hash_Table.Table'Last then
Iter := No_Iterator;
else
Iter.Item := null;
Iter.Elem := Hash_Table.Table (Iter.Index).Elem'Unrestricted_Access;
end if;
end Next;
function Current (Iter : Iterator) return Element is
begin
return Iter.Elem.all;
end Current;
end Sax.HTable;
|