init: 初始化rust项目
This commit is contained in:
29
src/util/linked_list.rs
Normal file
29
src/util/linked_list.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>,
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
ListNode { next: None, val }
|
||||
}
|
||||
}
|
||||
|
||||
// helper function for test
|
||||
pub fn to_list(vec: Vec<i32>) -> Option<Box<ListNode>> {
|
||||
let mut current = None;
|
||||
for &v in vec.iter().rev() {
|
||||
let mut node = ListNode::new(v);
|
||||
node.next = current;
|
||||
current = Some(Box::new(node));
|
||||
}
|
||||
current
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! linked {
|
||||
($($e:expr),*) => {to_list(vec![$($e.to_owned()), *])};
|
||||
($($e:expr,)*) => {to_list(vec![$($e.to_owned()), *])};
|
||||
}
|
8
src/util/mod.rs
Normal file
8
src/util/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#[macro_use]
|
||||
pub mod linked_list;
|
||||
#[macro_use]
|
||||
pub mod vec_string;
|
||||
#[macro_use]
|
||||
pub mod tree;
|
||||
#[macro_use]
|
||||
pub mod point;
|
23
src/util/point.rs
Normal file
23
src/util/point.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Point {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
#[inline]
|
||||
pub fn new(x: i32, y: i32) -> Self {
|
||||
Point { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! point {
|
||||
($($e:expr),*) => {
|
||||
{
|
||||
let vec = vec![$($e.to_owned()), *];
|
||||
Point::new(vec[0], vec[1])
|
||||
}
|
||||
};
|
||||
($($e:expr,)*) => (point![$($x),*])
|
||||
}
|
1
src/util/testing.rs
Normal file
1
src/util/testing.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
57
src/util/tree.rs
Normal file
57
src/util/tree.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
TreeNode {
|
||||
val,
|
||||
left: None,
|
||||
right: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_tree(vec: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
use std::collections::VecDeque;
|
||||
let head = Some(Rc::new(RefCell::new(TreeNode::new(vec[0].unwrap()))));
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back(head.as_ref().unwrap().clone());
|
||||
|
||||
for children in vec[1..].chunks(2) {
|
||||
let parent = queue.pop_front().unwrap();
|
||||
if let Some(v) = children[0] {
|
||||
parent.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(v))));
|
||||
queue.push_back(parent.borrow().left.as_ref().unwrap().clone());
|
||||
}
|
||||
if children.len() > 1 {
|
||||
if let Some(v) = children[1] {
|
||||
parent.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(v))));
|
||||
queue.push_back(parent.borrow().right.as_ref().unwrap().clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
head
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tree {
|
||||
() => {
|
||||
None
|
||||
};
|
||||
($($e:expr),*) => {
|
||||
{
|
||||
let vec = vec![$(stringify!($e)), *];
|
||||
let vec = vec.into_iter().map(|v| v.parse::<i32>().ok()).collect::<Vec<_>>();
|
||||
to_tree(vec)
|
||||
}
|
||||
};
|
||||
($($e:expr,)*) => {(tree![$($e),*])};
|
||||
}
|
5
src/util/vec_string.rs
Normal file
5
src/util/vec_string.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
#[macro_export]
|
||||
macro_rules! vec_string {
|
||||
($($e:expr),*) => {vec![$($e.to_owned()), *]};
|
||||
($($e:expr,)*) => {vec![$($e.to_owned()), *]};
|
||||
}
|
Reference in New Issue
Block a user