Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix clippy warnings #28

Merged
merged 8 commits into from
Aug 10, 2024
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
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 {
()

}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::type_complexity)]

mod set;
pub use set::*;

Expand Down
59 changes: 32 additions & 27 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ impl<K: Key> BPlusTreeSet<K> {
/// assert!(!set.remove(&2));
/// ```
#[inline]
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> bool
pub fn remove<Q>(&mut self, k: &Q) -> bool
where
Q: Ord,
Q: ?Sized + Ord,
K: Borrow<Q>,
{
self.tree.remove(k).is_some()
Expand All @@ -118,9 +118,9 @@ impl<K: Key> BPlusTreeSet<K> {
/// assert!(!set.contains(&1));
/// ```
#[inline]
pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
pub fn contains<Q>(&self, k: &Q) -> bool
where
Q: Ord,
Q: ?Sized + Ord,
K: Borrow<Q>,
{
self.tree.get(k).is_some()
Expand Down Expand Up @@ -206,28 +206,6 @@ impl<K: Key> BPlusTreeSet<K> {
}
}

/// Returns a iterator over the keys in the set
///
/// # Examples
/// ```rust
/// use sweep_bptree::BPlusTreeSet;
///
/// let mut set = BPlusTreeSet::<String>::new();
///
/// set.insert(1.to_string());
/// set.insert(2.to_string());
///
/// let keys = set.into_iter().collect::<Vec<_>>();
/// assert_eq!(keys.len(), 2);
///
/// ```
#[inline]
pub fn into_iter(self) -> iter::IntoIter<K> {
iter::IntoIter {
inner: self.tree.into_iter(),
}
}

/// Visits the elements representing the union,
/// i.e., all the elements in `self` or `other`, without duplicates,
/// in ascending order.
Expand Down Expand Up @@ -274,7 +252,34 @@ impl<K: Key> FromIterator<K> for BPlusTreeSet<K> {
}
}

mod iter {
impl<K: Key> IntoIterator for BPlusTreeSet<K> {
type Item = K;
type IntoIter = iter::IntoIter<K>;

/// Returns an iterator over the keys in the set
///
/// # Examples
/// ```rust
/// use sweep_bptree::BPlusTreeSet;
///
/// let mut set = BPlusTreeSet::<String>::new();
///
/// set.insert(1.to_string());
/// set.insert(2.to_string());
///
/// let keys = set.into_iter().collect::<Vec<_>>();
/// assert_eq!(keys.len(), 2);
///
/// ```
#[inline]
fn into_iter(self) -> Self::IntoIter {
iter::IntoIter {
inner: self.tree.into_iter(),
}
}
}

pub mod iter {
use std::{cmp::max, iter::FusedIterator};

use crate::merge_iter::MergeIterInner;
Expand Down
2 changes: 1 addition & 1 deletion src/tree/bulk_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<S: NodeStore> crate::BPlusTree<S> {
let mut leaf = LeafNode::<S::K, S::V>::new();
leaf.set_data(&mut data_iter);

if leaf.len() == 0 {
if leaf.is_empty() {
// there is no data left
break;
}
Expand Down
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
6 changes: 3 additions & 3 deletions 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 All @@ -93,7 +93,7 @@ pub struct EntryRefDetached {

impl EntryRefDetached {
/// This is a hack to get around the borrow checker
pub fn to_ref<TR>(self, tree: TR) -> EntryRef<TR> {
pub fn into_ref<TR>(self, tree: TR) -> EntryRef<TR> {
EntryRef::<TR> {
inner_stack: self.inner_stack,
leaf_id: self.leaf_id,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<TR> EntryRef<TR> {
}
}

pub fn to_detached(self) -> EntryRefDetached {
pub fn into_detached(self) -> EntryRefDetached {
EntryRefDetached {
inner_stack: self.inner_stack,
leaf_id: self.leaf_id,
Expand Down
14 changes: 10 additions & 4 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 All @@ -75,6 +75,11 @@ impl<K: Key, A: Argument<K>> InnerNode<K, A> {
self.size as usize
}

/// whether this node is empty
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Max key capacity
pub const fn max_capacity() -> u16 {
N as u16
Expand Down Expand Up @@ -195,10 +200,10 @@ impl<K: Key, A: Argument<K>> InnerNode<K, A> {

/// returns the child index for k
#[inline]
pub fn locate_child<Q: ?Sized>(&self, k: &Q) -> (usize, NodeId)
pub fn locate_child<Q>(&self, k: &Q) -> (usize, NodeId)
where
K: std::borrow::Borrow<Q>,
Q: Ord,
Q: ?Sized + Ord,
{
match self.keys().binary_search_by_key(&k, |f| f.borrow()) {
Err(idx) => {
Expand Down Expand Up @@ -266,6 +271,7 @@ impl<K: Key, A: Argument<K>> InnerNode<K, A> {

self.size = split_origin_size as u16;

#[allow(clippy::comparison_chain)]
if child_idx < split_origin_size {
// take node_size 4 as example
// new key 2, new node n
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
Loading
Loading