OSDN Git Service

2007-04-20 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / 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-2006, 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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Useful array-manipulation routines: given a set of separators, split
35 --  an array wherever the separators appear, and provide direct access
36 --  to the resulting slices.
37
38 with Ada.Finalization;
39
40 generic
41    type Element is (<>);
42    --  Element of the array, this must be a discrete type
43
44    type Element_Sequence is array (Positive range <>) of Element;
45    --  The array which is a sequence of element
46
47    type Element_Set is private;
48    --  This type represent a set of elements. This set does not defined a
49    --  specific order of the elements. The conversion of a sequence to a
50    --  set and membership tests in the set is performed using the routines
51    --  To_Set and Is_In defined below.
52
53    with function To_Set (Sequence : Element_Sequence) return Element_Set;
54    --  Returns an Element_Set given an Element_Sequence. Duplicate elements
55    --  can be ignored during this conversion.
56
57    with function Is_In (Item : Element; Set : Element_Set) return Boolean;
58    --  Returns True if Item is found in Set, False otherwise
59
60 package GNAT.Array_Split is
61
62    Index_Error : exception;
63    --  Raised by all operations below if Index > Field_Count (S)
64
65    type Separator_Mode is
66      (Single,
67       --  In this mode the array is cut at each element in the separator
68       --  set. If two separators are contiguous the result at that position
69       --  is an empty slice.
70
71       Multiple
72       --  In this mode contiguous separators are handled as a single
73       --  separator and no empty slice is created.
74      );
75
76    type Slice_Set is private;
77    --  This type uses by-reference semantics. This is a set of slices as
78    --  returned by Create or Set routines below. The abstraction represents
79    --  a set of items. Each item is a part of the original string named a
80    --  Slice. It is possible to access individual slices by using the Slice
81    --  routine below. The first slice in the Set is at the position/index
82    --  1. The total number of slices in the set is returned by Slice_Count.
83
84    procedure Create
85      (S          : out Slice_Set;
86       From       : Element_Sequence;
87       Separators : Element_Sequence;
88       Mode       : Separator_Mode := Single);
89    --  Create a cut array object. From is the source array, and Separators
90    --  is a sequence of Element along which to split the array. The source
91    --  array is sliced at separator boundaries. The separators are not
92    --  included as part of the resulting slices.
93    --
94    --  Note that if From is terminated by a separator an extra empty element
95    --  is added to the slice set. If From only contains a separator the slice
96    --  set contains two empty elements.
97
98    procedure Create
99      (S          : out Slice_Set;
100       From       : Element_Sequence;
101       Separators : Element_Set;
102       Mode       : Separator_Mode := Single);
103    --  Same as above but using a Element_Set
104
105    procedure Set
106      (S          : in out Slice_Set;
107       Separators : Element_Sequence;
108       Mode       : Separator_Mode := Single);
109    --  Change the set of separators. The source array will be split according
110    --  to this new set of separators.
111
112    procedure Set
113      (S          : in out Slice_Set;
114       Separators : Element_Set;
115       Mode       : Separator_Mode := Single);
116    --  Same as above but using a Element_Set
117
118    type Slice_Number is new Natural;
119    --  Type used to count number of slices
120
121    function Slice_Count (S : Slice_Set) return Slice_Number;
122    pragma Inline (Slice_Count);
123    --  Returns the number of slices (fields) in S
124
125    function Slice
126      (S     : Slice_Set;
127       Index : Slice_Number) return Element_Sequence;
128    pragma Inline (Slice);
129    --  Returns the slice at position Index. First slice is 1. If Index is 0
130    --  the whole array is returned including the separators (this is the
131    --  original source array).
132
133    type Position is (Before, After);
134    --  Used to designate position of separator
135
136    type Slice_Separators is array (Position) of Element;
137    --  Separators found before and after the slice
138
139    Array_End : constant Element;
140    --  This is the separator returned for the start or the end of the array
141
142    function Separators
143      (S     : Slice_Set;
144       Index : Slice_Number) return Slice_Separators;
145    --  Returns the separators used to slice (front and back) the slice at
146    --  position Index. For slices at start and end of the original array, the
147    --  Array_End value is returned for the corresponding outer bound. In
148    --  Multiple mode only the element closest to the slice is returned.
149    --  if Index = 0, returns (Array_End, Array_End).
150
151    type Separators_Indexes is array (Positive range <>) of Positive;
152
153    function Separators (S : Slice_Set) return Separators_Indexes;
154    --  Returns indexes of all separators used to slice original source array S
155
156 private
157
158    Array_End : constant Element := Element'First;
159
160    type Element_Access is access Element_Sequence;
161
162    type Counter is access Natural;
163
164    type Indexes_Access is access Separators_Indexes;
165
166    type Slice_Info is record
167       Start : Positive;
168       Stop  : Natural;
169    end record;
170    --  Starting/Ending position of a slice. This does not include separators
171
172    type Slices_Indexes is array (Slice_Number range <>) of Slice_Info;
173    type Slices_Access is access Slices_Indexes;
174    --  All indexes for fast access to slices. In the Slice_Set we keep only
175    --  the original array and the indexes where each slice start and stop.
176
177    type Slice_Set is new Ada.Finalization.Controlled with record
178       Ref_Counter : Counter;            -- Reference counter, by-address sem
179       Source      : Element_Access;
180       N_Slice     : Slice_Number := 0;  -- Number of slices found
181       Indexes     : Indexes_Access;
182       Slices      : Slices_Access;
183    end record;
184
185    procedure Initialize (S : in out Slice_Set);
186    procedure Adjust     (S : in out Slice_Set);
187    procedure Finalize   (S : in out Slice_Set);
188
189 end GNAT.Array_Split;