mirror of
https://github.com/Noratrieb/datastructures.git
synced 2026-01-14 09:25:01 +01:00
fix clippy lints
This commit is contained in:
parent
30472e36e7
commit
b88ec9ac10
4 changed files with 61 additions and 42 deletions
|
|
@ -37,7 +37,7 @@ fn create_random_packed_list_128(size: usize) -> PackedLinkedList<i32, 128> {
|
|||
|
||||
fn push_back(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("push_back");
|
||||
for i in [100, 1_0000_00].iter() {
|
||||
for i in [100, 1_000_000].iter() {
|
||||
group.bench_with_input(BenchmarkId::new("create_random_list", i), i, |b, i| {
|
||||
b.iter(|| create_random_list(*i))
|
||||
});
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ impl<T: Display + Debug> DisplayTree for Node<T> {
|
|||
|
||||
let mut current_nodes = vec![self];
|
||||
|
||||
while current_nodes.len() > 0 {
|
||||
while !current_nodes.is_empty() {
|
||||
// display node layer
|
||||
|
||||
let mut offset = 0;
|
||||
|
|
|
|||
|
|
@ -222,6 +222,13 @@ impl<T> LinkedList<T> {
|
|||
self.iter().count()
|
||||
}
|
||||
|
||||
/// Checks whether the list is empty
|
||||
///
|
||||
/// See [LinkedList::len]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Returns an iterator over the items
|
||||
pub fn iter(&self) -> Iter<T> {
|
||||
Iter::new(self)
|
||||
|
|
@ -231,11 +238,6 @@ impl<T> LinkedList<T> {
|
|||
pub fn iter_mut(&mut self) -> IterMut<T> {
|
||||
IterMut::new(self)
|
||||
}
|
||||
|
||||
/// Returns an iterator owning the items
|
||||
pub fn into_iter(self) -> IntoIter<T> {
|
||||
IntoIter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
/////
|
||||
|
|
@ -278,11 +280,20 @@ impl<T: PartialEq> PartialEq for LinkedList<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> IntoIterator for LinkedList<T> {
|
||||
type Item = T;
|
||||
type IntoIter = IntoIter<Self::Item>;
|
||||
|
||||
/// Returns an iterator owning the items
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
IntoIter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FromIterator<T> for LinkedList<T> {
|
||||
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
||||
let mut iter = iter.into_iter();
|
||||
let mut list = Self::new();
|
||||
while let Some(item) = iter.next() {
|
||||
for item in iter {
|
||||
list.push_back(item)
|
||||
}
|
||||
list
|
||||
|
|
@ -291,8 +302,7 @@ impl<T> FromIterator<T> for LinkedList<T> {
|
|||
|
||||
impl<T> Extend<T> for LinkedList<T> {
|
||||
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
||||
let mut iter = iter.into_iter();
|
||||
while let Some(item) = iter.next() {
|
||||
for item in iter {
|
||||
self.push_back(item)
|
||||
}
|
||||
}
|
||||
|
|
@ -339,10 +349,10 @@ impl<T> Node<T> {
|
|||
next: self.next,
|
||||
prev: NonNull::new(self as _),
|
||||
}));
|
||||
self.next.map(|mut next| {
|
||||
if let Some(mut next) = self.next {
|
||||
// SAFETY: All pointers should always be valid and created from a box
|
||||
unsafe { next.as_mut() }.prev = new_node
|
||||
});
|
||||
unsafe { next.as_mut() }.prev = new_node;
|
||||
}
|
||||
self.next = new_node;
|
||||
}
|
||||
|
||||
|
|
@ -353,10 +363,10 @@ impl<T> Node<T> {
|
|||
next: NonNull::new(self as _),
|
||||
prev: self.prev,
|
||||
}));
|
||||
self.prev.map(|mut next| {
|
||||
if let Some(mut next) = self.prev {
|
||||
// SAFETY: All pointers should always be valid and created from a box
|
||||
unsafe { next.as_mut() }.next = new_node
|
||||
});
|
||||
unsafe { next.as_mut() }.next = new_node;
|
||||
}
|
||||
self.prev = new_node;
|
||||
}
|
||||
|
||||
|
|
@ -449,7 +459,7 @@ impl<T> IntoIter<T> {
|
|||
|
||||
impl<T> Drop for IntoIter<T> {
|
||||
fn drop(&mut self) {
|
||||
while let Some(_) = self.next() {}
|
||||
for _ in self {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -59,6 +59,11 @@ impl<T, const COUNT: usize> PackedLinkedList<T, COUNT> {
|
|||
self.len
|
||||
}
|
||||
|
||||
// Whether the list is empty (O(1))
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Pushes a new value to the front of the list
|
||||
pub fn push_front(&mut self, element: T) {
|
||||
// SAFETY: All pointers should always point to valid memory,
|
||||
|
|
@ -109,9 +114,11 @@ impl<T, const COUNT: usize> PackedLinkedList<T, COUNT> {
|
|||
if node.size == 1 {
|
||||
// the last item, deallocate it
|
||||
let mut boxed = Box::from_raw(first.as_ptr());
|
||||
boxed.next.as_mut().map(|next| next.as_mut().prev = None);
|
||||
if let Some(next) = boxed.next.as_mut() {
|
||||
next.as_mut().prev = None;
|
||||
}
|
||||
self.first = boxed.next;
|
||||
if let None = self.first {
|
||||
if self.first.is_none() {
|
||||
// if this node was the last one, also remove it from the tail pointer
|
||||
self.last = None;
|
||||
}
|
||||
|
|
@ -143,12 +150,11 @@ impl<T, const COUNT: usize> PackedLinkedList<T, COUNT> {
|
|||
if node.size == 1 {
|
||||
// the last item, deallocate it
|
||||
let mut boxed = Box::from_raw(last.as_ptr());
|
||||
boxed
|
||||
.prev
|
||||
.as_mut()
|
||||
.map(|previous| previous.as_mut().next = None);
|
||||
if let Some(previous) = boxed.prev.as_mut() {
|
||||
previous.as_mut().next = None;
|
||||
}
|
||||
self.last = boxed.prev;
|
||||
if let None = self.last {
|
||||
if self.last.is_none() {
|
||||
// if this node was the last one, also remove it from the tail pointer
|
||||
self.first = None;
|
||||
}
|
||||
|
|
@ -209,38 +215,42 @@ impl<T, const COUNT: usize> PackedLinkedList<T, COUNT> {
|
|||
iter::IterMut::new(self)
|
||||
}
|
||||
|
||||
pub fn into_iter(self) -> iter::IntoIter<T, COUNT> {
|
||||
iter::IntoIter::new(self)
|
||||
}
|
||||
|
||||
fn insert_node_start(&mut self) {
|
||||
let node = Some(allocate_nonnull(Node::new(None, self.first)));
|
||||
self.first
|
||||
.as_mut()
|
||||
.map(|first| unsafe { first.as_mut().prev = node });
|
||||
if let Some(first) = self.first.as_mut() {
|
||||
unsafe { first.as_mut().prev = node };
|
||||
}
|
||||
self.first = node;
|
||||
if let None = self.last {
|
||||
if self.last.is_none() {
|
||||
self.last = node;
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_node_end(&mut self) {
|
||||
let node = Some(allocate_nonnull(Node::new(self.last, None)));
|
||||
self.last
|
||||
.as_mut()
|
||||
.map(|last| unsafe { last.as_mut().next = node });
|
||||
if let Some(last) = self.last.as_mut() {
|
||||
unsafe { last.as_mut().next = node };
|
||||
}
|
||||
self.last = node;
|
||||
if let None = self.first {
|
||||
if self.first.is_none() {
|
||||
self.first = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const COUNT: usize> IntoIterator for PackedLinkedList<T, COUNT> {
|
||||
type Item = T;
|
||||
type IntoIter = iter::IntoIter<Self::Item, COUNT>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
iter::IntoIter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const COUNT: usize> FromIterator<T> for PackedLinkedList<T, COUNT> {
|
||||
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
||||
let mut list = PackedLinkedList::new();
|
||||
let mut iter = iter.into_iter();
|
||||
while let Some(item) = iter.next() {
|
||||
for item in iter {
|
||||
list.push_back(item);
|
||||
}
|
||||
list
|
||||
|
|
@ -249,8 +259,7 @@ impl<T, const COUNT: usize> FromIterator<T> for PackedLinkedList<T, COUNT> {
|
|||
|
||||
impl<T, const COUNT: usize> Extend<T> for PackedLinkedList<T, COUNT> {
|
||||
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
||||
let mut iter = iter.into_iter();
|
||||
while let Some(item) = iter.next() {
|
||||
for item in iter {
|
||||
self.push_back(item);
|
||||
}
|
||||
}
|
||||
|
|
@ -675,7 +684,7 @@ mod iter {
|
|||
|
||||
impl<T, const COUNT: usize> Drop for IntoIter<T, COUNT> {
|
||||
fn drop(&mut self) {
|
||||
while let Some(_) = self.next() {}
|
||||
for _ in self {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue