cordyceps/
sorted_list.rs

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
//! [Intrusive], singly-linked, sorted, linked list.
//!
//! See the documentation for the [`SortedList`] and [`SortedListIter`] types for
//! details.
//!
//! [Intrusive]: crate#intrusive-data-structures

use crate::{Linked, Stack};
use core::{
    cmp::{Ord, Ordering},
    fmt,
    marker::PhantomData,
    ptr::NonNull,
};

pub use crate::stack::Links;

/// A sorted singly linked list
///
/// This behaves similar to [`Stack`], in that it is a singly linked list,
/// however items are stored in an ordered fashion. This means that insertion
/// is an _O_(_n_) operation, and retrieval of the first item is an _O_(1) operation.
///
/// It allows for a user selected ordering operation. If your type `T` implements
/// [`Ord`]:
///
/// * Consider using [`SortedList::new_min()`] if you want **smallest** items sorted first.
/// * Consider using [`SortedList::new_max()`] if you want **largest** items sorted first.
///
/// If your type `T` does NOT implement [`Ord`], or you want to use
/// a custom sorting anyway, consider using [`SortedList::new_with_cmp()`]
///
/// In order to be part of a `SortedList`, a type `T` must implement
/// the [`Linked`] trait for [`sorted_list::Links<T>`](Links), which is an alias for
/// [`stack::Links<T>`](Links). This means that you can link the same element into
/// either structure, but you can't have something that's linked into a `SortedList`
/// and a `Stack` at the same time (without wrapper structs that have separate sets
/// of links, left as an exercise for the reader).
///
/// Pushing elements into a `SortedList` takes ownership of those elements
/// through an owning [`Handle` type](Linked::Handle). Dropping a
/// `SortedList` drops all elements currently linked into the stack.
pub struct SortedList<T: Linked<Links<T>>> {
    head: Option<NonNull<T>>,
    // Returns if LHS is less/same/greater than RHS
    func: fn(&T, &T) -> Ordering,
}

#[inline]
fn invert_sort<T: Ord>(a: &T, b: &T) -> Ordering {
    // Inverted sort order!
    T::cmp(b, a)
}

impl<T> SortedList<T>
where
    T: Linked<Links<T>>,
    T: Ord,
{
    /// Create a new (empty) sorted list, sorted LEAST FIRST
    ///
    /// * Consider using [`SortedList::new_max()`] if you want **largest** items sorted first.
    /// * Consider using [`SortedList::new_with_cmp()`] if you want to provide your own sorting
    ///   implementation.
    ///
    /// If two items are considered of equal value, new values will be placed AFTER
    /// old values.
    #[must_use]
    pub const fn new_min() -> Self {
        Self::new_with_cmp(T::cmp)
    }

    /// Create a new sorted list, consuming the stack, sorted LEAST FIRST
    #[must_use]
    pub fn from_stack_min(stack: Stack<T>) -> Self {
        Self::from_stack_with_cmp(stack, T::cmp)
    }

    /// Create a new (empty) sorted list, sorted GREATEST FIRST
    ///
    /// * Consider using [`SortedList::new_min()`] if you want **smallest** items sorted first.
    /// * Consider using [`SortedList::new_with_cmp()`] if you want to provide your own sorting
    ///   implementation.
    ///
    /// If two items are considered of equal value, new values will be placed AFTER
    /// old values.
    #[must_use]
    pub const fn new_max() -> Self {
        Self::new_with_cmp(invert_sort::<T>)
    }

    /// Create a new sorted list, consuming the stack, sorted GREATEST FIRST
    #[must_use]
    pub fn from_stack_max(stack: Stack<T>) -> Self {
        Self::from_stack_with_cmp(stack, invert_sort::<T>)
    }
}

impl<T: Linked<Links<T>>> SortedList<T> {
    /// Create a new (empty) sorted list with the given ordering function
    ///
    /// If your type T implements [`Ord`]:
    ///
    /// * Consider using [`SortedList::new_min()`] if you want **smallest** items sorted first.
    /// * Consider using [`SortedList::new_max()`] if you want **largest** items sorted first.
    ///
    /// If `T` contained an `i32`, and you wanted the SMALLEST items at the
    /// front, then you could provide a function something like:
    ///
    /// ```rust
    /// let f: fn(&i32, &i32) -> core::cmp::Ordering = |lhs, rhs| {
    ///     lhs.cmp(rhs)
    /// };
    /// ```
    ///
    /// SortedList takes a function (and not just [Ord]) so you can use it on types
    /// that don't have a general way of ordering them, allowing you to select a
    /// specific metric within the sorting function.
    ///
    /// If two items are considered of equal value, new values will be placed AFTER
    /// old values.
    pub const fn new_with_cmp(f: fn(&T, &T) -> Ordering) -> Self {
        Self {
            func: f,
            head: None,
        }
    }

    /// Create a new sorted list, consuming the stack, using the provided ordering function
    pub fn from_stack_with_cmp(stack: Stack<T>, f: fn(&T, &T) -> Ordering) -> Self {
        let mut slist = Self::new_with_cmp(f);
        slist.extend(stack);
        slist
    }

    /// Pop the front-most item from the list, returning it by ownership (if it exists)
    ///
    /// Note that "front" here refers to the sorted ordering. If this list was created
    /// with [`SortedList::new_min`], the SMALLEST item will be popped. If this was
    /// created with [`SortedList::new_max`], the LARGEST item will be popped.
    ///
    /// This is an _O_(1) operation.
    #[must_use]
    pub fn pop_front(&mut self) -> Option<T::Handle> {
        test_trace!(?self.head, "SortedList::pop_front");
        let head = self.head.take()?;
        unsafe {
            // Safety: we have exclusive ownership over this chunk of stack.

            // advance the head link to the next node after the current one (if
            // there is one).
            self.head = T::links(head).as_mut().next.with_mut(|next| (*next).take());

            test_trace!(?self.head, "SortedList::pop -> popped");

            // return the current node
            Some(T::from_ptr(head))
        }
    }

    /// Insert a single item into the list, in its sorted order position
    ///
    /// Note that if the inserted item is [`Equal`](Ordering::Equal) to another
    /// item in the list, the newest item is always sorted AFTER the existing
    /// item.
    ///
    /// This is an _O_(_n_) operation.
    pub fn insert(&mut self, element: T::Handle) {
        let eptr = T::into_ptr(element);
        test_trace!(?eptr, ?self.head, "SortedList::insert");
        debug_assert!(
            unsafe { T::links(eptr).as_ref().next.with(|n| (*n).is_none()) },
            "Inserted items should not already be part of a list"
        );

        // Take a long-lived reference to the new element
        let eref = unsafe { eptr.as_ref() };

        // Special case for empty head
        //
        // If the head is null, then just place the item
        let Some(mut cursor) = self.head else {
            self.head = Some(eptr);
            return;
        };

        // Special case for head: do we replace current head with new element?
        {
            // compare, but make sure we drop the live reference to the cursor
            // so to be extra sure about NOT violating provenance, when we
            // potentially mutate the cursor below, and we really don't want
            // a shared reference to be live.
            let cmp = {
                let cref = unsafe { cursor.as_ref() };
                (self.func)(cref, eref)
            };

            // If cursor node is LESS or EQUAL: keep moving.
            // If cursor node is GREATER: we need to place the new item BEFORE
            if cmp == Ordering::Greater {
                unsafe {
                    let links = T::links(eptr).as_mut();
                    links.next.with_mut(|next| {
                        *next = self.head.replace(eptr);
                    });
                    return;
                }
            }
        }

        // On every iteration of the loop, we know that the new element should
        // be placed AFTER the current value of `cursor`, meaning that we need
        // to decide whether we:
        //
        // * just append (if next is null)
        // * insert between cursor and cursor.next (if elem is < c.next)
        // * just iterate (if elem >= c.next)
        loop {
            // Safety: We have exclusive access to the list, we are allowed to
            // access and mutate it (carefully)
            unsafe {
                // Get the cursor's links
                let clinks = T::links(cursor).as_mut();
                // Peek into the cursor's next item
                let next = clinks.next.with_mut(|next| {
                    // We can take a reference here, as we have exclusive access
                    let mutref = &mut *next;

                    if let Some(n) = mutref {
                        // If next is some, store this pointer
                        let nptr: NonNull<T> = *n;
                        // Then compare the next element with the new element
                        let cmp = {
                            let nref: &T = nptr.as_ref();
                            (self.func)(nref, eref)
                        };

                        if cmp == Ordering::Greater {
                            // As above, if cursor.next > element, then we
                            // need to insert between cursor and next.
                            //
                            // First, get the current element's links...
                            let elinks = T::links(eptr).as_mut();
                            // ...then store cursor.next.next in element.next,
                            // and store element in cursor.next.
                            elinks.next.with_mut(|enext| {
                                *enext = mutref.replace(eptr);
                            });
                            // If we have placed element, there is no next value
                            // for cursor.
                            None
                        } else {
                            // If cursor.next <= element, then we just need to
                            // iterate, so return the NonNull that represents
                            // cursor.next, so we can move cursor there.
                            Some(nptr)
                        }
                    } else {
                        // "just append" case - assign element to cursor.next
                        *mutref = Some(eptr);
                        // If we have placed element, there is no next value
                        // for cursor
                        None
                    }
                });

                // We do the assignment through this tricky return to ensure that the
                // mutable reference to cursor.next we held as "mutref" above has been
                // dropped, so we are not mutating `cursor` while a reference derived
                // from it's provenance is live.
                //
                // We also can't early return inside the loop because all of the body
                // is inside a closure.
                //
                // This might be overly cautious, refactor carefully (with miri).
                let Some(n) = next else {
                    // We're done, return
                    return;
                };
                cursor = n;
            }
        }
    }

    /// Iterate through the items of the list, in sorted order
    pub fn iter(&self) -> SortedListIter<'_, T> {
        SortedListIter {
            _plt: PhantomData,
            node: self.head,
        }
    }
}

impl<T: Linked<Links<T>>> Extend<T::Handle> for SortedList<T> {
    fn extend<I: IntoIterator<Item = T::Handle>>(&mut self, iter: I) {
        for elem in iter {
            self.insert(elem);
        }
    }
}

impl<T> fmt::Debug for SortedList<T>
where
    T: Linked<Links<T>>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { head, func: _ } = self;
        f.debug_struct("SortedList").field("head", head).finish()
    }
}

impl<T: Linked<Links<T>>> Drop for SortedList<T> {
    fn drop(&mut self) {
        // We just turn the list into a stack then run the stack drop code.
        // It already has correct + tested logic for dropping a singly
        // linked list of items one at a time.
        let stack = Stack {
            head: self.head.take(),
        };
        drop(stack);
    }
}

/// A borrowing iterator of a [`SortedList`]
pub struct SortedListIter<'a, T: Linked<Links<T>>> {
    _plt: PhantomData<&'a SortedList<T>>,
    node: Option<NonNull<T>>,
}

impl<'a, T: Linked<Links<T>>> Iterator for SortedListIter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        let nn = self.node.take()?;
        unsafe {
            // Advance our pointer to next
            let links = T::links(nn).as_ref();
            self.node = links.next.with(|t| *t);
            Some(nn.as_ref())
        }
    }
}

#[cfg(test)]
mod loom {
    use super::*;
    use crate::loom;
    use crate::stack::test_util::Entry;

    #[test]
    fn builtin_sort_min() {
        loom::model(|| {
            let mut slist = SortedList::<Entry>::new_min();
            // Insert out of order
            slist.insert(Entry::new(20));
            slist.insert(Entry::new(10));
            slist.insert(Entry::new(30));
            slist.insert(Entry::new(25));
            slist.insert(Entry::new(35));
            slist.insert(Entry::new(1));
            slist.insert(Entry::new(2));
            slist.insert(Entry::new(3));
            // expected is in order
            let expected = [1, 2, 3, 10, 20, 25, 30, 35];

            // Does iteration work (twice)?
            {
                let mut ct = 0;
                let siter = slist.iter();
                for (l, r) in expected.iter().zip(siter) {
                    ct += 1;
                    assert_eq!(*l, r.val);
                }
                assert_eq!(ct, expected.len());
            }
            {
                let mut ct = 0;
                let siter = slist.iter();
                for (l, r) in expected.iter().zip(siter) {
                    ct += 1;
                    assert_eq!(*l, r.val);
                }
                assert_eq!(ct, expected.len());
            }

            // Does draining work (once)?
            {
                let mut ct = 0;
                for exp in expected.iter() {
                    let act = slist.pop_front().unwrap();
                    ct += 1;
                    assert_eq!(*exp, act.val);
                }
                assert_eq!(ct, expected.len());
                assert!(slist.pop_front().is_none());
            }
        })
    }

    #[test]
    fn builtin_sort_max() {
        loom::model(|| {
            let mut slist = SortedList::<Entry>::new_max();
            // Insert out of order
            slist.insert(Entry::new(20));
            slist.insert(Entry::new(10));
            slist.insert(Entry::new(30));
            slist.insert(Entry::new(25));
            slist.insert(Entry::new(35));
            slist.insert(Entry::new(1));
            slist.insert(Entry::new(2));
            slist.insert(Entry::new(3));
            // expected is in order (reverse!)
            let expected = [35, 30, 25, 20, 10, 3, 2, 1];

            // Does iteration work (twice)?
            {
                let mut ct = 0;
                let siter = slist.iter();
                for (l, r) in expected.iter().zip(siter) {
                    ct += 1;
                    assert_eq!(*l, r.val);
                }
                assert_eq!(ct, expected.len());
            }
            {
                let mut ct = 0;
                let siter = slist.iter();
                for (l, r) in expected.iter().zip(siter) {
                    ct += 1;
                    assert_eq!(*l, r.val);
                }
                assert_eq!(ct, expected.len());
            }

            // Does draining work (once)?
            {
                let mut ct = 0;
                for exp in expected.iter() {
                    let act = slist.pop_front().unwrap();
                    ct += 1;
                    assert_eq!(*exp, act.val);
                }
                assert_eq!(ct, expected.len());
                assert!(slist.pop_front().is_none());
            }
        })
    }

    #[test]
    fn slist_basic() {
        loom::model(|| {
            let mut slist = SortedList::<Entry>::new_with_cmp(|lhs, rhs| lhs.val.cmp(&rhs.val));
            // Insert out of order
            slist.insert(Entry::new(20));
            slist.insert(Entry::new(10));
            slist.insert(Entry::new(30));
            slist.insert(Entry::new(25));
            slist.insert(Entry::new(35));
            slist.insert(Entry::new(1));
            slist.insert(Entry::new(2));
            slist.insert(Entry::new(3));
            // expected is in order
            let expected = [1, 2, 3, 10, 20, 25, 30, 35];

            // Does iteration work (twice)?
            {
                let mut ct = 0;
                let siter = slist.iter();
                for (l, r) in expected.iter().zip(siter) {
                    ct += 1;
                    assert_eq!(*l, r.val);
                }
                assert_eq!(ct, expected.len());
            }
            {
                let mut ct = 0;
                let siter = slist.iter();
                for (l, r) in expected.iter().zip(siter) {
                    ct += 1;
                    assert_eq!(*l, r.val);
                }
                assert_eq!(ct, expected.len());
            }

            // Does draining work (once)?
            {
                let mut ct = 0;
                for exp in expected.iter() {
                    let act = slist.pop_front().unwrap();
                    ct += 1;
                    assert_eq!(*exp, act.val);
                }
                assert_eq!(ct, expected.len());
                assert!(slist.pop_front().is_none());
            }

            // Do a little pop and remove dance to make sure we correctly unset
            // next on popped items (there is a debug assert for this)
            slist.insert(Entry::new(20));
            slist.insert(Entry::new(10));
            slist.insert(Entry::new(30));
            let x = slist.pop_front().unwrap();
            slist.insert(x);
            let y = slist.pop_front().unwrap();
            let z = slist.pop_front().unwrap();
            slist.insert(y);
            slist.insert(z);
        })
    }
}