-
Notifications
You must be signed in to change notification settings - Fork 3
/
generic_discrete_indefinite_map.ads
409 lines (402 loc) · 12.2 KB
/
generic_discrete_indefinite_map.ads
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
-- --
-- package Copyright (c) Dmitry A. Kazakov --
-- Generic_Discrete_Indefinite_Map Luebeck --
-- Interface Spring, 2012 --
-- --
-- Last revision : 14:26 27 May 2012 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
--
-- This package defines the type Map of discrete keys to indefinite
-- objects. The package is generic:
--
-- Key_Type - The type of the key used to order/access the map
-- Object_Type - The type of the items in the map
-- Minimal_Size - Minimal additionally allocated size
-- Increment - By which the set is enlarged if necessary
--
-- The parameter Increment controls set size growth. When there is no
-- free space in the set then it is enlarged by Size * Increment / 100.
-- Here Size is the current set size. The allocated amount of items
-- cannot be less than the parameter Minimal_Size specifies. So it will
-- be the initial set size after the first element is put in.
--
with Ada.Finalization;
with Generic_Map;
with Object.Handle;
generic
type Key_Type is (<>);
type Object_Type (<>) is private;
Minimal_Size : Positive := 64;
Increment : Natural := 50;
package Generic_Discrete_Indefinite_Map is
type Map is new Ada.Finalization.Controlled with private;
--
-- Add -- Add a new item to the set
--
-- Container - The set to be modified
-- Key / From, To - Key or range of keys for the item to be added
-- Item - The item to be added
--
-- This procedure adds Item to the map.
--
-- Exceptions :
--
-- Constraint_Error - There is already an item with the key equal to
-- Key.
--
procedure Add
( Container : in out Map;
Key : Key_Type;
Item : Object_Type
);
procedure Add
( Container : in out Map;
From, To : Key_Type;
Item : Object_Type
);
--
-- Add -- Add items from one set to another
--
-- Container - The set to be modified
-- Items - To be added
--
-- This procedure adds the items from Items to Container. An item is
-- added only if Container does not already has another item with the
-- equal key.
--
procedure Add (Container : in out Map; Items : Map);
--
-- Create -- Create a new empty map
--
-- Returns :
--
-- The new set
--
function Create return Map;
--
-- Create -- Create a singleton set
--
-- Key / From, To - Key or a range of keys to add
-- Item - The item to be added
--
-- Returns :
--
-- The new map containing Item
--
-- Exceptions :
--
-- Constaint_Error - From > To
--
function Create (Key : Key_Type; Item : Object_Type) return Map;
function Create (From, To : Key_Type; Item : Object_Type) return Map;
--
-- Erase -- Remove all items from the map
--
-- Container - The map
--
procedure Erase (Container : in out Map);
--
-- Finalize -- Destructor
--
-- Container - The map
--
-- When finalized the use count is decreased. Only if it becomes
-- negative, the set is finally removed.
--
procedure Finalize (Container : in out Map);
--
-- Find -- Find an element or a range in the set
--
-- Container - The set
-- Key / From, To - Key or a range of keys to search for
--
-- When search is successful the result is positive. Otherwise, it is
-- negative and indicates the position the item should be placed if
-- inserted into the list.
--
-- Returns :
--
-- [+] The index of the range containing the all keys
-- [-] The negated index of the position the item should be
--
-- Exceptions :
--
-- Constaint_Error - From..To is an empty or invalid interval
--
function Find (Container : Map; Key : Key_Type) return Integer;
function Find (Container : Map; From, To : Key_Type) return Integer;
--
-- From -- Get the lower bound of a range of keys by its index
--
-- Container - The set
-- Index - The index of the range
--
-- Returns :
--
-- The key
--
-- Exceptions :
--
-- Contraint_Error - Wrong index
--
function From (Container : Map; Index : Positive) return Key_Type;
--
-- Get -- Get an item by its key
--
-- Container - The map
-- Key - The key of the item to get
--
-- Returns :
--
-- The item
--
-- Exceptions :
--
-- Contraint_Error - Wrong key
--
function Get (Container : Map; Key : Key_Type) return Object_Type;
--
-- Get_Key -- Get a range of keys by its index
--
-- Container - The set
-- Index - The index of the range
-- From - The first element of the range
-- To - The laset element of the range
--
-- Exceptions :
--
-- Contraint_Error - Wrong index
--
procedure Get_Key
( Container : Map;
Index : Positive;
From : out Key_Type;
To : out Key_Type
);
--
-- Get_Size -- Get the number of ranges in the map
--
-- Container - The map
--
-- Returns :
--
-- The number of ranges
--
function Get_Size (Container : Map) return Natural;
--
-- Is_Empty -- Test if the map is empty
--
-- Container - The map
--
-- Returns :
--
-- True if the map contains no items
--
function Is_Empty (Container : Map) return Boolean;
--
-- Is_In -- Test if an item in the set
--
-- Container - The set
-- Key / From, To - Key or a range of keys to test
--
-- Returns :
--
-- True if the item or range is in the set
--
function Is_In (Container : Map; Key : Key_Type) return Boolean;
function Is_In (Container : Map; From, To : Key_Type) return Boolean;
--
-- Is_Not_In -- Test if a range of keys not in the map
--
-- Container - The set
-- From, To - A range to test
--
-- Returns :
--
-- True if no item from the range is in the set
--
function Is_Not_In (Container : Map; From, To : Key_Type)
return Boolean;
--
-- Range_Get -- Get an item by range index
--
-- Container - The map
-- Index - The number of the item to get
--
-- Returns :
--
-- The item
--
-- Exceptions :
--
-- Contraint_Error - Wrong index
--
function Range_Get (Container : Map; Index : Positive)
return Object_Type;
--
-- Range_Remove -- Remove range of keys from the map by its index
--
-- Container - The map
-- Index - Of the range to remove
--
-- Exceptions :
--
-- Contraint_Error - Wrong index
--
procedure Range_Remove (Container : in out Map; Index : Positive);
--
-- Range_Replace -- Replace an range in the map by its index
--
-- Container - The map to be modified
-- Index - Of the range to be replaced
-- Item - To be replaced
--
-- Exceptions :
--
-- Contraint_Error - Wrong index
--
procedure Range_Replace
( Container : in out Map;
Index : Positive;
Item : Object_Type
);
--
-- Remove -- Remove element from the set
--
-- Container - The set
-- Item / From, To - Key or a range of keys to remove
--
-- If the items are not in the set, nothing happens.
--
procedure Remove (Container : in out Map; Key : Key_Type);
procedure Remove (Container : in out Map; From, To : Key_Type);
--
-- Remove -- Remove elements of one map from another map
--
-- Container - The map
-- Items - The map of items to be removed
--
-- If an item from the map Items is not in the map Container, nothing
-- happens. Otherwise it is removed from Container.
--
procedure Remove (Container : in out Map; Items : Map);
--
-- Replace -- Replace an item in the map
--
-- Container - The map to be modified
-- Key / From, To - Key or a range of keys to add or replace
-- Item - To be added / replaced
--
-- This procedure adds Item to the set. The element is replaced if it is
-- already in the map.
--
-- Exceptions :
--
-- Contraint_Error - From > To
--
procedure Replace
( Container : in out Map;
Key : Key_Type;
Item : Object_Type
);
procedure Replace
( Container : in out Map;
From, To : Key_Type;
Item : Object_Type
);
--
-- Replace -- Replace items from one map in another
--
-- Container - The map to be modified
-- Items - To be added
--
-- This procedure adds the items from the map Items to the map
-- Container. An element is replaced if it is already in the map.
--
procedure Replace (Container : in out Map; Items : Map);
--
-- To -- Get the upper bound of a range of keys by its index
--
-- Container - The set
-- Index - The index of the range
--
-- Returns :
--
-- The key
--
-- Exceptions :
--
-- Contraint_Error - Wrong index
--
function To (Container : Map; Index : Positive) return Key_Type;
--
-- = -- Comparison
--
-- Left - A map
-- Right - A map
--
-- Returns :
--
-- True if both maps contain same items
--
function "=" (Left, Right : Map) return Boolean;
private
type Range_Type is record
From : Key_Type'Base;
To : Key_Type'Base;
end record;
function Intersect (Left, Right : Range_Type) return Boolean;
function Less (Left, Right : Range_Type) return Boolean;
pragma Inline (Create);
pragma Inline (Find);
pragma Inline (From);
pragma Inline (Get);
pragma Inline (Get_Key);
pragma Inline (Get_Size);
pragma Inline (Intersect);
pragma Inline (Is_Empty);
pragma Inline (Is_In);
pragma Inline (Less);
pragma Inline (Range_Get);
pragma Inline (Range_Remove);
pragma Inline (Range_Replace);
pragma Inline (To);
pragma Assert (Key_Type'Base'Last /= Key_Type'Base'First);
type Object_Ptr is access Object_Type;
type Token is new Object.Entity with record
Object : Object_Ptr;
end record;
type Token_Ptr is access Token'Class;
procedure Finalize (Reference : in out Token);
package Token_Handles is new Object.Handle (Token'Class, Token_Ptr);
use Token_Handles;
package Range_Maps is
new Generic_Map
( Key_Type => Range_Type,
Object_Type => Handle,
Minimal_Size => Minimal_Size,
Increment => Increment,
"=" => Intersect,
"<" => Less
);
type Map is new Range_Maps.Map with null record;
end Generic_Discrete_Indefinite_Map;