-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
[package] | ||
name = "extended-isolation-forest" | ||
version = "0.1.0" | ||
description = "rust port of the anomaly detection algorithm" | ||
authors = ["Nico Mandery <[email protected]>"] | ||
edition = "2018" | ||
|
||
#[features] | ||
#use-serde = ["serde", "num/serde"] | ||
|
||
[dependencies] | ||
num = "0.3" | ||
num = "0.4" | ||
rand = { version = "0.8", features = ["alloc"] } | ||
rand_distr = "0.4" | ||
#serde = { version = "1", optional = true, features = ["derive"] } | ||
|
||
#[dev-dependencies] | ||
#bincode = "1.3" | ||
serde = { version = "1", features = ["derive"] } | ||
|
||
[dev-dependencies] | ||
serde_json = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::{convert::TryInto, marker::PhantomData}; | ||
|
||
use serde::{ | ||
de::{SeqAccess, Visitor}, | ||
Deserialize, | ||
Deserializer, ser::SerializeTuple, Serialize, Serializer, | ||
}; | ||
|
||
// from https://github.com/serde-rs/serde/issues/1937#issuecomment-812137971 | ||
|
||
pub fn serialize<S: Serializer, T: Serialize, const N: usize>( | ||
data: &[T; N], | ||
ser: S, | ||
) -> Result<S::Ok, S::Error> { | ||
let mut s = ser.serialize_tuple(N)?; | ||
for item in data { | ||
s.serialize_element(item)?; | ||
} | ||
s.end() | ||
} | ||
|
||
struct ArrayVisitor<T, const N: usize>(PhantomData<T>); | ||
|
||
impl<'de, T, const N: usize> Visitor<'de> for ArrayVisitor<T, N> | ||
where | ||
T: Deserialize<'de>, | ||
{ | ||
type Value = [T; N]; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str(&format!("an array of length {}", N)) | ||
} | ||
|
||
#[inline] | ||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | ||
where | ||
A: SeqAccess<'de>, | ||
{ | ||
// can be optimized using MaybeUninit | ||
let mut data = Vec::with_capacity(N); | ||
for _ in 0..N { | ||
match (seq.next_element())? { | ||
Some(val) => data.push(val), | ||
None => return Err(serde::de::Error::invalid_length(N, &self)), | ||
} | ||
} | ||
match data.try_into() { | ||
Ok(arr) => Ok(arr), | ||
Err(_) => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
pub fn deserialize<'de, D, T, const N: usize>(deserializer: D) -> Result<[T; N], D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: Deserialize<'de>, | ||
{ | ||
deserializer.deserialize_tuple(N, ArrayVisitor::<T, N>(PhantomData)) | ||
} |