1use core::{
30 cmp::Ordering,
31 fmt,
32 marker::PhantomData,
33 mem::MaybeUninit,
34 ops::{Deref, DerefMut},
35 ptr,
36};
37
38#[cfg(feature = "zeroize")]
39use zeroize::Zeroize;
40
41mod storage {
42 use super::{LenType, Node, SortedLinkedListInner, SortedLinkedListView};
43
44 #[allow(private_bounds)]
74 pub trait SortedLinkedListStorage<T, Idx>: SortedLinkedListSealedStorage<T, Idx> {}
75
76 pub trait SortedLinkedListSealedStorage<T, Idx> {
77 fn borrow(&self) -> &[Node<T, Idx>];
80 fn borrow_mut(&mut self) -> &mut [Node<T, Idx>];
81 fn as_view<K>(
82 this: &SortedLinkedListInner<T, Idx, K, Self>,
83 ) -> &SortedLinkedListView<T, K, Idx>
84 where
85 Idx: LenType,
86 Self: SortedLinkedListStorage<T, Idx>;
87 fn as_mut_view<K>(
88 this: &mut SortedLinkedListInner<T, Idx, K, Self>,
89 ) -> &mut SortedLinkedListView<T, K, Idx>
90 where
91 Idx: LenType,
92 Self: SortedLinkedListStorage<T, Idx>;
93 }
94
95 pub struct SortedLinkedListStorageInner<T: ?Sized> {
97 pub(crate) buffer: T,
98 }
99
100 pub type OwnedSortedLinkedListStorage<T, Idx, const N: usize> =
103 SortedLinkedListStorageInner<[Node<T, Idx>; N]>;
104 pub type ViewSortedLinkedListStorage<T, Idx> = SortedLinkedListStorageInner<[Node<T, Idx>]>;
106
107 impl<T, Idx, const N: usize> SortedLinkedListSealedStorage<T, Idx>
108 for OwnedSortedLinkedListStorage<T, Idx, N>
109 {
110 fn borrow(&self) -> &[Node<T, Idx>] {
111 &self.buffer
112 }
113 fn borrow_mut(&mut self) -> &mut [Node<T, Idx>] {
114 &mut self.buffer
115 }
116 fn as_view<K>(
117 this: &SortedLinkedListInner<T, Idx, K, Self>,
118 ) -> &SortedLinkedListView<T, K, Idx>
119 where
120 Self: SortedLinkedListStorage<T, Idx>,
121 Idx: LenType,
122 {
123 this
124 }
125 fn as_mut_view<K>(
126 this: &mut SortedLinkedListInner<T, Idx, K, Self>,
127 ) -> &mut SortedLinkedListView<T, K, Idx>
128 where
129 Self: SortedLinkedListStorage<T, Idx>,
130 Idx: LenType,
131 {
132 this
133 }
134 }
135 impl<T, Idx, const N: usize> SortedLinkedListStorage<T, Idx>
136 for OwnedSortedLinkedListStorage<T, Idx, N>
137 {
138 }
139
140 impl<T, Idx> SortedLinkedListSealedStorage<T, Idx> for ViewSortedLinkedListStorage<T, Idx> {
141 fn borrow(&self) -> &[Node<T, Idx>] {
142 &self.buffer
143 }
144 fn borrow_mut(&mut self) -> &mut [Node<T, Idx>] {
145 &mut self.buffer
146 }
147 fn as_view<K>(
148 this: &SortedLinkedListInner<T, Idx, K, Self>,
149 ) -> &SortedLinkedListView<T, K, Idx>
150 where
151 Self: SortedLinkedListStorage<T, Idx>,
152 Idx: LenType,
153 {
154 this
155 }
156 fn as_mut_view<K>(
157 this: &mut SortedLinkedListInner<T, Idx, K, Self>,
158 ) -> &mut SortedLinkedListView<T, K, Idx>
159 where
160 Self: SortedLinkedListStorage<T, Idx>,
161 Idx: LenType,
162 {
163 this
164 }
165 }
166 impl<T, Idx> SortedLinkedListStorage<T, Idx> for ViewSortedLinkedListStorage<T, Idx> {}
167}
168pub use storage::{
169 OwnedSortedLinkedListStorage, SortedLinkedListStorage, ViewSortedLinkedListStorage,
170};
171
172use crate::len_type::LenType;
173
174pub struct Min;
176
177pub struct Max;
179
180pub trait Kind: private::Sealed {
182 #[doc(hidden)]
183 fn ordering() -> Ordering;
184}
185
186impl Kind for Min {
187 fn ordering() -> Ordering {
188 Ordering::Less
189 }
190}
191
192impl Kind for Max {
193 fn ordering() -> Ordering {
194 Ordering::Greater
195 }
196}
197
198mod private {
200 pub trait Sealed {}
201}
202
203impl private::Sealed for Max {}
204impl private::Sealed for Min {}
205
206#[cfg_attr(feature = "zeroize", derive(Zeroize))]
208pub struct Node<T, Idx> {
209 val: MaybeUninit<T>,
210 next: Idx,
211}
212
213pub struct SortedLinkedListInner<T, Idx, K, S>
219where
220 Idx: LenType,
221 S: SortedLinkedListStorage<T, Idx> + ?Sized,
222{
223 head: Idx,
224 free: Idx,
225 phantom: PhantomData<(K, T)>,
226 list: S,
227}
228
229pub type SortedLinkedList<T, K, const N: usize, Idx = usize> =
231 SortedLinkedListInner<T, Idx, K, OwnedSortedLinkedListStorage<T, Idx, N>>;
232
233pub type SortedLinkedListView<T, K, Idx> =
235 SortedLinkedListInner<T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>;
236
237macro_rules! impl_const_new {
238 ($ty:ty, $new_name:ident) => {
239 impl<T, K, const N: usize> SortedLinkedList<T, K, N, $ty> {
240 pub const fn $new_name() -> Self {
242 const {
243 assert!(
244 (<$ty>::MAX as usize) >= (N + 1),
245 "The capacity is larger than `LenT` can hold, increase the size of `LenT` or reduce the capacity"
246 );
247 }
248
249 let mut list = SortedLinkedList {
250 list: OwnedSortedLinkedListStorage {
251 buffer: [const {
252 Node {
253 val: MaybeUninit::uninit(),
254 next: <$ty>::MAX,
255 }
256 }; N],
257 },
258 head: <$ty>::MAX,
259 free: 0,
260 phantom: PhantomData,
261 };
262
263 if N == 0 {
264 list.free = <$ty>::MAX;
265 return list;
266 }
267
268 let mut free = 0;
269
270 while free < N - 1 {
272 list.list.buffer[free].next = free as $ty + 1;
273 free += 1;
274 }
275
276 list
277 }
278 }
279 };
280}
281
282impl_const_new!(u8, new_u8);
283impl_const_new!(u16, new_u16);
284impl_const_new!(usize, new_usize);
285
286impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
287where
288 Idx: LenType,
289 S: SortedLinkedListStorage<T, Idx> + ?Sized,
290{
291 pub fn as_view(&self) -> &SortedLinkedListView<T, K, Idx> {
293 S::as_view(self)
294 }
295
296 pub fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, K, Idx> {
298 S::as_mut_view(self)
299 }
300
301 #[inline(always)]
303 fn node_at(&self, index: usize) -> &Node<T, Idx> {
304 unsafe { self.list.borrow().get_unchecked(index) }
306 }
307
308 #[inline(always)]
310 fn node_at_mut(&mut self, index: usize) -> &mut Node<T, Idx> {
311 unsafe { self.list.borrow_mut().get_unchecked_mut(index) }
313 }
314
315 #[inline(always)]
317 fn write_data_in_node_at(&mut self, index: usize, data: T) {
318 unsafe {
320 self.node_at_mut(index).val.as_mut_ptr().write(data);
321 }
322 }
323
324 #[inline(always)]
326 fn read_data_in_node_at(&self, index: usize) -> &T {
327 unsafe { &*self.node_at(index).val.as_ptr() }
329 }
330
331 #[inline(always)]
333 fn read_mut_data_in_node_at(&mut self, index: usize) -> &mut T {
334 unsafe { &mut *self.node_at_mut(index).val.as_mut_ptr() }
336 }
337
338 #[inline(always)]
340 fn extract_data_in_node_at(&mut self, index: usize) -> T {
341 unsafe { self.node_at(index).val.as_ptr().read() }
343 }
344}
345
346impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
347where
348 T: Ord,
349 Idx: LenType,
350 K: Kind,
351 S: SortedLinkedListStorage<T, Idx> + ?Sized,
352{
353 pub unsafe fn push_unchecked(&mut self, value: T) {
361 let new = self.free.into_usize();
362
363 self.write_data_in_node_at(new, value);
365 self.free = self.node_at(new).next;
366
367 if let Some(head) = self.head.to_non_max() {
368 if self
370 .read_data_in_node_at(head)
371 .cmp(self.read_data_in_node_at(new))
372 == K::ordering()
373 {
374 let mut current = head;
376
377 while let Some(next) = self.node_at(current).next.to_non_max() {
378 if self
379 .read_data_in_node_at(next)
380 .cmp(self.read_data_in_node_at(new))
381 != K::ordering()
382 {
383 break;
384 }
385
386 current = next;
387 }
388
389 self.node_at_mut(new).next = self.node_at(current).next;
390 self.node_at_mut(current).next = Idx::from_usize(new);
391 } else {
392 self.node_at_mut(new).next = self.head;
393 self.head = Idx::from_usize(new);
394 }
395 } else {
396 self.node_at_mut(new).next = self.head;
397 self.head = Idx::from_usize(new);
398 }
399 }
400
401 pub fn push(&mut self, value: T) -> Result<(), T> {
425 if self.is_full() {
426 Err(value)
427 } else {
428 unsafe { self.push_unchecked(value) }
429 Ok(())
430 }
431 }
432
433 pub fn peek(&self) -> Option<&T> {
460 self.head
461 .to_non_max()
462 .map(|head| self.read_data_in_node_at(head))
463 }
464
465 pub unsafe fn pop_unchecked(&mut self) -> T {
471 let head = self.head.into_usize();
472 let current = head;
473 self.head = self.node_at(head).next;
474 self.node_at_mut(current).next = self.free;
475 self.free = Idx::from_usize(current);
476
477 self.extract_data_in_node_at(current)
478 }
479
480 pub fn pop(&mut self) -> Option<T> {
498 if self.is_empty() {
499 None
500 } else {
501 Some(unsafe { self.pop_unchecked() })
502 }
503 }
504
505 #[inline]
523 pub fn is_full(&self) -> bool {
524 self.free.to_non_max().is_none()
525 }
526
527 #[inline]
541 pub fn is_empty(&self) -> bool {
542 self.head.to_non_max().is_none()
543 }
544}
545
546impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
547where
548 T: Ord,
549 Idx: LenType,
550 K: Kind,
551 S: SortedLinkedListStorage<T, Idx> + ?Sized,
552{
553 pub fn iter(&self) -> IterView<'_, T, Idx, K> {
571 IterView {
572 list: S::as_view(self),
573 index: self.head,
574 }
575 }
576
577 pub fn find_mut<F>(&mut self, mut f: F) -> Option<FindMutView<'_, T, Idx, K>>
600 where
601 F: FnMut(&T) -> bool,
602 {
603 let head = self.head.to_non_max()?;
604
605 if f(self.read_data_in_node_at(head)) {
607 return Some(FindMutView {
608 is_head: true,
609 prev_index: Idx::MAX,
610 index: self.head,
611 list: S::as_mut_view(self),
612 maybe_changed: false,
613 });
614 }
615
616 let mut current = head;
617
618 while let Some(next) = self.node_at(current).next.to_non_max() {
619 if f(self.read_data_in_node_at(next)) {
620 return Some(FindMutView {
621 is_head: false,
622 prev_index: Idx::from_usize(current),
623 index: Idx::from_usize(next),
624 list: S::as_mut_view(self),
625 maybe_changed: false,
626 });
627 }
628
629 current = next;
630 }
631
632 None
633 }
634}
635
636pub struct IterView<'a, T, Idx, K>
638where
639 T: Ord,
640 Idx: LenType,
641 K: Kind,
642{
643 list: &'a SortedLinkedListInner<T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>,
644 index: Idx,
645}
646
647impl<'a, T, Idx, K> Iterator for IterView<'a, T, Idx, K>
648where
649 T: Ord,
650 Idx: LenType,
651 K: Kind,
652{
653 type Item = &'a T;
654
655 fn next(&mut self) -> Option<Self::Item> {
656 let index = self.index.to_non_max()?;
657
658 let node = self.list.node_at(index);
659 self.index = node.next;
660
661 Some(self.list.read_data_in_node_at(index))
662 }
663}
664
665pub struct FindMutView<'a, T, Idx, K>
667where
668 T: Ord,
669 Idx: LenType,
670 K: Kind,
671{
672 list: &'a mut SortedLinkedListView<T, K, Idx>,
673 is_head: bool,
674 prev_index: Idx,
675 index: Idx,
676 maybe_changed: bool,
677}
678
679impl<T, Idx, K> FindMutView<'_, T, Idx, K>
680where
681 T: Ord,
682 Idx: LenType,
683 K: Kind,
684{
685 fn pop_internal(&mut self) -> T {
686 if self.is_head {
687 unsafe { self.list.pop_unchecked() }
689 } else {
690 let prev = self.prev_index.into_usize();
692 let curr = self.index.into_usize();
693
694 self.list.node_at_mut(prev).next = self.list.node_at_mut(curr).next;
696
697 self.list.node_at_mut(curr).next = self.list.free;
699 self.list.free = self.index;
700
701 self.list.extract_data_in_node_at(curr)
702 }
703 }
704
705 #[inline]
728 pub fn pop(mut self) -> T {
729 self.pop_internal()
730 }
731
732 #[inline]
762 pub fn finish(self) {
763 drop(self);
764 }
765}
766
767impl<T, Idx, K> Drop for FindMutView<'_, T, Idx, K>
768where
769 T: Ord,
770 Idx: LenType,
771 K: Kind,
772{
773 fn drop(&mut self) {
774 if self.maybe_changed {
776 let val = self.pop_internal();
777 unsafe { self.list.push_unchecked(val) };
778 }
779 }
780}
781
782impl<T, Idx, K> Deref for FindMutView<'_, T, Idx, K>
783where
784 T: Ord,
785 Idx: LenType,
786 K: Kind,
787{
788 type Target = T;
789
790 fn deref(&self) -> &Self::Target {
791 self.list.read_data_in_node_at(self.index.into_usize())
792 }
793}
794
795impl<T, Idx, K> DerefMut for FindMutView<'_, T, Idx, K>
796where
797 T: Ord,
798 Idx: LenType,
799 K: Kind,
800{
801 fn deref_mut(&mut self) -> &mut Self::Target {
802 self.maybe_changed = true;
803 self.list.read_mut_data_in_node_at(self.index.into_usize())
804 }
805}
806
807impl<T, Idx, K, S> fmt::Debug for SortedLinkedListInner<T, Idx, K, S>
833where
834 T: Ord + core::fmt::Debug,
835 Idx: LenType,
836 K: Kind,
837 S: ?Sized + SortedLinkedListStorage<T, Idx>,
838{
839 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
840 f.debug_list().entries(self.iter()).finish()
841 }
842}
843
844impl<T, Idx, K, S> Drop for SortedLinkedListInner<T, Idx, K, S>
845where
846 Idx: LenType,
847 S: SortedLinkedListStorage<T, Idx> + ?Sized,
848{
849 fn drop(&mut self) {
850 let mut index = self.head;
851
852 while let Some(i) = index.to_non_max() {
853 let node = self.node_at_mut(i);
854 index = node.next;
855
856 unsafe {
857 ptr::drop_in_place(node.val.as_mut_ptr());
858 }
859 }
860 }
861}
862
863#[cfg(feature = "zeroize")]
864impl<T, Idx, K, S> Zeroize for SortedLinkedListInner<T, Idx, K, S>
865where
866 T: Ord + Zeroize,
867 Idx: LenType + Zeroize,
868 K: Kind,
869 S: SortedLinkedListStorage<T, Idx> + ?Sized,
870{
871 fn zeroize(&mut self) {
872 while let Some(mut item) = self.pop() {
873 item.zeroize();
874 }
875
876 let buffer = self.list.borrow_mut();
877 for elem in buffer {
878 elem.zeroize();
879 }
880 }
881}
882
883#[cfg(test)]
884mod tests {
885 use static_assertions::assert_not_impl_any;
886
887 use super::*;
888
889 assert_not_impl_any!(SortedLinkedList<*const (), (), 4>: Send);
891
892 #[test]
893 fn const_new() {
894 static mut _V1: SortedLinkedList<u32, Max, 100, u8> = SortedLinkedList::new_u8();
895 static mut _V2: SortedLinkedList<u32, Max, 10_000, u16> = SortedLinkedList::new_u16();
896 static mut _V3: SortedLinkedList<u32, Max, 100_000, usize> = SortedLinkedList::new_usize();
897 }
898
899 #[test]
900 fn test_peek() {
901 let mut ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
902
903 ll.push(1).unwrap();
904 assert_eq!(ll.peek().unwrap(), &1);
905
906 ll.push(2).unwrap();
907 assert_eq!(ll.peek().unwrap(), &2);
908
909 ll.push(3).unwrap();
910 assert_eq!(ll.peek().unwrap(), &3);
911
912 let mut ll: SortedLinkedList<u32, Min, 3, u8> = SortedLinkedList::new_u8();
913
914 ll.push(2).unwrap();
915 assert_eq!(ll.peek().unwrap(), &2);
916
917 ll.push(1).unwrap();
918 assert_eq!(ll.peek().unwrap(), &1);
919
920 ll.push(3).unwrap();
921 assert_eq!(ll.peek().unwrap(), &1);
922 }
923
924 #[test]
925 fn test_full() {
926 let mut ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
927 ll.push(1).unwrap();
928 ll.push(2).unwrap();
929 ll.push(3).unwrap();
930
931 assert!(ll.is_full());
932 }
933
934 #[test]
935 fn test_empty() {
936 let ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
937
938 assert!(ll.is_empty());
939 }
940
941 #[test]
942 fn test_zero_size() {
943 let ll: SortedLinkedList<u32, Max, 0, u8> = SortedLinkedList::new_u8();
944
945 assert!(ll.is_empty());
946 assert!(ll.is_full());
947 }
948
949 #[test]
950 fn test_rejected_push() {
951 let mut ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
952 ll.push(1).unwrap();
953 ll.push(2).unwrap();
954 ll.push(3).unwrap();
955
956 let r = ll.push(4);
958
959 assert_eq!(r, Err(4));
960 }
961
962 #[test]
963 fn test_updating() {
964 let mut ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
965 ll.push(1).unwrap();
966 ll.push(2).unwrap();
967 ll.push(3).unwrap();
968
969 let mut find = ll.find_mut(|v| *v == 2).unwrap();
970
971 *find += 1000;
972 find.finish();
973
974 assert_eq!(ll.peek().unwrap(), &1002);
975
976 let mut find = ll.find_mut(|v| *v == 3).unwrap();
977
978 *find += 1000;
979 find.finish();
980
981 assert_eq!(ll.peek().unwrap(), &1003);
982
983 ll.find_mut(|v| *v == 1003).unwrap().pop();
985
986 assert_eq!(ll.peek().unwrap(), &1002);
987 }
988
989 #[test]
990 fn test_updating_1() {
991 let mut ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
992 ll.push(1).unwrap();
993
994 let v = ll.pop().unwrap();
995
996 assert_eq!(v, 1);
997 }
998
999 #[test]
1000 fn test_updating_2() {
1001 let mut ll: SortedLinkedList<u32, Max, 3, u8> = SortedLinkedList::new_u8();
1002 ll.push(1).unwrap();
1003
1004 let mut find = ll.find_mut(|v| *v == 1).unwrap();
1005
1006 *find += 1000;
1007 find.finish();
1008
1009 assert_eq!(ll.peek().unwrap(), &1001);
1010 }
1011
1012 #[test]
1013 #[cfg(feature = "zeroize")]
1014 fn test_sorted_linked_list_zeroize() {
1015 use zeroize::Zeroize;
1016
1017 let mut list: SortedLinkedList<u8, Max, 8, u8> = SortedLinkedList::new_u8();
1018 for i in 1..=8 {
1019 list.push(i).unwrap();
1020 }
1021
1022 assert_eq!(list.is_empty(), false);
1023 assert!(list.is_full());
1024 assert_eq!(list.peek(), Some(&8));
1025
1026 list.pop();
1027 list.pop();
1028 list.push(100).unwrap();
1029
1030 assert_eq!(list.peek(), Some(&100));
1031
1032 list.zeroize();
1033
1034 assert_eq!(list.peek(), None);
1035 assert!(list.is_empty());
1036
1037 unsafe {
1038 for node in &list.list.buffer {
1039 assert_eq!(node.val.assume_init(), 0);
1040 }
1041 }
1042 }
1043
1044 fn _test_variance<'a: 'b, 'b>(
1045 x: SortedLinkedList<&'a (), Max, 42, u8>,
1046 ) -> SortedLinkedList<&'b (), Max, 42, u8> {
1047 x
1048 }
1049 fn _test_variance_view<'a: 'b, 'b, 'c>(
1050 x: &'c SortedLinkedListView<&'a (), Max, u8>,
1051 ) -> &'c SortedLinkedListView<&'b (), Max, u8> {
1052 x
1053 }
1054}