File : g-arrspl.ads


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT COMPILER COMPONENTS                         --
   4 --                                                                          --
   5 --                     G N A T . A R R A Y _ S P L I T                      --
   6 --                                                                          --
   7 --                                 S p e c                                  --
   8 --                                                                          --
   9 --          Copyright (C) 2002-2015, Free Software Foundation, Inc.         --
  10 --                                                                          --
  11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  12 -- terms of the  GNU General Public License as published  by the Free Soft- --
  13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  17 --                                                                          --
  18 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  21 --                                                                          --
  22 -- You should have received a copy of the GNU General Public License and    --
  23 -- a copy of the GCC Runtime Library Exception along with this program;     --
  24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  25 -- <http://www.gnu.org/licenses/>.                                          --
  26 --                                                                          --
  27 -- GNAT was originally developed  by the GNAT team at  New York University. --
  28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  29 --                                                                          --
  30 ------------------------------------------------------------------------------
  31 
  32 --  Useful array-manipulation routines: given a set of separators, split
  33 --  an array wherever the separators appear, and provide direct access
  34 --  to the resulting slices.
  35 
  36 with Ada.Finalization;
  37 
  38 generic
  39    type Element is (<>);
  40    --  Element of the array, this must be a discrete type
  41 
  42    type Element_Sequence is array (Positive range <>) of Element;
  43    --  The array which is a sequence of element
  44 
  45    type Element_Set is private;
  46    --  This type represent a set of elements. This set does not define a
  47    --  specific order of the elements. The conversion of a sequence to a
  48    --  set and membership tests in the set is performed using the routines
  49    --  To_Set and Is_In defined below.
  50 
  51    with function To_Set (Sequence : Element_Sequence) return Element_Set;
  52    --  Returns an Element_Set given an Element_Sequence. Duplicate elements
  53    --  can be ignored during this conversion.
  54 
  55    with function Is_In (Item : Element; Set : Element_Set) return Boolean;
  56    --  Returns True if Item is found in Set, False otherwise
  57 
  58 package GNAT.Array_Split is
  59 
  60    Index_Error : exception;
  61    --  Raised by all operations below if Index > Field_Count (S)
  62 
  63    type Separator_Mode is
  64      (Single,
  65       --  In this mode the array is cut at each element in the separator
  66       --  set. If two separators are contiguous the result at that position
  67       --  is an empty slice.
  68 
  69       Multiple
  70       --  In this mode contiguous separators are handled as a single
  71       --  separator and no empty slice is created.
  72      );
  73 
  74    type Slice_Set is private;
  75    --  This type uses by-reference semantics. This is a set of slices as
  76    --  returned by Create or Set routines below. The abstraction represents
  77    --  a set of items. Each item is a part of the original array named a
  78    --  Slice. It is possible to access individual slices by using the Slice
  79    --  routine below. The first slice in the Set is at the position/index
  80    --  1. The total number of slices in the set is returned by Slice_Count.
  81 
  82    procedure Create
  83      (S          : out Slice_Set;
  84       From       : Element_Sequence;
  85       Separators : Element_Sequence;
  86       Mode       : Separator_Mode := Single);
  87    --  Create a cut array object. From is the source array, and Separators
  88    --  is a sequence of Element along which to split the array. The source
  89    --  array is sliced at separator boundaries. The separators are not
  90    --  included as part of the resulting slices.
  91    --
  92    --  Note that if From is terminated by a separator an extra empty element
  93    --  is added to the slice set. If From only contains a separator the slice
  94    --  set contains two empty elements.
  95 
  96    procedure Create
  97      (S          : out Slice_Set;
  98       From       : Element_Sequence;
  99       Separators : Element_Set;
 100       Mode       : Separator_Mode := Single);
 101    --  Same as above but using a Element_Set
 102 
 103    procedure Set
 104      (S          : in out Slice_Set;
 105       Separators : Element_Sequence;
 106       Mode       : Separator_Mode := Single);
 107    --  Change the set of separators. The source array will be split according
 108    --  to this new set of separators.
 109 
 110    procedure Set
 111      (S          : in out Slice_Set;
 112       Separators : Element_Set;
 113       Mode       : Separator_Mode := Single);
 114    --  Same as above but using a Element_Set
 115 
 116    type Slice_Number is new Natural;
 117    --  Type used to count number of slices
 118 
 119    function Slice_Count (S : Slice_Set) return Slice_Number;
 120    pragma Inline (Slice_Count);
 121    --  Returns the number of slices (fields) in S
 122 
 123    function Slice
 124      (S     : Slice_Set;
 125       Index : Slice_Number) return Element_Sequence;
 126    pragma Inline (Slice);
 127    --  Returns the slice at position Index. First slice is 1. If Index is 0
 128    --  the whole array is returned including the separators (this is the
 129    --  original source array).
 130 
 131    type Position is (Before, After);
 132    --  Used to designate position of separator
 133 
 134    type Slice_Separators is array (Position) of Element;
 135    --  Separators found before and after the slice
 136 
 137    Array_End : constant Element;
 138    --  This is the separator returned for the start or the end of the array
 139 
 140    function Separators
 141      (S     : Slice_Set;
 142       Index : Slice_Number) return Slice_Separators;
 143    --  Returns the separators used to slice (front and back) the slice at
 144    --  position Index. For slices at start and end of the original array, the
 145    --  Array_End value is returned for the corresponding outer bound. In
 146    --  Multiple mode only the element closest to the slice is returned.
 147    --  if Index = 0, returns (Array_End, Array_End).
 148 
 149    type Separators_Indexes is array (Positive range <>) of Positive;
 150 
 151    function Separators (S : Slice_Set) return Separators_Indexes;
 152    --  Returns indexes of all separators used to slice original source array S
 153 
 154 private
 155 
 156    Array_End : constant Element := Element'First;
 157 
 158    type Element_Access is access Element_Sequence;
 159 
 160    type Indexes_Access is access Separators_Indexes;
 161 
 162    type Slice_Info is record
 163       Start : Positive;
 164       Stop  : Natural;
 165    end record;
 166    --  Starting/Ending position of a slice. This does not include separators
 167 
 168    type Slices_Indexes is array (Slice_Number range <>) of Slice_Info;
 169    type Slices_Access is access Slices_Indexes;
 170    --  All indexes for fast access to slices. In the Slice_Set we keep only
 171    --  the original array and the indexes where each slice start and stop.
 172 
 173    type Data is record
 174       Ref_Counter : Natural;            -- Reference counter, by-address sem
 175       Source      : Element_Access;
 176       N_Slice     : Slice_Number := 0;  -- Number of slices found
 177       Indexes     : Indexes_Access;
 178       Slices      : Slices_Access;
 179    end record;
 180    type Data_Access is access all Data;
 181 
 182    type Slice_Set is new Ada.Finalization.Controlled with record
 183       D : Data_Access;
 184    end record;
 185 
 186    procedure Initialize (S : in out Slice_Set);
 187    procedure Adjust     (S : in out Slice_Set);
 188    procedure Finalize   (S : in out Slice_Set);
 189 
 190 end GNAT.Array_Split;