-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmitry Dygalo <[email protected]>
- Loading branch information
1 parent
c9eac5c
commit 7bff3d3
Showing
7 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::sync::Arc; | ||
|
||
pub struct List<T> { | ||
head: Option<Arc<Node<T>>>, | ||
} | ||
|
||
impl<T: Clone> Clone for List<T> { | ||
fn clone(&self) -> Self { | ||
List { | ||
head: self.head.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<T> Drop for List<T> { | ||
fn drop(&mut self) { | ||
let mut current = self.head.take(); | ||
while let Some(node) = current { | ||
if let Ok(mut node) = Arc::try_unwrap(node) { | ||
current = node.next.take(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T> List<T> { | ||
pub(crate) fn new() -> Self { | ||
Self { head: None } | ||
} | ||
#[must_use] | ||
pub fn is_empty(&self) -> bool { | ||
self.head.is_none() | ||
} | ||
#[must_use] | ||
pub fn push_front(&self, value: T) -> Self { | ||
List { | ||
head: Some(Arc::new(Node { | ||
value, | ||
next: self.head.clone(), | ||
})), | ||
} | ||
} | ||
#[must_use] | ||
pub fn iter(&self) -> Iter<'_, T> { | ||
Iter { | ||
current: self.head.as_ref(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Node<T> { | ||
value: T, | ||
next: Option<Arc<Node<T>>>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Iter<'a, T> { | ||
current: Option<&'a Arc<Node<T>>>, | ||
} | ||
|
||
impl<'a, T> Iterator for Iter<'a, T> { | ||
type Item = &'a T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.current.map(|current| { | ||
let value = ¤t.value; | ||
self.current = current.next.as_ref(); | ||
value | ||
}) | ||
} | ||
} | ||
|
||
impl<'a, T> IntoIterator for &'a List<T> { | ||
type IntoIter = Iter<'a, T>; | ||
type Item = &'a T; | ||
fn into_iter(self) -> Self::IntoIter { | ||
self.iter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters