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-table.ads < prev    next >
Text File  |  2000-07-19  |  8KB  |  150 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                            G N A T . T A B L E                           --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --              Copyright (C) 1998 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. --  Resizable one dimensional array support
  36.  
  37. --  This package provides an implementation of dynamically resizable one
  38. --  dimensional arrays. The idea is to mimic the normal Ada semantics for
  39. --  arrays as closely as possible with the one additional capability of
  40. --  dynamically modifying the value of the Last attribute.
  41.  
  42. generic
  43.    type Table_Component_Type is private;
  44.    type Table_Index_Type     is range <>;
  45.  
  46.    Table_Low_Bound : Table_Index_Type;
  47.    Table_Initial   : Positive;
  48.    Table_Increment : Natural;
  49.  
  50. package GNAT.Table is
  51. pragma Elaborate_Body (Table);
  52.  
  53.    --  Table_Component_Type and Table_Index_Type specify the type of the
  54.    --  array, Table_Low_Bound is the lower bound. Index_type must be an
  55.    --  integer type. The effect is roughly to declare:
  56.  
  57.    --    Table : array (Table_Low_Bound .. <>) of Table_Component_Type;
  58.  
  59.    --  The Table_Component_Type can be any Ada type but note that default
  60.    --  initialization will NOT occur for the array elements.
  61.  
  62.    --  The Table_Initial values controls the allocation of the table when
  63.    --  it is first allocated, either by default, or by an explicit Init call.
  64.  
  65.    --  The Table_Increment value controls the amount of increase, if the
  66.    --  table has to be increased in size. The value given is a percentage
  67.    --  value (e.g. 100 = increase table size by 100%, i.e. double it).
  68.  
  69.    --  The Last and Set_Last subprograms provide control over the current
  70.    --  logical allocation. They are quite efficient, so they can be used
  71.    --  freely (expensive reallocation occurs only at major granularity
  72.    --  chunks controlled by the allocation parameters).
  73.  
  74.    --  Note: we do not make the table components aliased, since this would
  75.    --  restrict the use of table for discriminated types. If it is necessary
  76.    --  to take the access of a table element, use Unrestricted_Access.
  77.  
  78.    type Table_Type is
  79.      array (Table_Index_Type range <>) of Table_Component_Type;
  80.  
  81.    subtype Big_Table_Type is
  82.      Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
  83.    --  We work with pointers to a bogus array type that is constrained
  84.    --  with the maximum possible range bound. This means that the pointer
  85.    --  is a thin pointer, which is more efficient. Since subscript checks
  86.    --  in any case must be on the logical, rather than physical bounds,
  87.    --  safety is not compromised by this approach.
  88.  
  89.    type Table_Ptr is access all Big_Table_Type;
  90.    --  The table is actually represented as a pointer to allow reallocation
  91.  
  92.    Table : aliased Table_Ptr := null;
  93.    --  The table itself. The lower bound is the value of Low_Bound.
  94.    --  Logically the upper bound is the current value of Last (although
  95.    --  the actual size of the allocated table may be larger than this).
  96.    --  The program may only access and modify Table entries in the range
  97.    --  First .. Last.
  98.  
  99.    Locked : Boolean := False;
  100.    --  Table expansion is permitted only if this switch is set to False. A
  101.    --  client may set Locked to True, in which case any attempt to expand
  102.    --  the table will cause an assertion failure. Note that while a table
  103.    --  is locked, its address in memory remains fixed and unchanging.
  104.  
  105.    procedure Init;
  106.    --  This procedure allocates a new table of size Initial (freeing any
  107.    --  previously allocated larger table). It is not necessary to call
  108.    --  Init when a table is first instantiated (since the instantiation does
  109.    --  the same initialization steps). However, it is harmless to do so, and
  110.    --  Init is convenient in reestablishing a table for new use.
  111.  
  112.    function Last return Table_Index_Type;
  113.    pragma Inline (Last);
  114.    --  Returns the current value of the last used entry in the table, which
  115.    --  can then be used as a subscript for Table. Note that the only way to
  116.    --  modify Last is to call the Set_Last procedure. Last must always be
  117.    --  used to determine the logically last entry.
  118.  
  119.    procedure Release;
  120.    --  Storage is allocated in chunks according to the values given in the
  121.    --  Initial and Increment parameters. A call to Release releases all
  122.    --  storage that is allocated, but is not logically part of the current
  123.    --  array value. Current array values are not affected by this call.
  124.  
  125.    First : constant Table_Index_Type := Table_Low_Bound;
  126.    --  Export First as synonym for Low_Bound (parallel with use of Last)
  127.  
  128.    procedure Set_Last (New_Val : Table_Index_Type);
  129.    pragma Inline (Set_Last);
  130.    --  This procedure sets Last to the indicated value. If necessary the
  131.    --  table is reallocated to accomodate the new value (i.e. on return
  132.    --  the allocated table has an upper bound of at least Last). If Set_Last
  133.    --  reduces the size of the table, then logically entries are removed
  134.    --  from the table. If Set_Last increases the size of the table, then
  135.    --  new entries are logically added to the table.
  136.  
  137.    procedure Increment_Last;
  138.    pragma Inline (Increment_Last);
  139.    --  Adds 1 to Last (same as Set_Last (Last + 1).
  140.  
  141.    procedure Decrement_Last;
  142.    pragma Inline (Decrement_Last);
  143.    --  Subtracts 1 from Last (same as Set_Last (Last - 1).
  144.  
  145.    function Allocate (Num : Integer := 1) return Table_Index_Type;
  146.    pragma Inline (Allocate);
  147.    --  Adds Num to Last, and returns the old value of Last + 1.
  148.  
  149. end GNAT.Table;
  150.