OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [ada/] [a-cohase.ads] - Blame information for rev 454

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 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-2009, 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
private with Ada.Containers.Hash_Tables;
35
private with Ada.Streams;
36
private with Ada.Finalization;
37
 
38
generic
39
   type Element_Type is private;
40
 
41
   with function Hash (Element : Element_Type) return Hash_Type;
42
 
43
   with function Equivalent_Elements
44
          (Left, Right : Element_Type) return Boolean;
45
 
46
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
47
 
48
package Ada.Containers.Hashed_Sets is
49
   pragma Preelaborate;
50
   pragma Remote_Types;
51
 
52
   type Set is tagged private;
53
   pragma Preelaborable_Initialization (Set);
54
 
55
   type Cursor is private;
56
   pragma Preelaborable_Initialization (Cursor);
57
 
58
   Empty_Set : constant Set;
59
   --  Set objects declared without an initialization expression are
60
   --  initialized to the value Empty_Set.
61
 
62
   No_Element : constant Cursor;
63
   --  Cursor objects declared without an initialization expression are
64
   --  initialized to the value No_Element.
65
 
66
   function "=" (Left, Right : Set) return Boolean;
67
   --  For each element in Left, set equality attempts to find the equal
68
   --  element in Right; if a search fails, then set equality immediately
69
   --  returns False. The search works by calling Hash to find the bucket in
70
   --  the Right set that corresponds to the Left element. If the bucket is
71
   --  non-empty, the search calls the generic formal element equality operator
72
   --  to compare the element (in Left) to the element of each node in the
73
   --  bucket (in Right); the search terminates when a matching node in the
74
   --  bucket is found, or the nodes in the bucket are exhausted. (Note that
75
   --  element equality is called here, not Equivalent_Elements. Set equality
76
   --  is the only operation in which element equality is used. Compare set
77
   --  equality to Equivalent_Sets, which does call Equivalent_Elements.)
78
 
79
   function Equivalent_Sets (Left, Right : Set) return Boolean;
80
   --  Similar to set equality, with the difference that the element in Left is
81
   --  compared to the elements in Right using the generic formal
82
   --  Equivalent_Elements operation instead of element equality.
83
 
84
   function To_Set (New_Item : Element_Type) return Set;
85
   --  Constructs a singleton set comprising New_Element. To_Set calls Hash to
86
   --  determine the bucket for New_Item.
87
 
88
   function Capacity (Container : Set) return Count_Type;
89
   --  Returns the current capacity of the set. Capacity is the maximum length
90
   --  before which rehashing in guaranteed not to occur.
91
 
92
   procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
93
   --  Adjusts the current capacity, by allocating a new buckets array. If the
94
   --  requested capacity is less than the current capacity, then the capacity
95
   --  is contracted (to a value not less than the current length). If the
96
   --  requested capacity is greater than the current capacity, then the
97
   --  capacity is expanded (to a value not less than what is requested). In
98
   --  either case, the nodes are rehashed from the old buckets array onto the
99
   --  new buckets array (Hash is called once for each existing element in
100
   --  order to compute the new index), and then the old buckets array is
101
   --  deallocated.
102
 
103
   function Length (Container : Set) return Count_Type;
104
   --  Returns the number of items in the set
105
 
106
   function Is_Empty (Container : Set) return Boolean;
107
   --  Equivalent to Length (Container) = 0
108
 
109
   procedure Clear (Container : in out Set);
110
   --  Removes all of the items from the set
111
 
112
   function Element (Position : Cursor) return Element_Type;
113
   --  Returns the element of the node designated by the cursor
114
 
115
   procedure Replace_Element
116
     (Container : in out Set;
117
      Position  : Cursor;
118
      New_Item  : Element_Type);
119
   --  If New_Item is equivalent (as determined by calling Equivalent_Elements)
120
   --  to the element of the node designated by Position, then New_Element is
121
   --  assigned to that element. Otherwise, it calls Hash to determine the
122
   --  bucket for New_Item. If the bucket is not empty, then it calls
123
   --  Equivalent_Elements for each node in that bucket to determine whether
124
   --  New_Item is equivalent to an element in that bucket. If
125
   --  Equivalent_Elements returns True then Program_Error is raised (because
126
   --  an element may appear only once in the set); otherwise, New_Item is
127
   --  assigned to the node designated by Position, and the node is moved to
128
   --  its new bucket.
129
 
130
   procedure Query_Element
131
     (Position : Cursor;
132
      Process  : not null access procedure (Element : Element_Type));
133
   --  Calls Process with the element (having only a constant view) of the node
134
   --  designed by the cursor.
135
 
136
   procedure Move (Target : in out Set; Source : in out Set);
137
   --  Clears Target (if it's not empty), and then moves (not copies) the
138
   --  buckets array and nodes from Source to Target.
139
 
140
   procedure Insert
141
     (Container : in out Set;
142
      New_Item  : Element_Type;
143
      Position  : out Cursor;
144
      Inserted  : out Boolean);
145
   --  Conditionally inserts New_Item into the set. If New_Item is already in
146
   --  the set, then Inserted returns False and Position designates the node
147
   --  containing the existing element (which is not modified). If New_Item is
148
   --  not already in the set, then Inserted returns True and Position
149
   --  designates the newly-inserted node containing New_Item. The search for
150
   --  an existing element works as follows. Hash is called to determine
151
   --  New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
152
   --  is called to compare New_Item to the element of each node in that
153
   --  bucket. If the bucket is empty, or there were no equivalent elements in
154
   --  the bucket, the search "fails" and the New_Item is inserted in the set
155
   --  (and Inserted returns True); otherwise, the search "succeeds" (and
156
   --  Inserted returns False).
157
 
158
   procedure Insert  (Container : in out Set; New_Item : Element_Type);
159
   --  Attempts to insert New_Item into the set, performing the usual insertion
160
   --  search (which involves calling both Hash and Equivalent_Elements); if
161
   --  the search succeeds (New_Item is equivalent to an element already in the
162
   --  set, and so was not inserted), then this operation raises
163
   --  Constraint_Error. (This version of Insert is similar to Replace, but
164
   --  having the opposite exception behavior. It is intended for use when you
165
   --  want to assert that the item is not already in the set.)
166
 
167
   procedure Include (Container : in out Set; New_Item : Element_Type);
168
   --  Attempts to insert New_Item into the set. If an element equivalent to
169
   --  New_Item is already in the set (the insertion search succeeded, and
170
   --  hence New_Item was not inserted), then the value of New_Item is assigned
171
   --  to the existing element. (This insertion operation only raises an
172
   --  exception if cursor tampering occurs. It is intended for use when you
173
   --  want to insert the item in the set, and you don't care whether an
174
   --  equivalent element is already present.)
175
 
176
   procedure Replace (Container : in out Set; New_Item : Element_Type);
177
   --  Searches for New_Item in the set; if the search fails (because an
178
   --  equivalent element was not in the set), then it raises
179
   --  Constraint_Error. Otherwise, the existing element is assigned the value
180
   --  New_Item. (This is similar to Insert, but with the opposite exception
181
   --  behavior. It is intended for use when you want to assert that the item
182
   --  is already in the set.)
183
 
184
   procedure Exclude (Container : in out Set; Item : Element_Type);
185
   --  Searches for Item in the set, and if found, removes its node from the
186
   --  set and then deallocates it. The search works as follows. The operation
187
   --  calls Hash to determine the item's bucket; if the bucket is not empty,
188
   --  it calls Equivalent_Elements to compare Item to the element of each node
189
   --  in the bucket. (This is the deletion analog of Include. It is intended
190
   --  for use when you want to remove the item from the set, but don't care
191
   --  whether the item is already in the set.)
192
 
193
   procedure Delete  (Container : in out Set; Item : Element_Type);
194
   --  Searches for Item in the set (which involves calling both Hash and
195
   --  Equivalent_Elements). If the search fails, then the operation raises
196
   --  Constraint_Error. Otherwise it removes the node from the set and then
197
   --  deallocates it. (This is the deletion analog of non-conditional
198
   --  Insert. It is intended for use when you want to assert that the item is
199
   --  already in the set.)
200
 
201
   procedure Delete (Container : in out Set; Position : in out Cursor);
202
   --  Removes the node designated by Position from the set, and then
203
   --  deallocates the node. The operation calls Hash to determine the bucket,
204
   --  and then compares Position to each node in the bucket until there's a
205
   --  match (it does not call Equivalent_Elements).
206
 
207
   procedure Union (Target : in out Set; Source : Set);
208
   --  The operation first calls Reserve_Capacity if the current capacity is
209
   --  less than the sum of the lengths of Source and Target. It then iterates
210
   --  over the Source set, and conditionally inserts each element into Target.
211
 
212
   function Union (Left, Right : Set) return Set;
213
   --  The operation first copies the Left set to the result, and then iterates
214
   --  over the Right set to conditionally insert each element into the result.
215
 
216
   function "or" (Left, Right : Set) return Set renames Union;
217
 
218
   procedure Intersection (Target : in out Set; Source : Set);
219
   --  Iterates over the Target set (calling First and Next), calling Find to
220
   --  determine whether the element is in Source. If an equivalent element is
221
   --  not found in Source, the element is deleted from Target.
222
 
223
   function Intersection (Left, Right : Set) return Set;
224
   --  Iterates over the Left set, calling Find to determine whether the
225
   --  element is in Right. If an equivalent element is found, it is inserted
226
   --  into the result set.
227
 
228
   function "and" (Left, Right : Set) return Set renames Intersection;
229
 
230
   procedure Difference (Target : in out Set; Source : Set);
231
   --  Iterates over the Source (calling First and Next), calling Find to
232
   --  determine whether the element is in Target. If an equivalent element is
233
   --  found, it is deleted from Target.
234
 
235
   function Difference (Left, Right : Set) return Set;
236
   --  Iterates over the Left set, calling Find to determine whether the
237
   --  element is in the Right set. If an equivalent element is not found, the
238
   --  element is inserted into the result set.
239
 
240
   function "-" (Left, Right : Set) return Set renames Difference;
241
 
242
   procedure Symmetric_Difference (Target : in out Set; Source : Set);
243
   --  The operation first calls Reserve_Capacity if the current capacity is
244
   --  less than the sum of the lengths of Source and Target. It then iterates
245
   --  over the Source set, searching for the element in Target (calling Hash
246
   --  and Equivalent_Elements). If an equivalent element is found, it is
247
   --  removed from Target; otherwise it is inserted into Target.
248
 
249
   function Symmetric_Difference (Left, Right : Set) return Set;
250
   --  The operation first iterates over the Left set. It calls Find to
251
   --  determine whether the element is in the Right set. If no equivalent
252
   --  element is found, the element from Left is inserted into the result. The
253
   --  operation then iterates over the Right set, to determine whether the
254
   --  element is in the Left set. If no equivalent element is found, the Right
255
   --  element is inserted into the result.
256
 
257
   function "xor" (Left, Right : Set) return Set
258
     renames Symmetric_Difference;
259
 
260
   function Overlap (Left, Right : Set) return Boolean;
261
   --  Iterates over the Left set (calling First and Next), calling Find to
262
   --  determine whether the element is in the Right set. If an equivalent
263
   --  element is found, the operation immediately returns True. The operation
264
   --  returns False if the iteration over Left terminates without finding any
265
   --  equivalent element in Right.
266
 
267
   function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
268
   --  Iterates over Subset (calling First and Next), calling Find to determine
269
   --  whether the element is in Of_Set. If no equivalent element is found in
270
   --  Of_Set, the operation immediately returns False. The operation returns
271
   --  True if the iteration over Subset terminates without finding an element
272
   --  not in Of_Set (that is, every element in Subset is equivalent to an
273
   --  element in Of_Set).
274
 
275
   function First (Container : Set) return Cursor;
276
   --  Returns a cursor that designates the first non-empty bucket, by
277
   --  searching from the beginning of the buckets array.
278
 
279
   function Next (Position : Cursor) return Cursor;
280
   --  Returns a cursor that designates the node that follows the current one
281
   --  designated by Position. If Position designates the last node in its
282
   --  bucket, the operation calls Hash to compute the index of this bucket,
283
   --  and searches the buckets array for the first non-empty bucket, starting
284
   --  from that index; otherwise, it simply follows the link to the next node
285
   --  in the same bucket.
286
 
287
   procedure Next (Position : in out Cursor);
288
   --  Equivalent to Position := Next (Position)
289
 
290
   function Find
291
     (Container : Set;
292
      Item      : Element_Type) return Cursor;
293
   --  Searches for Item in the set. Find calls Hash to determine the item's
294
   --  bucket; if the bucket is not empty, it calls Equivalent_Elements to
295
   --  compare Item to each element in the bucket. If the search succeeds, Find
296
   --  returns a cursor designating the node containing the equivalent element;
297
   --  otherwise, it returns No_Element.
298
 
299
   function Contains (Container : Set; Item : Element_Type) return Boolean;
300
   --  Equivalent to Find (Container, Item) /= No_Element
301
 
302
   function Has_Element (Position : Cursor) return Boolean;
303
   --  Equivalent to Position /= No_Element
304
 
305
   function Equivalent_Elements (Left, Right : Cursor) return Boolean;
306
   --  Returns the result of calling Equivalent_Elements with the elements of
307
   --  the nodes designated by cursors Left and Right.
308
 
309
   function Equivalent_Elements
310
     (Left  : Cursor;
311
      Right : Element_Type) return Boolean;
312
   --  Returns the result of calling Equivalent_Elements with element of the
313
   --  node designated by Left and element Right.
314
 
315
   function Equivalent_Elements
316
     (Left  : Element_Type;
317
      Right : Cursor) return Boolean;
318
   --  Returns the result of calling Equivalent_Elements with element Left and
319
   --  the element of the node designated by Right.
320
 
321
   procedure Iterate
322
     (Container : Set;
323
      Process   : not null access procedure (Position : Cursor));
324
   --  Calls Process for each node in the set
325
 
326
   generic
327
      type Key_Type (<>) is private;
328
 
329
      with function Key (Element : Element_Type) return Key_Type;
330
 
331
      with function Hash (Key : Key_Type) return Hash_Type;
332
 
333
      with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
334
 
335
   package Generic_Keys is
336
 
337
      function Key (Position : Cursor) return Key_Type;
338
      --  Applies generic formal operation Key to the element of the node
339
      --  designated by Position.
340
 
341
      function Element (Container : Set; Key : Key_Type) return Element_Type;
342
      --  Searches (as per the key-based Find) for the node containing Key, and
343
      --  returns the associated element.
344
 
345
      procedure Replace
346
        (Container : in out Set;
347
         Key       : Key_Type;
348
         New_Item  : Element_Type);
349
      --  Searches (as per the key-based Find) for the node containing Key, and
350
      --  then replaces the element of that node (as per the element-based
351
      --  Replace_Element).
352
 
353
      procedure Exclude (Container : in out Set; Key : Key_Type);
354
      --  Searches for Key in the set, and if found, removes its node from the
355
      --  set and then deallocates it. The search works by first calling Hash
356
      --  (on Key) to determine the bucket; if the bucket is not empty, it
357
      --  calls Equivalent_Keys to compare parameter Key to the value of
358
      --  generic formal operation Key applied to element of each node in the
359
      --  bucket.
360
 
361
      procedure Delete (Container : in out Set; Key : Key_Type);
362
      --  Deletes the node containing Key as per Exclude, with the difference
363
      --  that Constraint_Error is raised if Key is not found.
364
 
365
      function Find (Container : Set; Key : Key_Type) return Cursor;
366
      --  Searches for the node containing Key, and returns a cursor
367
      --  designating the node. The search works by first calling Hash (on Key)
368
      --  to determine the bucket. If the bucket is not empty, the search
369
      --  compares Key to the element of each node in the bucket, and returns
370
      --  the matching node. The comparison itself works by applying the
371
      --  generic formal Key operation to the element of the node, and then
372
      --  calling generic formal operation Equivalent_Keys.
373
 
374
      function Contains (Container : Set; Key : Key_Type) return Boolean;
375
      --  Equivalent to Find (Container, Key) /= No_Element
376
 
377
      procedure Update_Element_Preserving_Key
378
        (Container : in out Set;
379
         Position  : Cursor;
380
         Process   : not null access
381
                       procedure (Element : in out Element_Type));
382
      --  Calls Process with the element of the node designated by Position,
383
      --  but with the restriction that the key-value of the element is not
384
      --  modified. The operation first makes a copy of the value returned by
385
      --  applying generic formal operation Key on the element of the node, and
386
      --  then calls Process with the element. The operation verifies that the
387
      --  key-part has not been modified by calling generic formal operation
388
      --  Equivalent_Keys to compare the saved key-value to the value returned
389
      --  by applying generic formal operation Key to the post-Process value of
390
      --  element. If the key values compare equal then the operation
391
      --  completes. Otherwise, the node is removed from the map and
392
      --  Program_Error is raised.
393
 
394
   end Generic_Keys;
395
 
396
private
397
 
398
   pragma Inline (Next);
399
 
400
   type Node_Type;
401
   type Node_Access is access Node_Type;
402
 
403
   type Node_Type is limited record
404
      Element : Element_Type;
405
      Next    : Node_Access;
406
   end record;
407
 
408
   package HT_Types is
409
     new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
410
 
411
   type Set is new Ada.Finalization.Controlled with record
412
      HT : HT_Types.Hash_Table_Type;
413
   end record;
414
 
415
   overriding
416
   procedure Adjust (Container : in out Set);
417
 
418
   overriding
419
   procedure Finalize (Container : in out Set);
420
 
421
   use HT_Types;
422
   use Ada.Finalization;
423
   use Ada.Streams;
424
 
425
   type Set_Access is access all Set;
426
   for Set_Access'Storage_Size use 0;
427
 
428
   type Cursor is record
429
      Container : Set_Access;
430
      Node      : Node_Access;
431
   end record;
432
 
433
   procedure Write
434
     (Stream : not null access Root_Stream_Type'Class;
435
      Item   : Cursor);
436
 
437
   for Cursor'Write use Write;
438
 
439
   procedure Read
440
     (Stream : not null access Root_Stream_Type'Class;
441
      Item   : out Cursor);
442
 
443
   for Cursor'Read use Read;
444
 
445
   No_Element : constant Cursor := (Container => null, Node => null);
446
 
447
   procedure Write
448
     (Stream    : not null access Root_Stream_Type'Class;
449
      Container : Set);
450
 
451
   for Set'Write use Write;
452
 
453
   procedure Read
454
     (Stream    : not null access Root_Stream_Type'Class;
455
      Container : out Set);
456
 
457
   for Set'Read use Read;
458
 
459
   Empty_Set : constant Set := (Controlled with HT => (null, 0, 0, 0));
460
 
461
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.