[][src]Struct xor_distance_exercise::xor_distance::XorDistance

pub struct XorDistance<T: PrimInt + Unsigned> { /* fields omitted */ }

Xor distance structure holding set of Unsigned Integer points.

Examples

extern crate xor_distance_exercise;

use xor_distance_exercise::xor_distance::XorDistance;

let xor_distance: XorDistance<u64> = XorDistance::new(vec![
    0, 1, 2, 4, 6, 8, 12, 18, 19, 20, 21, 22, 406, 407, 408, 409, 410, 444, 445,
]);

// Get four xor-closest number to the position number 300, ordered from the closest to the 4-th closest.
let result = xor_distance.closest(300, 4);

// Reverse the operation to get a possible position number.
let guess_pos = xor_distance.reverse_closest(&result).unwrap();

Methods

impl<T: PrimInt + BitOps + Unsigned> XorDistance<T>[src]

pub fn new(points: Vec<T>) -> Self[src]

pub fn closest(&self, x: T, count: usize) -> Vec<T>[src]

Return up to requested count of closest points to the provided x, ordered from the closest to the n-th closest, where n is the count.

The returned closest points count my be lower than the specified count and equal to all points count only in the case that: count > points.len().

Examples

extern crate xor_distance_exercise;

use xor_distance_exercise::xor_distance::XorDistance;

let xor_distance: XorDistance<u64> = XorDistance::new(vec![
    0, 1, 2, 4, 6, 8, 12, 18, 19, 20, 21, 22, 406, 407, 408, 409, 410, 444, 445,
]);

let x = 200;
let count = 10;

let closest_points = xor_distance.closest(x, count);

pub fn reverse_closest(&self, closest_points: &[T]) -> Option<T>[src]

Return a Some(x) such that self.closest(x) equals closest_points and return None in case such a x does not exists.

Examples

extern crate xor_distance_exercise;

use xor_distance_exercise::xor_distance::XorDistance;

let xor_distance: XorDistance<u64> = XorDistance::new(vec![
    0, 1, 2, 4, 6, 8, 12, 18, 19, 20, 21, 22, 406, 407, 408, 409, 410, 444, 445,
]);

let x = 200;
let count = 10;

// Get closest points and reversed guess of `x`
let closest_points = xor_distance.closest(x, count);
let x_guess = xor_distance.reverse_closest(&closest_points).unwrap();

// Check that both `x` and `guess_x` produce the same result.
assert_eq!(closest_points, xor_distance.closest(x_guess, count));

pub fn form_inequalities(&self, closest_points: &[T]) -> Vec<(T, T)>[src]

Auto Trait Implementations

impl<T> Send for XorDistance<T> where
    T: Send

impl<T> Sync for XorDistance<T> where
    T: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]