qubit-collections 0.2.2

Custom collection types for specialized access and traversal patterns
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! A primary-key map with an independently managed ordered secondary index.

mod detach_range;
mod detached_entry_mut;
mod entry_mut;
mod entry_ref;
mod extract_range;
mod index_state;
mod internal;
mod owned_entry;
mod try_insert_error;

use std::borrow::Borrow;
use std::collections::hash_map::RandomState;
use std::fmt;
use std::hash::BuildHasher;
use std::hash::Hash;
use std::ops::Bound;
use std::ops::RangeBounds;

pub use self::detach_range::DetachRange;
pub use self::detached_entry_mut::DetachedEntryMut;
pub use self::entry_mut::EntryMut;
pub use self::entry_ref::EntryRef;
pub use self::extract_range::ExtractRange;
pub use self::index_state::IndexState;
use self::internal::InternalState;
use self::internal::Record;
use self::internal::Sequence;
use self::internal::SlotId;
pub use self::owned_entry::OwnedEntry;
pub use self::try_insert_error::TryInsertError;

/// Expanded bounds over an order key and its stable sequence.
type SequenceBounds<O> = (Bound<(O, Sequence)>, Bound<(O, Sequence)>);

/// A primary-key map with an independently managed ordered secondary index.
///
/// The primary key is stored exactly once. The ordered index stores private
/// arena identifiers, so `K` does not need to implement [`Clone`]. Equal order
/// keys retain attachment order. A record may be detached from ordered
/// operations while remaining addressable through its primary key.
///
/// `insert` replaces an occupied primary key and returns the previous record.
/// Use [`Self::try_insert`] when replacement would indicate a caller error.
///
/// This type performs no internal locking. It is [`Send`] and [`Sync`] exactly
/// when its type parameters and hash builder are, which makes it suitable for
/// external synchronization with types such as [`std::sync::Mutex`] or
/// [`std::sync::RwLock`].
///
/// # Interior Mutability
///
/// Keys and orders must remain logically unchanged while stored, including
/// changes made through interior-mutability types. Mutating either component
/// without rebuilding its index is a logic error and may make lookups or
/// ordered operations return incorrect results.
///
/// # Panic and Unwind Safety
///
/// Cross-index mutations are guarded by a poison flag. If a user-provided
/// [`Hash`], [`Eq`], [`Ord`], [`Clone`], or destructor implementation panics
/// after mutation starts, all later operations panic instead of exposing
/// potentially inconsistent indexes. The poisoned map should be discarded.
/// Stable attachment sequences use `u64`; insertion, attachment, or attached
/// reordering panics if that sequence space is exhausted. [`Self::clear`]
/// resets the sequence allocator.
///
/// # Examples
///
/// ```
/// use qubit_collections::map::OrderedIndexMap;
///
/// let mut queue = OrderedIndexMap::new();
/// queue.try_insert("job", 10, "ready")?;
/// let rejected = queue
///     .try_insert("job", 20, "duplicate")
///     .expect_err("the primary key is already occupied");
/// assert_eq!(("job", 20, "duplicate"), rejected.into_parts());
/// assert_eq!(Some(&"ready"), queue.get("job"));
/// # Ok::<(), qubit_collections::map::ordered_index_map::TryInsertError<&str, i32, &str>>(())
/// ```
///
/// # Type Parameters
///
/// * `K` - Unique primary key type.
/// * `O` - Possibly non-unique ordered secondary key type.
/// * `V` - Stored value type.
/// * `S` - Hash builder used by the primary index.
#[must_use = "the map owns its records and ordered index"]
pub struct OrderedIndexMap<K, O, V, S = RandomState> {
    /// Owning arena and private indexes.
    state: InternalState<K, O, V>,
    /// Hash builder for primary keys.
    hash_builder: S,
    /// Whether a cross-index mutation unwound before restoring consistency.
    poisoned: bool,
}

impl<K, O, V> OrderedIndexMap<K, O, V, RandomState> {
    /// Creates an empty map.
    ///
    /// # Returns
    ///
    /// An empty map using a randomly seeded hash builder.
    #[must_use = "the new map must be retained to store records"]
    #[inline]
    pub fn new() -> Self {
        Self::with_hasher(RandomState::new())
    }

    /// Creates an empty map with primary capacity.
    ///
    /// # Parameters
    ///
    /// * `capacity` - Number of primary records to reserve.
    ///
    /// # Returns
    ///
    /// An empty map with space for at least `capacity` records.
    ///
    /// # Panics
    ///
    /// Panics if reserving `capacity` records overflows or allocation fails.
    #[must_use = "the new map must be retained to store records"]
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_and_hasher(capacity, RandomState::new())
    }
}

impl<K, O, V, S> OrderedIndexMap<K, O, V, S> {
    /// Creates an empty map with a caller-provided hash builder.
    ///
    /// # Parameters
    ///
    /// * `hash_builder` - Builder used to hash primary keys.
    ///
    /// # Returns
    ///
    /// An empty map using `hash_builder`.
    #[must_use = "the new map must be retained to store records"]
    #[inline]
    pub const fn with_hasher(hash_builder: S) -> Self {
        Self {
            state: InternalState::new(),
            hash_builder,
            poisoned: false,
        }
    }

    /// Creates an empty map with primary capacity and a hash builder.
    ///
    /// # Parameters
    ///
    /// * `capacity` - Number of primary records to reserve.
    /// * `hash_builder` - Builder used to hash primary keys.
    ///
    /// # Returns
    ///
    /// An empty map configured with both arguments.
    ///
    /// # Panics
    ///
    /// Panics if reserving `capacity` records overflows or allocation fails.
    #[must_use = "the new map must be retained to store records"]
    #[inline]
    pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
        Self {
            state: InternalState::with_capacity(capacity),
            hash_builder,
            poisoned: false,
        }
    }

    /// Returns the number of primary records in either attachment state.
    ///
    /// # Returns
    ///
    /// The total number of attached and detached records.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[must_use]
    #[inline]
    pub fn len(&self) -> usize {
        self.assert_healthy();
        self.state.arena.len()
    }

    /// Reports whether the primary map contains no records.
    ///
    /// # Returns
    ///
    /// `true` when the map has neither attached nor detached records.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[must_use]
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.assert_healthy();
        self.state.arena.len() == 0
    }

    /// Returns the number of records attached to the ordered index.
    ///
    /// # Returns
    ///
    /// The number of records visible through ordered operations.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[must_use]
    #[inline]
    pub fn attached_len(&self) -> usize {
        self.assert_healthy();
        self.state.ordered.len()
    }

    /// Reports whether the ordered index contains no records.
    ///
    /// # Returns
    ///
    /// `true` when no record is attached to the ordered index.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[must_use]
    #[inline]
    pub fn is_attached_empty(&self) -> bool {
        self.assert_healthy();
        self.state.ordered.is_empty()
    }

    /// Returns the number of records accepted without growing primary storage.
    ///
    /// # Returns
    ///
    /// The shared capacity supported by both primary storage structures.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[must_use]
    #[inline]
    pub fn capacity(&self) -> usize {
        self.assert_healthy();
        self.state.arena.capacity().min(self.state.primary.capacity())
    }

    /// Removes every record while retaining allocated primary capacity.
    ///
    /// # Panics
    ///
    /// Panics when the map is poisoned or a stored destructor panics.
    #[inline]
    pub fn clear(&mut self) {
        self.with_mutation(|state, _| {
            state.primary.clear();
            state.ordered.clear();
            state.arena.clear();
            state.next_sequence = 0;
        });
    }

    /// Returns views of every primary record in unspecified order.
    ///
    /// Detached records are included.
    ///
    /// # Returns
    ///
    /// An iterator over all attached and detached records.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = EntryRef<'_, K, O, V>> {
        self.assert_healthy();
        self.state.primary.iter().map(|id| self.entry_ref(*id))
    }

    /// Returns attached records in ascending secondary-key order.
    ///
    /// Equal secondary keys retain attachment order.
    ///
    /// # Returns
    ///
    /// A double-ended, exact-size iterator over attached records.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[inline]
    pub fn iter_ordered(&self) -> impl DoubleEndedIterator<Item = EntryRef<'_, K, O, V>> + ExactSizeIterator {
        self.assert_healthy();
        self.state.ordered.values().map(|id| self.entry_ref(*id))
    }

    /// Returns attached values in ascending secondary-key order.
    ///
    /// Equal secondary keys retain attachment order.
    ///
    /// # Returns
    ///
    /// A double-ended, exact-size iterator over attached values.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[inline]
    pub fn values_ordered(&self) -> impl DoubleEndedIterator<Item = &V> + ExactSizeIterator {
        self.iter_ordered().map(|entry| entry.value)
    }

    /// Returns the first attached record.
    ///
    /// # Returns
    ///
    /// The lowest ordered record, or `None` when no record is attached.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned.
    #[must_use]
    #[inline]
    pub fn first(&self) -> Option<EntryRef<'_, K, O, V>>
    where
        O: Ord,
    {
        self.assert_healthy();
        let id = *self.state.ordered.first_key_value()?.1;
        Some(self.entry_ref(id))
    }

    /// Returns attached records whose order keys fall within `range`.
    ///
    /// # Parameters
    ///
    /// * `range` - Inclusive or exclusive bounds over secondary keys.
    ///
    /// # Returns
    ///
    /// A double-ended iterator over attached records in stable ordered-index
    /// order.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, the start bound is greater than the end
    /// bound, equal start and end bounds are both excluded, or cloning a range
    /// bound panics.
    #[inline]
    pub fn range<R>(&self, range: R) -> impl DoubleEndedIterator<Item = EntryRef<'_, K, O, V>>
    where
        O: Clone + Ord,
        R: RangeBounds<O>,
    {
        self.assert_healthy();
        self.state
            .ordered
            .range(order_range_bounds(&range))
            .map(|(_, id)| self.entry_ref(*id))
    }

    /// Requires that no earlier cross-index mutation unwound.
    #[inline]
    fn assert_healthy(&self) {
        assert!(
            !self.poisoned,
            "OrderedIndexMap is poisoned after a prior mutation panic",
        );
    }

    /// Runs one cross-index update under the poison guard.
    #[inline]
    fn with_mutation<R>(&mut self, operation: impl FnOnce(&mut InternalState<K, O, V>, &S) -> R) -> R {
        self.assert_healthy();
        self.poisoned = true;
        let result = operation(&mut self.state, &self.hash_builder);
        self.poisoned = false;
        result
    }

    /// Converts one occupied slot into a shared public view.
    #[inline]
    fn entry_ref(&self, id: SlotId) -> EntryRef<'_, K, O, V> {
        let record = self
            .state
            .arena
            .get(id)
            .expect("primary index must reference an occupied arena slot");
        EntryRef {
            key: &record.key,
            order: &record.order,
            value: &record.value,
            state: record.state(),
        }
    }

    /// Converts one occupied slot into an exclusive public value view.
    #[inline]
    fn entry_mut(&mut self, id: SlotId) -> EntryMut<'_, K, O, V> {
        let record = self
            .state
            .arena
            .get_mut(id)
            .expect("primary index must reference an occupied arena slot");
        let state = record.state();
        EntryMut {
            key: &record.key,
            order: &record.order,
            value: &mut record.value,
            state,
        }
    }

    /// Converts one detached slot into its specialized value view.
    #[inline]
    fn detached_entry_mut(&mut self, id: SlotId) -> DetachedEntryMut<'_, K, O, V> {
        let record = self
            .state
            .arena
            .get_mut(id)
            .expect("primary index must reference an occupied arena slot");
        assert!(
            record.sequence.is_none(),
            "detached entry view requires a detached record",
        );
        DetachedEntryMut {
            key: &record.key,
            order: &record.order,
            value: &mut record.value,
        }
    }
}

impl<K, O, V, S> OrderedIndexMap<K, O, V, S>
where
    S: BuildHasher,
{
    /// Reserves primary capacity for at least `additional` more records.
    ///
    /// # Parameters
    ///
    /// * `additional` - Additional primary records expected.
    ///
    /// # Panics
    ///
    /// Panics when capacity overflows, allocation fails, the map is poisoned,
    /// or hashing a stored key panics. A panic after reservation starts
    /// poisons the map.
    pub fn reserve(&mut self, additional: usize)
    where
        K: Hash,
    {
        self.with_mutation(|state, hash_builder| {
            state.arena.reserve(additional);
            let arena = &state.arena;
            state.primary.reserve(additional, |id| {
                hash_builder.hash_one(
                    &arena
                        .get(*id)
                        .expect("primary index must reference an occupied arena slot")
                        .key,
                )
            });
        });
    }

    /// Reports whether a borrowed primary key is present.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to locate.
    ///
    /// # Returns
    ///
    /// `true` when the primary index contains an equivalent key.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or hashing or comparing `key` panics.
    #[must_use]
    #[inline]
    pub fn contains_key<Q>(&self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.find_slot(key).is_some()
    }

    /// Returns a stored value by borrowed primary key.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to locate.
    ///
    /// # Returns
    ///
    /// The stored value, or `None` when `key` is absent.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or hashing or comparing `key` panics.
    #[must_use]
    #[inline]
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let id = self.find_slot(key)?;
        Some(&self.state.arena.get(id)?.value)
    }

    /// Returns exclusive value access by borrowed primary key.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to locate.
    ///
    /// # Returns
    ///
    /// Exclusive access to the stored value, or `None` when `key` is absent.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or hashing or comparing `key` panics.
    #[must_use]
    #[inline]
    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let id = self.find_slot(key)?;
        Some(&mut self.state.arena.get_mut(id)?.value)
    }

    /// Returns a complete shared record view by borrowed primary key.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to locate.
    ///
    /// # Returns
    ///
    /// A shared record view, or `None` when `key` is absent.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or hashing or comparing `key` panics.
    #[must_use]
    #[inline]
    pub fn get_entry<Q>(&self, key: &Q) -> Option<EntryRef<'_, K, O, V>>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.find_slot(key).map(|id| self.entry_ref(id))
    }

    /// Returns a complete record view with exclusive value access.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to locate.
    ///
    /// # Returns
    ///
    /// A record view with exclusive value access, or `None` when `key` is
    /// absent.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or hashing or comparing `key` panics.
    #[must_use]
    #[inline]
    pub fn get_entry_mut<Q>(&mut self, key: &Q) -> Option<EntryMut<'_, K, O, V>>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let id = self.find_slot(key)?;
        Some(self.entry_mut(id))
    }

    /// Finds one arena slot through the primary hash index.
    #[inline]
    fn find_slot<Q>(&self, key: &Q) -> Option<SlotId>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.assert_healthy();
        let hash = self.hash_builder.hash_one(key);
        self.state
            .primary
            .find(hash, |id| {
                self.state
                    .arena
                    .get(*id)
                    .is_some_and(|record| record.key.borrow() == key)
            })
            .copied()
    }
}

impl<K, O, V, S> OrderedIndexMap<K, O, V, S>
where
    K: Eq + Hash,
    O: Ord + Clone,
    S: BuildHasher,
{
    /// Inserts an attached record or replaces the record with the same key.
    ///
    /// Replacement returns the complete old record and its prior attachment
    /// state. The new record receives a fresh stable attachment sequence.
    ///
    /// # Parameters
    ///
    /// * `key` - Unique primary key for the new record.
    /// * `order` - Ordered secondary key for the new record.
    /// * `value` - Value stored by the new record.
    ///
    /// # Returns
    ///
    /// The replaced record in its prior attachment state, or `None` when
    /// `key` was vacant.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, stable sequence allocation is exhausted,
    /// or user-provided hashing, equality, ordering, cloning, or destruction
    /// code panics. A panic after mutation starts poisons the map.
    pub fn insert(&mut self, key: K, order: O, value: V) -> Option<OwnedEntry<K, O, V>> {
        self.assert_healthy();
        let hash = self.hash_builder.hash_one(&key);
        let previous_id = self.find_slot(&key);
        let previous_ordered_key = previous_id.and_then(|id| {
            let record = self
                .state
                .arena
                .get(id)
                .expect("primary index must reference an occupied arena slot");
            record.sequence.map(|sequence| (record.order.clone(), sequence))
        });
        let indexed_order = order.clone();

        self.with_mutation(move |state, hash_builder| {
            let previous = previous_id.map(|id| {
                remove_primary_id(state, hash, id);
                if let Some(ordered_key) = previous_ordered_key {
                    let removed = state.ordered.remove(&ordered_key);
                    assert_eq!(removed, Some(id), "ordered index must reference replaced slot");
                }
                owned_from_record(state.arena.remove(id).expect("replaced primary slot must be occupied"))
            });

            let sequence = state.allocate_sequence();
            let id = state.arena.insert(Record {
                key,
                order,
                value,
                sequence: Some(sequence),
            });
            let arena = &state.arena;
            state.primary.insert_unique(hash, id, |candidate| {
                hash_builder.hash_one(
                    &arena
                        .get(*candidate)
                        .expect("primary index must reference an occupied arena slot")
                        .key,
                )
            });
            let old = state.ordered.insert((indexed_order, sequence), id);
            assert!(old.is_none(), "ordered index sequence must be unique");
            previous
        })
    }

    /// Inserts a record only when its primary key is vacant.
    ///
    /// Unlike [`Self::insert`], this method never replaces an existing record.
    /// A rejected key leaves every map record and both indexes unchanged.
    ///
    /// # Parameters
    ///
    /// * `key` - Unique primary key proposed for the record.
    /// * `order` - Ordered secondary key proposed for the record.
    /// * `value` - Value proposed for the record.
    ///
    /// # Returns
    ///
    /// An exclusive view of the inserted record, or a [`TryInsertError`]
    /// containing the rejected key, order, and value when `key` is occupied.
    ///
    /// # Errors
    ///
    /// Returns [`TryInsertError`] containing the untouched input when `key`
    /// already exists.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, if stable sequence allocation is
    /// exhausted, or if user-provided hashing, equality, ordering, cloning, or
    /// destruction code panics. A panic after mutation starts poisons the map.
    pub fn try_insert(&mut self, key: K, order: O, value: V) -> Result<EntryMut<'_, K, O, V>, TryInsertError<K, O, V>> {
        self.assert_healthy();
        if self.find_slot(&key).is_some() {
            return Err(TryInsertError::new(key, order, value));
        }
        let hash = self.hash_builder.hash_one(&key);
        let indexed_order = order.clone();
        let id = self.with_mutation(move |state, hash_builder| {
            let sequence = state.allocate_sequence();
            let id = state.arena.insert(Record {
                key,
                order,
                value,
                sequence: Some(sequence),
            });
            let arena = &state.arena;
            state.primary.insert_unique(hash, id, |candidate| {
                hash_builder.hash_one(
                    &arena
                        .get(*candidate)
                        .expect("primary index must reference an occupied arena slot")
                        .key,
                )
            });
            let previous = state.ordered.insert((indexed_order, sequence), id);
            assert!(previous.is_none(), "ordered index sequence must be unique");
            id
        });
        Ok(self.entry_mut(id))
    }

    /// Removes and returns a record in either attachment state.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to remove.
    ///
    /// # Returns
    ///
    /// The removed record and its prior attachment state, or `None` when
    /// `key` is absent.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or user-provided hashing, equality,
    /// ordering, cloning, or destruction code panics. A panic after mutation
    /// starts poisons the map.
    pub fn remove<Q>(&mut self, key: &Q) -> Option<OwnedEntry<K, O, V>>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.assert_healthy();
        let hash = self.hash_builder.hash_one(key);
        let id = self.find_slot(key)?;
        let ordered_key = {
            let record = self
                .state
                .arena
                .get(id)
                .expect("primary index must reference an occupied arena slot");
            record.sequence.map(|sequence| (record.order.clone(), sequence))
        };
        Some(self.remove_slot(hash, id, ordered_key))
    }

    /// Detaches a record from ordered operations while retaining it by key.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to detach.
    ///
    /// # Returns
    ///
    /// Exclusive value access for the detached record, or `None` when `key` is
    /// absent or already detached.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or user-provided hashing, equality,
    /// ordering, cloning, or destruction code panics. A panic after mutation
    /// starts poisons the map.
    pub fn detach<Q>(&mut self, key: &Q) -> Option<DetachedEntryMut<'_, K, O, V>>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.assert_healthy();
        let id = self.find_slot(key)?;
        let ordered_key = {
            let record = self
                .state
                .arena
                .get(id)
                .expect("primary index must reference an occupied arena slot");
            (record.order.clone(), record.sequence?)
        };
        self.detach_slot(id, ordered_key);
        Some(self.detached_entry_mut(id))
    }

    /// Attaches a detached record using its retained order.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to attach.
    ///
    /// # Returns
    ///
    /// Exclusive value access for the attached record, or `None` when `key` is
    /// absent or already attached.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, stable sequence allocation is exhausted,
    /// or user-provided hashing, equality, ordering, cloning, or destruction
    /// code panics. A panic after mutation starts poisons the map.
    pub fn attach<Q>(&mut self, key: &Q) -> Option<EntryMut<'_, K, O, V>>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.assert_healthy();
        let id = self.find_slot(key)?;
        let indexed_order = {
            let record = self
                .state
                .arena
                .get(id)
                .expect("primary index must reference an occupied arena slot");
            if record.sequence.is_some() {
                return None;
            }
            record.order.clone()
        };
        self.with_mutation(|state, _| {
            let sequence = state.allocate_sequence();
            let old = state.ordered.insert((indexed_order, sequence), id);
            assert!(old.is_none(), "ordered index sequence must be unique");
            state
                .arena
                .get_mut(id)
                .expect("attached primary slot must be occupied")
                .sequence = Some(sequence);
        });
        Some(self.entry_mut(id))
    }

    /// Replaces a record's retained order while preserving attachment state.
    ///
    /// Attached records receive a fresh stable sequence.
    ///
    /// # Parameters
    ///
    /// * `key` - Borrowed form of the primary key to reorder.
    /// * `order` - New ordered secondary key retained by the record.
    ///
    /// # Returns
    ///
    /// The previous order, or `None` when `key` is absent.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, stable sequence allocation is exhausted,
    /// or user-provided hashing, equality, ordering, cloning, or destruction
    /// code panics. A panic after mutation starts poisons the map.
    pub fn set_order<Q>(&mut self, key: &Q, order: O) -> Option<O>
    where
        K: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.assert_healthy();
        let id = self.find_slot(key)?;
        let (old_ordered_key, indexed_order) = {
            let record = self
                .state
                .arena
                .get(id)
                .expect("primary index must reference an occupied arena slot");
            (
                record.sequence.map(|sequence| (record.order.clone(), sequence)),
                record.sequence.map(|_| order.clone()),
            )
        };
        Some(self.with_mutation(|state, _| {
            let new_sequence = indexed_order.as_ref().map(|_| state.allocate_sequence());
            if let Some(old_key) = old_ordered_key {
                let removed = state.ordered.remove(&old_key);
                assert_eq!(removed, Some(id), "ordered index must reference reordered slot");
            }
            if let (Some(indexed_order), Some(sequence)) = (indexed_order, new_sequence) {
                let old = state.ordered.insert((indexed_order, sequence), id);
                assert!(old.is_none(), "ordered index sequence must be unique");
            }
            let record = state
                .arena
                .get_mut(id)
                .expect("reordered primary slot must be occupied");
            record.sequence = new_sequence;
            std::mem::replace(&mut record.order, order)
        }))
    }

    /// Returns a lending cursor that detaches attached records in `range`.
    ///
    /// Each `next` or `next_back` call returns exclusive access to the value
    /// just detached. The cursor keeps the map exclusively borrowed.
    /// Dropping the cursor early leaves unvisited records attached.
    ///
    /// # Parameters
    ///
    /// * `range` - Inclusive or exclusive bounds over secondary keys.
    ///
    /// # Returns
    ///
    /// A lending, double-ended cursor over matching attached records.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, the start bound is greater than the end
    /// bound, equal start and end bounds are both excluded, or cloning a range
    /// bound panics.
    pub fn detach_range<R>(&mut self, range: R) -> DetachRange<'_, K, O, V, S>
    where
        R: RangeBounds<O>,
    {
        self.assert_healthy();
        DetachRange {
            map: self,
            bounds: order_range_bounds(&range),
        }
    }

    /// Returns an iterator that removes attached records in `range`.
    ///
    /// Dropping the iterator early leaves records not yet yielded in the map.
    ///
    /// # Parameters
    ///
    /// * `range` - Inclusive or exclusive bounds over secondary keys.
    ///
    /// # Returns
    ///
    /// A double-ended iterator that owns each removed record.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned, the start bound is greater than the end
    /// bound, equal start and end bounds are both excluded, or cloning a range
    /// bound panics.
    pub fn extract_range<R>(&mut self, range: R) -> ExtractRange<'_, K, O, V, S>
    where
        R: RangeBounds<O>,
    {
        self.assert_healthy();
        ExtractRange {
            map: self,
            bounds: order_range_bounds(&range),
        }
    }
}

impl<K, O, V, S> OrderedIndexMap<K, O, V, S>
where
    K: Hash,
    O: Ord,
    S: BuildHasher,
{
    /// Removes and returns the first attached record.
    ///
    /// # Returns
    ///
    /// The lowest ordered record, or `None` when no record is attached.
    ///
    /// # Panics
    ///
    /// Panics if the map is poisoned or hashing the primary key or destroying
    /// the ordered-index key panics. A destructor panic after removal starts
    /// poisons the map.
    pub fn pop_first(&mut self) -> Option<OwnedEntry<K, O, V>> {
        self.assert_healthy();
        let id = *self.state.ordered.first_key_value().map(|(_, id)| id)?;
        let hash = {
            let record = self
                .state
                .arena
                .get(id)
                .expect("ordered index must reference an occupied arena slot");
            self.hash_builder.hash_one(&record.key)
        };
        Some(self.with_mutation(|state, _| {
            remove_primary_id(state, hash, id);
            let (ordered_key, removed_id) = state
                .ordered
                .pop_first()
                .expect("first ordered slot must remain present");
            assert_eq!(removed_id, id, "first ordered slot must match the preflight slot",);
            drop(ordered_key);
            owned_from_record(state.arena.remove(id).expect("removed primary slot must be occupied"))
        }))
    }
}

impl<K, O, V, S> OrderedIndexMap<K, O, V, S>
where
    O: Ord,
{
    /// Detaches one slot with a prevalidated ordered-index key.
    fn detach_slot(&mut self, id: SlotId, ordered_key: (O, Sequence)) {
        self.with_mutation(|state, _| {
            let removed = state.ordered.remove(&ordered_key);
            assert_eq!(removed, Some(id), "ordered index must reference detached slot");
            state
                .arena
                .get_mut(id)
                .expect("detached primary slot must be occupied")
                .sequence = None;
        });
    }

    /// Removes one slot and both of its index references.
    fn remove_slot(&mut self, hash: u64, id: SlotId, ordered_key: Option<(O, Sequence)>) -> OwnedEntry<K, O, V> {
        self.with_mutation(|state, _| {
            remove_primary_id(state, hash, id);
            if let Some(ordered_key) = ordered_key {
                let removed = state.ordered.remove(&ordered_key);
                assert_eq!(removed, Some(id), "ordered index must reference removed slot");
            }
            owned_from_record(state.arena.remove(id).expect("removed primary slot must be occupied"))
        })
    }
}

impl<K, O, V, S> Default for OrderedIndexMap<K, O, V, S>
where
    S: Default,
{
    /// Creates an empty map with the hash builder's default value.
    #[inline]
    fn default() -> Self {
        Self::with_hasher(S::default())
    }
}

impl<K, O, V, S> Clone for OrderedIndexMap<K, O, V, S>
where
    K: Clone,
    O: Clone,
    V: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        self.assert_healthy();
        Self {
            state: self.state.clone(),
            hash_builder: self.hash_builder.clone(),
            poisoned: false,
        }
    }
}

impl<K, O, V, S> fmt::Debug for OrderedIndexMap<K, O, V, S>
where
    K: fmt::Debug,
    O: fmt::Debug,
    V: fmt::Debug,
    S: fmt::Debug,
{
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.assert_healthy();
        formatter
            .debug_struct("OrderedIndexMap")
            .field("state", &self.state)
            .field("hash_builder", &self.hash_builder)
            .finish()
    }
}

/// Removes one exact slot identifier from the primary hash index.
fn remove_primary_id<K, O, V>(state: &mut InternalState<K, O, V>, hash: u64, id: SlotId) {
    let entry = state
        .primary
        .find_entry(hash, |candidate| *candidate == id)
        .expect("primary index must reference removed slot");
    let (removed, _) = entry.remove();
    assert_eq!(removed, id, "primary index removed an unexpected slot");
}

/// Converts a private arena record into its public owned representation.
fn owned_from_record<K, O, V>(record: Record<K, O, V>) -> OwnedEntry<K, O, V> {
    let state = record.state();
    OwnedEntry {
        key: record.key,
        order: record.order,
        value: record.value,
        state,
    }
}

/// Expands order bounds to cover all stable sequences for matching orders.
fn order_range_bounds<O, R>(range: &R) -> SequenceBounds<O>
where
    O: Clone + Ord,
    R: RangeBounds<O>,
{
    assert_valid_order_range(range);
    (
        match range.start_bound() {
            Bound::Included(order) => Bound::Included((order.clone(), Sequence(0))),
            Bound::Excluded(order) => Bound::Excluded((order.clone(), Sequence(u64::MAX))),
            Bound::Unbounded => Bound::Unbounded,
        },
        match range.end_bound() {
            Bound::Included(order) => Bound::Included((order.clone(), Sequence(u64::MAX))),
            Bound::Excluded(order) => Bound::Excluded((order.clone(), Sequence(0))),
            Bound::Unbounded => Bound::Unbounded,
        },
    )
}

/// Requires an order range to satisfy the standard ordered-map contract.
///
/// # Panics
///
/// Panics if the start bound is greater than the end bound, or equal start and
/// end bounds are both excluded.
fn assert_valid_order_range<O, R>(range: &R)
where
    O: Ord,
    R: RangeBounds<O>,
{
    let (start, start_excluded) = match range.start_bound() {
        Bound::Included(order) => (Some(order), false),
        Bound::Excluded(order) => (Some(order), true),
        Bound::Unbounded => (None, false),
    };
    let (end, end_excluded) = match range.end_bound() {
        Bound::Included(order) => (Some(order), false),
        Bound::Excluded(order) => (Some(order), true),
        Bound::Unbounded => (None, false),
    };
    if let (Some(start), Some(end)) = (start, end) {
        assert!(start <= end, "range start is greater than range end",);
        assert!(
            start != end || !(start_excluded && end_excluded),
            "range start and end are equal and excluded",
        );
    }
}