Skip to content

Commit

Permalink
Merge pull request #9 from alecmocatta/serialize-zeroable
Browse files Browse the repository at this point in the history
Make Zeroable (de)serializable
  • Loading branch information
mergify[bot] committed Jun 9, 2020
2 parents 67b68a6 + c07eb2e commit b4e6da6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 43 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "streaming_algorithms"
version = "0.1.1"
version = "0.1.2"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <alec@mocatta.net>"]
categories = ["data-structures","algorithms","science"]
Expand All @@ -10,12 +10,12 @@ 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.1"
documentation = "https://docs.rs/streaming_algorithms/0.1.2"
readme = "README.md"
edition = "2018"

[badges]
azure-devops = { project = "alecmocatta/streaming_algorithms", pipeline = "tests" }
azure-devops = { project = "alecmocatta/streaming_algorithms", pipeline = "tests", build = "16" }
maintenance = { status = "actively-developed" }

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -2,9 +2,9 @@

[![Crates.io](https://img.shields.io/crates/v/streaming_algorithms.svg?maxAge=86400)](https://crates.io/crates/streaming_algorithms)
[![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/latest?branchName=master)
[![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.1)
[📖 Docs](https://docs.rs/streaming_algorithms/0.1.2) | [💬 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
12 changes: 6 additions & 6 deletions azure-pipelines.yml
Expand Up @@ -14,19 +14,19 @@ jobs:
endpoint: alecmocatta
default:
rust_toolchain: nightly
rust_lint_toolchain: nightly-2019-07-19
rust_lint_toolchain: nightly-2020-06-09
rust_flags: ''
rust_features: ''
rust_target_check: ''
rust_target_build: ''
rust_target_run: ''
matrix:
windows:
imageName: 'vs2017-win2016'
rust_target_run: 'x86_64-pc-windows-msvc i686-pc-windows-msvc' # currently broken building crate-type=lib: x86_64-pc-windows-gnu i686-pc-windows-gnu
imageName: 'windows-latest'
rust_target_run: 'x86_64-pc-windows-msvc i686-pc-windows-msvc x86_64-pc-windows-gnu'
mac:
imageName: 'macos-10.13'
rust_target_run: 'x86_64-apple-darwin i686-apple-darwin'
imageName: 'macos-latest'
rust_target_run: 'x86_64-apple-darwin'
linux:
imageName: 'ubuntu-16.04'
imageName: 'ubuntu-latest'
rust_target_run: 'x86_64-unknown-linux-gnu i686-unknown-linux-gnu x86_64-unknown-linux-musl i686-unknown-linux-musl'
12 changes: 6 additions & 6 deletions src/distinct.rs
Expand Up @@ -54,7 +54,7 @@ use std::{
use twox_hash::XxHash;

mod consts;
use self::consts::*;
use self::consts::{BIAS_DATA, RAW_ESTIMATE_DATA, TRESHOLD_DATA};

/// Like [`HyperLogLog`] but implements `Ord` and `Eq` by using the estimate of the cardinality.
#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -429,7 +429,7 @@ impl<V: ?Sized> IntersectPlusUnionIsPlus for HyperLogLog<V> {

#[cfg(target_feature = "avx512bw")] // TODO
mod simd_types {
use super::*;
use super::packed_simd;
pub type u8s = packed_simd::u8x64;
pub type u8s_sad_out = packed_simd::u64x8;
pub type f32s = packed_simd::f32x16;
Expand All @@ -439,7 +439,7 @@ mod simd_types {
#[cfg(target_feature = "avx2")]
mod simd_types {
#![allow(non_camel_case_types)]
use super::*;
use super::packed_simd;
pub type u8s = packed_simd::u8x32;
pub type u8s_sad_out = packed_simd::u64x4;
pub type f32s = packed_simd::f32x8;
Expand All @@ -449,7 +449,7 @@ mod simd_types {
#[cfg(all(not(target_feature = "avx2"), target_feature = "sse2"))]
mod simd_types {
#![allow(non_camel_case_types)]
use super::*;
use super::packed_simd;
pub type u8s = packed_simd::u8x16;
pub type u8s_sad_out = packed_simd::u64x2;
pub type f32s = packed_simd::f32x4;
Expand All @@ -459,14 +459,14 @@ mod simd_types {
#[cfg(all(not(target_feature = "avx2"), not(target_feature = "sse2")))]
mod simd_types {
#![allow(non_camel_case_types)]
use super::*;
use super::packed_simd;
pub type u8s = packed_simd::u8x8;
pub type u8s_sad_out = u64;
pub type f32s = packed_simd::f32x2;
pub type u32s = packed_simd::u32x2;
pub type u8sq = packed_simd::u8x2;
}
use self::simd_types::*;
use self::simd_types::{f32s, u32s, u8s, u8s_sad_out, u8sq};

struct Sad<X>(PhantomData<fn(X)>);
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
@@ -1,6 +1,8 @@
//! SIMD-accelerated implementations of various [streaming algorithms](https://en.wikipedia.org/wiki/Streaming_algorithm).
//!
//! **[Crates.io](https://crates.io/crates/streaming_algorithms) │ [Repo](https://github.com/alecmocatta/streaming_algorithms)**
//! <p style="font-family: 'Fira Sans',sans-serif;padding:0.3em 0"><strong>
//! <a href="https://crates.io/crates/streaming_algorithms">📦&nbsp;&nbsp;Crates.io</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://github.com/alecmocatta/streaming_algorithms">📑&nbsp;&nbsp;GitHub</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://constellation.zulipchat.com/#narrow/stream/213236-subprojects">💬&nbsp;&nbsp;Chat</a>
//! </strong></p>
//!
//! This library is a work in progress. PRs are very welcome! Currently implemented algorithms include:
//!
Expand All @@ -21,8 +23,8 @@
//!
//! 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.1")]
#![feature(specialization, try_trait)]
#![doc(html_root_url = "https://docs.rs/streaming_algorithms/0.1.2")]
#![feature(specialization)]
#![warn(
missing_copy_implementations,
missing_debug_implementations,
Expand All @@ -44,7 +46,10 @@
clippy::op_ref,
clippy::needless_pass_by_value,
clippy::suspicious_op_assign_impl,
clippy::float_cmp
clippy::float_cmp,
clippy::unsafe_derive_deserialize,
clippy::must_use_candidate,
clippy::unused_self
)]

mod count_min;
Expand Down
1 change: 0 additions & 1 deletion src/sample.rs
Expand Up @@ -220,7 +220,6 @@ impl<T> ops::AddAssign for SampleUnstable<T> {
#[cfg(test)]
mod test {
use super::*;
use rand;
use std::collections::HashMap;

#[test]
Expand Down
28 changes: 7 additions & 21 deletions src/top.rs
Expand Up @@ -153,13 +153,14 @@ 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, Debug)]
#[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> {
Expand All @@ -184,25 +185,6 @@ impl<T> From<Option<T>> for Zeroable<T> {
}
}
}
impl<T> std::ops::Try for Zeroable<T> {
type Ok = T;
type Error = std::option::NoneError;

#[inline]
fn into_result(self) -> Result<T, std::option::NoneError> {
self.ok_or(std::option::NoneError)
}

#[inline]
fn from_ok(v: T) -> Self {
Zeroable::Nonzero(v)
}

#[inline]
fn from_error(_: std::option::NoneError) -> Self {
Zeroable::Zero
}
}
impl<T> iter::Sum for Zeroable<T>
where
Self: iter::Sum<T>,
Expand Down Expand Up @@ -231,7 +213,11 @@ impl<
where
I: Iterator<Item = Top<A, C>>,
{
let mut total = iter.next()?;
let mut total = if let Some(total) = iter.next() {
total
} else {
return Zeroable::Zero;
};
for sample in iter {
total += sample;
}
Expand Down

0 comments on commit b4e6da6

Please sign in to comment.