Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jan 17, 2024
1 parent 2a4ff75 commit 9fdc455
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 58 deletions.
61 changes: 6 additions & 55 deletions data_structures/src/list/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ pub struct DoublyLinkedList<T> {

pub struct IntoIter<T>(DoublyLinkedList<T>);

pub struct Iter<'a, T: 'a> {
length: usize,
head: NodePtr<T>,
tail: NodePtr<T>,
marker: PhantomData<&'a Node<T>>,
}

impl<T> Node<T> {
#[must_use]
pub fn new(value: T) -> Rc<RefCell<Self>> {
Expand Down Expand Up @@ -175,22 +168,14 @@ impl<T> DoublyLinkedList<T> {
.as_ref()
.map(|node| RefMut::map(node.borrow_mut(), |node| &mut node.value))
}
}

#[inline]
#[must_use]
pub fn into_iter(self) -> IntoIter<T> {
IntoIter(self)
}
impl<T> IntoIterator for DoublyLinkedList<T> {
type Item = T;
type IntoIter = IntoIter<T>;

#[inline]
#[must_use]
pub fn iter(&self) -> Iter<T> {
Iter {
length: self.length,
head: self.head.clone(),
tail: self.tail.clone(),
marker: PhantomData,
}
fn into_iter(self) -> Self::IntoIter {
IntoIter(self)
}
}

Expand All @@ -202,40 +187,6 @@ impl<T> Drop for DoublyLinkedList<T> {
}
}

/*
impl<'a, T> Iterator for Iter<'a, T> {
type Item = Ref<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
self.head.take().map(|old_head| {
if let Some(new_head) = old_head.borrow_mut().next.take() {
self.head = Some(new_head);
} else {
self.tail.take();
}
self.length -= 1;
Ref::map(old_head.borrow(), |node| &node.value)
})
}
}
impl<T: Clone> DoubleEndedIterator for Iter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
let mut result = None;
self.current = self
.current
.as_ref()
.and_then(|current: &Rc<RefCell<Node<T>>>| {
// TOOD(Shaohua): Replace with try_borrow()
let current: Ref<'_, Node<T>> = current.borrow();
result = Some(current.value.clone());
current.previous.clone()
});
result
}
}
*/

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

Expand Down
18 changes: 18 additions & 0 deletions data_structures/src/list/single_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ impl<T> Iterator for IntoIter<T> {
}
}

impl<'a, T> IntoIterator for &'a LinkedListV1<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

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

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions leetcode/0001.two-sum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ fn solution2(nums: Vec<i32>, target: i32) -> Vec<i32> {

fn solution3(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut visited = HashMap::with_capacity(nums.len());
for i in 0..nums.len() {
if let Some(&j) = visited.get(&(target - nums[i])) {
for (i, item) in nums.iter().enumerate() {
if let Some(&j) = visited.get(&(target - item)) {
return vec![j as i32, i as i32];
} else {
visited.insert(nums[i], i);
visited.insert(item, i);
}
}

Expand Down

0 comments on commit 9fdc455

Please sign in to comment.