home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / g-htable.ads < prev    next >
Text File  |  2000-07-19  |  9KB  |  196 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                          G N A T . H T A B L E                           --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.17 $
  10. --                                                                          --
  11. --           Copyright (C) 1995-1999 Ada Core Technologies, Inc.            --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
  32. --                                                                          --
  33. ------------------------------------------------------------------------------
  34.  
  35. --  Hash table searching routines
  36.  
  37. --  This package contains two separate packages. The Simple_Htable package
  38. --  provides a very simple abstraction that asosicates one element to one
  39. --  key values and takes care of all allocation automatically using the heap.
  40. --  The Static_Htable package provides a more complex interface that allows
  41. --  complete control over allocation.
  42.  
  43. package GNAT.HTable is
  44. pragma Preelaborate (HTable);
  45.  
  46.    -------------------
  47.    -- Simple_HTable --
  48.    -------------------
  49.  
  50.    --  A simple hash table abstraction, easy to instantiate, easy to use.
  51.    --  The table associates one element to one key with the procedure Set.
  52.    --  Get retrieves the Element stored for a given Key. The efficiency of
  53.    --  retrieval is function of the size of the Table parameterized by
  54.    --  Header_Num and the hashing function Hash.
  55.  
  56.    generic
  57.       type Header_Num is range <>;
  58.       --  An integer type indicating the number and range of hash headers.
  59.  
  60.       type Element is private;
  61.       --  The type of element to be stored
  62.  
  63.       No_Element : Element;
  64.       --  The object that is returned by Get when no element has been set for
  65.       --  a given key
  66.  
  67.       type Key is private;
  68.       with function Hash  (F : Key)      return Header_Num;
  69.       with function Equal (F1, F2 : Key) return Boolean;
  70.  
  71.    package Simple_HTable is
  72.  
  73.       procedure Set (K : Key; E : Element);
  74.       --  Associates an element with a given key. Overrides any previously
  75.       --  associated element.
  76.  
  77.       procedure Reset;
  78.       --  Removes and frees all elements in the table
  79.  
  80.       function  Get (K : Key) return Element;
  81.       --  Returns the Element associated with a key or No_Element if the
  82.       --  given key has not associated element
  83.  
  84.       --  Simple iterator over the complete Htable. No specific order will
  85.       --  be chose, the only guarantee is that all elements will be
  86.       --  returned, if Get_Next is called sufficiently.
  87.  
  88.       procedure Remove (K : Key);
  89.       --  Removes the latest inserted element pointer associated with the
  90.       --  given key if any, does nothing if none.
  91.  
  92.       function Get_First return Element;
  93.       --  Returns No_Element if the Htable is empty, otherwise returns one
  94.       --  non specified element. There is no guarantee that 2 calls to this
  95.       --  function will return the same element.
  96.  
  97.       function Get_Next return Element;
  98.       --  Returns a non-specified element that has not been returned by the
  99.       --  same function since the last call to Get_First or No_Element if
  100.       --  there is no such element. If there is no call to 'Set' in between
  101.       --  Get_Next calls, all the elements of the Htable will be traversed.
  102.    end Simple_HTable;
  103.  
  104.    -------------------
  105.    -- Static_HTable --
  106.    -------------------
  107.  
  108.    --  A low-level Hash-Table abstraction, not as easy to instantiate as
  109.    --  Simple_HTable but designed to allow complete control over the
  110.    --  allocation of necessary data structures. Particularly useful when
  111.    --  dynamic allocation is not desired. The model is that "Element"
  112.    --  contains its own Key that can be retrieved by "Get_Key". Furthermore,
  113.    --  "Element" provides a link that can be used by the HTable for linking
  114.    --   elements with same hash codes:
  115.  
  116.    --       Element
  117.  
  118.    --         +-------------------+
  119.    --         |       Key         |
  120.    --         +-------------------+
  121.    --         :   other data      :
  122.    --         +-------------------+
  123.    --         |     Next Elmt     |
  124.    --         +-------------------+
  125.  
  126.    generic
  127.       type Header_Num is range <>;
  128.       --  An integer type indicating the number and range of hash headers.
  129.  
  130.       type Element (<>) is limited private;
  131.       --  The type of element to be stored
  132.  
  133.       type Elmt_Ptr is private;
  134.       --  The type used to reference an element (will usually be an access
  135.       --  type, but could be some other form of type such as an integer type).
  136.  
  137.       Null_Ptr : Elmt_Ptr;
  138.       --  The null value of the Elmt_Ptr type.
  139.  
  140.       with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
  141.       with function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
  142.       --  The type must provide an internal link for the sake of the
  143.       --  staticness of the HTable.
  144.  
  145.       type Key is limited private;
  146.       with function Get_Key (E : Elmt_Ptr) return Key;
  147.       with function Hash    (F : Key)      return Header_Num;
  148.       with function Equal   (F1, F2 : Key) return Boolean;
  149.  
  150.    package Static_HTable is
  151.  
  152.       procedure Reset;
  153.       --  Resets the hash table by setting all its elements to Null_Ptr. The
  154.       --  effect is to clear the hash table so that it can be reused. For the
  155.       --  most common case where Elmt_Ptr is an access type, and Null_Ptr is
  156.       --  null, this is only needed if the same table is reused in a new
  157.       --  context. If Elmt_Ptr is other than an access type, or Null_Ptr is
  158.       --  other than null, then Reset must be called before the first use
  159.       --  of the hash table.
  160.  
  161.       procedure Set (E : Elmt_Ptr);
  162.       --  Insert the element pointer in the HTable
  163.  
  164.       function  Get (K : Key) return Elmt_Ptr;
  165.       --  Returns the latest inserted element pointer with the given Key
  166.       --  or null if none.
  167.  
  168.       procedure Remove (K : Key);
  169.       --  Removes the latest inserted element pointer associated with the
  170.       --  given key if any, does nothing if none.
  171.  
  172.       function Get_First return Elmt_Ptr;
  173.       --  Returns Null_Ptr if the Htable is empty, otherwise returns one
  174.       --  non specified element. There is no guarantee that 2 calls to this
  175.       --  function will return the same element.
  176.  
  177.       function Get_Next return Elmt_Ptr;
  178.       --  Returns a non-specified element that has not been returned by the
  179.       --  same function since the last call to Get_First or Null_Ptr if
  180.       --  there is no such element or Get_First has bever been called. If
  181.       --  there is no call to 'Set' in between Get_Next calls, all the
  182.       --  elements of the Htable will be traversed.
  183.    end Static_HTable;
  184.  
  185.    ----------
  186.    -- Hash --
  187.    ----------
  188.  
  189.    --  A generic hashing function working on String keys
  190.  
  191.    generic
  192.       type Header_Num is range <>;
  193.    function Hash (Key : String) return Header_Num;
  194.  
  195. end GNAT.HTable;
  196.