Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
modelflat committed Feb 1, 2024
1 parent e4a7006 commit 8356155
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 95 deletions.
18 changes: 5 additions & 13 deletions hloo_core/src/bit_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
fn compute_mask(pos: usize, len: usize, word_size: usize) -> u64 {
assert!(
0 < word_size && word_size <= 64,
"word size {} is not supported",
word_size
"word size {word_size} is not supported"
);
assert!(
pos + len <= word_size,
Expand Down Expand Up @@ -92,7 +91,7 @@ impl BitBlock {

/// Length of this block in words
pub fn len_words(&self, word_size: usize) -> usize {
let rem = if self.len() % word_size == 0 { 0 } else { 1 };
let rem = usize::from(self.len() % word_size != 0);
self.len() / word_size + rem
}

Expand All @@ -107,7 +106,7 @@ impl BitBlock {
let start_idx = self.start_pos().max(word_start_idx);
let end_idx = self.end_pos().min(word_end_idx);
let part_len = end_idx - start_idx + 1;
parts.push(BitBlock::new(self.idx, start_idx, part_len))
parts.push(BitBlock::new(self.idx, start_idx, part_len));
}
parts
}
Expand Down Expand Up @@ -283,15 +282,8 @@ impl std::fmt::Display for BitOp {
src_word,
src_mask,
dst_word,
} => write!(
f,
"a[{}] = ( a[{}] & {:0width$b} )",
dst_word,
src_word,
src_mask,
width = fmt_width
),
Self::Copy { src_word, dst_word } => write!(f, "a[{}] = a[{}]", dst_word, src_word),
} => write!(f, "a[{dst_word}] = ( a[{src_word}] & {src_mask:0fmt_width$b} )"),
Self::Copy { src_word, dst_word } => write!(f, "a[{dst_word}] = a[{src_word}]"),
}
}
}
Expand Down
34 changes: 19 additions & 15 deletions hloo_core/src/permutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct Permutation {
}

impl Permutation {
pub fn from_blocks(head: usize, blocks: Vec<BitBlock>) -> Self {
let permuted_blocks = create_permuted_blocks(&blocks);
pub fn from_blocks(head: usize, blocks: &[BitBlock]) -> Self {
let permuted_blocks = create_permuted_blocks(blocks);
Self {
head,
blocks: permuted_blocks,
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Permutation {

pub fn mask_words(&self, word_size: usize) -> usize {
let bits = self.mask_bits();
bits / word_size + if bits % word_size == 0 { 0 } else { 1 }
bits / word_size + usize::from(bits % word_size != 0)
}
}

Expand All @@ -67,7 +67,7 @@ fn compile_permutation(
let grouped_by_dst_word = ops.group_by(|op| (op.dst_word(), op.src_word()));

let mut result: HashMap<_, Vec<_>> = HashMap::new();
for ((dst_word, src_word), mut ops) in grouped_by_dst_word.into_iter() {
for ((dst_word, src_word), mut ops) in &grouped_by_dst_word {
let mut prev_op = ops.next().expect("empty group");
let mut word_ops = Vec::new();
for op in ops {
Expand All @@ -82,7 +82,7 @@ fn compile_permutation(
}
}
word_ops.push(prev_op);
prev_op = op
prev_op = op;
}
word_ops.push(prev_op);
result
Expand All @@ -94,11 +94,11 @@ fn compile_permutation(
}

fn split_bits_into_blocks(f: usize, r: usize) -> Vec<BitBlock> {
assert!(f >= r, "{} is not enough bits to split into {} blocks", f, r);
assert!(f >= r, "{f} is not enough bits to split into {r} blocks");
let mut blocks = Vec::with_capacity(r);
let mut acc = 0;
for i in 0..r {
let size = f / r + if i < f % r { 1 } else { 0 };
let size = f / r + usize::from(i < f % r);
blocks.push(BitBlock::new(i, acc, size));
acc += size;
}
Expand All @@ -124,25 +124,29 @@ fn create_permuted_blocks(reordered_blocks: &[BitBlock]) -> Vec<PermutedBitBlock
permuted
}

/// Creates bit permutations from given parameters.
///
/// # Panics
/// This function panics if either of the following is true:
/// - `total_bits` is not divisible by `word_bits`
/// - `total_bits` < k
/// - `r` == 0
/// - `k` == 0
pub fn create_permutations(total_bits: usize, word_bits: usize, r: usize, k: usize) -> Vec<Permutation> {
assert!(
total_bits % word_bits == 0,
"total_bits has to be divisible by word_bits (tb={} wb={})",
total_bits,
word_bits
"total_bits has to be divisible by word_bits (tb={total_bits} wb={word_bits})"
);
assert!(
total_bits >= k,
"total_bits must be able to fit k (tb={} k={})",
total_bits,
k,
"total_bits must be able to fit k (tb={total_bits} k={k})",
);
assert!(r != 0 && k != 0, "r and k cannot be 0 (r={} k={})", r, k);
assert!(r != 0 && k != 0, "r and k cannot be 0 (r={r} k={k})");
let blocks = split_bits_into_blocks(total_bits, r);
(0..r)
.combinations(k)
.map(|order| reorder_blocks(&blocks, &order))
.map(|blocks| Permutation::from_blocks(k, blocks))
.map(|blocks| Permutation::from_blocks(k, &blocks))
.collect()
}

Expand Down
12 changes: 6 additions & 6 deletions hloo_macros/src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl ToTokens for Bits<'_> {
let byte_size = full_size / 8;
let word_size = self.word_size;
let word_bytes = self.word_size / 8;
let word_range_le = 0..self.n_words;
let word_range_be = word_range_le.clone();
let word_range_xor = word_range_le.clone();
let word_max = word_range_le.clone().map(|_| word_type_name.clone());
let word_range = 0..self.n_words;
let word_range_be = word_range.clone();
let word_range_xor = word_range.clone();
let word_max = word_range.clone().map(|_| word_type_name.clone());

let data_type = match TypeArray::from_string(&format!("[{}; {}]", self.word_type_name, self.n_words)) {
Ok(arr) => Type::Array(arr),
Expand Down Expand Up @@ -72,8 +72,8 @@ impl ToTokens for Bits<'_> {
panic!("should have length {}", #byte_size);
}
let mut data: #storage_type_name = Default::default();
#(data[#word_range_le] = #word_type_name::from_be_bytes(
raw_data[#word_range_le*#word_bytes..(#word_range_le + 1)*#word_bytes]
#(data[#word_range] = #word_type_name::from_be_bytes(
raw_data[#word_range*#word_bytes..(#word_range + 1)*#word_bytes]
.try_into()
.expect("slice with incorrect length")
));*;
Expand Down
3 changes: 1 addition & 2 deletions hloo_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub fn make_permutations(item: TokenStream) -> TokenStream {
let word_bits = params.w.unwrap_or(64);
assert!(
[8, 16, 32, 64].contains(&word_bits),
"word size {} is not supported",
word_bits
"word size {word_bits} is not supported"
);
let n_words = params.f / word_bits;
assert!(params.f % word_bits == 0 && n_words > 0);
Expand Down
2 changes: 1 addition & 1 deletion hloo_macros/src/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ impl ToTokens for Permutation<'_> {
}
}
};
tokens.extend(code)
tokens.extend(code);
}
}
15 changes: 5 additions & 10 deletions src/index/memmap_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,23 @@ mod tests {
assert_eq!(
index.data().len(),
data_part_1.len(),
"[{}] index length is wrong after first insert",
i
"[{i}] index length is wrong after first insert"
);
assert_eq!(
index.data(),
expected_first,
"[{}] index contents is wrong after first insert",
i
"[{i}] index contents is wrong after first insert"
);
index.insert(&data_part_2).unwrap();
assert_eq!(
index.data().len(),
data_part_1.len() + data_part_2.len(),
"[{}] index length is wrong after second insert",
i
"[{i}] index length is wrong after second insert"
);
assert_eq!(
index.data(),
expected_second,
"[{}] index contents is wrong after second insert",
i
"[{i}] index contents is wrong after second insert"
);
}
}
Expand Down Expand Up @@ -236,8 +232,7 @@ mod tests {
assert_eq!(
index.data(),
expected,
"[{}] index contents is wrong after second insert",
i
"[{i}] index contents is wrong after second insert"
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ where
/// Get permuter reference.
fn permuter(&self) -> &dyn BitPermuter<K, M>;

/// Get currently used BlockLocator.
/// Get currently used `BlockLocator`.
fn block_locator(&self) -> BlockLocator;

/// Get data as a slice.
Expand Down Expand Up @@ -237,6 +237,6 @@ mod tests {
SearchResultItem::new(2, 1),
],
"pos 0-2 - data"
)
);
}
}
7 changes: 0 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,18 @@
//! let memmap_lookup = lookup64::MemMapLookup::<i64>::create(&path);
//! ```
// #![warn(clippy::pedantic)]
#![warn(clippy::redundant_closure_for_method_calls)]

pub mod index;
pub mod lookup;
pub mod util;

pub mod mmvec;

use std::sync::Arc;

pub use hloo_core;
pub use hloo_macros::make_permutations;

pub use index::Index;
pub use lookup::{Lookup, SimpleLookup};

pub type DynIndex<K, V, M, E> = Arc<dyn Index<K, V, M, Error = E>>;

pub type DynBitPermuter<B, M> = Box<dyn hloo_core::BitPermuter<B, M>>;

/// This macro serves as an initialization step to create lookups with specified configuration.
Expand Down
2 changes: 1 addition & 1 deletion src/lookup/lookup_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl From<&[u8]> for DynBits {
match value.len() {
lookup64::Bits::SIZE_BYTES => Self::Bits64(lookup64::Bits::from_le_bytes(value)),
lookup256::Bits::SIZE_BYTES => Self::Bits256(lookup256::Bits::from_le_bytes(value)),
len => panic!("invalid slice size: {}", len),
len => panic!("invalid slice size: {len}"),
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/lookup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ pub struct SimpleLookup<K, V, M, I> {
}

impl<K, V, M, I> SimpleLookup<K, V, M, I> {
#[must_use]
pub fn new(indexes: Vec<I>) -> Self {
Self {
indexes,
_dummy: Default::default(),
_dummy: PhantomData,
}
}
}
Expand All @@ -138,8 +139,8 @@ where
) -> Result<Self, <I as PersistentIndex<K, M>>::Error> {
let mut indexes = Vec::new();
for (i, p) in permuters.into_iter().enumerate() {
let index_path = path.join(format!("index_{:04}_{:016x}.dat", i, sig));
indexes.push(I::create(p, sig, &index_path)?)
let index_path = path.join(format!("index_{i:04}_{sig:016x}.dat"));
indexes.push(I::create(p, sig, &index_path)?);
}
Ok(Self::new(indexes))
}
Expand All @@ -151,8 +152,8 @@ where
) -> Result<Self, <I as PersistentIndex<K, M>>::Error> {
let mut indexes = Vec::new();
for (i, p) in permuters.into_iter().enumerate() {
let index_path = path.join(format!("index_{:04}_{:016x}.dat", i, sig));
indexes.push(I::load(p, sig, &index_path)?)
let index_path = path.join(format!("index_{i:04}_{sig:016x}.dat"));
indexes.push(I::load(p, sig, &index_path)?);
}
Ok(Self::new(indexes))
}
Expand Down
Loading

0 comments on commit 8356155

Please sign in to comment.