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-2019, 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-- As a special exception under Section 7 of GPL version 3, you are granted -- 19-- additional permissions described in the GCC Runtime Library Exception, -- 20-- version 3.1, as published by the Free Software Foundation. -- 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 36with Ada.Finalization; 37 38generic 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 58package GNAT.Array_Split is 59 pragma Preelaborate; 60 61 Index_Error : exception; 62 -- Raised by all operations below if Index > Field_Count (S) 63 64 type Separator_Mode is 65 (Single, 66 -- In this mode the array is cut at each element in the separator 67 -- set. If two separators are contiguous the result at that position 68 -- is an empty slice. 69 70 Multiple 71 -- In this mode contiguous separators are handled as a single 72 -- separator and no empty slice is created. 73 ); 74 75 type Slice_Set is private; 76 -- This type uses by-reference semantics. This is a set of slices as 77 -- returned by Create or Set routines below. The abstraction represents 78 -- a set of items. Each item is a part of the original array named a 79 -- Slice. It is possible to access individual slices by using the Slice 80 -- routine below. The first slice in the Set is at the position/index 81 -- 1. The total number of slices in the set is returned by Slice_Count. 82 83 procedure Create 84 (S : out Slice_Set; 85 From : Element_Sequence; 86 Separators : Element_Sequence; 87 Mode : Separator_Mode := Single); 88 -- Create a cut array object. From is the source array, and Separators 89 -- is a sequence of Element along which to split the array. The source 90 -- array is sliced at separator boundaries. The separators are not 91 -- included as part of the resulting slices. 92 -- 93 -- Note that if From is terminated by a separator an extra empty element 94 -- is added to the slice set. If From only contains a separator the slice 95 -- set contains two empty elements. 96 97 procedure Create 98 (S : out Slice_Set; 99 From : Element_Sequence; 100 Separators : Element_Set; 101 Mode : Separator_Mode := Single); 102 -- Same as above but using a Element_Set 103 104 procedure Set 105 (S : in out Slice_Set; 106 Separators : Element_Sequence; 107 Mode : Separator_Mode := Single); 108 -- Change the set of separators. The source array will be split according 109 -- to this new set of separators. 110 111 procedure Set 112 (S : in out Slice_Set; 113 Separators : Element_Set; 114 Mode : Separator_Mode := Single); 115 -- Same as above but using a Element_Set 116 117 type Slice_Number is new Natural; 118 -- Type used to count number of slices 119 120 function Slice_Count (S : Slice_Set) return Slice_Number; 121 pragma Inline (Slice_Count); 122 -- Returns the number of slices (fields) in S 123 124 function Slice 125 (S : Slice_Set; 126 Index : Slice_Number) return Element_Sequence; 127 pragma Inline (Slice); 128 -- Returns the slice at position Index. First slice is 1. If Index is 0 129 -- the whole array is returned including the separators (this is the 130 -- original source array). 131 132 type Position is (Before, After); 133 -- Used to designate position of separator 134 135 type Slice_Separators is array (Position) of Element; 136 -- Separators found before and after the slice 137 138 Array_End : constant Element; 139 -- This is the separator returned for the start or the end of the array 140 141 function Separators 142 (S : Slice_Set; 143 Index : Slice_Number) return Slice_Separators; 144 -- Returns the separators used to slice (front and back) the slice at 145 -- position Index. For slices at start and end of the original array, the 146 -- Array_End value is returned for the corresponding outer bound. In 147 -- Multiple mode only the element closest to the slice is returned. 148 -- if Index = 0, returns (Array_End, Array_End). 149 150 type Separators_Indexes is array (Positive range <>) of Positive; 151 152 function Separators (S : Slice_Set) return Separators_Indexes; 153 -- Returns indexes of all separators used to slice original source array S 154 155private 156 157 Array_End : constant Element := Element'First; 158 159 type Element_Access is access Element_Sequence; 160 161 type Indexes_Access is access Separators_Indexes; 162 163 type Slice_Info is record 164 Start : Positive; 165 Stop : Natural; 166 end record; 167 -- Starting/Ending position of a slice. This does not include separators 168 169 type Slices_Indexes is array (Slice_Number range <>) of Slice_Info; 170 type Slices_Access is access Slices_Indexes; 171 -- All indexes for fast access to slices. In the Slice_Set we keep only 172 -- the original array and the indexes where each slice start and stop. 173 174 type Data is record 175 Ref_Counter : Natural; -- Reference counter, by-address sem 176 Source : Element_Access; 177 N_Slice : Slice_Number := 0; -- Number of slices found 178 Indexes : Indexes_Access; 179 Slices : Slices_Access; 180 end record; 181 type Data_Access is access all Data; 182 183 type Slice_Set is new Ada.Finalization.Controlled with record 184 D : Data_Access; 185 end record; 186 187 procedure Initialize (S : in out Slice_Set); 188 procedure Adjust (S : in out Slice_Set); 189 procedure Finalize (S : in out Slice_Set); 190 191end GNAT.Array_Split; 192