Skip to content

Instantly share code, notes, and snippets.

View DonghyungKo's full-sized avatar
🎯
Focusing

DonghyungKo DonghyungKo

🎯
Focusing
View GitHub Profile
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
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!(
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;
pub struct Unique<T: ?Sized> {
ptr: *const T,
_marker: PhantomData<T>,
}
pub struct Vec<T> {
ptr: *const T,
cap: usize,
len: usize,
_marker: PhantomData<T>
}
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]
// 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)
// 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]
// 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)
}
// 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]