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

Support no_std #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ keywords = ["bloom", "filter"]
readme = "README.md"

[dependencies]
bit-vec = "0.6.3"
bit-vec = { version = "0.6.3", default-features = false }
getrandom = { version = "0.2.10", optional = true }
siphasher = "1.0.0"
libm = "0.2.7"
siphasher = { version = "1.0.0", default-features = false }

[features]
default = ["random"]
default = ["random", "std"]
random = ["getrandom"]
serde = ["siphasher/serde_std", "bit-vec/serde"]
serde = ["std", "siphasher/serde_std", "bit-vec/serde"]
std = ["bit-vec/std", "siphasher/std"]
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
//! This is a simple but fast Bloom filter implementation, that requires only
//! 2 hash functions, generated with SipHash-1-3 using randomized keys.

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(non_camel_case_types, non_upper_case_globals, unused_qualifications)]
#![allow(clippy::unreadable_literal, clippy::bool_comparison)]

use std::cmp;
use std::convert::TryFrom;
use std::f64;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use core::cmp;
use core::convert::TryFrom;
use core::f64;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;

use bit_vec::BitVec;
#[cfg(feature = "random")]
Expand Down Expand Up @@ -142,7 +143,7 @@ impl<T: ?Sized> Bloom<T> {
assert!(fp_p > 0.0 && fp_p < 1.0);
let log2 = f64::consts::LN_2;
let log2_2 = log2 * log2;
((items_count as f64) * f64::ln(fp_p) / (-8.0 * log2_2)).ceil() as usize
libm::ceil((items_count as f64) * libm::log(fp_p) / (-8.0 * log2_2)) as usize
}

/// Record the presence of an item.
Expand Down Expand Up @@ -192,6 +193,7 @@ impl<T: ?Sized> Bloom<T> {
}

/// Return the bitmap as a vector of bytes
#[cfg(feature = "std")]
pub fn bitmap(&self) -> Vec<u8> {
self.bit_vec.to_bytes()
}
Expand Down Expand Up @@ -220,7 +222,7 @@ impl<T: ?Sized> Bloom<T> {
fn optimal_k_num(bitmap_bits: u64, items_count: usize) -> u32 {
let m = bitmap_bits as f64;
let n = items_count as f64;
let k_num = (m / n * f64::ln(2.0f64)).ceil() as u32;
let k_num = libm::ceil(m / n * libm::log(2.0f64)) as u32;
cmp::max(k_num, 1)
}

Expand Down
6 changes: 4 additions & 2 deletions tests/bloom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bloomfilter::{reexports::getrandom::getrandom, Bloom};
use bloomfilter::Bloom;
#[cfg(feature = "random")]
use bloomfilter::reexports::getrandom::getrandom;

#[test]
#[cfg(feature = "random")]
Expand Down Expand Up @@ -34,7 +36,7 @@ fn bloom_test_clear() {
}

#[test]
#[cfg(feature = "random")]
#[cfg(all(feature = "random", feature = "std"))]
fn bloom_test_load() {
let mut original = Bloom::new(10, 80);
let mut k = vec![0u8, 16];
Expand Down