paged-collections 0.2.0

Paged sparse sets, maps and stable-address containers
Documentation
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
//! [`PagedPriorityQueue`]: an indexed max-heap with a paged key index.
//!
//! The greatest priority is returned first. Use [`core::cmp::Reverse`] for a
//! min-queue. Equal priorities have unspecified order. Keys and priorities
//! occupy parallel contiguous arrays in heap order; a four-way heap keeps
//! insertion, priority changes and removal to O(log n) comparisons.
//!
//! Lookups use [`PagedSparseSet`]'s paged index. With [`Flat`] they are O(1);
//! [`Radix`](crate::page::Radix) and [`Tree`](crate::page::Tree) add their
//! directory lookup costs to each index access. Only occupied key ranges
//! allocate index pages.
//!
//! A slot holds one key. [`push`](PagedPriorityQueue::push) evicts another
//! generation of that key; [`try_push`](PagedPriorityQueue::try_push) refuses
//! it. Lookups always check the whole key, including its generation.
//!
//! # Panic safety and ordering
//!
//! If a priority comparison or a closure passed to
//! [`change_priority_with`](PagedPriorityQueue::change_priority_with) panics,
//! all remaining entries stay findable and paired with their priorities.
//! A mutation already made remains made, but heap order may be broken, so
//! subsequent maximum and sorted-order results are unspecified. Changing a
//! stored priority's ordering through interior mutability is a logic error
//! with the same consequence. These errors cannot cause undefined behaviour.
//!
//! ```
//! use paged_collections::priority_queue::PagedPriorityQueue;
//!
//! let mut queue: PagedPriorityQueue<u32, i32> = PagedPriorityQueue::new();
//! assert_eq!(queue.push(7, 10), None);
//! assert_eq!(queue.push(3_000_000, 20), None);
//! assert_eq!(queue.change_priority(&7, 30), Some(10));
//! assert_eq!(queue.pop(), Some((7, 30)));
//! assert_eq!(queue.pop(), Some((3_000_000, 20)));
//! ```
//!
//! With the `rayon` feature, the shared parallel iterator is indexed and
//! follows heap order.
//!
//! ```rust
//! # #[cfg(feature = "rayon")] {
//! use paged_collections::priority_queue::PagedPriorityQueue;
//! use rayon::prelude::*;
//!
//! let mut queue: PagedPriorityQueue<u32, i32> = PagedPriorityQueue::new();
//! queue.push(7, 10);
//! queue.push(2, 20);
//! assert_eq!(queue.par_iter().map(|(&k, &p)| (k, p)).collect::<Vec<_>>(), [(2, 20), (7, 10)]);
//! # }
//! ```

use alloc::collections::TryReserveError;
use alloc::vec::Vec;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::iter::{FusedIterator, Zip};
use core::marker::PhantomData;
use core::slice;

use crate::hash::hash_unordered;
use crate::key::Key;
use crate::page::{DEFAULT_PAGE, Directory, Flat};
use crate::paged_vec::capacity_overflow;
use crate::sparse_set::{PagedSparseSet, Slot};

const ARITY: usize = 4;

/// An indexed max-priority queue with a paged index and dense heap storage.
///
/// `PAGE` is the non-zero number of index slots per page. `D` selects the
/// directory: [`Flat`] (default), [`Radix`](crate::page::Radix), or
/// [`Tree`](crate::page::Tree). See [`crate::page`]. At most `u32::MAX`
/// entries can be stored. The greatest priority is at position zero; ties
/// have unspecified order. See the [module docs](self) for panic safety.
pub struct PagedPriorityQueue<K, P, const PAGE: usize = DEFAULT_PAGE, D: Directory = Flat> {
    // Invariants: equal lengths; priorities[i] belongs to keys.as_slice()[i].
    // For i > 0, priorities[(i - 1) / ARITY] >= priorities[i], unless user
    // code panicked or changed ordering through interior mutability.
    keys: PagedSparseSet<K, PAGE, D>,
    priorities: Vec<P>,
}

impl<K: Key, P, const PAGE: usize, D: Directory> PagedPriorityQueue<K, P, PAGE, D> {
    /// Creates an empty queue without allocating.
    pub fn new() -> Self {
        Self {
            keys: PagedSparseSet::new(),
            priorities: Vec::new(),
        }
    }

    /// Creates an empty queue with room for at least `capacity` entries in
    /// its dense arrays. Index pages are allocated on demand.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            keys: PagedSparseSet::with_capacity(capacity),
            priorities: Vec::with_capacity(capacity),
        }
    }

    /// Number of entries in the queue.
    pub fn len(&self) -> usize {
        self.priorities.len()
    }

    /// Whether the queue has no entries.
    pub fn is_empty(&self) -> bool {
        self.priorities.is_empty()
    }

    /// Number of entries the dense arrays can hold without reallocating.
    pub fn capacity(&self) -> usize {
        self.keys.capacity().min(self.priorities.capacity())
    }

    /// Reserves room for at least `additional` more entries in both dense arrays.
    pub fn reserve(&mut self, additional: usize) {
        self.keys.reserve(additional);
        self.priorities.reserve(additional);
    }

    /// Reserves room for at least `additional` more entries in both dense arrays.
    ///
    /// Returns an error if the capacity overflows or allocation fails. On
    /// error the entries, capacity and array addresses are preserved.
    ///
    /// # Examples
    ///
    /// ```
    /// use paged_collections::priority_queue::PagedPriorityQueue;
    ///
    /// let mut queue: PagedPriorityQueue<usize, i32> = PagedPriorityQueue::new();
    /// queue.try_reserve(16).unwrap();
    /// assert!(queue.capacity() >= 16);
    /// ```
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        let needed = self
            .len()
            .checked_add(additional)
            .ok_or_else(capacity_overflow)?;
        if self.keys.capacity() >= needed {
            return self.priorities.try_reserve(additional);
        }
        if self.priorities.capacity() >= needed {
            return self.keys.try_reserve(additional);
        }

        // Stage the priority allocation so a key allocation failure leaves
        // both original arrays in place. Moving priorities into the staged
        // buffer cannot allocate or run user code.
        let mut priorities = Vec::new();
        priorities.try_reserve(needed)?;
        self.keys.try_reserve(additional)?;
        for priority in self.priorities.drain(..) {
            priorities.push(priority);
        }
        self.priorities = priorities;
        Ok(())
    }

    /// Shrinks the dense arrays and index directory. Frees every index page
    /// if the queue is empty.
    pub fn shrink_to_fit(&mut self) {
        self.keys.shrink_to_fit();
        self.priorities.shrink_to_fit();
    }

    /// Removes every entry, dropping its priority. Index pages and dense
    /// capacity stay allocated for reuse.
    pub fn clear(&mut self) {
        // Vec::clear sets its length to zero before running P::drop. Clear
        // the keys first, so even a panicking destructor leaves equal lengths.
        self.keys.clear();
        self.priorities.clear();
    }

    /// Number of allocated pages in the key index.
    pub fn allocated_pages(&self) -> usize {
        self.keys.allocated_pages()
    }

    /// The stored key and greatest priority, or `None` if empty.
    pub fn peek(&self) -> Option<(&K, &P)> {
        self.keys.as_slice().first().zip(self.priorities.first())
    }

    /// Whether the exact key is present, including its generation.
    pub fn contains_key(&self, key: &K) -> bool {
        self.keys.contains(*key)
    }

    /// The priority of the exact key, or `None` if absent.
    pub fn priority(&self, key: &K) -> Option<&P> {
        self.position(key).map(|pos| &self.priorities[pos])
    }

    /// Heap position of the exact key, or `None` if absent.
    ///
    /// Positions index both slices and can change on any heap mutation.
    pub fn position(&self, key: &K) -> Option<usize> {
        self.keys.position(*key)
    }

    /// Iterates over entries in heap order, which is otherwise unspecified.
    pub fn iter(&self) -> Iter<'_, K, P> {
        self.keys.iter().zip(self.priorities.iter())
    }

    /// Iterates over keys in heap order.
    pub fn keys(&self) -> slice::Iter<'_, K> {
        self.keys.iter()
    }

    /// Iterates over priorities in heap order.
    pub fn priorities(&self) -> slice::Iter<'_, P> {
        self.priorities.iter()
    }

    /// Keys in heap order; position zero has the greatest priority.
    pub fn key_slice(&self) -> &[K] {
        self.keys.as_slice()
    }

    /// Priorities in heap order, parallel to [`key_slice`](Self::key_slice).
    pub fn priority_slice(&self) -> &[P] {
        &self.priorities
    }

    /// Removes all entries eagerly and iterates over them in arbitrary order.
    ///
    /// Index pages and dense capacity are kept. Dropping the iterator drops
    /// its remaining entries; forgetting it still leaves the queue empty.
    /// The queue stays mutably borrowed until the iterator is released.
    pub fn drain(&mut self) -> Drain<'_, K, P, PAGE, D> {
        // Allocate before either length changes. All following pushes fit;
        // copying keys and moving priorities runs no user code.
        let mut entries = Vec::with_capacity(self.len());
        for (key, priority) in self.keys.iter().copied().zip(self.priorities.drain(..)) {
            entries.push((key, priority));
        }
        self.keys.clear();
        Drain {
            inner: entries.into_iter(),
            _borrow: PhantomData,
        }
    }

    /// Swaps complete entries without comparisons or allocation.
    fn swap_positions(&mut self, a: usize, b: usize) {
        self.keys.swap_positions(a, b);
        self.priorities.swap(a, b);
    }
}

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> PagedPriorityQueue<K, P, PAGE, D> {
    /// Inserts a key or updates its priority, returning the previous priority
    /// of that same key. An equal stored key is kept.
    ///
    /// If another generation holds the slot, it is evicted and `None` is
    /// returned. The replacement starts at the old entry's position and is
    /// then sifted to restore heap order.
    ///
    /// # Panics
    ///
    /// Panics if a new entry would exceed `u32::MAX` entries, if storage
    /// cannot be allocated, or if comparing priorities panics. Allocation
    /// failure leaves entries unchanged; a comparison panic has the
    /// guarantees described in the [module docs](self).
    pub fn push(&mut self, key: K, priority: P) -> Option<P> {
        if let Some(pos) = self.keys.occupant_pos(key.index()) {
            let stored = self.keys.as_slice()[pos];
            // Equality is user code too: finish it before changing either array.
            let same = stored == key;
            if !same {
                self.keys.replace(key);
            }
            let old = core::mem::replace(&mut self.priorities[pos], priority);
            self.repair(pos);
            if same { Some(old) } else { None }
        } else {
            // Reserve priorities first: once insert appends the key, push
            // cannot allocate or call user code, so lengths stay in step.
            self.priorities.reserve(1);
            self.keys.insert(key);
            self.priorities.push(priority);
            self.sift_up(self.len() - 1);
            None
        }
    }

    /// Inserts or updates as [`push`](Self::push), but returns the entry
    /// previously in `key`'s slot, whether it was `key` itself or another
    /// generation that is now evicted (as
    /// [`PagedSparseMap::replace`](crate::sparse_map::PagedSparseMap::replace)).
    ///
    /// Unlike `push`, this stores `key` even when the old key is equal to it.
    /// Panics under the same conditions as `push`.
    ///
    /// ```
    /// use paged_collections::id_alloc::IdAllocator;
    /// use paged_collections::priority_queue::PagedPriorityQueue;
    ///
    /// let mut ids = IdAllocator::new();
    /// let old = ids.alloc();
    /// ids.free(old);
    /// let new = ids.alloc(); // same slot, next generation
    /// let mut queue: PagedPriorityQueue<_, i32> = PagedPriorityQueue::new();
    /// queue.push(old, 5);
    /// assert_eq!(queue.replace(new, 7), Some((old, 5)));
    /// assert_eq!(queue.replace(new, 9), Some((new, 7)));
    /// assert_eq!(queue.pop(), Some((new, 9)));
    /// ```
    pub fn replace(&mut self, key: K, priority: P) -> Option<(K, P)> {
        if let Some(pos) = self.keys.occupant_pos(key.index()) {
            let old_key = self.keys.overwrite_at(pos, key);
            let old = core::mem::replace(&mut self.priorities[pos], priority);
            self.repair(pos);
            Some((old_key, old))
        } else {
            self.priorities.reserve(1);
            self.keys.insert(key);
            self.priorities.push(priority);
            self.sift_up(self.len() - 1);
            None
        }
    }

    /// Inserts or updates as [`push`](Self::push), but returns the supplied
    /// pair unchanged if another generation holds the slot.
    ///
    /// Updating the same key is allowed and returns its previous priority.
    /// Panics under the same conditions as `push`.
    pub fn try_push(&mut self, key: K, priority: P) -> Result<Option<P>, (K, P)> {
        if self
            .keys
            .occupant(key.index())
            .is_some_and(|stored| *stored != key)
        {
            return Err((key, priority));
        }
        Ok(self.push(key, priority))
    }

    /// Inserts an absent key, or updates it only if `priority` is strictly
    /// greater. Returns whether the queue changed.
    ///
    /// An absent generation evicts another generation as [`push`](Self::push)
    /// does, regardless of the evicted priority. Panics as `push` does.
    pub fn push_increase(&mut self, key: K, priority: P) -> bool {
        if self.priority(&key).is_some_and(|old| priority <= *old) {
            return false;
        }
        self.push(key, priority);
        true
    }

    /// Inserts an absent key, or updates it only if `priority` is strictly
    /// smaller. Returns whether the queue changed.
    ///
    /// An absent generation evicts another generation as [`push`](Self::push)
    /// does, regardless of the evicted priority. Panics as `push` does.
    pub fn push_decrease(&mut self, key: K, priority: P) -> bool {
        if self.priority(&key).is_some_and(|old| priority >= *old) {
            return false;
        }
        self.push(key, priority);
        true
    }

    /// Changes the priority of a present key, returning its old priority.
    /// Returns `None` without changing the queue if the exact key is absent.
    ///
    /// A comparison panic has the guarantees in the [module docs](self).
    pub fn change_priority(&mut self, key: &K, priority: P) -> Option<P> {
        let pos = self.position(key)?;
        let old = core::mem::replace(&mut self.priorities[pos], priority);
        self.repair(pos);
        Some(old)
    }

    /// Calls `change` on a present key's priority and restores heap order.
    /// Returns whether the exact key was present; an absent key does not
    /// call the closure.
    ///
    /// If the closure or a comparison panics, its changes remain and heap
    /// order may be broken, but entries stay findable and correctly paired.
    pub fn change_priority_with(&mut self, key: &K, change: impl FnOnce(&mut P)) -> bool {
        let Some(pos) = self.position(key) else {
            return false;
        };
        change(&mut self.priorities[pos]);
        self.repair(pos);
        true
    }

    /// Removes and returns the entry with the greatest priority.
    ///
    /// Returns `None` if empty. If comparing priorities panics, the removed
    /// entry is dropped and the remaining entries stay findable and paired,
    /// though heap order may be broken.
    pub fn pop(&mut self) -> Option<(K, P)> {
        if self.is_empty() {
            None
        } else {
            Some(self.remove_at(0))
        }
    }

    /// Removes and returns the stored key and priority for the exact key.
    ///
    /// Returns `None` if absent. A comparison panic has the same guarantees
    /// as [`pop`](Self::pop).
    pub fn remove(&mut self, key: &K) -> Option<(K, P)> {
        let pos = self.position(key)?;
        Some(self.remove_at(pos))
    }

    fn remove_at(&mut self, pos: usize) -> (K, P) {
        // Remove by position, not by key equality (user code): the key and
        // priority arrays stay in step even if `K: Eq` misbehaves. Neither
        // swap_remove can allocate or run P code.
        let key = self.keys.remove_at(pos);
        let priority = self.priorities.swap_remove(pos);
        if pos < self.len() {
            self.repair(pos);
        }
        (key, priority)
    }

    /// Consumes the queue into entries in ascending priority order.
    /// Equal priorities have unspecified order.
    pub fn into_sorted_vec(self) -> Vec<(K, P)> {
        let mut entries: Vec<_> = self.into_sorted_iter().collect();
        entries.reverse();
        entries
    }

    /// Consumes the queue and repeatedly pops entries in descending priority
    /// order. Equal priorities have unspecified order.
    pub fn into_sorted_iter(self) -> IntoSortedIter<K, P, PAGE, D> {
        IntoSortedIter { queue: self }
    }

    fn repair(&mut self, pos: usize) {
        if pos > 0 && self.priorities[pos] > self.priorities[(pos - 1) / ARITY] {
            self.sift_up(pos);
        } else {
            self.sift_down(pos);
        }
    }

    fn sift_up(&mut self, mut pos: usize) {
        while pos > 0 {
            let parent = (pos - 1) / ARITY;
            if self.priorities[parent] >= self.priorities[pos] {
                break;
            }
            // Every completed swap restores pairing and the key index
            // before the next comparison can panic.
            self.swap_positions(parent, pos);
            pos = parent;
        }
    }

    fn sift_down(&mut self, mut pos: usize) {
        // Checked arithmetic also handles a leaf near usize::MAX on 32-bit
        // targets, where multiplying its position by ARITY could overflow.
        while let Some(first) = pos.checked_mul(ARITY).and_then(|n| n.checked_add(1)) {
            if first >= self.len() {
                break;
            }
            let end = first.saturating_add(ARITY).min(self.len());
            let mut greatest = first;
            for child in first + 1..end {
                if self.priorities[child] > self.priorities[greatest] {
                    greatest = child;
                }
            }
            if self.priorities[pos] >= self.priorities[greatest] {
                break;
            }
            self.swap_positions(pos, greatest);
            pos = greatest;
        }
    }

    #[cfg(test)]
    fn check(&self) {
        assert_eq!(self.keys.len(), self.priorities.len());
        for (pos, key) in self.keys.iter().enumerate() {
            assert_eq!(self.position(key), Some(pos));
            if pos > 0 {
                assert!(self.priorities[(pos - 1) / ARITY] >= self.priorities[pos]);
            }
        }
    }
}

impl<K: Key, P, const PAGE: usize, D: Directory> Default for PagedPriorityQueue<K, P, PAGE, D> {
    fn default() -> Self {
        Self::new()
    }
}

impl<K: Key, P: Clone, const PAGE: usize, D: Directory> Clone for PagedPriorityQueue<K, P, PAGE, D>
where
    D::Store<Slot, PAGE>: Clone,
{
    fn clone(&self) -> Self {
        Self {
            keys: self.keys.clone(),
            priorities: self.priorities.clone(),
        }
    }
}

impl<K: Key + fmt::Debug, P: fmt::Debug, const PAGE: usize, D: Directory> fmt::Debug
    for PagedPriorityQueue<K, P, PAGE, D>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

/// Compares priorities by exact key using [`priority`](PagedPriorityQueue::priority),
/// across page sizes and directories. Equality ignores heap layout.
impl<K: Key, P: PartialEq<Q>, Q, const PAGE: usize, D: Directory, const P2: usize, D2: Directory>
    PartialEq<PagedPriorityQueue<K, Q, P2, D2>> for PagedPriorityQueue<K, P, PAGE, D>
{
    fn eq(&self, other: &PagedPriorityQueue<K, Q, P2, D2>) -> bool {
        self.len() == other.len()
            && self.iter().all(|(key, priority)| {
                other
                    .priority(key)
                    .is_some_and(|other_priority| priority == other_priority)
            })
    }
}

/// Queue [`PartialEq`] is an equivalence relation when priorities implement [`Eq`].
impl<K: Key, P: Eq, const PAGE: usize, D: Directory> Eq for PagedPriorityQueue<K, P, PAGE, D> {}

/// Hashes the key-priority pairs in any order, consistent with [`PartialEq`].
impl<K: Key + Hash, P: Hash, const PAGE: usize, D: Directory> Hash
    for PagedPriorityQueue<K, P, PAGE, D>
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        hash_unordered(self.iter(), state);
    }
}

/// Repeatedly pushes entries, so later entries in a slot win.
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> Extend<(K, P)>
    for PagedPriorityQueue<K, P, PAGE, D>
{
    fn extend<I: IntoIterator<Item = (K, P)>>(&mut self, iter: I) {
        for (key, priority) in iter {
            self.push(key, priority);
        }
    }
}

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FromIterator<(K, P)>
    for PagedPriorityQueue<K, P, PAGE, D>
{
    fn from_iter<I: IntoIterator<Item = (K, P)>>(iter: I) -> Self {
        let mut queue = Self::new();
        queue.extend(iter);
        queue
    }
}

impl<'a, K: Key, P, const PAGE: usize, D: Directory> IntoIterator
    for &'a PagedPriorityQueue<K, P, PAGE, D>
{
    type Item = (&'a K, &'a P);
    type IntoIter = Iter<'a, K, P>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<K: Key, P, const PAGE: usize, D: Directory> IntoIterator
    for PagedPriorityQueue<K, P, PAGE, D>
{
    type Item = (K, P);
    type IntoIter = IntoIter<K, P>;

    fn into_iter(self) -> Self::IntoIter {
        self.keys.into_iter().zip(self.priorities)
    }
}

/// Borrowed iterator over entries in heap order, from
/// [`iter`](PagedPriorityQueue::iter).
pub type Iter<'a, K, P> = Zip<slice::Iter<'a, K>, slice::Iter<'a, P>>;

/// Owning iterator over entries in arbitrary heap order.
pub type IntoIter<K, P> = Zip<alloc::vec::IntoIter<K>, alloc::vec::IntoIter<P>>;

/// Owning iterator over entries in descending priority order, from
/// [`into_sorted_iter`](PagedPriorityQueue::into_sorted_iter).
pub struct IntoSortedIter<K, P, const PAGE: usize = DEFAULT_PAGE, D: Directory = Flat> {
    queue: PagedPriorityQueue<K, P, PAGE, D>,
}

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> Iterator for IntoSortedIter<K, P, PAGE, D> {
    type Item = (K, P);

    fn next(&mut self) -> Option<Self::Item> {
        self.queue.pop()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.queue.len();
        (len, Some(len))
    }
}

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> ExactSizeIterator
    for IntoSortedIter<K, P, PAGE, D>
{
}
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FusedIterator
    for IntoSortedIter<K, P, PAGE, D>
{
}

/// Iterator over eagerly removed entries, from [`drain`](PagedPriorityQueue::drain).
/// Dropping it drops any remaining entries; the queue is already empty.
pub struct Drain<'a, K, P, const PAGE: usize = DEFAULT_PAGE, D: Directory = Flat> {
    inner: alloc::vec::IntoIter<(K, P)>,
    _borrow: PhantomData<&'a mut PagedPriorityQueue<K, P, PAGE, D>>,
}

impl<K, P, const PAGE: usize, D: Directory> Iterator for Drain<'_, K, P, PAGE, D> {
    type Item = (K, P);

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<K, P, const PAGE: usize, D: Directory> DoubleEndedIterator for Drain<'_, K, P, PAGE, D> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

impl<K, P, const PAGE: usize, D: Directory> ExactSizeIterator for Drain<'_, K, P, PAGE, D> {}
impl<K, P, const PAGE: usize, D: Directory> FusedIterator for Drain<'_, K, P, PAGE, D> {}

#[cfg(feature = "rayon")]
mod rayon_impl {
    use super::*;
    use rayon::prelude::*;

    impl<'a, K: Key + Sync, P: Sync, const PAGE: usize, D: Directory> IntoParallelIterator
        for &'a PagedPriorityQueue<K, P, PAGE, D>
    {
        type Item = (&'a K, &'a P);
        type Iter = rayon::iter::Zip<rayon::slice::Iter<'a, K>, rayon::slice::Iter<'a, P>>;

        fn into_par_iter(self) -> Self::Iter {
            self.key_slice()
                .par_iter()
                .zip(self.priority_slice().par_iter())
        }
    }
}

#[cfg(feature = "serde")]
mod serde_impl {
    use super::*;
    use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
    use serde::ser::{Serialize, Serializer};

    /// Serializes as a sequence of `(key, priority)` pairs in heap order.
    impl<K: Key + Serialize, P: Serialize, const PAGE: usize, D: Directory> Serialize
        for PagedPriorityQueue<K, P, PAGE, D>
    {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.collect_seq(self.iter())
        }
    }

    /// Deserializes pairs by repeated `push`, so later entries in a slot win.
    /// Memory follows key indices: use Radix or Tree for widely spread keys.
    impl<'de, K, P, const PAGE: usize, D> Deserialize<'de> for PagedPriorityQueue<K, P, PAGE, D>
    where
        K: Key + Deserialize<'de>,
        P: Ord + Deserialize<'de>,
        D: Directory,
    {
        fn deserialize<De: Deserializer<'de>>(deserializer: De) -> Result<Self, De::Error> {
            struct SeqVisitor<K, P, const PAGE: usize, D>(PhantomData<(K, P, D)>);

            impl<'de, K, P, const PAGE: usize, D> Visitor<'de> for SeqVisitor<K, P, PAGE, D>
            where
                K: Key + Deserialize<'de>,
                P: Ord + Deserialize<'de>,
                D: Directory,
            {
                type Value = PagedPriorityQueue<K, P, PAGE, D>;

                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str("a sequence of key-priority pairs")
                }

                fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
                    // Cap both entry count and bytes from an untrusted hint.
                    let entry = core::mem::size_of::<K>().saturating_add(core::mem::size_of::<P>());
                    let bytes = usize::try_from(1_u32 << 20).unwrap_or(usize::MAX);
                    let cap = bytes.checked_div(entry).unwrap_or(usize::MAX).min(4096);
                    let mut queue =
                        PagedPriorityQueue::with_capacity(seq.size_hint().unwrap_or(0).min(cap));
                    while let Some((key, priority)) = seq.next_element()? {
                        queue.push(key, priority);
                    }
                    Ok(queue)
                }
            }

            deserializer.deserialize_seq(SeqVisitor::<K, P, PAGE, D>(PhantomData))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::cmp::Reverse;

    #[test]
    fn updates_remove_and_pop_restore_heap_order() {
        let mut queue: PagedPriorityQueue<u16, i32, 4> = PagedPriorityQueue::new();
        queue.check();
        for key in 0..80 {
            assert_eq!(queue.push(key, i32::from(key)), None);
            queue.check();
        }
        assert_eq!(queue.push(3, 100), Some(3));
        queue.check();
        assert_eq!(queue.peek(), Some((&3, &100)));
        assert_eq!(queue.try_push(3, -100), Ok(Some(100)));
        queue.check();
        assert_eq!(queue.change_priority(&1, 120), Some(1));
        queue.check();
        assert!(queue.change_priority_with(&1, |p| *p = -120));
        queue.check();
        assert_eq!(queue.change_priority(&100, 0), None);
        queue.check();
        assert!(!queue.change_priority_with(&100, |_| panic!("absent")));
        queue.check();
        // Remove from the middle, the end, and the root.
        let middle = queue.key_slice()[queue.len() / 2];
        assert_eq!(queue.remove(&middle).map(|e| e.0), Some(middle));
        queue.check();
        let last = *queue.key_slice().last().unwrap();
        assert_eq!(queue.remove(&last).map(|e| e.0), Some(last));
        queue.check();
        assert_eq!(queue.remove(&100), None);
        queue.check();
        let mut previous = i32::MAX;
        while let Some((_, priority)) = queue.pop() {
            assert!(priority <= previous);
            previous = priority;
            queue.check();
        }
        assert_eq!(queue.pop(), None);
        queue.check();
    }

    #[test]
    fn strict_changes_ties_and_min_queue() {
        let mut queue: PagedPriorityQueue<u16, i32, 1> = PagedPriorityQueue::new();
        assert!(queue.push_increase(1, 10));
        queue.check();
        for priority in [9, 10] {
            assert!(!queue.push_increase(1, priority));
            queue.check();
        }
        assert!(queue.push_increase(1, 11));
        queue.check();
        for priority in [11, 12] {
            assert!(!queue.push_decrease(1, priority));
            queue.check();
        }
        assert!(queue.push_decrease(1, 10));
        queue.check();
        assert!(queue.push_decrease(2, 10));
        queue.check();
        assert_eq!(queue.clone().into_sorted_vec().len(), 2);
        let mut min: PagedPriorityQueue<u16, Reverse<i32>> = PagedPriorityQueue::new();
        for (key, value) in [(1, 10), (2, -2), (3, 10)] {
            min.push(key, Reverse(value));
            min.check();
        }
        assert_eq!(min.pop(), Some((2, Reverse(-2))));
        min.check();
    }

    #[test]
    fn iteration_sorting_and_eager_drain() {
        let mut queue: PagedPriorityQueue<u16, i32, 4> = PagedPriorityQueue::default();
        for entry in [(1, 5), (9, 2), (2, 5), (1, 7)] {
            queue.extend([entry]);
            queue.check();
        }
        assert_eq!(queue.len(), 3);
        assert_eq!(queue.iter().len(), 3);
        assert_eq!(queue.keys().copied().collect::<Vec<_>>(), queue.key_slice());
        assert_eq!(
            queue.priorities().copied().collect::<Vec<_>>(),
            queue.priority_slice()
        );
        assert_eq!(
            queue.iter().next_back(),
            queue.key_slice().last().zip(queue.priority_slice().last())
        );
        let entries: Vec<_> = queue.iter().map(|(&k, &p)| (k, p)).collect();
        assert_eq!(queue.clone().into_iter().collect::<Vec<_>>(), entries);
        assert_eq!((&queue).into_iter().count(), 3);
        assert_eq!(
            queue
                .clone()
                .into_sorted_vec()
                .iter()
                .map(|e| e.1)
                .collect::<Vec<_>>(),
            [2, 5, 7]
        );
        let mut sorted = queue.clone().into_sorted_iter();
        assert_eq!(sorted.len(), 3);
        assert_eq!(sorted.next(), Some((1, 7)));
        assert_eq!(sorted.len(), 2);
        assert_eq!(sorted.by_ref().map(|e| e.1).collect::<Vec<_>>(), [5, 2]);
        assert_eq!(sorted.next(), None);
        let capacity = queue.capacity();
        let pages = queue.allocated_pages();
        let mut drain = queue.drain();
        assert_eq!(drain.len(), 3);
        assert_eq!(drain.next(), entries.first().copied());
        assert_eq!(drain.next_back(), entries.last().copied());
        drop(drain);
        queue.check();
        assert!(queue.is_empty());
        assert_eq!(queue.capacity(), capacity);
        assert_eq!(queue.allocated_pages(), pages);
        queue.push(1, 1);
        queue.check();
        // Miri reports the deliberate leak; there, drop instead (removal
        // is eager either way, which this test checks natively).
        let drain = queue.drain();
        if cfg!(miri) {
            drop(drain);
        } else {
            core::mem::forget(drain);
        }
        queue.check();
        assert!(queue.is_empty());
        assert_eq!(queue.pop(), None);
        queue.check();
    }

    #[test]
    fn capacity_pages_and_non_ordered_priorities() {
        let mut queue: PagedPriorityQueue<u16, i32, 4> = PagedPriorityQueue::with_capacity(5);
        queue.check();
        assert!(queue.capacity() >= 5);
        assert_eq!(queue.allocated_pages(), 0);
        for key in [1, 2, 40] {
            queue.push(key, 0);
            queue.check();
        }
        queue.reserve(20);
        queue.check();
        assert!(queue.capacity() >= 23);
        assert_eq!(queue.allocated_pages(), 2);
        let capacity = queue.capacity();
        queue.clear();
        queue.check();
        assert_eq!(queue.capacity(), capacity);
        assert_eq!(queue.allocated_pages(), 2);
        for key in [1, 2, 40] {
            assert!(!queue.contains_key(&key));
        }
        queue.push(40, 1);
        queue.check();
        assert!(!queue.contains_key(&1));
        queue.shrink_to_fit();
        queue.check();
        assert_eq!(queue.allocated_pages(), 2);
        queue.clear();
        queue.check();
        queue.shrink_to_fit();
        queue.check();
        assert_eq!(queue.allocated_pages(), 0);

        // Construction, inspection, clearing and arbitrary iteration do not
        // require P: Ord, even though heap mutations do.
        let mut unordered: PagedPriorityQueue<u16, f32> = PagedPriorityQueue::new();
        unordered.reserve(1);
        assert_eq!(unordered.peek(), None);
        assert_eq!(unordered.priority(&0), None);
        assert_eq!(unordered.clone().into_iter().count(), 0);
        assert_eq!(unordered.drain().count(), 0);
        unordered.clear();
        unordered.shrink_to_fit();
        assert!(unordered.key_slice().is_empty());
        assert!(unordered.priority_slice().is_empty());
    }
}