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
import argparse | |
parser = argparse.ArgumentParser(description="Sum two non-negative integers") | |
parser.add_argument("a", type=int, help="first non-negative integer") | |
parser.add_argument("b", type=int, help="second non-negative integer") | |
args = parser.parse_args() | |
if __name__ == "__main__": | |
a:int = args.a |
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
pub struct Vec<T> { | |
ptr: Unique<T>, | |
cap: usize, | |
len: usize, | |
} | |
impl<T> Vec<T> { | |
/// Create a Vec with Unique<T> which owns a dangling pointer | |
fn new() -> Self { | |
assert!( |
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
pub struct Unique<T: ?Sized> { | |
ptr: *const T, | |
_marker: PhantomData<T>, | |
} | |
impl<T: Sized> Unique<T> { | |
/// Creates a new `Unique` that is dangling, but well-aligned | |
/// This is useful for initializing types which lazily allocate, like `Vec` | |
fn empty() -> Self { | |
let ptr = std::mem::align_of::<T>() as *mut T; |
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
pub struct Unique<T: ?Sized> { | |
ptr: *const T, | |
_marker: PhantomData<T>, | |
} |
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
pub struct Vec<T> { | |
ptr: *const T, | |
cap: usize, | |
len: usize, | |
_marker: PhantomData<T> | |
} |
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
impl<T> LinkedList<T> { | |
/// Pop element from the back of LinkedList, or return `None` if empty | |
#[inline] | |
pub fn pop_back(&mut self) -> Option<T> { | |
self.pop_back_node().map(Node::into_element) | |
} | |
/// Pop node from the back of LinkedList, or return `None` if empty | |
/// This will unlink the last node from LinkedList | |
#[inline] |
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
// getter methods (by index) | |
impl<T> LinkedList<T> { | |
/// Provides a reference to i-th element or `None` if i-th node does not exists | |
#[inline] | |
pub fn get(&mut self, index: u32) -> Option<&T> { | |
if index > self.length { | |
return None; | |
} | |
self.get_ith_node(self.head, index) |
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
// getter methods (front/back) | |
impl<T> LinkedList<T> { | |
/// Provides a reference to the front element or `None` LinkedList is empty | |
#[inline] | |
pub fn front(&self) -> Option<&T> { | |
unsafe { self.head.as_ref().map(|node| &(node.as_ref().val)) } | |
} | |
/// Provides a mutable reference to the front element or `None` LinkedList is empty | |
#[inline] |
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
// setter methods (by index) | |
impl<T> LinkedList<T> { | |
/// Insert node to i-th index of LinkedList. | |
/// If given index if bigger than length of LinkedList, | |
/// Node will be attached to the tail of LinkedList | |
#[inline] | |
pub fn insert(&mut self, val: T, index: u32) { | |
self.insert_node(Box::new(Node::new(val)), index) | |
} |
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
// setter methods | |
impl<T> LinkedList<T> { | |
/// Appends element to the back. | |
#[inline] | |
pub fn push_back(&mut self, val: T) { | |
self.push_back_node(Box::new(Node::new(val))) | |
} | |
/// Appends element to the front | |
#[inline] |
NewerOlder