Skip to content

Commit fea7725

Browse files
authored
Replace unnecessary owned-type to borrowed-type (TheAlgorithms#525)
1 parent e6be8cc commit fea7725

22 files changed

Lines changed: 116 additions & 189 deletions

src/data_structures/lazy_segment_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<T: Debug + Default + Ord + Copy + Display + AddAssign + Add<Output = T>> La
114114
return;
115115
}
116116

117-
let lazy = self.lazy[idx].unwrap_or(T::default());
117+
let lazy = self.lazy[idx].unwrap_or_default();
118118
self.lazy[idx] = None;
119119

120120
let mid = (element_range.start + element_range.end) / 2;

src/data_structures/rb_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ mod tests {
616616
#[test]
617617
fn find() {
618618
let mut tree = RBTree::<usize, char>::new();
619-
for (k, v) in String::from("hello, world!").chars().enumerate() {
619+
for (k, v) in "hello, world!".chars().enumerate() {
620620
tree.insert(k, v);
621621
}
622622
assert_eq!(*tree.find(&3).unwrap_or(&'*'), 'l');
@@ -628,7 +628,7 @@ mod tests {
628628
#[test]
629629
fn insert() {
630630
let mut tree = RBTree::<usize, char>::new();
631-
for (k, v) in String::from("hello, world!").chars().enumerate() {
631+
for (k, v) in "hello, world!".chars().enumerate() {
632632
tree.insert(k, v);
633633
}
634634
let s: String = tree.iter().map(|x| x.value).collect();
@@ -638,7 +638,7 @@ mod tests {
638638
#[test]
639639
fn delete() {
640640
let mut tree = RBTree::<usize, char>::new();
641-
for (k, v) in String::from("hello, world!").chars().enumerate() {
641+
for (k, v) in "hello, world!".chars().enumerate() {
642642
tree.insert(k, v);
643643
}
644644
tree.delete(&1);

src/data_structures/trie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
{
3434
let mut node = &mut self.root;
3535
for c in key.into_iter() {
36-
node = node.children.entry(c).or_insert_with(Node::default);
36+
node = node.children.entry(c).or_default();
3737
}
3838
node.value = Some(value);
3939
}

src/dynamic_programming/is_subsequence.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// by deleting some (can be none) of the characters without disturbing the relative
44
// positions of the remaining characters.
55
// (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
6-
pub fn is_subsequence(str1: String, str2: String) -> bool {
6+
pub fn is_subsequence(str1: &str, str2: &str) -> bool {
77
let mut it1 = 0;
88
let mut it2 = 0;
99

@@ -27,7 +27,7 @@ mod tests {
2727

2828
#[test]
2929
fn test() {
30-
assert!(is_subsequence(String::from("abc"), String::from("ahbgdc")));
31-
assert!(!is_subsequence(String::from("axc"), String::from("ahbgdc")));
30+
assert!(is_subsequence("abc", "ahbgdc"));
31+
assert!(!is_subsequence("axc", "ahbgdc"));
3232
}
3333
}

src/dynamic_programming/longest_common_substring.rs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Longest common substring via Dynamic Programming
22
// longest_common_substring(a, b) returns the length of longest common substring between the strings a and b.
3-
pub fn longest_common_substring(text1: String, text2: String) -> i32 {
3+
pub fn longest_common_substring(text1: &str, text2: &str) -> i32 {
44
let m = text1.len();
55
let n = text2.len();
66

@@ -32,74 +32,44 @@ mod tests {
3232

3333
#[test]
3434
fn test1() {
35-
assert_eq!(
36-
longest_common_substring(String::from(""), String::from("")),
37-
0
38-
);
35+
assert_eq!(longest_common_substring("", ""), 0);
3936
}
4037
#[test]
4138
fn test2() {
42-
assert_eq!(
43-
longest_common_substring(String::from("a"), String::from("")),
44-
0
45-
);
39+
assert_eq!(longest_common_substring("a", ""), 0);
4640
}
4741
#[test]
4842
fn test3() {
49-
assert_eq!(
50-
longest_common_substring(String::from(""), String::from("a")),
51-
0
52-
);
43+
assert_eq!(longest_common_substring("", "a"), 0);
5344
}
5445
#[test]
5546
fn test4() {
56-
assert_eq!(
57-
longest_common_substring(String::from("a"), String::from("a")),
58-
1
59-
);
47+
assert_eq!(longest_common_substring("a", "a"), 1);
6048
}
6149
#[test]
6250
fn test5() {
63-
assert_eq!(
64-
longest_common_substring(String::from("abcdef"), String::from("bcd")),
65-
3
66-
);
51+
assert_eq!(longest_common_substring("abcdef", "bcd"), 3);
6752
}
6853
#[test]
6954
fn test6() {
70-
assert_eq!(
71-
longest_common_substring(String::from("abcdef"), String::from("xabded")),
72-
2
73-
);
55+
assert_eq!(longest_common_substring("abcdef", "xabded"), 2);
7456
}
7557
#[test]
7658
fn test7() {
77-
assert_eq!(
78-
longest_common_substring(String::from("GeeksforGeeks"), String::from("GeeksQuiz")),
79-
5
80-
);
59+
assert_eq!(longest_common_substring("GeeksforGeeks", "GeeksQuiz"), 5);
8160
}
8261
#[test]
8362
fn test8() {
84-
assert_eq!(
85-
longest_common_substring(String::from("abcdxyz"), String::from("xyzabcd")),
86-
4
87-
);
63+
assert_eq!(longest_common_substring("abcdxyz", "xyzabcd"), 4);
8864
}
8965
#[test]
9066
fn test9() {
91-
assert_eq!(
92-
longest_common_substring(String::from("zxabcdezy"), String::from("yzabcdezx")),
93-
6
94-
);
67+
assert_eq!(longest_common_substring("zxabcdezy", "yzabcdezx"), 6);
9568
}
9669
#[test]
9770
fn test10() {
9871
assert_eq!(
99-
longest_common_substring(
100-
String::from("OldSite:GeeksforGeeks.org"),
101-
String::from("NewSite:GeeksQuiz.com")
102-
),
72+
longest_common_substring("OldSite:GeeksforGeeks.org", "NewSite:GeeksQuiz.com"),
10373
10
10474
);
10575
}

src/general/huffman_encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<T> PartialEq for HuffmanNode<T> {
2828

2929
impl<T> PartialOrd for HuffmanNode<T> {
3030
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31-
Some(self.frequency.cmp(&other.frequency).reverse())
31+
Some(self.cmp(other))
3232
}
3333
}
3434

src/graph/astar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<V: Ord + Copy, E: Ord + Copy> PartialOrd for Candidate<V, E> {
1818
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1919
// Note the inverted order; we want nodes with lesser weight to have
2020
// higher priority
21-
other.estimated_weight.partial_cmp(&self.estimated_weight)
21+
Some(self.cmp(other))
2222
}
2323
}
2424

@@ -111,8 +111,8 @@ mod tests {
111111
}
112112

113113
fn add_edge<V: Ord + Copy, E: Ord>(graph: &mut Graph<V, E>, v1: V, v2: V, c: E) {
114-
graph.entry(v1).or_insert_with(BTreeMap::new).insert(v2, c);
115-
graph.entry(v2).or_insert_with(BTreeMap::new);
114+
graph.entry(v1).or_default().insert(v2, c);
115+
graph.entry(v2).or_default();
116116
}
117117

118118
#[test]

src/graph/bellman_ford.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ mod tests {
9090
use std::collections::BTreeMap;
9191

9292
fn add_edge<V: Ord + Copy, E: Ord>(graph: &mut Graph<V, E>, v1: V, v2: V, c: E) {
93-
graph.entry(v1).or_insert_with(BTreeMap::new).insert(v2, c);
94-
graph.entry(v2).or_insert_with(BTreeMap::new);
93+
graph.entry(v1).or_default().insert(v2, c);
94+
graph.entry(v2).or_default();
9595
}
9696

9797
#[test]

src/graph/dijkstra.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ mod tests {
5656
use std::collections::BTreeMap;
5757

5858
fn add_edge<V: Ord + Copy, E: Ord>(graph: &mut Graph<V, E>, v1: V, v2: V, c: E) {
59-
graph.entry(v1).or_insert_with(BTreeMap::new).insert(v2, c);
60-
graph.entry(v2).or_insert_with(BTreeMap::new);
59+
graph.entry(v1).or_default().insert(v2, c);
60+
graph.entry(v2).or_default();
6161
}
6262

6363
#[test]

src/graph/dinic_maxflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ mod tests {
194194
let max_flow = flow.find_maxflow(i32::MAX);
195195
assert_eq!(max_flow, 23);
196196

197-
let mut sm_out = vec![0; 7];
198-
let mut sm_in = vec![0; 7];
197+
let mut sm_out = [0; 7];
198+
let mut sm_in = [0; 7];
199199

200200
let flow_edges = flow.get_flow_edges(i32::MAX);
201201
for e in flow_edges {

0 commit comments

Comments
 (0)