Skip to content

Commit

Permalink
idomatic structure rust code, no include macro (#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
rongyi authored Oct 23, 2024
1 parent 0c60037 commit ef3010b
Show file tree
Hide file tree
Showing 47 changed files with 57 additions and 80 deletions.
3 changes: 1 addition & 2 deletions codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
use rand::Rng;

/* 随机访问元素 */
Expand Down
4 changes: 1 addition & 3 deletions codes/rust/chapter_array_and_linkedlist/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use list_node::ListNode;
use hello_algo_rust::include::{print_util, ListNode};
use std::cell::RefCell;
use std::rc::Rc;

Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_array_and_linkedlist/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Created Time: 2023-01-18
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* Driver Code */
fn main() {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_array_and_linkedlist/my_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 列表类 */
#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序遍历:例题一 */
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序遍历:例题二 */
fn pre_order(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序遍历:例题三 */
fn pre_order(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 判断当前状态是否为解 */
fn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use list_node::ListNode;
use hello_algo_rust::include::{print_util, ListNode, TreeNode};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use tree_node::TreeNode;

/* 函数 */
fn function() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
use rand::seq::SliceRandom;
use rand::thread_rng;

Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_divide_and_conquer/build_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* Author: codingonion ([email protected])
*/

use hello_algo_rust::include::{print_util, TreeNode};
use std::collections::HashMap;
use std::{cell::RefCell, rc::Rc};
include!("../include/include.rs");
use tree_node::TreeNode;

/* 构建二叉树:分治 */
fn dfs(
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_graph/graph_adjacency_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/vertex.rs");
pub use hello_algo_rust::include::{vals_to_vets, vets_to_vals, Vertex};

use std::collections::HashMap;

Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_hashing/build_in_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* Author: WSL0809 ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::ListNode;

use crate::list_node::ListNode;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_hashing/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_heap/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::collections::BinaryHeap;

Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_heap/my_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 大顶堆 */
struct MaxHeap {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_heap/top_k.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::cmp::Reverse;
use std::collections::BinaryHeap;
Expand Down
4 changes: 1 addition & 3 deletions codes/rust/chapter_searching/hashing_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use list_node::ListNode;
use hello_algo_rust::include::ListNode;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
Expand Down
4 changes: 1 addition & 3 deletions codes/rust/chapter_searching/linear_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use list_node::ListNode;
use hello_algo_rust::include::ListNode;
use std::cell::RefCell;
use std::rc::Rc;

Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_searching/two_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
use std::collections::HashMap;

/* 方法一:暴力枚举 */
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/bubble_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 冒泡排序 */
fn bubble_sort(nums: &mut [i32]) {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/bucket_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 桶排序 */
fn bucket_sort(nums: &mut [f64]) {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/counting_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 计数排序 */
// 简单实现,无法用于排序对象
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/heap_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
fn sift_down(nums: &mut [i32], n: usize, mut i: usize) {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/insertion_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: xBLACKICEx ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 插入排序 */
fn insertion_sort(nums: &mut [i32]) {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/radix_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
fn digit(num: i32, exp: i32) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/selection_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: WSL0809 ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 选择排序 */
fn selection_sort(nums: &mut [i32]) {
Expand Down
4 changes: 1 addition & 3 deletions codes/rust/chapter_stack_and_queue/array_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* Created Time: 2023-03-11
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
/* 基于环形数组实现的双向队列 */
struct ArrayDeque {
nums: Vec<i32>, // 用于存储双向队列元素的数组
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_stack_and_queue/array_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: WSL0809 ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 基于数组实现的栈 */
struct ArrayStack<T> {
Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_stack_and_queue/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Author: codingonion ([email protected]), xBLACKICEx ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
use std::collections::VecDeque;

/* Driver Code */
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_stack_and_queue/linkedlist_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::cell::RefCell;
use std::rc::Rc;
Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_stack_and_queue/linkedlist_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::{print_util, ListNode};

use list_node::ListNode;
use std::cell::RefCell;
use std::rc::Rc;

Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_stack_and_queue/linkedlist_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::{print_util, ListNode};

use list_node::ListNode;
use std::cell::RefCell;
use std::rc::Rc;

Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_stack_and_queue/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected]), xBLACKICEx ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::collections::VecDeque;

Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_stack_and_queue/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* Driver Code */
pub fn main() {
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_tree/array_binary_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::{print_util, tree_node};

/* 数组表示下的二叉树类 */
struct ArrayBinaryTree {
Expand Down
3 changes: 1 addition & 2 deletions codes/rust/chapter_tree/avl_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::{print_util, TreeNode};

use std::cell::RefCell;
use std::cmp::Ordering;
use std::rc::Rc;
use tree_node::TreeNode;

type OptionTreeNodeRc = Option<Rc<RefCell<TreeNode>>>;

Expand Down
4 changes: 2 additions & 2 deletions codes/rust/chapter_tree/binary_search_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* Author: xBLACKICEx ([email protected])、night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::cell::RefCell;
use std::cmp::Ordering;
use std::rc::Rc;

use tree_node::TreeNode;
use hello_algo_rust::include::TreeNode;

type OptionTreeNodeRc = Option<Rc<RefCell<TreeNode>>>;

Expand Down
Loading

0 comments on commit ef3010b

Please sign in to comment.