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
//! A lock-free singly-linked list.

use core::{
    iter::{FromIterator, FusedIterator},
    marker::PhantomData,
    ops::{Deref, DerefMut},
    ptr,
    sync::atomic::{AtomicPtr, Ordering},
};

/// A lock-free singly-linked list.
pub struct LinkedList<T> {
    head: AtomicPtr<Node<T>>,
}

/// A node of [`LinkedList`].
#[repr(C)]
pub struct Node<T> {
    next: *mut Node<T>,
    /// The value attached to this node.
    pub value: T,
}

/// An owning iterator over the elements of a [`LinkedList`].
pub struct IntoIter<T> {
    list: LinkedList<T>,
}

/// An iterator produced by [`LinkedList::iter_mut`] or
/// [`LinkedList::iter_mut_unchecked`].
pub struct IterMut<'a, T> {
    curr: *mut Node<T>,
    marker: PhantomData<&'a mut Node<T>>,
}

/// An iterator produced by [`LinkedList::drain_filter`].
pub struct DrainFilter<'a, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    raw: DrainFilterRaw<'a, T, F>,
}

/// An iterator produced by [`LinkedList::drain_filter_raw`].
pub struct DrainFilterRaw<'a, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    head: &'a AtomicPtr<Node<T>>,
    prev: *mut Node<T>,
    curr: *mut Node<T>,
    filter: F,
}

unsafe impl<T> Sync for LinkedList<T> {}

impl<T> LinkedList<T> {
    /// Creates an empty [`LinkedList`].
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::LinkedList;
    ///
    /// let list: LinkedList<u32> = LinkedList::new();
    /// ```
    #[inline]
    pub const fn new() -> Self {
        Self { head: AtomicPtr::new(ptr::null_mut()) }
    }

    /// Returns `true` if the [`LinkedList`] is empty.
    ///
    /// This operation should compute in *O*(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::LinkedList;
    ///
    /// let list = LinkedList::new();
    /// assert!(list.is_empty());
    ///
    /// list.push("foo");
    /// assert!(!list.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.head.load(Ordering::Relaxed).is_null()
    }

    /// Adds an element first in the list.
    ///
    /// This operation should compute in *O*(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::LinkedList;
    ///
    /// let list = LinkedList::new();
    ///
    /// list.push(2);
    /// list.push(1);
    /// assert_eq!(list.pop().unwrap(), 1);
    /// assert_eq!(list.pop().unwrap(), 2);
    /// ```
    #[inline]
    pub fn push(&self, data: T) {
        unsafe { self.push_raw(Box::into_raw(Box::new(Node::from(data)))) };
    }

    /// Adds a pre-allocated element first in the list.
    ///
    /// This operation should compute in *O*(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::linked_list::{LinkedList, Node};
    ///
    /// let list = LinkedList::new();
    ///
    /// let foo = Box::into_raw(Box::new(Node::from("foo")));
    /// unsafe { list.push_raw(foo) };
    /// assert_eq!(unsafe { **foo }, "foo");
    /// ```
    ///
    /// # Safety
    ///
    /// The `node` parameter must point to a valid allocation. Other list
    /// methods, which drop nodes in-place, assume that `node` is created using
    /// [`Box::from_raw`].
    #[inline]
    pub unsafe fn push_raw(&self, node: *mut Node<T>) {
        loop {
            let curr = self.head.load(Ordering::Relaxed);
            unsafe { (*node).next = curr };
            if self
                .head
                .compare_exchange_weak(curr, node, Ordering::Release, Ordering::Relaxed)
                .is_ok()
            {
                break;
            }
        }
    }

    /// Removes the first element and returns it, or `None` if the list is
    /// empty.
    ///
    /// This operation should compute in *O*(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::LinkedList;
    ///
    /// let d = LinkedList::new();
    /// assert_eq!(d.pop(), None);
    ///
    /// d.push(1);
    /// d.push(3);
    /// assert_eq!(d.pop(), Some(3));
    /// assert_eq!(d.pop(), Some(1));
    /// assert_eq!(d.pop(), None);
    /// ```
    #[inline]
    pub fn pop(&self) -> Option<T> {
        unsafe { self.pop_raw().map(|node| Box::from_raw(node).value) }
    }

    /// Removes the first element and returns a raw pointer to it, or `None` if
    /// the list is empty.
    ///
    /// This operation should compute in *O*(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::linked_list::{LinkedList, Node};
    ///
    /// let list = LinkedList::new();
    ///
    /// let foo = Box::into_raw(Box::new(Node::from("foo")));
    /// unsafe {
    ///     list.push_raw(foo);
    ///     let foo = list.pop_raw().unwrap();
    ///     list.push_raw(foo);
    /// }
    ///
    /// assert_eq!(unsafe { **foo }, "foo");
    /// ```
    ///
    /// # Safety
    ///
    /// It's responsibility of the caller to de-allocate the node.
    #[inline]
    pub unsafe fn pop_raw(&self) -> Option<*mut Node<T>> {
        loop {
            let curr = self.head.load(Ordering::Acquire);
            if curr.is_null() {
                break None;
            }
            let next = unsafe { (*curr).next };
            if self
                .head
                .compare_exchange_weak(curr, next, Ordering::Relaxed, Ordering::Relaxed)
                .is_ok()
            {
                break Some(curr);
            }
        }
    }

    /// Provides a forward iterator with mutable references.
    ///
    /// # Examples
    ///
    /// ```
    /// use drone_core::sync::LinkedList;
    ///
    /// let mut list: LinkedList<u32> = LinkedList::new();
    ///
    /// list.push(0);
    /// list.push(1);
    /// list.push(2);
    ///
    /// for element in list.iter_mut() {
    ///     *element += 10;
    /// }
    ///
    /// let mut iter = list.iter_mut();
    /// assert_eq!(iter.next(), Some(&mut 12));
    /// assert_eq!(iter.next(), Some(&mut 11));
    /// assert_eq!(iter.next(), Some(&mut 10));
    /// assert_eq!(iter.next(), None);
    /// ```
    #[inline]
    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        // Because `self` is a unique reference, no node can be deleted.
        unsafe { self.iter_mut_unchecked() }
    }

    /// Unsafe variant of [`iter_mut`] with non-mutable `self`.
    ///
    /// # Safety
    ///
    /// While the returned iterator is alive:
    ///
    /// * Nodes shouldn't be removed.
    /// * No other methods that produce mutable references (including this
    ///   method) should be called.
    #[inline]
    pub unsafe fn iter_mut_unchecked(&self) -> IterMut<'_, T> {
        IterMut { curr: self.head.load(Ordering::Acquire), marker: PhantomData }
    }

    /// Creates an iterator which uses a closure to determine if an element
    /// should be removed.
    ///
    /// If the closure returns `true`, then the element is removed and yielded.
    /// If the closure returns `false`, the element will remain in the list and
    /// will not be yielded by the iterator.
    ///
    /// Note that `drain_filter` lets you mutate every element in the filter
    /// closure, regardless of whether you choose to keep or remove it.
    ///
    /// # Examples
    ///
    /// Splitting a list into evens and odds, reusing the original list:
    ///
    /// ```
    /// use drone_core::sync::LinkedList;
    ///
    /// let mut numbers: LinkedList<u32> = LinkedList::new();
    /// numbers.extend(&[1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]);
    ///
    /// let evens = numbers.drain_filter(|x| *x % 2 == 0).collect::<LinkedList<_>>();
    /// let odds = numbers;
    ///
    /// assert_eq!(evens.into_iter().collect::<Vec<_>>(), vec![2, 4, 6, 8, 14]);
    /// assert_eq!(odds.into_iter().collect::<Vec<_>>(), vec![15, 13, 11, 9, 5, 3, 1]);
    /// ```
    #[inline]
    pub fn drain_filter<'a, F: 'a>(
        &'a mut self,
        mut filter: F,
    ) -> DrainFilter<'_, T, impl FnMut(*mut Node<T>) -> bool>
    where
        F: FnMut(&mut T) -> bool,
    {
        // Because `self` is a unique reference, both safety invariants are
        // upholding.
        unsafe { DrainFilter { raw: self.drain_filter_raw(move |node| filter(&mut *node)) } }
    }

    /// Raw variant of [`drain_filter`].
    ///
    /// # Safety
    ///
    /// It's responsibility of the caller to de-allocate returned nodes.
    ///
    /// While the returned iterator is alive:
    ///
    /// * Nodes shouldn't be removed.
    /// * No other methods that produce mutable references (including this
    ///   method) should be called.
    #[inline]
    pub unsafe fn drain_filter_raw<F>(
        &self,
        filter: F,
    ) -> DrainFilterRaw<'_, T, impl FnMut(*mut Node<T>) -> bool>
    where
        F: FnMut(*mut Node<T>) -> bool,
    {
        DrainFilterRaw {
            head: &self.head,
            prev: ptr::null_mut(),
            curr: self.head.load(Ordering::Acquire),
            filter,
        }
    }
}

impl<T> Drop for LinkedList<T> {
    #[inline]
    fn drop(&mut self) {
        let mut curr = self.head.load(Ordering::Acquire);
        while !curr.is_null() {
            let next = unsafe { (*curr).next };
            drop(unsafe { Box::from_raw(curr) });
            curr = next;
        }
    }
}

impl<T> Extend<T> for LinkedList<T> {
    #[inline]
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for elem in iter {
            self.push(elem);
        }
    }
}

impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
    #[inline]
    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
        self.extend(iter.into_iter().cloned());
    }
}

impl<T> FromIterator<T> for LinkedList<T> {
    #[inline]
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut list = Self::new();
        list.extend(iter);
        list
    }
}

impl<T> IntoIterator for LinkedList<T> {
    type IntoIter = IntoIter<T>;
    type Item = T;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        IntoIter { list: self }
    }
}

impl<T> Iterator for IntoIter<T> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<T> {
        let curr = self.list.head.load(Ordering::Acquire);
        if curr.is_null() {
            None
        } else {
            let next = unsafe { (*curr).next };
            self.list.head.store(next, Ordering::Relaxed);
            Some(unsafe { Box::from_raw(curr) }.value)
        }
    }
}

impl<T> FusedIterator for IntoIter<T> {}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.curr.is_null() {
            None
        } else {
            let curr = self.curr;
            self.curr = unsafe { (*self.curr).next };
            Some(unsafe { &mut (*curr).value })
        }
    }
}

impl<T> FusedIterator for IterMut<'_, T> {}

impl<T, F> DrainFilter<'_, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    /// Returns `true` if the iterator has reached the end of the linked list.
    #[inline]
    pub fn is_end(&self) -> bool {
        self.raw.is_end()
    }
}

impl<T, F> Iterator for DrainFilter<'_, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.raw.next().map(|node| unsafe { Box::from_raw(node).value })
    }
}

impl<T, F> FusedIterator for DrainFilter<'_, T, F> where F: FnMut(*mut Node<T>) -> bool {}

impl<T, F> DrainFilterRaw<'_, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    /// Returns `true` if the iterator has reached the end of the linked list.
    #[inline]
    pub fn is_end(&self) -> bool {
        self.curr.is_null()
    }

    fn cut_out(&mut self, next: *mut Node<T>) {
        if self.prev.is_null() {
            match self.head.compare_exchange(self.curr, next, Ordering::Relaxed, Ordering::Relaxed)
            {
                Ok(_) => return,
                Err(prev) => {
                    self.prev = prev;
                    while unsafe { (*self.prev).next } != self.curr {
                        self.prev = unsafe { (*self.prev).next };
                    }
                }
            }
        }
        unsafe { (*self.prev).next = next };
    }
}

impl<T, F> Iterator for DrainFilterRaw<'_, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    type Item = *mut Node<T>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        while !self.is_end() {
            let next = unsafe { (*self.curr).next };
            if (self.filter)(self.curr) {
                self.cut_out(next);
                let node = self.curr;
                self.curr = next;
                return Some(node);
            }
            self.prev = self.curr;
            self.curr = next;
        }
        None
    }
}

impl<T, F> Drop for DrainFilterRaw<'_, T, F>
where
    F: FnMut(*mut Node<T>) -> bool,
{
    #[inline]
    fn drop(&mut self) {
        self.for_each(drop);
    }
}

impl<T, F> FusedIterator for DrainFilterRaw<'_, T, F> where F: FnMut(*mut Node<T>) -> bool {}

impl<T> From<T> for Node<T> {
    #[inline]
    fn from(value: T) -> Self {
        Self { value, next: ptr::null_mut() }
    }
}

impl<T> Deref for Node<T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T> DerefMut for Node<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut T {
        &mut self.value
    }
}

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

    #[test]
    fn drain_filter_test() {
        let mut m: LinkedList<u32> = LinkedList::new();
        m.extend(&[1, 2, 3, 4, 5, 6]);
        let deleted = m.drain_filter(|v| *v < 4).collect::<Vec<_>>();

        assert_eq!(deleted, &[3, 2, 1]);
        assert_eq!(m.into_iter().collect::<Vec<_>>(), &[6, 5, 4]);
    }

    #[test]
    fn drain_to_empty_test() {
        let mut m: LinkedList<u32> = LinkedList::new();
        m.extend(&[1, 2, 3, 4, 5, 6]);
        let deleted = m.drain_filter(|_| true).collect::<Vec<_>>();

        assert_eq!(deleted, &[6, 5, 4, 3, 2, 1]);
        assert_eq!(m.into_iter().collect::<Vec<_>>(), &[]);
    }
}