diff mbox

[Ada] New iterator form for GNAT.HTable.Simple_HTable

Message ID 20101011104325.GA26064@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Oct. 11, 2010, 10:43 a.m. UTC
This patch adds a new more useful version of the iterator to the simple
hash table. This new version returns both the key and value of the entry.
The following test shows it in action:

with GNAT.HTable; use GNAT.HTable;
with Text_IO; use Text_IO;
procedure TestHash is

   subtype E is Character;
   subtype K is Natural;
   subtype H is Natural range 0 .. 130;

   function Hash (F : K) return H is
   begin
      return F mod 131;
   end Hash;

   package HH is new
     GNAT.HTable.Simple_HTable
       (Header_Num => H,
        Element    => E,
        No_Element => ' ',
        Key        => K,
        Hash       => Hash,
        Equal      => "=");
   use HH;

   KK : K := 0;
   EE : E;

begin
   Set (1223, 'X');
   Set (1459, 'Y');
   Set (2342, 'Z');

   Get_First (KK, EE);
   while EE /= ' ' loop
      Put_Line (KK'Img & ' ' & EE);
      Get_Next (KK, EE);
   end loop;
end TestHash;

The output is:

 1459 Y
 1223 X
 2342 Z

Note: strictly speaking this is an invalid test since the output order is
undefined, but in practice it will never change, so the test works fine.

Tested on x86_64-pc-linux-gnu, committed on trunk

2010-10-11  Robert Dewar  <dewar@adacore.com>

	* g-htable.ads (Get_First): New procedural version for Simple_HTable
	(Get_Next): New procedural version for Simple_HTable
	* s-htable.adb (Get_First): New procedural version for Simple_HTable
	(Get_Next): New procedural version for Simple_HTable
	* s-htable.ads (Get_First): New procedural version for Simple_HTable
	(Get_Next): New procedural version for Simple_HTable
diff mbox

Patch

Index: g-htable.ads
===================================================================
--- g-htable.ads	(revision 165256)
+++ g-htable.ads	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---                     Copyright (C) 1995-2009, AdaCore                     --
+--                     Copyright (C) 1995-2010, AdaCore                     --
 --                                                                          --
 -- 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- --
@@ -111,6 +111,20 @@  package GNAT.HTable is
    --     --  same function since the last call to Get_First or No_Element if
    --     --  there is no such element. If there is no call to 'Set' in between
    --     --  Get_Next calls, all the elements of the HTable will be traversed.
+
+   --     procedure Get_First (K : out Key; E : out Element);
+   --     --  This version of the iterator returns a key/element pair. A non-
+   --     --  specified entry is returned, and there is no guarantee that two
+   --     --  calls to this procedure will return the same element.
+
+   --     procedure Get_Next (K : out Key; E : out Element);
+   --     --  This version of the iterator returns a key/element pair. It
+   --     --  returns a non-specified element that has not been returned since
+   --     --  the last call to Get_First. If there is no remaining element,
+   --     --  then E is set to No_Element, and the value in K is undefined.
+   --     --  If there is no call to Set in between Get_Next calls, all the
+   --     --  elements of the HTable will be traversed.
+
    --  end Simple_HTable;
 
    -------------------
Index: s-htable.adb
===================================================================
--- s-htable.adb	(revision 165299)
+++ s-htable.adb	(working copy)
@@ -246,6 +246,17 @@  package body System.HTable is
          end if;
       end Get_First;
 
+      procedure Get_First (K : in out Key; E : out Element) is
+         Tmp : constant Elmt_Ptr := Tab.Get_First;
+      begin
+         if Tmp = null then
+            E := No_Element;
+         else
+            K := Tmp.K;
+            E := Tmp.E;
+         end if;
+      end Get_First;
+
       -------------
       -- Get_Key --
       -------------
@@ -269,6 +280,17 @@  package body System.HTable is
          end if;
       end Get_Next;
 
+      procedure Get_Next (K : in out Key; E : out Element) is
+         Tmp : constant Elmt_Ptr := Tab.Get_Next;
+      begin
+         if Tmp = null then
+            E := No_Element;
+         else
+            K := Tmp.K;
+            E := Tmp.E;
+         end if;
+      end Get_Next;
+
       ----------
       -- Next --
       ----------
Index: s-htable.ads
===================================================================
--- s-htable.ads	(revision 165277)
+++ s-htable.ads	(working copy)
@@ -94,8 +94,24 @@  package System.HTable is
       function Get_Next return Element;
       --  Returns a non-specified element that has not been returned by the
       --  same function since the last call to Get_First or No_Element if
-      --  there is no such element. If there is no call to 'Set' in between
+      --  there is no such element. If there is no call to Set in between
       --  Get_Next calls, all the elements of the HTable will be traversed.
+
+      procedure Get_First (K : in out Key; E : out Element);
+      --  This version of the iterator returns a key/element pair. A non-
+      --  specified entry is returned, and there is no guarantee that two
+      --  calls to this procedure will return the same element. If the table
+      --  is empty, E is set to No_Element, and K is unchanged, otherwise
+      --  K and E are set to the first returned entry.
+
+      procedure Get_Next (K : in out Key; E : out Element);
+      --  This version of the iterator returns a key/element pair. It returns
+      --  a non-specified element that has not been returned since the last
+      --  call to Get_First. If there is no remaining element, then E is set
+      --  to No_Element, and the value in K is unchanged, otherwise K and E
+      --  are set to the next entry. If there is no call to Set in between
+      --  Get_Next calls, all the elements of the HTable will be traversed.
+
    end Simple_HTable;
 
    -------------------