matreshka_league_21.0.0_0c8f4d47/source/league/league-stream_element_vectors.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--         Localization, Internationalization, Globalization for Ada        --
--                                                                          --
--                        Runtime Library Component                         --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2010-2013, Vadim Godunko <vgodunko@gmail.com>                --
-- All rights reserved.                                                     --
--                                                                          --
-- Redistribution and use in source and binary forms, with or without       --
-- modification, are permitted provided that the following conditions       --
-- are met:                                                                 --
--                                                                          --
--  * Redistributions of source code must retain the above copyright        --
--    notice, this list of conditions and the following disclaimer.         --
--                                                                          --
--  * Redistributions in binary form must reproduce the above copyright     --
--    notice, this list of conditions and the following disclaimer in the   --
--    documentation and/or other materials provided with the distribution.  --
--                                                                          --
--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
--    contributors may be used to endorse or promote products derived from  --
--    this software without specific prior written permission.              --
--                                                                          --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3859 $ $Date: 2013-04-12 23:13:56 +0400 (Пт, 12 апр 2013) $
------------------------------------------------------------------------------

package body League.Stream_Element_Vectors is

   use type Ada.Streams.Stream_Element_Array;
   use type Ada.Streams.Stream_Element_Offset;
   use Matreshka.Internals.Stream_Element_Vectors;

   ---------
   -- "=" --
   ---------

   overriding function "="
    (Left  : Stream_Element_Vector;
     Right : Stream_Element_Vector) return Boolean is
   begin
      return
        Left.Data = Right.Data
          or else
           (Left.Data.Length = Right.Data.Length
              and then
                Left.Data.Value (0 .. Left.Data.Length - 1)
                  = Right.Data.Value (0 .. Right.Data.Length - 1));
   end "=";

   ---------
   -- "=" --
   ---------

   not overriding function "="
    (Left  : Stream_Element_Vector;
     Right : Ada.Streams.Stream_Element_Array) return Boolean is
   begin
      if Left.Data.Length = Right'Length then
         return
           Left.Data.Value (0 .. Left.Data.Length - 1) = Right;

      else
         return False;
      end if;
   end "=";

   ---------
   -- "=" --
   ---------

   not overriding function "="
    (Left  : Ada.Streams.Stream_Element_Array;
     Right : Stream_Element_Vector) return Boolean is
   begin
      if Left'Length = Right.Data.Length then
         return
           Left = Right.Data.Value (0 .. Right.Data.Length - 1);

      else
         return False;
      end if;
   end "=";

   ------------
   -- Adjust --
   ------------

   overriding procedure Adjust (Self : in out Stream_Element_Vector) is
   begin
      Reference (Self.Data);
   end Adjust;

   ------------
   -- Append --
   ------------

   procedure Append
    (Self : in out Stream_Element_Vector'Class;
     Item : Ada.Streams.Stream_Element)
   is
      Data : constant Shared_Stream_Element_Vector_Access
        := Allocate (Self.Data.Length + 1);

   begin
      Data.Length := Self.Data.Length + 1;
      Data.Value (0 .. Self.Data.Length - 1) :=
        Self.Data.Value (0 .. Self.Data.Length - 1);
      Data.Value (Data.Length - 1) := Item;
      Fill_Tail (Data);
      Dereference (Self.Data);
      Self.Data := Data;
   end Append;

   ------------
   -- Append --
   ------------

   procedure Append
    (Self : in out Stream_Element_Vector'Class;
     Item : Ada.Streams.Stream_Element_Array)
   is
      Data : constant Shared_Stream_Element_Vector_Access
        := Allocate (Self.Data.Length + Item'Length);

   begin
      Data.Length := Self.Data.Length + Item'Length;
      Data.Value (0 .. Self.Data.Length - 1) :=
        Self.Data.Value (0 .. Self.Data.Length - 1);
      Data.Value (Self.Data.Length .. Data.Length - 1) := Item;
      Fill_Tail (Data);
      Dereference (Self.Data);
      Self.Data := Data;
   end Append;

   ------------
   -- Append --
   ------------

   procedure Append
    (Self : in out Stream_Element_Vector'Class;
     Item : Stream_Element_Vector'Class)
   is
      Data : constant Shared_Stream_Element_Vector_Access
        := Allocate (Self.Data.Length + Item.Data.Length);

   begin
      Data.Length := Self.Data.Length + Item.Data.Length;
      Data.Value (0 .. Self.Data.Length - 1) :=
        Self.Data.Value (0 .. Self.Data.Length - 1);
      Data.Value (Self.Data.Length .. Data.Length - 1) :=
        Item.Data.Value (0 .. Item.Data.Length - 1);
      Fill_Tail (Data);
      Dereference (Self.Data);
      Self.Data := Data;
   end Append;

   -----------
   -- Clear --
   -----------

   procedure Clear (Self : in out Stream_Element_Vector) is
   begin
      Dereference (Self.Data);
      Self.Data := MISEV.Empty_Shared_Stream_Element_Vector'Access;
   end Clear;

   -------------
   -- Element --
   -------------

   function Element
    (Self  : Stream_Element_Vector'Class;
     Index : Ada.Streams.Stream_Element_Offset) return Ada.Streams.Stream_Element is
   begin
      if Index <= Self.Data.Length then
         return Self.Data.Value (Index - 1);

      else
         raise Constraint_Error with "Index is out of range";
      end if;
   end Element;

   --------------
   -- Finalize --
   --------------

   overriding procedure Finalize (Self : in out Stream_Element_Vector) is
   begin
      --  Finalize can be called more than once (as specified by language
      --  standard), thus implementation should provide protection from
      --  multiple finalization.

      if Self.Data /= null then
         Dereference (Self.Data);
      end if;
   end Finalize;

   ----------
   -- Hash --
   ----------

   function Hash (Self : Stream_Element_Vector) return League.Hash_Type is
   begin
      return Hash (Self.Data);
   end Hash;

   --------------
   -- Is_Empty --
   --------------

   function Is_Empty (Self : Stream_Element_Vector) return Boolean is
   begin
      return Self.Data.Length = 0;
   end Is_Empty;

   ------------
   -- Length --
   ------------

   function Length
    (Self : Stream_Element_Vector) return Ada.Streams.Stream_Element_Offset is
   begin
      return Self.Data.Length;
   end Length;

   ----------
   -- Read --
   ----------

   procedure Read
    (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
     Item   : out Stream_Element_Vector)
   is
      Length : Ada.Streams.Stream_Element_Offset;

   begin
      --  Read length of the stream element array.

      Ada.Streams.Stream_Element_Offset'Read (Stream, Length);

      --  Release shared object. XXX Object mutation can be used here for
      --  performance improvement.

      Dereference (Item.Data);

      if Length = 0 then
         --  Shared empty object is used for empty stream element array.

         Item.Data := Empty_Shared_Stream_Element_Vector'Access;

      else
         --  Allocate shared object and read data into it.

         Item.Data := Allocate (Length);
         Item.Data.Length := Length;
         Ada.Streams.Stream_Element_Array'Read
          (Stream, Item.Data.Value (0 .. Length - 1));
         Fill_Tail (Item.Data);
      end if;
   end Read;

   -----------------------------
   -- To_Stream_Element_Array --
   -----------------------------

   function To_Stream_Element_Array
    (Item : Stream_Element_Vector) return Ada.Streams.Stream_Element_Array is
   begin
      return Item.Data.Value (0 .. Item.Data.Length - 1);
   end To_Stream_Element_Array;

   ------------------------------
   -- To_Stream_Element_Vector --
   ------------------------------

   function To_Stream_Element_Vector
    (Item : Ada.Streams.Stream_Element_Array) return Stream_Element_Vector
   is
      Data : constant Shared_Stream_Element_Vector_Access
        := Allocate (Item'Length);

   begin
      Data.Length := Item'Length;
      Data.Value (0 .. Data.Length - 1) := Item;
      Fill_Tail (Data);

      return (Ada.Finalization.Controlled with Data => Data);
   end To_Stream_Element_Vector;

   -----------
   -- Write --
   -----------

   procedure Write
    (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
     Item   : Stream_Element_Vector) is
   begin
      Ada.Streams.Stream_Element_Offset'Write (Stream, Item.Data.Length);

      if Item.Data.Length /= 0 then
         Ada.Streams.Stream_Element_Array'Write
          (Stream, Item.Data.Value (0 .. Item.Data.Length - 1));
      end if;
   end Write;

end League.Stream_Element_Vectors;