Skip to content

Commit

Permalink
clippy auto fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shuoli84 committed Aug 8, 2024
1 parent 26dd47f commit 871bb0b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 59 deletions.
8 changes: 4 additions & 4 deletions src/argument/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<G: Clone + Ord> GroupCount<G> {
GroupCount::One(ref g2, c2),
) => {
if g2.cmp(&max_group.0) == Ordering::Equal {
max_group.1 = max_group.1 + c2;
max_group.1 += c2;
} else {
max_group = (g2.clone(), *c2);
group_count += 1;
Expand Down Expand Up @@ -350,10 +350,10 @@ where
}
}

return match slot {
match slot {
Ok(_) => Ok(Some((group_for_key, count))),
Err(_) => Err(Some((group_for_key, count))),
};
}
}
}

Expand Down Expand Up @@ -425,7 +425,7 @@ mod visit {
}
}

return Some(self.element_count);
Some(self.element_count)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/argument/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ impl<K: Key> Argument<K> for () {

#[inline(always)]
fn from_leaf(_: &[K]) -> Self {
()

}

#[inline(always)]
fn from_inner(_: &[K], _: &[Self]) -> Self {
()

}
}
22 changes: 11 additions & 11 deletions src/tree/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'k, K: Key + 'k> Cursor<K> {
leaf_id_hint: leaf_id,
offset_hint: 0,
},
&kv.1,
kv.1,
))
}

Expand All @@ -69,13 +69,13 @@ impl<'k, K: Key + 'k> Cursor<K> {
leaf_id_hint: leaf_id,
offset_hint: leaf.len() - 1,
},
&kv.1,
kv.1,
))
}

/// Get the `Cursor` points to the prev key-value pair. If the key for `self` is deleted, then
/// this returns the cursor for the key value pair just under the deleted key.
pub fn prev<'a, 'b, S: NodeStore<K = K>>(&'a self, tree: &'b BPlusTree<S>) -> Option<Self> {
pub fn prev<S: NodeStore<K = K>>(&self, tree: &BPlusTree<S>) -> Option<Self> {
self.prev_with_value(tree).map(|x| x.0)
}

Expand Down Expand Up @@ -125,13 +125,13 @@ impl<'k, K: Key + 'k> Cursor<K> {
leaf_id_hint: leaf_id,
offset_hint: offset,
},
&kv.1,
kv.1,
))
}

/// Get the `Cursor` points to the next key-value pair. If the key for `self` is deleted, then
/// this returns the cursor for the key value pair just larger than the deleted key.
pub fn next<'a, 'b, S: NodeStore<K = K>>(&'a self, tree: &'b BPlusTree<S>) -> Option<Self> {
pub fn next<S: NodeStore<K = K>>(&self, tree: &BPlusTree<S>) -> Option<Self> {
self.next_with_value(tree).map(|x| x.0)
}

Expand Down Expand Up @@ -164,7 +164,7 @@ impl<'k, K: Key + 'k> Cursor<K> {
leaf_id_hint: leaf_id,
offset_hint: next_offset,
},
&kv.1,
kv.1,
))
} else {
let leaf_id = leaf.next()?;
Expand All @@ -177,13 +177,13 @@ impl<'k, K: Key + 'k> Cursor<K> {
leaf_id_hint: leaf_id,
offset_hint: 0,
},
&kv.1,
kv.1,
))
}
}

/// whether current cursor is still valid
pub fn exists<'a, 'b, S: NodeStore<K = K>>(&'a self, tree: &'b BPlusTree<S>) -> bool {
pub fn exists<S: NodeStore<K = K>>(&self, tree: &BPlusTree<S>) -> bool {
self.value(tree).is_some()
}

Expand All @@ -195,7 +195,7 @@ impl<'k, K: Key + 'k> Cursor<K> {
let (_, leaf) = self.locate_leaf(tree)?;

match leaf.try_data_at(self.offset_hint) {
Some(kv) if kv.0.eq(&self.k) => Some(&kv.1),
Some(kv) if kv.0.eq(&self.k) => Some(kv.1),
_ => {
// todo: consider update self?
let (_, value) = leaf.locate_slot_with_value(&self.k);
Expand All @@ -205,8 +205,8 @@ impl<'k, K: Key + 'k> Cursor<K> {
}

#[inline]
fn locate_leaf<'a, 'b, S: NodeStore<K = K>>(
&'a self,
fn locate_leaf<'b, S: NodeStore<K = K>>(
&self,
tree: &'b BPlusTree<S>,
) -> Option<(LeafNodeId, &'b LeafNode<S::K, S::V>)> {
let leaf_id = self.leaf_id_hint;
Expand Down
2 changes: 1 addition & 1 deletion src/tree/entry_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl VisitStack {
offset,
child_id,
} = self.stack[self.len as usize];
Some((id, offset as usize, child_id))
Some((id, offset, child_id))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tree/inner_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ impl<K: Key, A: Argument<K>> Clone for InnerNode<K, A> {
}

Self {
size: self.size.clone(),
size: self.size,
slot_key: new_key,
child_id: self.child_id.clone(),
child_id: self.child_id,
arguments: new_arguments,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/tree/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<S: NodeStore> Iterator for IntoIter<S> {
// safety: right after we called this, the pos moves to next.
let kv = unsafe { leaf.take_data(offset) };
self.pos = (leaf_id, offset + 1);
return Some(kv);
Some(kv)
} else {
// move to next leaf
match leaf.next() {
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<S: NodeStore> DoubleEndedIterator for IntoIter<S> {
// safety: right after we called this, the pos moves to next.
let kv = unsafe { leaf.take_data(offset - 1) };
self.end = (leaf_id, offset - 1);
return Some(kv);
Some(kv)
} else {
// move to prev leaf
match leaf.prev() {
Expand Down
26 changes: 10 additions & 16 deletions src/tree/leaf_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ where
}

Self {
size: self.size.clone(),
size: self.size,
slot_key: new_key,
slot_value: new_value,
prev: self.prev.clone(),
next: self.next.clone(),
prev: self.prev,
next: self.next,
}
}
}
Expand Down Expand Up @@ -120,14 +120,8 @@ impl<K: Key, V> LeafNode<K, V> {

pub fn key_range(&self) -> (Option<K>, Option<K>) {
debug_assert!(self.len() > 0);
let start = match self.prev {
Some(_) => Some(unsafe { self.key_area(0).assume_init_ref().clone() }),
None => None,
};
let end = match self.next {
Some(_) => Some(unsafe { self.key_area(self.len() - 1).assume_init_ref().clone() }),
None => None,
};
let start = self.prev.map(|_| unsafe { self.key_area(0).assume_init_ref().clone() });
let end = self.next.map(|_| unsafe { self.key_area(self.len() - 1).assume_init_ref().clone() });
(start, end)
}

Expand Down Expand Up @@ -239,7 +233,7 @@ impl<K: Key, V> LeafNode<K, V> {
self_leaf_id: LeafNodeId,
) -> Box<Self> {
let split_origin_size = Self::split_origin_size() as usize;
let split_new_size = N - split_origin_size as usize;
let split_new_size = N - split_origin_size;

let mut new_node = Self::new();
new_node.prev = Some(self_leaf_id);
Expand All @@ -248,16 +242,16 @@ impl<K: Key, V> LeafNode<K, V> {
unsafe {
slice_utils::move_to_slice(
self.key_area_mut(split_origin_size..N),
new_node.key_area_mut(..split_new_size as usize),
new_node.key_area_mut(..split_new_size),
);
slice_utils::move_to_slice(
self.value_area_mut(split_origin_size..N),
new_node.value_area_mut(..split_new_size as usize),
new_node.value_area_mut(..split_new_size),
);
};

if insert_idx < split_origin_size {
let new_size = split_origin_size as usize + 1;
let new_size = split_origin_size + 1;
unsafe {
slice_utils::slice_insert(self.key_area_mut(..new_size), insert_idx, item.0);
slice_utils::slice_insert(self.value_area_mut(..new_size), insert_idx, item.1);
Expand Down Expand Up @@ -326,7 +320,7 @@ impl<K: Key, V> LeafNode<K, V> {
// if the child split, then the new key should inserted idx + 1
(idx, {
let v = unsafe { self.value_area(idx).assume_init_ref() };
Some(&v)
Some(v)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ where
self.remove_impl(k).map(|kv| kv.1)
}

fn key_to_ref<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<EntryRef<&Self>>
fn key_to_ref<Q: ?Sized>(&self, k: &Q) -> Option<EntryRef<&Self>>
where
Q: Ord,
S::K: Borrow<Q>,
Expand Down
34 changes: 14 additions & 20 deletions src/tree/tree_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,14 @@ impl<S: NodeStore> BPlusTree<S> {
child_idx: usize,
deleted_item: (S::K, S::V),
) -> DeleteDescendResult<S::K, S::V> {
if child_idx > 0 {
if Self::try_rotate_right_for_inner_node(&mut self.node_store, node, child_idx - 1)
.is_some()
{
self.st.rotate_right_inner += 1;
return DeleteDescendResult::Done(deleted_item);
}
if child_idx > 0 && Self::try_rotate_right_for_inner_node(&mut self.node_store, node, child_idx - 1)
.is_some() {
self.st.rotate_right_inner += 1;
return DeleteDescendResult::Done(deleted_item);
}
if child_idx < node.len() {
if Self::try_rotate_left_for_inner_node(&mut self.node_store, node, child_idx).is_some()
{
self.st.rotate_left_inner += 1;
return DeleteDescendResult::Done(deleted_item);
}
if child_idx < node.len() && Self::try_rotate_left_for_inner_node(&mut self.node_store, node, child_idx).is_some() {
self.st.rotate_left_inner += 1;
return DeleteDescendResult::Done(deleted_item);
}

let merge_slot = if child_idx > 0 {
Expand Down Expand Up @@ -211,25 +205,25 @@ impl<S: NodeStore> BPlusTree<S> {
FixAction::MergeLeft => {
self.st.merge_with_left_leaf += 1;
// merge with prev node
let result = Self::merge_leaf_node_left(

Self::merge_leaf_node_left(
&mut self.node_store,
node,
child_idx - 1,
key_idx_in_child,
);
result
)
}
FixAction::MergeRight => {
self.st.merge_with_right_leaf += 1;
// merge with next node
let result = Self::merge_leaf_node_with_right(


Self::merge_leaf_node_with_right(
&mut self.node_store,
node,
child_idx,
key_idx_in_child,
);

result
)
}
}
}
Expand Down

0 comments on commit 871bb0b

Please sign in to comment.