Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
josephglanville committed Mar 23, 2020
1 parent 5bed058 commit 94bb5e2
Show file tree
Hide file tree
Showing 40 changed files with 691 additions and 294 deletions.
12 changes: 5 additions & 7 deletions benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ fn create(c: &mut Criterion) {
}

fn insert(c: &mut Criterion) {
c.bench_function(
"insert 1", |b| {
c.bench_function("insert 1", |b| {
b.iter(|| {
let mut bitmap = RoaringBitmap::new();
bitmap.insert(1);
bitmap
});
});

c.bench_function(
"insert 2", |b| {
c.bench_function("insert 2", |b| {
b.iter(|| {
let mut bitmap = RoaringBitmap::new();
bitmap.insert(1);
bitmap.insert(2);
bitmap
bitmap.insert(1);
bitmap.insert(2);
bitmap
});
});
}
Expand Down
33 changes: 25 additions & 8 deletions src/bitmap/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::slice;
use std::iter::Peekable;
use std::slice;

use crate::RoaringBitmap;
use super::container::Container;
use crate::RoaringBitmap;

struct Pairs<'a>(Peekable<slice::Iter<'a, Container>>, Peekable<slice::Iter<'a, Container>>);
struct Pairs<'a>(
Peekable<slice::Iter<'a, Container>>,
Peekable<slice::Iter<'a, Container>>,
);

impl RoaringBitmap {
fn pairs<'a>(&'a self, other: &'a RoaringBitmap) -> Pairs<'a> {
Pairs(self.containers.iter().peekable(), other.containers.iter().peekable())
Pairs(
self.containers.iter().peekable(),
other.containers.iter().peekable(),
)
}

/// Returns true if the set has no elements in common with other. This is equivalent to
Expand Down Expand Up @@ -63,8 +69,14 @@ impl RoaringBitmap {
for pair in self.pairs(other) {
match pair {
(None, _) => (),
(_, None) => { return false; },
(Some(c1), Some(c2)) => if !c1.is_subset(c2) { return false; },
(_, None) => {
return false;
}
(Some(c1), Some(c2)) => {
if !c1.is_subset(c2) {
return false;
}
}
}
}
true
Expand Down Expand Up @@ -101,7 +113,12 @@ impl<'a> Iterator for Pairs<'a> {
type Item = (Option<&'a Container>, Option<&'a Container>);

fn next(&mut self) -> Option<Self::Item> {
enum Which { Left, Right, Both, None };
enum Which {
Left,
Right,
Both,
None,
};
let which = match (self.0.peek(), self.1.peek()) {
(None, None) => Which::None,
(Some(_), None) => Which::Left,
Expand All @@ -111,7 +128,7 @@ impl<'a> Iterator for Pairs<'a> {
(key1, key2) if key1 < key2 => Which::Left,
(key1, key2) if key1 > key2 => Which::Right,
(_, _) => unreachable!(),
}
},
};
match which {
Which::Left => Some((self.0.next(), None)),
Expand Down
6 changes: 3 additions & 3 deletions src/bitmap/container.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use super::store::{self, Store};
use super::util;
use super::store::{ self, Store };

const ARRAY_LIMIT: u64 = 4096;

Expand Down Expand Up @@ -122,7 +122,7 @@ impl<'a> IntoIterator for &'a Container {
fn into_iter(self) -> Iter<'a> {
Iter {
key: self.key,
inner: (&self.store).into_iter()
inner: (&self.store).into_iter(),
}
}
}
Expand All @@ -134,7 +134,7 @@ impl IntoIterator for Container {
fn into_iter(self) -> Iter<'static> {
Iter {
key: self.key,
inner: self.store.into_iter()
inner: self.store.into_iter(),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/bitmap/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ impl fmt::Debug for RoaringBitmap {
if self.len() < 16 {
write!(f, "RoaringBitmap<{:?}>", self.iter().collect::<Vec<u32>>())
} else {
write!(f, "RoaringBitmap<{:?} values between {:?} and {:?}>", self.len(), self.min().unwrap(), self.max().unwrap())
write!(
f,
"RoaringBitmap<{:?} values between {:?} and {:?}>",
self.len(),
self.min().unwrap(),
self.max().unwrap()
)
}
}
}
28 changes: 15 additions & 13 deletions src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::RoaringBitmap;

use super::util;
use super::container::Container;
use super::util;
use std::ops::Range;

impl RoaringBitmap {
Expand All @@ -14,7 +14,9 @@ impl RoaringBitmap {
/// let mut rb = RoaringBitmap::new();
/// ```
pub fn new() -> RoaringBitmap {
RoaringBitmap { containers: Vec::new() }
RoaringBitmap {
containers: Vec::new(),
}
}

/// Adds a value to the set. Returns `true` if the value was not already present in the set.
Expand All @@ -36,7 +38,7 @@ impl RoaringBitmap {
Err(loc) => {
self.containers.insert(loc, Container::new(key));
&mut self.containers[loc]
},
}
};
container.insert(index)
}
Expand Down Expand Up @@ -87,7 +89,10 @@ impl RoaringBitmap {
/// assert_eq!(rb.remove_range(2..4), 2);
/// ```
pub fn remove_range(&mut self, range: Range<u64>) -> u64 {
assert!(range.end <= u64::from(u32::max_value()) + 1, "can't index past 2**32");
assert!(
range.end <= u64::from(u32::max_value()) + 1,
"can't index past 2**32"
);
if range.start == range.end {
return 0;
}
Expand All @@ -114,8 +119,7 @@ impl RoaringBitmap {
result += self.containers[index].len;
self.containers.remove(index);
continue;
}
else {
} else {
result += self.containers[index].remove_range(a, b);
if self.containers[index].len == 0 {
self.containers.remove(index);
Expand Down Expand Up @@ -201,10 +205,7 @@ impl RoaringBitmap {
/// assert_eq!(rb.len(), 2);
/// ```
pub fn len(&self) -> u64 {
self.containers
.iter()
.map(|container| container.len)
.sum()
self.containers.iter().map(|container| container.len).sum()
}

/// Returns the minimum value in the set (if the set is non-empty).
Expand All @@ -222,11 +223,11 @@ impl RoaringBitmap {
/// assert_eq!(rb.min(), Some(3));
/// ```
pub fn min(&self) -> Option<u32> {
self.containers.first()
self.containers
.first()
.map(|head| util::join(head.key, head.min()))
}


/// Returns the maximum value in the set (if the set is non-empty).
///
/// # Examples
Expand All @@ -242,7 +243,8 @@ impl RoaringBitmap {
/// assert_eq!(rb.max(), Some(4));
/// ```
pub fn max(&self) -> Option<u32> {
self.containers.last()
self.containers
.last()
.map(|tail| util::join(tail.key, tail.max()))
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/bitmap/iter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::iter::{ self, FromIterator };
use std::iter::{self, FromIterator};
use std::slice;
use std::vec;

use crate::RoaringBitmap;
use super::container::Container;
use crate::RoaringBitmap;

/// An iterator for `RoaringBitmap`.
pub struct Iter<'a> {
inner: iter::FlatMap<slice::Iter<'a, Container>, &'a Container, fn(&'a Container) -> &'a Container>,
inner: iter::FlatMap<
slice::Iter<'a, Container>,
&'a Container,
fn(&'a Container) -> &'a Container,
>,
size_hint: u64,
}

Expand All @@ -19,7 +23,9 @@ pub struct IntoIter {

impl<'a> Iter<'a> {
fn new(containers: &[Container]) -> Iter {
fn identity<T>(t: T) -> T { t }
fn identity<T>(t: T) -> T {
t
}
let size_hint = containers.iter().map(|c| c.len).sum();
Iter {
inner: containers.iter().flat_map(identity as _),
Expand All @@ -30,7 +36,9 @@ impl<'a> Iter<'a> {

impl IntoIter {
fn new(containers: Vec<Container>) -> IntoIter {
fn identity<T>(t: T) -> T { t }
fn identity<T>(t: T) -> T {
t
}
let size_hint = containers.iter().map(|c| c.len).sum();
IntoIter {
inner: containers.into_iter().flat_map(identity as _),
Expand Down
8 changes: 4 additions & 4 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
mod store;
mod container;
mod util;
mod fmt;
mod store;
mod util;

// Order of these modules matters as it determines the `impl` blocks order in
// the docs
mod cmp;
mod inherent;
mod iter;
mod ops;
mod cmp;
mod serialization;

pub use self::iter::Iter;
pub use self::iter::IntoIter;
pub use self::iter::Iter;

/// A compressed bitmap using the [Roaring bitmap compression scheme](http://roaringbitmap.org).
///
Expand Down
17 changes: 8 additions & 9 deletions src/bitmap/ops.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::ops::{
BitAnd, BitAndAssign,
BitOr, BitOrAssign,
BitXor, BitXorAssign,
Sub, SubAssign
};
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};

use crate::RoaringBitmap;

Expand Down Expand Up @@ -81,7 +76,9 @@ impl RoaringBitmap {
while index < self.containers.len() {
let key = self.containers[index].key;
match other.containers.binary_search_by_key(&key, |c| c.key) {
Err(_) => { self.containers.remove(index); }
Err(_) => {
self.containers.remove(index);
}
Ok(loc) => {
self.containers[index].intersect_with(&other.containers[loc]);
if self.containers[index].len == 0 {
Expand Down Expand Up @@ -135,8 +132,10 @@ impl RoaringBitmap {
} else {
index += 1;
}
},
_ => { index += 1; }
}
_ => {
index += 1;
}
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/bitmap/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io;
use byteorder::{ LittleEndian, ReadBytesExt, WriteBytesExt };

use crate::RoaringBitmap;
use super::store::Store;
use super::container::Container;
use super::store::Store;
use crate::RoaringBitmap;

const SERIAL_COOKIE_NO_RUNCONTAINER: u32 = 12346;
const SERIAL_COOKIE: u16 = 12347;
Expand All @@ -27,16 +27,14 @@ impl RoaringBitmap {
/// assert_eq!(rb1, rb2);
/// ```
pub fn serialized_size(&self) -> usize {
let container_sizes: usize = self.containers.iter().map(|container| {
match container.store {
Store::Array(ref values) => {
8 + values.len() * 2
}
Store::Bitmap(..) => {
8 + 8 * 1024
}
}
}).sum();
let container_sizes: usize = self
.containers
.iter()
.map(|container| match container.store {
Store::Array(ref values) => 8 + values.len() * 2,
Store::Bitmap(..) => 8 + 8 * 1024,
})
.sum();

// header + container sizes
8 + container_sizes
Expand Down Expand Up @@ -125,18 +123,18 @@ impl RoaringBitmap {
} else if (cookie as u16) == SERIAL_COOKIE {
return Err(io::Error::new(
io::ErrorKind::Other,
"run containers are unsupported"));
"run containers are unsupported",
));
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"unknown cookie value"));
return Err(io::Error::new(io::ErrorKind::Other, "unknown cookie value"));
}
};

if size > u16::max_value() as usize {
return Err(io::Error::new(
io::ErrorKind::Other,
"size is greater than supported"));
"size is greater than supported",
));
}

let mut description_bytes = vec![0u8; size * 4];
Expand Down
Loading

0 comments on commit 94bb5e2

Please sign in to comment.