Skip to content

Commit

Permalink
Merge pull request #10 from alecmocatta/nix-zeroable
Browse files Browse the repository at this point in the history
Replace the Zeroable struct with Option
  • Loading branch information
mergify[bot] authored Jun 17, 2020
2 parents b6b06ac + fbcb908 commit f8038f5
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 66 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "streaming_algorithms"
version = "0.1.2"
version = "0.2.0"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <[email protected]>"]
categories = ["data-structures","algorithms","science"]
Expand All @@ -10,7 +10,7 @@ SIMD-accelerated implementations of various streaming algorithms, including Coun
"""
repository = "https://github.com/alecmocatta/streaming_algorithms"
homepage = "https://github.com/alecmocatta/streaming_algorithms"
documentation = "https://docs.rs/streaming_algorithms/0.1.2"
documentation = "https://docs.rs/streaming_algorithms/0.2.0"
readme = "README.md"
edition = "2018"

Expand All @@ -20,7 +20,6 @@ maintenance = { status = "actively-developed" }

[dependencies]
twox-hash = "1.1"
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }
rand = { version = "0.7", features = ["small_rng"] }
packed_simd = { version = "0.3", features = ["into_bits"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/streaming_algorithms.svg?maxAge=2592000)](#License)
[![Build Status](https://dev.azure.com/alecmocatta/streaming_algorithms/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/streaming_algorithms/_build?definitionId=16)

[📖 Docs](https://docs.rs/streaming_algorithms/0.1.2) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects)
[📖 Docs](https://docs.rs/streaming_algorithms/0.2.0/streaming_algorithms/) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects)

SIMD-accelerated implementations of various [streaming algorithms](https://en.wikipedia.org/wiki/Streaming_algorithm).

Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
parameters:
endpoint: alecmocatta
default:
rust_toolchain: nightly
rust_lint_toolchain: nightly-2020-06-09
rust_toolchain: nightly-2020-06-10
rust_lint_toolchain: nightly-2020-06-10
rust_flags: ''
rust_features: ''
rust_target_check: ''
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//!
//! As these implementations are often in hot code paths, unsafe is used, albeit only when necessary to a) achieve the asymptotically optimal algorithm or b) mitigate an observed bottleneck.

#![doc(html_root_url = "https://docs.rs/streaming_algorithms/0.1.2")]
#![doc(html_root_url = "https://docs.rs/streaming_algorithms/0.2.0")]
#![feature(specialization)]
#![warn(
missing_copy_implementations,
Expand Down
62 changes: 3 additions & 59 deletions src/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,76 +152,20 @@ impl<'a, A: Hash + Eq + Clone + Debug, C: Ord + Debug + 'a> Debug for TopIter<'a
}
}

/// For the result of a `std::iter::sum()` when an additive identity (i.e. zero) can't be constructed (in the case where we're summing an empty iterator).
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
pub enum Zeroable<T> {
/// Zero
Zero,
/// Nonzero
Nonzero(T),
}
#[allow(clippy::missing_errors_doc)]
impl<T> Zeroable<T> {
/// Transform to a `Result<T, E>`.
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
match self {
Zeroable::Nonzero(v) => Ok(v),
Zeroable::Zero => Err(err),
}
}
/// Converts to an `Option<T>`.
pub fn nonzero(self) -> Option<T> {
match self {
Zeroable::Nonzero(v) => Some(v),
Zeroable::Zero => None,
}
}
}
impl<T> From<Option<T>> for Zeroable<T> {
fn from(a: Option<T>) -> Self {
match a {
Some(a) => Zeroable::Nonzero(a),
None => Zeroable::Zero,
}
}
}
impl<T> iter::Sum for Zeroable<T>
where
Self: iter::Sum<T>,
{
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.filter_map(|item| {
if let Zeroable::Nonzero(item) = item {
Some(item)
} else {
None
}
})
.sum()
}
}

impl<
A: Hash + Eq + Clone,
C: Ord + New + Clone + for<'a> ops::AddAssign<&'a C> + for<'a> UnionAssign<&'a C> + Intersect,
> iter::Sum<Top<A, C>> for Zeroable<Top<A, C>>
> iter::Sum<Top<A, C>> for Option<Top<A, C>>
{
fn sum<I>(mut iter: I) -> Self
where
I: Iterator<Item = Top<A, C>>,
{
let mut total = if let Some(total) = iter.next() {
total
} else {
return Zeroable::Zero;
};
let mut total = iter.next()?;
for sample in iter {
total += sample;
}
Zeroable::Nonzero(total)
Some(total)
}
}
impl<
Expand Down

0 comments on commit f8038f5

Please sign in to comment.