Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/data_structures/b_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,18 @@ impl BTreeProps {
self.insert_non_full(&mut node.children[u_index], key);
}
}

fn traverse_node<T: Ord + Debug>(&self, node: &Node<T>, depth: usize) {
fn traverse_node<T: Ord + Debug>(node: &Node<T>, depth: usize) {
if node.is_leaf() {
print!(" {0:{<1$}{2:?}{0:}<1$} ", "", depth, node.keys);
} else {
let _depth = depth + 1;
for (index, key) in node.keys.iter().enumerate() {
self.traverse_node(&node.children[index], _depth);
Self::traverse_node(&node.children[index], _depth);
// Check https://doc.rust-lang.org/std/fmt/index.html
// And https://stackoverflow.com/a/35280799/2849127
print!("{0:{<1$}{2:?}{0:}<1$}", "", depth, key);
}
self.traverse_node(node.children.last().unwrap(), _depth);
Self::traverse_node(node.children.last().unwrap(), _depth);
}
}
}
Expand Down Expand Up @@ -141,7 +140,7 @@ where
}

pub fn traverse(&self) {
self.props.traverse_node(&self.root, 0);
BTreeProps::traverse_node(&self.root, 0);
println!();
}

Expand Down
8 changes: 4 additions & 4 deletions src/data_structures/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ impl<T> LinkedList<T> {
}
}

pub fn get(&mut self, index: i32) -> Option<&T> {
self.get_ith_node(self.head, index)
pub fn get(&mut self, index: i32) -> Option<&'static T> {
Self::get_ith_node(self.head, index)
}

fn get_ith_node(&mut self, node: Option<NonNull<Node<T>>>, index: i32) -> Option<&T> {
fn get_ith_node(node: Option<NonNull<Node<T>>>, index: i32) -> Option<&'static T> {
match node {
None => None,
Some(next_ptr) => match index {
0 => Some(unsafe { &(*next_ptr.as_ptr()).val }),
_ => self.get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1),
_ => Self::get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dynamic_programming/edit_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn edit_distance_se(str_a: &str, str_b: &str) -> u32 {
// c is distances[i][j-1] and s is distances[i-1][j-1] at the beginning of each round of iteration
char_b = str_b[j - 1];
c = min(
s + if char_a == char_b { 0 } else { 1 },
s + u32::from(char_a != char_b),
min(c + 1, distances[j] + 1),
);
// c is updated to distances[i][j], and will thus become distances[i][j-1] for the next cell
Expand Down
7 changes: 3 additions & 4 deletions src/general/huffman_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl<T> Ord for HuffmanNode<T> {
impl<T: Clone + Copy + Ord> HuffmanNode<T> {
/// Turn the tree into the map that can be used in encoding
pub fn get_alphabet(
&self,
height: u32,
path: u64,
node: &HuffmanNode<T>,
Expand All @@ -60,8 +59,8 @@ impl<T: Clone + Copy + Ord> HuffmanNode<T> {
);
}
None => {
self.get_alphabet(height + 1, path, node.left.as_ref().unwrap(), map);
self.get_alphabet(
Self::get_alphabet(height + 1, path, node.left.as_ref().unwrap(), map);
Self::get_alphabet(
height + 1,
path | (1 << height),
node.right.as_ref().unwrap(),
Expand Down Expand Up @@ -103,7 +102,7 @@ impl<T: Clone + Copy + Ord> HuffmanDictionary<T> {
});
}
let root = queue.pop().unwrap();
root.get_alphabet(0, 0, &root, &mut alph);
HuffmanNode::get_alphabet(0, 0, &root, &mut alph);
HuffmanDictionary {
alphabet: alph,
root,
Expand Down
6 changes: 2 additions & 4 deletions src/graph/floyd_warshall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn floyd_warshall<V: Ord + Copy, E: Ord + Copy + Add<Output = E>>(
});
}
}
let keys = map.iter().map(|(k, _)| *k).collect::<Vec<_>>();
let keys = map.keys().copied().collect::<Vec<_>>();
for &k in &keys {
for &i in &keys {
if map[&i].get(&k).is_none() {
Expand All @@ -49,9 +49,7 @@ pub fn floyd_warshall<V: Ord + Copy, E: Ord + Copy + Add<Output = E>>(
match entry_i_j {
Some(&e) => {
if e > entry_i_k + entry_k_j {
map.entry(i)
.or_insert(BTreeMap::new())
.insert(j, entry_i_k + entry_k_j);
map.entry(i).or_default().insert(j, entry_i_k + entry_k_j);
}
}
None => {
Expand Down
2 changes: 1 addition & 1 deletion src/graph/prufer_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn prufer_encode<V: Ord + Copy>(tree: &Graph<V>) -> Vec<V> {

#[inline]
fn add_directed_edge<V: Ord + Copy>(tree: &mut Graph<V>, a: V, b: V) {
tree.entry(a).or_insert(vec![]).push(b);
tree.entry(a).or_default().push(b);
}

#[inline]
Expand Down