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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#![feature(try_from, nonzero, trusted_len, fused, const_max_value, core_intrinsics, const_fn)]
extern crate core;
#[cfg(feature="serde")]
extern crate serde;
#[cfg(feature = "petgraph")]
extern crate petgraph;
use std::marker::PhantomData;
use std::iter::{self, FromIterator};
use std::borrow::Borrow;
use std::ops::{Index, IndexMut};
use std::fmt::{self, Debug, Formatter};
pub mod table;
#[cfg(feature="serde")]
mod serialization;
mod utils;
mod integer_id;
pub use integer_id::IntegerId;
use table::{
EntryTable, EntryIterable, DenseEntryTable,
SafeEntries, SafeEntriesMut, DirectEntryTable
};
pub type DirectIdMap<K, V> = IdMap<K, V, DirectEntryTable<K, V>>;
pub type OrderedIdMap<K, V> = IdMap<K, V, DenseEntryTable<K, V>>;
pub struct IdMap<K: IntegerId, V, T: EntryTable<K, V> = DenseEntryTable<K, V>> {
entries: T,
marker: PhantomData<DirectEntryTable<K, V>>
}
impl<K: IntegerId, V> IdMap<K, V, DirectEntryTable<K, V>> {
#[inline]
pub fn new_direct() -> Self {
IdMap::new_other()
}
#[inline]
pub fn with_capacity_direct(capacity: usize) -> Self {
IdMap::with_capacity_other(capacity)
}
}
impl<K: IntegerId, V> IdMap<K, V> {
#[inline]
pub fn new() -> Self {
IdMap {
entries: DenseEntryTable::new(),
marker: PhantomData
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
IdMap {
entries: DenseEntryTable::with_capacity(capacity),
marker: PhantomData
}
}
}
impl<K: IntegerId, V, T: EntryTable<K, V>> IdMap<K, V, T> {
#[inline]
pub fn new_other() -> Self {
IdMap {
entries: T::new(),
marker: PhantomData
}
}
#[inline]
pub fn with_capacity_other(capacity: usize) -> Self {
IdMap {
entries: T::with_capacity(capacity),
marker: PhantomData
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn max_id(&self) -> Option<u64> {
self.entries.max_id()
}
#[inline]
pub fn get<Q: Borrow<K>>(&self, key: Q) -> Option<&V> {
let key = key.borrow();
self.entries.get(key)
}
#[inline]
pub fn get_mut<Q: Borrow<K>>(&mut self, key: Q) -> Option<&mut V> {
let key = key.borrow();
self.entries.get_mut(key)
}
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.entries.insert(key, value)
}
#[inline]
pub fn remove<Q: Borrow<K>>(&mut self, key: Q) -> Option<V> {
let key = key.borrow();
self.entries.swap_remove(key)
}
#[inline]
pub fn entry(&mut self, key: K) -> Entry<K, V, T> {
if self.entries.get(&key).is_some() {
Entry::Occupied(OccupiedEntry {
map: self, key
})
} else {
Entry::Vacant(VacantEntry { key, map: self })
}
}
#[inline]
pub fn iter(&self) -> Iter<K, V, T> {
Iter(SafeEntries::new(&self.entries))
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<K, V, T> {
IterMut(SafeEntriesMut::new(&mut self.entries))
}
#[inline]
pub fn keys(&self) -> Keys<K, V, T> {
Keys(SafeEntries::new(&self.entries))
}
#[inline]
pub fn values(&self) -> Values<K, V, T> {
Values(SafeEntries::new(&self.entries))
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<K, V, T> {
ValuesMut(SafeEntriesMut::new(&mut self.entries))
}
#[inline]
pub fn clear(&mut self) {
self.entries.clear();
}
#[inline]
pub fn retain<F>(&mut self, func: F) where F: FnMut(&K, &V) -> bool {
self.entries.retain(func);
}
#[inline]
pub fn reserve(&mut self, amount: usize) {
self.entries.reserve(amount);
}
#[inline]
pub fn raw_debug(&self) -> RawDebug<K, V, T> where K: Debug, V: Debug {
RawDebug(self)
}
}
impl<K, V, T> Clone for IdMap<K, V, T> where K: IntegerId + Clone, V: Clone, T: EntryTable<K, V> {
#[inline]
fn clone(&self) -> Self {
IdMap {
entries: self.entries.cloned(),
marker: PhantomData
}
}
}
pub struct RawDebug<'a, K: IntegerId + 'a, V: 'a, T: 'a + EntryTable<K, V>>(&'a IdMap<K, V, T>);
impl<'a, K, V, T> Debug for RawDebug<'a, K, V, T>
where K: IntegerId + Debug, V: Debug, T: EntryTable<K, V>, K: 'a, V: 'a, T: 'a {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("IdMap")
.field("entries", self.0.entries.raw_debug())
.finish()
}
}
impl<K, V1, T1, V2, T2> PartialEq<IdMap<K, V2, T2>> for IdMap<K, V1, T1>
where K: IntegerId, V1: PartialEq<V2>,
T1: EntryTable<K, V1>, T2: EntryTable<K, V2>, {
fn eq(&self, other: &IdMap<K, V2, T2>) -> bool {
if self.entries.len() != other.entries.len() {
return false;
}
self.iter().all(|(key, value)| {
other.get(key).map_or(false, |other_value| value == other_value)
})
}
}
#[macro_export]
macro_rules! idmap {
($($key:expr => $value:expr),*) => {
{
let entries = vec![$(($key, $value)),*];
let mut result = $crate::IdMap::with_capacity(entries.len());
result.extend(entries);
result
}
};
}
impl<K: IntegerId, V, T: EntryTable<K, V>> Default for IdMap<K, V, T> {
#[inline]
fn default() -> Self {
IdMap::new_other()
}
}
impl<K: IntegerId, V, T: EntryTable<K, V>> Index<K> for IdMap<K, V, T> {
type Output = V;
#[inline]
fn index(&self, key: K) -> &V {
&self[&key]
}
}
impl<K: IntegerId, V, T: EntryTable<K, V>> IndexMut<K> for IdMap<K, V, T> {
#[inline]
fn index_mut(&mut self, key: K) -> &mut V {
&mut self[&key]
}
}
impl<'a, K: IntegerId, V, T: EntryTable<K, V>> Index<&'a K> for IdMap<K, V, T> {
type Output = V;
#[inline]
fn index(&self, key: &'a K) -> &V {
if let Some(value) = self.get(key) {
value
} else {
panic!("Missing entry for {:?}", key)
}
}
}
impl<'a, K: IntegerId, V, T: EntryTable<K, V>> IndexMut<&'a K> for IdMap<K, V, T> {
#[inline]
fn index_mut(&mut self, key: &'a K) -> &mut V {
if let Some(value) = self.get_mut(key) {
value
} else {
panic!("Missing entry for {:?}", key)
}
}
}
impl<K: IntegerId, V: Debug, T: EntryTable<K, V>> Debug for IdMap<K, V, T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut d = f.debug_map();
for (key, value) in self.iter() {
d.entry(key, value);
}
d.finish()
}
}
pub enum Entry<'a, K: IntegerId + 'a, V: 'a, T: 'a + EntryTable<K, V>> {
Occupied(OccupiedEntry<'a, K, V, T>),
Vacant(VacantEntry<'a, K, V, T>)
}
impl<'a, K: IntegerId + 'a, V: 'a, T: EntryTable<K, V>> Entry<'a, K, V, T> {
#[inline]
pub fn or_insert(self, value: V) -> &'a mut V {
self.or_insert_with(|| value)
}
#[inline]
pub fn or_insert_with<F>(self, func: F) -> &'a mut V where F: FnOnce() -> V {
match self {
Entry::Occupied(entry) => entry.value(),
Entry::Vacant(entry) => entry.or_insert_with(func)
}
}
}
pub struct OccupiedEntry<'a, K: IntegerId + 'a, V: 'a, T: 'a + EntryTable<K, V>> {
map: &'a mut IdMap<K, V, T>,
key: K,
}
impl<'a, K: IntegerId + 'a, V: 'a, T: EntryTable<K, V>> OccupiedEntry<'a, K, V, T> {
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn value(self) -> &'a mut V {
self.map.entries.get_mut(&self.key).unwrap()
}
}
pub struct VacantEntry<'a, K: IntegerId + 'a, V: 'a, T: EntryTable<K, V> + 'a> {
map: &'a mut IdMap<K, V, T>,
key: K,
}
impl<'a, K: IntegerId + 'a, V: 'a, T: EntryTable<K, V> + 'a> VacantEntry<'a, K, V, T> {
#[inline]
pub fn insert(self, value: V) -> &'a mut V {
self.map.entries.insert_vacant(self.key, value)
}
#[inline]
pub fn or_insert_with<F>(self, func: F) -> &'a mut V where F: FnOnce() -> V {
self.insert(func())
}
}
impl<K: IntegerId, V, T: EntryTable<K, V>> IntoIterator for IdMap<K, V, T> {
type Item = (K, V);
type IntoIter = <T as IntoIterator>::IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.entries.into_iter()
}
}
impl<'a, K: IntegerId + 'a, V: 'a, T: 'a> IntoIterator for &'a IdMap<K, V, T>
where T: EntryTable<K, V> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V, T> IntoIterator for &'a mut IdMap<K, V, T>
where T: EntryTable<K, V>, K: IntegerId + 'a, V: 'a, T: 'a {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<K: IntegerId, V, T: EntryTable<K, V>> Extend<(K, V)> for IdMap<K, V, T> {
fn extend<I: IntoIterator<Item=(K, V)>>(&mut self, iter: I) {
let iter = iter.into_iter();
if let Some(size) = iter.size_hint().1 {
self.reserve(size);
}
for (key, value) in iter {
self.insert(key, value);
}
}
}
impl<K: IntegerId, V, T: EntryTable<K, V>> FromIterator<(K, V)> for IdMap<K, V, T> {
#[inline]
fn from_iter<I>(iterable: I) -> Self where I: IntoIterator<Item=(K, V)> {
let mut result = Self::new_other();
result.extend(iterable);
result
}
}
impl<'a, K, V, T> FromIterator<(&'a K, &'a V)> for IdMap<K, V, T>
where K: IntegerId + Clone + 'a, V: Clone + 'a, T: EntryTable<K, V> {
#[inline]
fn from_iter<I>(iterable: I) -> Self where I: IntoIterator<Item=(&'a K, &'a V)> {
let mut result = Self::new_other();
result.extend(iterable);
result
}
}
impl<'a, K, V, T> Extend<(&'a K, &'a V)> for IdMap<K, V, T>
where K: IntegerId + Clone + 'a, V: Clone + 'a, T: EntryTable<K, V> {
#[inline]
fn extend<I: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|(key, value)| (key.clone(), value.clone())))
}
}
macro_rules! delegating_iter {
($name:ident, $target:ident, $lifetime:tt, $key:tt, $value:tt, [ $item:ty ], |$handle:ident| $next:expr) => {
pub struct $name<$lifetime, $key, $value, I>($target<$lifetime, $key, $value, I>)
where $key: IntegerId + $lifetime, $value: $lifetime, I: 'a + EntryIterable<$key, $value>;
impl<$lifetime, $key, $value, I> Iterator for $name<$lifetime, $key, $value, I>
where $key: IntegerId + $lifetime, $value: $lifetime, I: 'a + EntryIterable<$key, $value> {
type Item = $item;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn next(&mut self) -> Option<$item> {
let $handle = &mut self.0;
$next
}
}
impl<$lifetime, $key, $value, I> iter::FusedIterator for $name<$lifetime, $key, $value, I>
where $key: IntegerId + $lifetime, $value: $lifetime,
I: 'a + EntryIterable<$key, $value>, I: iter::FusedIterator {}
impl<$lifetime, $key, $value, I> iter::ExactSizeIterator for $name<$lifetime, $key, $value, I>
where $key: IntegerId + $lifetime, $value: $lifetime,
I: 'a + EntryIterable<$key, $value>, I: iter::ExactSizeIterator {}
unsafe impl<$lifetime, $key, $value, I> iter::TrustedLen for $name<$lifetime, $key, $value, I>
where $key: IntegerId + $lifetime, $value: $lifetime,
I: 'a + EntryIterable<$key, $value>, I: iter::TrustedLen {}
};
}
delegating_iter!(IterMut, SafeEntriesMut, 'a, K, V, [ (&'a K, &'a mut V) ], |handle| handle.next().map(|(_, key, value)| (key, value)));
delegating_iter!(ValuesMut, SafeEntriesMut, 'a, K, V, [ &'a mut V ], |handle| handle.next().map(|(_, _, value)| value));
pub struct Iter<'a, K, V, I>(SafeEntries<'a, K, V, I>)
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>;
impl <'a, K, V, I> Iterator for Iter<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V> {
type Item = (&'a K, &'a V);
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn next(&mut self) -> Option<(&'a K, &'a V)> {
self.0.next().map(|(_, key, value)| (key, value))
}
}
impl <'a, K, V, I> iter::FusedIterator for Iter<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::FusedIterator {}
impl <'a, K, V, I> iter::ExactSizeIterator for Iter<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::ExactSizeIterator {}
unsafe impl <'a, K, V, I> iter::TrustedLen for Iter<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::TrustedLen {}
pub struct Keys<'a, K, V, I>(SafeEntries<'a, K, V, I>)
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>;
impl <'a, K, V, I> Iterator for Keys<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V> {
type Item = &'a K;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn next(&mut self) -> Option<&'a K> {
self.0.next().map(|(_, key, _)| key)
}
}
impl <'a, K, V, I> iter::FusedIterator for Keys<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::FusedIterator {}
impl <'a, K, V, I> iter::ExactSizeIterator for Keys<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::ExactSizeIterator {}
unsafe impl <'a, K, V, I> iter::TrustedLen for Keys<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::TrustedLen {}
pub struct Values<'a, K, V, I>(SafeEntries<'a, K, V, I>)
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>;
impl <'a, K, V, I> Iterator for Values<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V> {
type Item = &'a V;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn next(&mut self) -> Option<&'a V> {
self.0.next().map(|(_, _, value)| value)
}
}
impl <'a, K, V, I> iter::FusedIterator for Values<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::FusedIterator {}
impl <'a, K, V, I> iter::ExactSizeIterator for Values<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::ExactSizeIterator {}
unsafe impl <'a, K, V, I> iter::TrustedLen for Values<'a, K, V, I>
where K: IntegerId + 'a, V: 'a, I: 'a + EntryIterable<K, V>, I: iter::TrustedLen {}
#[doc(hidden)]
#[cold] #[inline(never)]
pub fn _invalid_id(id: u64) -> ! {
panic!("ID is invalid: {}", id);
}