Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
henke443 committed Apr 18, 2024
1 parent 8009860 commit 77c5e6e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion benches/fast_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(feature = "unstable")]
use linked_list::{LinkedListWalker, Walker};

use fast_list::{LinkedList as FastLinkedList};
use fast_list::LinkedList as FastLinkedList;
use std::collections::LinkedList as StdLinkedList;
use std::collections::VecDeque;
use std::vec::Vec;
Expand Down
11 changes: 2 additions & 9 deletions src/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,12 @@ impl<T> LinkedList<T> {
}

/// Returns the next index of the item with the given index.
pub fn cursor_next(
&self,
item: LinkedListIndex,
) -> Option<LinkedListIndex> {
pub fn cursor_next(&self, item: LinkedListIndex) -> Option<LinkedListIndex> {
self.items.get(item).and_then(|item| item.next_index)
}

/// Returns the previous index of the item with the given index.
pub fn cursor_prev(
&self,
item: LinkedListIndex,
) -> Option<LinkedListIndex> {
pub fn cursor_prev(&self, item: LinkedListIndex) -> Option<LinkedListIndex> {
self.items.get(item).and_then(|item| item.next_index)
}

Expand Down Expand Up @@ -407,7 +401,6 @@ impl<T> LinkedList<T> {
// }
let mut current = index;


let first = self.remove(index);
if let Some(first) = first {
new_list.push_back(first.value);
Expand Down
21 changes: 10 additions & 11 deletions tests/test_linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,49 @@ fn test_readme_example_1() {
assert_eq!(list.head().unwrap().value, 42);

assert_eq!(list.iter().count(), 102); // 101 items from our range, one item from push_back
// These two iterators are equivalent
// These two iterators are equivalent
assert_eq!(
list.iter_next(list.head.unwrap()).count(),
list.iter_next(list.head.unwrap()).count(),
list.iter().count()
);
}

#[test]
fn test_readme_example_2() {
use fast_list::LinkedList;
use std::thread;
use std::sync::{Arc, Mutex};

use std::thread;

let mut list = LinkedList::new();
let indexes = Arc::new(list.extend(0..10_000));

// You can also get the ordered indexes with something like this:
// let indexes = Arc::new(
// list.cursor_next(list.head.unwrap()).collect::<Vec<_>>()
// );

let list_mut = Arc::new(Mutex::new(list));

let mut threads = Vec::new();
for _ in 0..3 {
let list_mut = Arc::clone(&list_mut);
let indexes = Arc::clone(&indexes);
let t = thread::spawn(move || {
for index in indexes.iter().take(9_000) {
for index in indexes.iter().take(9_000) {
list_mut.lock().unwrap().remove(*index); // returns None if the index does not exist
}
});
threads.push(t);
}

for t in threads {
t.join().unwrap();
}

// Even though remove() is called 9000*3 times, only 9000 items are removed.
{
assert_eq!(list_mut.lock().unwrap().head().unwrap().value, 9_000);
}

}

#[test]
Expand Down

0 comments on commit 77c5e6e

Please sign in to comment.