OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [ada/] [a-cohase.ads] - Blame information for rev 706

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 706 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT LIBRARY COMPONENTS                          --
4
--                                                                          --
5
--           A D A . C O N T A I N E R S . H A S H E D _ S E T S            --
6
--                                                                          --
7
--                                 S p e c                                  --
8
--                                                                          --
9
--          Copyright (C) 2004-2012, Free Software Foundation, Inc.         --
10
--                                                                          --
11
-- This specification is derived from the Ada Reference Manual for use with --
12
-- GNAT. The copyright notice above, and the license provisions that follow --
13
-- apply solely to the  contents of the part following the private keyword. --
14
--                                                                          --
15
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
16
-- terms of the  GNU General Public License as published  by the Free Soft- --
17
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
18
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
21
--                                                                          --
22
-- As a special exception under Section 7 of GPL version 3, you are granted --
23
-- additional permissions described in the GCC Runtime Library Exception,   --
24
-- version 3.1, as published by the Free Software Foundation.               --
25
--                                                                          --
26
-- You should have received a copy of the GNU General Public License and    --
27
-- a copy of the GCC Runtime Library Exception along with this program;     --
28
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
29
-- <http://www.gnu.org/licenses/>.                                          --
30
--                                                                          --
31
-- This unit was originally developed by Matthew J Heaney.                  --
32
------------------------------------------------------------------------------
33
 
34
with Ada.Iterator_Interfaces;
35
 
36
private with Ada.Containers.Hash_Tables;
37
private with Ada.Streams;
38
private with Ada.Finalization;
39
 
40
generic
41
   type Element_Type is private;
42
 
43
   with function Hash (Element : Element_Type) return Hash_Type;
44
 
45
   with function Equivalent_Elements
46
          (Left, Right : Element_Type) return Boolean;
47
 
48
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
49
 
50
package Ada.Containers.Hashed_Sets is
51
   pragma Preelaborate;
52
   pragma Remote_Types;
53
 
54
   type Set is tagged private
55
   with
56
      Constant_Indexing => Constant_Reference,
57
      Default_Iterator  => Iterate,
58
      Iterator_Element  => Element_Type;
59
 
60
   pragma Preelaborable_Initialization (Set);
61
 
62
   type Cursor is private;
63
   pragma Preelaborable_Initialization (Cursor);
64
 
65
   Empty_Set : constant Set;
66
   --  Set objects declared without an initialization expression are
67
   --  initialized to the value Empty_Set.
68
 
69
   No_Element : constant Cursor;
70
   --  Cursor objects declared without an initialization expression are
71
   --  initialized to the value No_Element.
72
 
73
   function Has_Element (Position : Cursor) return Boolean;
74
   --  Equivalent to Position /= No_Element
75
 
76
   package Set_Iterator_Interfaces is new
77
     Ada.Iterator_Interfaces (Cursor, Has_Element);
78
 
79
   function "=" (Left, Right : Set) return Boolean;
80
   --  For each element in Left, set equality attempts to find the equal
81
   --  element in Right; if a search fails, then set equality immediately
82
   --  returns False. The search works by calling Hash to find the bucket in
83
   --  the Right set that corresponds to the Left element. If the bucket is
84
   --  non-empty, the search calls the generic formal element equality operator
85
   --  to compare the element (in Left) to the element of each node in the
86
   --  bucket (in Right); the search terminates when a matching node in the
87
   --  bucket is found, or the nodes in the bucket are exhausted. (Note that
88
   --  element equality is called here, not Equivalent_Elements. Set equality
89
   --  is the only operation in which element equality is used. Compare set
90
   --  equality to Equivalent_Sets, which does call Equivalent_Elements.)
91
 
92
   function Equivalent_Sets (Left, Right : Set) return Boolean;
93
   --  Similar to set equality, with the difference that the element in Left is
94
   --  compared to the elements in Right using the generic formal
95
   --  Equivalent_Elements operation instead of element equality.
96
 
97
   function To_Set (New_Item : Element_Type) return Set;
98
   --  Constructs a singleton set comprising New_Element. To_Set calls Hash to
99
   --  determine the bucket for New_Item.
100
 
101
   function Capacity (Container : Set) return Count_Type;
102
   --  Returns the current capacity of the set. Capacity is the maximum length
103
   --  before which rehashing in guaranteed not to occur.
104
 
105
   procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
106
   --  Adjusts the current capacity, by allocating a new buckets array. If the
107
   --  requested capacity is less than the current capacity, then the capacity
108
   --  is contracted (to a value not less than the current length). If the
109
   --  requested capacity is greater than the current capacity, then the
110
   --  capacity is expanded (to a value not less than what is requested). In
111
   --  either case, the nodes are rehashed from the old buckets array onto the
112
   --  new buckets array (Hash is called once for each existing element in
113
   --  order to compute the new index), and then the old buckets array is
114
   --  deallocated.
115
 
116
   function Length (Container : Set) return Count_Type;
117
   --  Returns the number of items in the set
118
 
119
   function Is_Empty (Container : Set) return Boolean;
120
   --  Equivalent to Length (Container) = 0
121
 
122
   procedure Clear (Container : in out Set);
123
   --  Removes all of the items from the set
124
 
125
   function Element (Position : Cursor) return Element_Type;
126
   --  Returns the element of the node designated by the cursor
127
 
128
   procedure Replace_Element
129
     (Container : in out Set;
130
      Position  : Cursor;
131
      New_Item  : Element_Type);
132
   --  If New_Item is equivalent (as determined by calling Equivalent_Elements)
133
   --  to the element of the node designated by Position, then New_Element is
134
   --  assigned to that element. Otherwise, it calls Hash to determine the
135
   --  bucket for New_Item. If the bucket is not empty, then it calls
136
   --  Equivalent_Elements for each node in that bucket to determine whether
137
   --  New_Item is equivalent to an element in that bucket. If
138
   --  Equivalent_Elements returns True then Program_Error is raised (because
139
   --  an element may appear only once in the set); otherwise, New_Item is
140
   --  assigned to the node designated by Position, and the node is moved to
141
   --  its new bucket.
142
 
143
   procedure Query_Element
144
     (Position : Cursor;
145
      Process  : not null access procedure (Element : Element_Type));
146
   --  Calls Process with the element (having only a constant view) of the node
147
   --  designed by the cursor.
148
 
149
   type Constant_Reference_Type
150
     (Element : not null access constant Element_Type) is private
151
        with Implicit_Dereference => Element;
152
 
153
   function Constant_Reference
154
     (Container : aliased Set;
155
      Position  : Cursor) return Constant_Reference_Type;
156
   pragma Inline (Constant_Reference);
157
 
158
   procedure Assign (Target : in out Set; Source : Set);
159
 
160
   function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
161
 
162
   procedure Move (Target : in out Set; Source : in out Set);
163
   --  Clears Target (if it's not empty), and then moves (not copies) the
164
   --  buckets array and nodes from Source to Target.
165
 
166
   procedure Insert
167
     (Container : in out Set;
168
      New_Item  : Element_Type;
169
      Position  : out Cursor;
170
      Inserted  : out Boolean);
171
   --  Conditionally inserts New_Item into the set. If New_Item is already in
172
   --  the set, then Inserted returns False and Position designates the node
173
   --  containing the existing element (which is not modified). If New_Item is
174
   --  not already in the set, then Inserted returns True and Position
175
   --  designates the newly-inserted node containing New_Item. The search for
176
   --  an existing element works as follows. Hash is called to determine
177
   --  New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
178
   --  is called to compare New_Item to the element of each node in that
179
   --  bucket. If the bucket is empty, or there were no equivalent elements in
180
   --  the bucket, the search "fails" and the New_Item is inserted in the set
181
   --  (and Inserted returns True); otherwise, the search "succeeds" (and
182
   --  Inserted returns False).
183
 
184
   procedure Insert  (Container : in out Set; New_Item : Element_Type);
185
   --  Attempts to insert New_Item into the set, performing the usual insertion
186
   --  search (which involves calling both Hash and Equivalent_Elements); if
187
   --  the search succeeds (New_Item is equivalent to an element already in the
188
   --  set, and so was not inserted), then this operation raises
189
   --  Constraint_Error. (This version of Insert is similar to Replace, but
190
   --  having the opposite exception behavior. It is intended for use when you
191
   --  want to assert that the item is not already in the set.)
192
 
193
   procedure Include (Container : in out Set; New_Item : Element_Type);
194
   --  Attempts to insert New_Item into the set. If an element equivalent to
195
   --  New_Item is already in the set (the insertion search succeeded, and
196
   --  hence New_Item was not inserted), then the value of New_Item is assigned
197
   --  to the existing element. (This insertion operation only raises an
198
   --  exception if cursor tampering occurs. It is intended for use when you
199
   --  want to insert the item in the set, and you don't care whether an
200
   --  equivalent element is already present.)
201
 
202
   procedure Replace (Container : in out Set; New_Item : Element_Type);
203
   --  Searches for New_Item in the set; if the search fails (because an
204
   --  equivalent element was not in the set), then it raises
205
   --  Constraint_Error. Otherwise, the existing element is assigned the value
206
   --  New_Item. (This is similar to Insert, but with the opposite exception
207
   --  behavior. It is intended for use when you want to assert that the item
208
   --  is already in the set.)
209
 
210
   procedure Exclude (Container : in out Set; Item : Element_Type);
211
   --  Searches for Item in the set, and if found, removes its node from the
212
   --  set and then deallocates it. The search works as follows. The operation
213
   --  calls Hash to determine the item's bucket; if the bucket is not empty,
214
   --  it calls Equivalent_Elements to compare Item to the element of each node
215
   --  in the bucket. (This is the deletion analog of Include. It is intended
216
   --  for use when you want to remove the item from the set, but don't care
217
   --  whether the item is already in the set.)
218
 
219
   procedure Delete  (Container : in out Set; Item : Element_Type);
220
   --  Searches for Item in the set (which involves calling both Hash and
221
   --  Equivalent_Elements). If the search fails, then the operation raises
222
   --  Constraint_Error. Otherwise it removes the node from the set and then
223
   --  deallocates it. (This is the deletion analog of non-conditional
224
   --  Insert. It is intended for use when you want to assert that the item is
225
   --  already in the set.)
226
 
227
   procedure Delete (Container : in out Set; Position : in out Cursor);
228
   --  Removes the node designated by Position from the set, and then
229
   --  deallocates the node. The operation calls Hash to determine the bucket,
230
   --  and then compares Position to each node in the bucket until there's a
231
   --  match (it does not call Equivalent_Elements).
232
 
233
   procedure Union (Target : in out Set; Source : Set);
234
   --  The operation first calls Reserve_Capacity if the current capacity is
235
   --  less than the sum of the lengths of Source and Target. It then iterates
236
   --  over the Source set, and conditionally inserts each element into Target.
237
 
238
   function Union (Left, Right : Set) return Set;
239
   --  The operation first copies the Left set to the result, and then iterates
240
   --  over the Right set to conditionally insert each element into the result.
241
 
242
   function "or" (Left, Right : Set) return Set renames Union;
243
 
244
   procedure Intersection (Target : in out Set; Source : Set);
245
   --  Iterates over the Target set (calling First and Next), calling Find to
246
   --  determine whether the element is in Source. If an equivalent element is
247
   --  not found in Source, the element is deleted from Target.
248
 
249
   function Intersection (Left, Right : Set) return Set;
250
   --  Iterates over the Left set, calling Find to determine whether the
251
   --  element is in Right. If an equivalent element is found, it is inserted
252
   --  into the result set.
253
 
254
   function "and" (Left, Right : Set) return Set renames Intersection;
255
 
256
   procedure Difference (Target : in out Set; Source : Set);
257
   --  Iterates over the Source (calling First and Next), calling Find to
258
   --  determine whether the element is in Target. If an equivalent element is
259
   --  found, it is deleted from Target.
260
 
261
   function Difference (Left, Right : Set) return Set;
262
   --  Iterates over the Left set, calling Find to determine whether the
263
   --  element is in the Right set. If an equivalent element is not found, the
264
   --  element is inserted into the result set.
265
 
266
   function "-" (Left, Right : Set) return Set renames Difference;
267
 
268
   procedure Symmetric_Difference (Target : in out Set; Source : Set);
269
   --  The operation first calls Reserve_Capacity if the current capacity is
270
   --  less than the sum of the lengths of Source and Target. It then iterates
271
   --  over the Source set, searching for the element in Target (calling Hash
272
   --  and Equivalent_Elements). If an equivalent element is found, it is
273
   --  removed from Target; otherwise it is inserted into Target.
274
 
275
   function Symmetric_Difference (Left, Right : Set) return Set;
276
   --  The operation first iterates over the Left set. It calls Find to
277
   --  determine whether the element is in the Right set. If no equivalent
278
   --  element is found, the element from Left is inserted into the result. The
279
   --  operation then iterates over the Right set, to determine whether the
280
   --  element is in the Left set. If no equivalent element is found, the Right
281
   --  element is inserted into the result.
282
 
283
   function "xor" (Left, Right : Set) return Set
284
     renames Symmetric_Difference;
285
 
286
   function Overlap (Left, Right : Set) return Boolean;
287
   --  Iterates over the Left set (calling First and Next), calling Find to
288
   --  determine whether the element is in the Right set. If an equivalent
289
   --  element is found, the operation immediately returns True. The operation
290
   --  returns False if the iteration over Left terminates without finding any
291
   --  equivalent element in Right.
292
 
293
   function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
294
   --  Iterates over Subset (calling First and Next), calling Find to determine
295
   --  whether the element is in Of_Set. If no equivalent element is found in
296
   --  Of_Set, the operation immediately returns False. The operation returns
297
   --  True if the iteration over Subset terminates without finding an element
298
   --  not in Of_Set (that is, every element in Subset is equivalent to an
299
   --  element in Of_Set).
300
 
301
   function First (Container : Set) return Cursor;
302
   --  Returns a cursor that designates the first non-empty bucket, by
303
   --  searching from the beginning of the buckets array.
304
 
305
   function Next (Position : Cursor) return Cursor;
306
   --  Returns a cursor that designates the node that follows the current one
307
   --  designated by Position. If Position designates the last node in its
308
   --  bucket, the operation calls Hash to compute the index of this bucket,
309
   --  and searches the buckets array for the first non-empty bucket, starting
310
   --  from that index; otherwise, it simply follows the link to the next node
311
   --  in the same bucket.
312
 
313
   procedure Next (Position : in out Cursor);
314
   --  Equivalent to Position := Next (Position)
315
 
316
   function Find
317
     (Container : Set;
318
      Item      : Element_Type) return Cursor;
319
   --  Searches for Item in the set. Find calls Hash to determine the item's
320
   --  bucket; if the bucket is not empty, it calls Equivalent_Elements to
321
   --  compare Item to each element in the bucket. If the search succeeds, Find
322
   --  returns a cursor designating the node containing the equivalent element;
323
   --  otherwise, it returns No_Element.
324
 
325
   function Contains (Container : Set; Item : Element_Type) return Boolean;
326
   --  Equivalent to Find (Container, Item) /= No_Element
327
 
328
   function Equivalent_Elements (Left, Right : Cursor) return Boolean;
329
   --  Returns the result of calling Equivalent_Elements with the elements of
330
   --  the nodes designated by cursors Left and Right.
331
 
332
   function Equivalent_Elements
333
     (Left  : Cursor;
334
      Right : Element_Type) return Boolean;
335
   --  Returns the result of calling Equivalent_Elements with element of the
336
   --  node designated by Left and element Right.
337
 
338
   function Equivalent_Elements
339
     (Left  : Element_Type;
340
      Right : Cursor) return Boolean;
341
   --  Returns the result of calling Equivalent_Elements with element Left and
342
   --  the element of the node designated by Right.
343
 
344
   procedure Iterate
345
     (Container : Set;
346
      Process   : not null access procedure (Position : Cursor));
347
   --  Calls Process for each node in the set
348
 
349
   function Iterate
350
     (Container : Set) return Set_Iterator_Interfaces.Forward_Iterator'Class;
351
 
352
   generic
353
      type Key_Type (<>) is private;
354
 
355
      with function Key (Element : Element_Type) return Key_Type;
356
 
357
      with function Hash (Key : Key_Type) return Hash_Type;
358
 
359
      with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
360
 
361
   package Generic_Keys is
362
 
363
      function Key (Position : Cursor) return Key_Type;
364
      --  Applies generic formal operation Key to the element of the node
365
      --  designated by Position.
366
 
367
      function Element (Container : Set; Key : Key_Type) return Element_Type;
368
      --  Searches (as per the key-based Find) for the node containing Key, and
369
      --  returns the associated element.
370
 
371
      procedure Replace
372
        (Container : in out Set;
373
         Key       : Key_Type;
374
         New_Item  : Element_Type);
375
      --  Searches (as per the key-based Find) for the node containing Key, and
376
      --  then replaces the element of that node (as per the element-based
377
      --  Replace_Element).
378
 
379
      procedure Exclude (Container : in out Set; Key : Key_Type);
380
      --  Searches for Key in the set, and if found, removes its node from the
381
      --  set and then deallocates it. The search works by first calling Hash
382
      --  (on Key) to determine the bucket; if the bucket is not empty, it
383
      --  calls Equivalent_Keys to compare parameter Key to the value of
384
      --  generic formal operation Key applied to element of each node in the
385
      --  bucket.
386
 
387
      procedure Delete (Container : in out Set; Key : Key_Type);
388
      --  Deletes the node containing Key as per Exclude, with the difference
389
      --  that Constraint_Error is raised if Key is not found.
390
 
391
      function Find (Container : Set; Key : Key_Type) return Cursor;
392
      --  Searches for the node containing Key, and returns a cursor
393
      --  designating the node. The search works by first calling Hash (on Key)
394
      --  to determine the bucket. If the bucket is not empty, the search
395
      --  compares Key to the element of each node in the bucket, and returns
396
      --  the matching node. The comparison itself works by applying the
397
      --  generic formal Key operation to the element of the node, and then
398
      --  calling generic formal operation Equivalent_Keys.
399
 
400
      function Contains (Container : Set; Key : Key_Type) return Boolean;
401
      --  Equivalent to Find (Container, Key) /= No_Element
402
 
403
      procedure Update_Element_Preserving_Key
404
        (Container : in out Set;
405
         Position  : Cursor;
406
         Process   : not null access
407
                       procedure (Element : in out Element_Type));
408
      --  Calls Process with the element of the node designated by Position,
409
      --  but with the restriction that the key-value of the element is not
410
      --  modified. The operation first makes a copy of the value returned by
411
      --  applying generic formal operation Key on the element of the node, and
412
      --  then calls Process with the element. The operation verifies that the
413
      --  key-part has not been modified by calling generic formal operation
414
      --  Equivalent_Keys to compare the saved key-value to the value returned
415
      --  by applying generic formal operation Key to the post-Process value of
416
      --  element. If the key values compare equal then the operation
417
      --  completes. Otherwise, the node is removed from the set and
418
      --  Program_Error is raised.
419
 
420
      type Reference_Type (Element : not null access Element_Type) is private
421
        with Implicit_Dereference => Element;
422
 
423
      function Reference_Preserving_Key
424
        (Container : aliased in out Set;
425
         Position  : Cursor) return Reference_Type;
426
 
427
      function Constant_Reference
428
        (Container : aliased Set;
429
         Key       : Key_Type) return Constant_Reference_Type;
430
 
431
      function Reference_Preserving_Key
432
        (Container : aliased in out Set;
433
         Key       : Key_Type) return Reference_Type;
434
 
435
   private
436
      type Reference_Type (Element : not null access Element_Type)
437
         is null record;
438
 
439
      use Ada.Streams;
440
 
441
      procedure Read
442
        (Stream : not null access Root_Stream_Type'Class;
443
         Item   : out Reference_Type);
444
 
445
      for Reference_Type'Read use Read;
446
 
447
      procedure Write
448
        (Stream : not null access Root_Stream_Type'Class;
449
         Item   : Reference_Type);
450
 
451
      for Reference_Type'Write use Write;
452
 
453
   end Generic_Keys;
454
 
455
private
456
   pragma Inline (Next);
457
 
458
   type Node_Type;
459
   type Node_Access is access Node_Type;
460
 
461
   type Node_Type is limited record
462
      Element : aliased Element_Type;
463
      Next    : Node_Access;
464
   end record;
465
 
466
   package HT_Types is
467
     new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
468
 
469
   type Set is new Ada.Finalization.Controlled with record
470
      HT : HT_Types.Hash_Table_Type;
471
   end record;
472
 
473
   overriding procedure Adjust (Container : in out Set);
474
 
475
   overriding procedure Finalize (Container : in out Set);
476
 
477
   use HT_Types;
478
   use Ada.Finalization;
479
   use Ada.Streams;
480
 
481
   procedure Write
482
     (Stream    : not null access Root_Stream_Type'Class;
483
      Container : Set);
484
 
485
   for Set'Write use Write;
486
 
487
   procedure Read
488
     (Stream    : not null access Root_Stream_Type'Class;
489
      Container : out Set);
490
 
491
   for Set'Read use Read;
492
 
493
   type Set_Access is access all Set;
494
   for Set_Access'Storage_Size use 0;
495
 
496
   type Cursor is record
497
      Container : Set_Access;
498
      Node      : Node_Access;
499
   end record;
500
 
501
   procedure Write
502
     (Stream : not null access Root_Stream_Type'Class;
503
      Item   : Cursor);
504
 
505
   for Cursor'Write use Write;
506
 
507
   procedure Read
508
     (Stream : not null access Root_Stream_Type'Class;
509
      Item   : out Cursor);
510
 
511
   for Cursor'Read use Read;
512
 
513
   type Reference_Control_Type is
514
      new Controlled with record
515
         Container : Set_Access;
516
      end record;
517
 
518
   overriding procedure Adjust (Control : in out Reference_Control_Type);
519
   pragma Inline (Adjust);
520
 
521
   overriding procedure Finalize (Control : in out Reference_Control_Type);
522
   pragma Inline (Finalize);
523
 
524
   type Constant_Reference_Type
525
     (Element : not null access constant Element_Type) is
526
      record
527
         Control : Reference_Control_Type;
528
      end record;
529
 
530
   procedure Read
531
     (Stream : not null access Root_Stream_Type'Class;
532
      Item   : out Constant_Reference_Type);
533
 
534
   for Constant_Reference_Type'Read use Read;
535
 
536
   procedure Write
537
     (Stream : not null access Root_Stream_Type'Class;
538
      Item   : Constant_Reference_Type);
539
 
540
   for Constant_Reference_Type'Write use Write;
541
 
542
   Empty_Set : constant Set := (Controlled with HT => (null, 0, 0, 0));
543
 
544
   No_Element : constant Cursor := (Container => null, Node => null);
545
 
546
end Ada.Containers.Hashed_Sets;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.