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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#[doc(hidden)]
pub mod deps {
pub use paste::paste;
pub use slab::Slab;
}
#[macro_export]
macro_rules! n_key_set {
{
$(#[$meta:meta])*
$vis:vis struct $mapname:ident $(<$($P:ident),*>)? for $V:ty
$( where [ $($constr:tt)+ ] )?
{
$($body:tt)+
}
} => {
n_key_set!{
$(#[$meta])*
$vis struct [$($($P),*)?] $mapname [$($($P),*)?] for $V
$( [ where $($constr)+ ] )?
{
$( $body )+
}
}
};
{
$(#[$meta:meta])*
$vis:vis struct [$($($G:tt)+)?] $mapname:ident [$($($P:tt)+)?] for $V:ty
$( [ where $($constr:tt)+ ])?
{
$( $(( $($flag:ident)+ ))? $key:ident : $KEY:ty $({ $($source:tt)+ })? ),+
$(,)?
}
} => {
$crate::n_key_set::deps::paste!{
$( #[$meta] )*
#[doc = concat!(
"A set of elements of type ", stringify!($V), " whose members can be accessed by multiple keys.",
"\n\nThe keys are:",
$( " * `", stringify!($key), "` (`",stringify!($KEY),"`)\n" ,
$(" (", stringify!($($flag)+), ")", )?
)+
"\
Each member has a value for *each* required key, and up to one value
for *each* optional key.
The set contains at most one member for any value of a given key.
# Requirements
Key types must have consistent `Hash` and `Eq` implementations, as
they will be used as keys in a `HashMap`.
If all keys are optional, then every element in this set
must have at least one non-None key.
An element must not change its keys over time through interior
mutability.
⚠️ If *any* of these rules is violated, the consequences are unspecified,
and could include panics or wrong answers (but not memory-unsafety).
# Limitations
This could be more efficient in space and time.
",
)]
$vis struct $mapname $(<$($G)*>)?
where $( $KEY : std::hash::Hash + Eq + Clone , )+ $($($constr)+)?
{
$([<$key _map>]: std::collections::HashMap<$KEY, usize> , )+
values: $crate::n_key_set::deps::Slab<$V>,
}
#[allow(dead_code)] impl $(<$($G)*>)? $mapname $(<$($P)*>)?
where $( $KEY : std::hash::Hash + Eq + Clone , )+ $($($constr)+)?
{
#[doc = concat!("Construct a new ", stringify!($mapname))]
$vis fn new() -> Self {
Self::with_capacity(0)
}
#[doc = concat!("Construct a new ", stringify!($mapname), " with a given capacity.")]
$vis fn with_capacity(n: usize) -> Self {
Self {
$([<$key _map>]: std::collections::HashMap::with_capacity(n),)*
values: $crate::n_key_set::deps::Slab::with_capacity(n),
}
}
$(
#[doc = concat!("Return a reference to the element whose `", stringify!($key), "` is `key`.")]
$vis fn [<by_ $key>] <BorrowAsKey_>(&self, key: &BorrowAsKey_) -> Option<&$V>
where $KEY : std::borrow::Borrow<BorrowAsKey_>,
BorrowAsKey_: std::hash::Hash + Eq + ?Sized
{
self.[<$key _map>].get(key).map(|idx| self.values.get(*idx).expect("inconsistent state"))
}
#[doc = concat!("Return a mutable reference to the element whose `", stringify!($key),
"` is `key`.")]
$vis unsafe fn [<by_ $key _mut>] <BorrowAsKey_>(
&mut self,
key: &BorrowAsKey_
) -> Option<&mut $V>
where $KEY : std::borrow::Borrow<BorrowAsKey_>,
BorrowAsKey_: std::hash::Hash + Eq + ?Sized
{
self.[<$key _map>]
.get(key)
.map(|idx| self.values.get_mut(*idx).expect("inconsistent state"))
}
#[doc = concat!("Return true if this set contains an element whose `", stringify!($key),
"` is `key`.")]
$vis fn [<contains_ $key>] <BorrowAsKey_>(&mut self, $key: &BorrowAsKey_) -> bool
where $KEY : std::borrow::Borrow<BorrowAsKey_>,
BorrowAsKey_: std::hash::Hash + Eq + ?Sized
{
self.[<$key _map>].get($key).is_some()
}
#[doc = concat!("Remove the element whose `", stringify!($key), "` is `key`")]
#[doc=stringify!($key)]
$vis fn [<remove_by_ $key>] <BorrowAsKey_>(&mut self, $key: &BorrowAsKey_) -> Option<$V>
where $KEY : std::borrow::Borrow<BorrowAsKey_>,
BorrowAsKey_: std::hash::Hash + Eq + ?Sized
{
self.[<$key _map>]
.get($key)
.copied()
.map(|old_idx| self.remove_at(old_idx).expect("inconsistent state"))
}
#[doc = concat!("Modify the element with the given value for `", stringify!($key),
" by applying `func` to it.")]
$vis fn [<modify_by_$key>] <BorrowAsKey_, F_>(
&mut self,
$key: &BorrowAsKey_,
func: F_) -> Vec<$V>
where
$KEY : std::borrow::Borrow<BorrowAsKey_>,
BorrowAsKey_: std::hash::Hash + Eq + ?Sized,
F_: FnOnce(&mut $V)
{
if let Some(idx) = self.[<$key _map>].get($key) {
self.modify_at(*idx, func)
} else {
Vec::new()
}
}
)+
$vis fn values(&self) -> impl Iterator<Item=&$V> + '_ {
self.values.iter().map(|(_, v)| v)
}
$vis fn into_values(self) -> impl Iterator<Item=$V> {
self.values.into_iter().map(|(_, v)| v)
}
$vis fn try_insert(&mut self, value: $V) -> Result<Vec<$V>, $crate::n_key_set::Error> {
if self.capacity() > 32 && self.len() < self.capacity() / 4 {
self.compact()
}
let mut replaced = Vec::new();
$(
replaced.extend(
$crate::n_key_set!( @access(value, ($($($flag)+)?) $key : $KEY $({$($source)+})?) )
.and_then(|key| self.[<remove_by_$key>](key))
);
)*
let new_idx = self.values.insert(value);
let value_ref = self.values.get(new_idx).expect("we just inserted this");
let mut some_key_found = false;
$(
$crate::n_key_set!( @access(value_ref, ($($($flag)+)?) $key : $KEY $({$($source)+})?) )
.map(|key| {
self.[<$key _map>].insert(key.to_owned(), new_idx);
some_key_found = true;
});
)*
if ! some_key_found {
self.values.remove(new_idx); return Err($crate::n_key_set::Error::NoKeys);
}
Ok(replaced)
}
$vis fn insert(&mut self, value: $V) -> Vec<$V> {
self.try_insert(value)
.expect("Tried to add a value with no key!")
}
$vis fn len(&self) -> usize {
self.values.len()
}
$vis fn is_empty(&self) -> bool {
self.values.len() == 0
}
$vis fn capacity(&self) -> usize {
self.values.capacity()
}
$vis fn retain<F>(&mut self, mut pred: F)
where F: FnMut(&$V) -> bool,
{
for idx in 0..self.values.capacity() {
if self.values.get(idx).map(&mut pred) == Some(false) {
self.remove_at(idx);
}
}
}
fn remove_at(&mut self, idx: usize) -> Option<$V> {
if let Some(removed) = self.values.try_remove(idx) {
$(
let $key = $crate::n_key_set!( @access(removed, ($($($flag)+)?) $key : $KEY $({$($source)+})?) );
if let Some($key) = $key {
let old_idx = self.[<$key _map>].remove($key);
assert_eq!(old_idx, Some(idx));
}
)*
Some(removed)
} else {
None
}
}
fn modify_at<F_>(&mut self, idx: usize, func: F_) -> Vec<$V>
where
F_: FnOnce(&mut $V)
{
let value = self.values.get_mut(idx).expect("invalid index");
$(
let [<orig_$key>] = $crate::n_key_set!( @access(value, ($($($flag)+)?) $key : $KEY $({$($source)+})?) )
.map(|elt| elt.to_owned()) ;
)+
func(value);
$(
let [<new_$key>] = $crate::n_key_set!( @access( value, ($($($flag)+)?) $key : $KEY $({$($source)+})?) ) ;
)+
let keys_changed = $(
[<orig_$key>].as_ref().map(std::borrow::Borrow::borrow) != [<new_$key>]
)||+ ;
if keys_changed {
let found_any_keys = $( [<new_$key>].is_some() )||+ ;
$(
if let Some(orig) = [<orig_ $key>] {
let removed = self.[<$key _map>].remove(&orig);
assert_eq!(removed, Some(idx));
}
)+
let removed = self.values.remove(idx);
if found_any_keys {
self.insert(removed)
} else {
vec![removed]
}
} else {
vec![]
}
}
fn compact(&mut self) {
let old_value = std::mem::replace(self, Self::with_capacity(self.len()));
for item in old_value.into_values() {
self.insert(item);
}
}
$vis fn check_invariants(&self) {
#![allow(noop_method_call)] use std::borrow::Borrow;
$(
for (k,idx) in self.[<$key _map>].iter() {
let val = self.values.get(*idx).expect("Dangling entry in hashmap.");
assert!(
Some((k).borrow()) ==
$crate::n_key_set!( @access(val, ($($($flag)+)?) $key : $KEY $({$($source)+})?) ),
"Inconsistent key between hashmap and value."
)
}
)+
for (idx, val) in self.values.iter() {
let mut found_any_key = false;
$(
if let Some(k) = $crate::n_key_set!( @access(val, ($($($flag)+)?) $key : $KEY $({$($source)+})?) ) {
found_any_key = true;
assert!(
self.[<$key _map>].get(k) == Some(&idx),
"Value not found at correct index"
)
}
stringify!($key);
)+
assert!(found_any_key, "Found a value with no keys.");
}
}
}
impl $(<$($G)*>)? Default for $mapname $(<$($P)*>)?
where $( $KEY : std::hash::Hash + Eq + Clone , )* $($($constr)+)?
{
fn default() -> Self {
$mapname::new()
}
}
impl $(<$($G)*>)? FromIterator<$V> for $mapname $(<$($P)*>)?
where $( $KEY : std::hash::Hash + Eq + Clone , )* $($($constr)+)?
{
fn from_iter<IntoIter_>(iter: IntoIter_) -> Self
where
IntoIter_: IntoIterator<Item = $V>
{
let iter = iter.into_iter();
let mut set = Self::with_capacity(iter.size_hint().0);
for value in iter {
set.insert(value);
}
set
}
}
}
};
{ @access($ex:expr, (Option) $key:ident : $t:ty ) } => {
$ex.key()
};
{ @access($ex:expr, () $key:ident : $t:ty) } => {
Some($ex.key())
};
{ @access($ex:expr, (Option) $key:ident : $t:ty { . $field:tt } ) } => {
$ex.$field.as_ref()
};
{ @access($ex:expr, () $key:ident : $t:ty { . $field:tt } ) } => {
Some(&$ex.$field)
};
{ @access($ex:expr, (Option) $key:ident : $t:ty { $func:ident () } ) } => {
$ex.$func()
};
{ @access($ex:expr, () $key:ident : $t:ty { $func:ident () } ) } => {
Some($ex.$func())
};
}
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("Tried to insert a value with no keys")]
NoKeys,
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
n_key_set! {
#[derive(Clone, Debug)]
struct Tuple2Set<A,B> for (A,B) {
first: A { .0 },
second: B { .1 },
}
}
#[test]
fn basic() {
let mut set = Tuple2Set::new();
assert!(set.is_empty());
set.insert((0_u32, 99_u16));
assert_eq!(set.contains_first(&0), true);
assert_eq!(set.contains_second(&99), true);
assert_eq!(set.contains_first(&99), false);
assert_eq!(set.contains_second(&0), false);
assert_eq!(set.by_first(&0), Some(&(0, 99)));
assert_eq!(set.by_second(&99), Some(&(0, 99)));
assert_eq!(set.by_first(&99), None);
assert_eq!(set.by_second(&0), None);
assert_eq!(set.insert((12, 34)), vec![]);
assert_eq!(set.len(), 2);
assert!(set.capacity() >= 2);
assert_eq!(set.by_first(&0), Some(&(0, 99)));
assert_eq!(set.by_first(&12), Some(&(12, 34)));
assert_eq!(set.remove_by_second(&99), Some((0, 99)));
assert_eq!(set.len(), 1);
set.insert((34, 56));
set.insert((56, 78));
set.insert((78, 90));
assert_eq!(set.len(), 4);
assert_eq!(set.insert((12, 123)), vec![(12, 34)]);
let mut replaced = set.insert((12, 56));
replaced.sort();
assert_eq!(replaced, vec![(12, 123), (34, 56)]);
assert_eq!(set.len(), 3);
assert_eq!(set.is_empty(), false);
set.check_invariants();
let mut all_members: Vec<_> = set.values().collect();
all_members.sort();
assert_eq!(all_members, vec![&(12, 56), &(56, 78), &(78, 90)]);
let mut drained_members: Vec<_> = set.into_values().collect();
drained_members.sort();
assert_eq!(drained_members, vec![(12, 56), (56, 78), (78, 90)]);
}
#[test]
fn retain_and_compact() {
let mut set: Tuple2Set<String, String> = (1..=1000)
.map(|idx| (format!("A={}", idx), format!("B={}", idx)))
.collect();
assert_eq!(set.len(), 1000);
let cap_orig = set.capacity();
assert!(cap_orig >= set.len());
set.retain(|(a, _)| a.len() <= 3);
assert_eq!(set.len(), 9);
assert_eq!(set.capacity(), cap_orig);
set.check_invariants();
assert!(set
.insert(("A=0".to_string(), "B=0".to_string()))
.is_empty());
assert!(set.capacity() < cap_orig);
assert_eq!(set.len(), 10);
for idx in 0..=9 {
assert!(set.contains_first(&format!("A={}", idx)));
}
set.check_invariants();
}
#[test]
fn modify_value() {
let mut set: Tuple2Set<i32, i32> = (1..=100).map(|idx| (idx, idx * idx)).collect();
set.check_invariants();
let v = set.modify_by_first(&30, |elt| elt.1 = 256);
set.check_invariants();
assert_eq!(v.len(), 1);
assert_eq!(v[0], (16, 256));
assert_eq!(set.by_second(&256).unwrap(), &(30, 256));
assert_eq!(set.by_first(&30).unwrap(), &(30, 256));
let v = set.modify_by_first(&30, |elt| *elt = (-100, -100));
set.check_invariants();
assert_eq!(v.len(), 0);
assert_eq!(set.by_first(&30), None);
assert_eq!(set.by_second(&256), None);
assert_eq!(set.by_first(&-100).unwrap(), &(-100, -100));
assert_eq!(set.by_second(&-100).unwrap(), &(-100, -100));
set.check_invariants();
}
#[allow(dead_code)]
struct Weekday {
dow: u8,
name: &'static str,
lucky_number: Option<u16>,
}
#[allow(dead_code)]
impl Weekday {
fn dow(&self) -> &u8 {
&self.dow
}
fn name(&self) -> &str {
self.name
}
fn lucky_number(&self) -> Option<&u16> {
self.lucky_number.as_ref()
}
}
n_key_set! {
struct WeekdaySet for Weekday {
idx: u8 { dow() },
(Option) lucky: u16 { lucky_number() },
name: String { name() }
}
}
n_key_set! {
struct['a] ArrayMap['a] for (String, [&'a u32;10]) {
name: String { .0 }
}
}
n_key_set! {
struct['a, const N:usize] ArrayMap2['a, N] for (String, [&'a u32;N]) {
name: String { .0 }
}
}
}