-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Try replace callstack with a linked list #6747
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db68fac
Try replace callstack with a linked list
jfecher d427768
Fix unit tests
jfecher d304ebc
Clippy
jfecher 7ed0bf4
Format
jfecher dc9de60
Merge branch 'master' into jf/linked-list
TomAFrench 38e1efa
Reverse list iteration
jfecher 2ec21ff
Merge branch 'master' into jf/linked-list
jfecher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,187 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
/// A shared linked list type intended to be cloned | ||
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub struct List<T> { | ||
head: Arc<Node<T>>, | ||
len: usize, | ||
} | ||
|
||
#[derive(Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
enum Node<T> { | ||
#[default] | ||
Nil, | ||
Cons(T, Arc<Node<T>>), | ||
} | ||
|
||
impl<T> Default for List<T> { | ||
fn default() -> Self { | ||
List { head: Arc::new(Node::Nil), len: 0 } | ||
} | ||
} | ||
|
||
impl<T> List<T> { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// This is actually a push_front since we just create a new head for the | ||
/// list. This is done so that the tail of the list can still be shared. | ||
/// In the case of call stacks, the last node will be main, while the top | ||
/// of the call stack will be the head of this list. | ||
pub fn push_back(&mut self, value: T) { | ||
self.len += 1; | ||
self.head = Arc::new(Node::Cons(value, self.head.clone())); | ||
} | ||
|
||
/// It is more efficient to iterate from the head of the list towards the tail. | ||
/// For callstacks this means from the top of the call stack towards main. | ||
fn iter_rev(&self) -> IterRev<T> { | ||
IterRev { head: &self.head, len: self.len } | ||
} | ||
|
||
pub fn clear(&mut self) { | ||
*self = Self::default(); | ||
} | ||
|
||
pub fn append(&mut self, other: Self) | ||
where | ||
T: Copy + std::fmt::Debug, | ||
{ | ||
let other = other.into_iter().collect::<Vec<_>>(); | ||
|
||
for item in other { | ||
self.push_back(item); | ||
} | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.len | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.len == 0 | ||
} | ||
|
||
fn pop_front(&mut self) -> Option<T> | ||
where | ||
T: Copy, | ||
{ | ||
match self.head.as_ref() { | ||
Node::Nil => None, | ||
Node::Cons(value, rest) => { | ||
let value = *value; | ||
self.head = rest.clone(); | ||
self.len -= 1; | ||
Some(value) | ||
} | ||
} | ||
} | ||
|
||
pub fn truncate(&mut self, len: usize) | ||
where | ||
T: Copy, | ||
{ | ||
if self.len > len { | ||
for _ in 0..self.len - len { | ||
self.pop_front(); | ||
} | ||
} | ||
} | ||
|
||
pub fn unit(item: T) -> Self { | ||
let mut this = Self::default(); | ||
this.push_back(item); | ||
this | ||
} | ||
|
||
pub fn back(&self) -> Option<&T> { | ||
match self.head.as_ref() { | ||
Node::Nil => None, | ||
Node::Cons(item, _) => Some(item), | ||
} | ||
} | ||
} | ||
|
||
pub struct IterRev<'a, T> { | ||
head: &'a Node<T>, | ||
len: usize, | ||
} | ||
|
||
impl<T> IntoIterator for List<T> | ||
where | ||
T: Copy + std::fmt::Debug, | ||
{ | ||
type Item = T; | ||
|
||
type IntoIter = std::iter::Rev<std::vec::IntoIter<T>>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
let items: Vec<_> = self.iter_rev().copied().collect(); | ||
items.into_iter().rev() | ||
} | ||
} | ||
|
||
impl<'a, T> IntoIterator for &'a List<T> { | ||
type Item = &'a T; | ||
|
||
type IntoIter = std::iter::Rev<<Vec<&'a T> as IntoIterator>::IntoIter>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
let items: Vec<_> = self.iter_rev().collect(); | ||
items.into_iter().rev() | ||
} | ||
} | ||
|
||
impl<'a, T> Iterator for IterRev<'a, T> { | ||
type Item = &'a T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match self.head { | ||
Node::Nil => None, | ||
Node::Cons(value, rest) => { | ||
self.head = rest; | ||
Some(value) | ||
} | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(0, Some(self.len)) | ||
} | ||
} | ||
|
||
impl<'a, T> ExactSizeIterator for IterRev<'a, T> {} | ||
|
||
impl<T> std::fmt::Debug for List<T> | ||
where | ||
T: std::fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "[")?; | ||
for (i, item) in self.iter_rev().enumerate() { | ||
if i != 0 { | ||
write!(f, ", ")?; | ||
} | ||
write!(f, "{item:?}")?; | ||
} | ||
write!(f, "]") | ||
} | ||
} | ||
|
||
impl<T> std::fmt::Display for List<T> | ||
where | ||
T: std::fmt::Display, | ||
{ | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "[")?; | ||
for (i, item) in self.iter_rev().enumerate() { | ||
if i != 0 { | ||
write!(f, ", ")?; | ||
} | ||
write!(f, "{item}")?; | ||
} | ||
write!(f, "]") | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both
push_back
andpop_front
operate on the head; I understand what you meant bypush_back
actually being a "push_front", but wouldn't it be better to name both of these head operations to be the same? I thoughtpop_front
will go to the opposite end frompush_back
in an O(n) operation. Why not justpush
andpop
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's my mistake. They were originally both
*_back
to make it more easy for me to migrate existing code, then I changedpop_front
to be the front to be more accurate but thought renaming the publicpush_back
to front as well could be confusing to CallStack users who want to add something to the conceptual "back" of the structure, unaware that the structure is essentially reversed internally.I'd be for renaming it back to pop_back.