Struct subset_map::SubsetMap [] [src]

pub struct SubsetMap<E, P> { /* fields omitted */ }

A map like data structure where the keys are subsets made of combinations of the original sets.

Methods

impl<E, P> SubsetMap<E, P> where
    E: Clone
[src]

[src]

Creates a new instance where the payloads are initialized with a closure that is passed the current subset of elements.

This function assigns values to those combinations where the given closure init returns Some.

Example

use subset_map::*;

let subset_map = SubsetMap::new(&[1, 2], |x| {
    let sum = x.iter().sum::<i32>();
    if sum == 1 {
        None
    } else {
        Some(sum)
    }
});

assert_eq!(subset_map.get(&[1]), None);
assert_eq!(subset_map.get(&[2]), Some(&2));
assert_eq!(subset_map.get(&[1, 2]), Some(&3));
assert_eq!(subset_map.get(&[]), None);
assert_eq!(subset_map.get(&[2, 1]), None);
assert_eq!(subset_map.get(&[0]), None);
assert_eq!(subset_map.get(&[0, 1]), None);

[src]

Creates a new instance where the payloads are initialized with a closure that is passed the current subset of elements.

This fuction will assign an element to each subset.

Example

use subset_map::*;

let subset_map = SubsetMap::fill(&[1, 2], |x| x.iter().sum::<i32>());
assert_eq!(subset_map.get(&[1]), Some(&1));
assert_eq!(subset_map.get(&[2]), Some(&2));
assert_eq!(subset_map.get(&[1, 2]), Some(&3));
assert_eq!(subset_map.get(&[]), None);
assert_eq!(subset_map.get(&[2, 1]), None);
assert_eq!(subset_map.get(&[0]), None);
assert_eq!(subset_map.get(&[0, 1]), None);

[src]

Initializes the SubsetMap with a closure that can fail. This function initializes all those subsets with the returned payloads where the init closure returned an Result::Ok(Option::Some) given that all calls on the closure returned Result::Ok.

Failure of the init closure will result in a failure of the whole initialization process.

Example

The whole initialization process fails.

use subset_map::*;

let subset_map = SubsetMap::init(&[1, 2], |x| {
    let sum = x.iter().sum::<i32>();
    if sum == 1 {
        Ok(Some(sum))
    } else if sum == 2 {
        Ok(None)
    } else {
        Err("bang!")
    }
});

assert_eq!(subset_map, Err("bang!"));

[src]

Initializes the SubsetMap with a closure that can fail. This function initializes all subsets with the returned payloads given that all calls on the closure returned Result::Ok.

Failure of the init closure will result in a failure of the whole initialization process.

Example

use subset_map::*;

let subset_map = SubsetMap::init_filled(&[1, 2], |x| {
    let sum = x.iter().sum::<i32>();
    if sum != 3 {
        Ok(sum)
    } else {
        Err("bang!")
    }
});

assert_eq!(subset_map, Err("bang!"));

[src]

Creates a new instance where the payloads are all initialized with the same value.

Example

use subset_map::*;

let subset_map = SubsetMap::with_value(&[1, 2], || 42);
assert_eq!(subset_map.get(&[1]), Some(&42));
assert_eq!(subset_map.get(&[2]), Some(&42));
assert_eq!(subset_map.get(&[1, 2]), Some(&42));
assert_eq!(subset_map.get(&[]), None);
assert_eq!(subset_map.get(&[2, 1]), None);
assert_eq!(subset_map.get(&[0]), None);
assert_eq!(subset_map.get(&[0, 1]), None);

[src]

Creates a new instance where the payloads are all initialized with the Default::default() value of the payload type. Creates a new instance where the payloads are all initialized with the same value.

Example

use subset_map::*;

let subset_map = SubsetMap::with_default(&[1, 2]);
assert_eq!(subset_map.get(&[1]), Some(&0));
assert_eq!(subset_map.get(&[2]), Some(&0));
assert_eq!(subset_map.get(&[1, 2]), Some(&0));
assert_eq!(subset_map.get(&[]), None);
assert_eq!(subset_map.get(&[2, 1]), None);
assert_eq!(subset_map.get(&[0]), None);
assert_eq!(subset_map.get(&[0, 1]), None);

[src]

Gets a payload by the given subset.

Only "perfect" matches on subset are returned.

The function returns None regardless of whether subset was part of the map or there was no payload assigned to the given subset.

use subset_map::*;

let subset_map = SubsetMap::new(&[1, 2, 3], |x| {
    let payload = x.iter().cloned().collect::<Vec<_>>();
    if payload.len() == 1 {
        None
    } else {
        Some(payload)
    }
});
assert_eq!(subset_map.get(&[1]), None);
assert_eq!(subset_map.get(&[2]), None);
assert_eq!(subset_map.get(&[3]), None);
assert_eq!(subset_map.get(&[1, 2]), Some(&vec![1, 2]));
assert_eq!(subset_map.get(&[2, 3]), Some(&vec![2, 3]));
assert_eq!(subset_map.get(&[1, 3]), Some(&vec![1, 3]));
assert_eq!(subset_map.get(&[1, 2, 3]), Some(&vec![1, 2, 3]));

assert_eq!(subset_map.get(&[]), None);
assert_eq!(subset_map.get(&[7]), None);
assert_eq!(subset_map.get(&[3, 2, 1]), None);
assert_eq!(subset_map.get(&[1, 3, 2]), None);
assert_eq!(subset_map.get(&[3, 2, 1]), None);
assert_eq!(subset_map.get(&[2, 1]), None);

[src]

Looks up a payload by the given subset and returns the corresponding owned value.

The function returns None regardless of wether subset was part of the map or there was no payload assigned to the given subset.

Only perfect matches on subset are returned. See get.

[src]

Looks up a subset and maybe returns the assigned payload.

Elements in subset that are not part of the initial set are skipped.

The returned LookupResult may still contain an optional payload. None indicates that the subset was matched but there was no payload for the given subset.

Use this method if you are interested whether there was a matching subset and then process the maybe present payload. Otherwise use find or lookup.

Example

use subset_map::*;

let subset_map = SubsetMap::new(&[1u32, 2, 3], |x| {
    if x == &[2] {
        None
    } else {
        let payload = x.iter().cloned().collect::<Vec<_>>();
        Some(payload)
    }
});

let empty: &[u32] = &[];

// A perfect match with a payload:
let match_result = subset_map.lookup(&[1]);
assert_eq!(match_result.payload(), Some(&vec![1]));
assert_eq!(match_result.excluded_elements(), empty);
assert_eq!(match_result.is_match(), true);
assert_eq!(match_result.is_perfect(), true);
assert_eq!(match_result.is_excluded(), false);

// A perfect match that has no payload:
let match_result = subset_map.lookup(&[2]);
assert_eq!(match_result.payload(), None);
assert_eq!(match_result.excluded_elements(), empty);
assert_eq!(match_result.is_match(), true);
assert_eq!(match_result.is_perfect(), true);
assert_eq!(match_result.is_excluded(), false);

// There is no answer at all:
let match_result = subset_map.lookup(&[42]);
assert_eq!(match_result.is_no_match(), true);
assert_eq!(match_result.is_perfect(), false);
assert_eq!(match_result.is_excluded(), false);
assert_eq!(match_result.excluded_elements(), empty);

// A nearby match but that has a payload:
let match_result = subset_map.lookup(&[3,1]);
assert_eq!(match_result.payload(), Some(&vec![3]));
assert_eq!(match_result.excluded_elements(), &[1]);
assert_eq!(match_result.is_perfect(), false);
assert_eq!(match_result.is_excluded(), true);
assert_eq!(match_result.is_match(), true);

[src]

Finds a payload by the given subset.

Elements in subset that are not part of the initial set are skipped.

If there was no match or no payload for the given subset FindResult::NotFound is returned.

Use this function if you are mostly interested in payloads and how they were matched. Otherwise use lookup or get

Example

use subset_map::*;

let subset_map = SubsetMap::new(&[1u32, 2, 3], |x| {
    if x == &[2] {
        None
    } else {
        let payload = x.iter().cloned().collect::<Vec<_>>();
        Some(payload)
    }
});

let empty: &[u32] = &[];

// A perfect match with a payload:
let match_result = subset_map.find(&[1]);

assert_eq!(match_result.payload(), Some(&vec![1]));
assert_eq!(match_result.is_found(), true);
assert_eq!(match_result.is_found_and_perfect(), true);
assert_eq!(match_result.is_found_and_excluded(), false);
assert_eq!(match_result.excluded_elements(), empty);

// A perfect match that has no payload:
let match_result = subset_map.find(&[2]);

assert_eq!(match_result.payload(), None);
assert_eq!(match_result.is_found(), false);
assert_eq!(match_result.is_found_and_perfect(), false);
assert_eq!(match_result.is_found_and_excluded(), false);
assert_eq!(match_result.excluded_elements(), empty);

// There is no answer at all:
let match_result = subset_map.find(&[42]);

assert_eq!(match_result.payload(), None);
assert_eq!(match_result.is_not_found(), true);
assert_eq!(match_result.is_found_and_perfect(), false);
assert_eq!(match_result.is_found_and_excluded(), false);
assert_eq!(match_result.excluded_elements(), empty);

// A nearby match but that has a payload:
let match_result = subset_map.find(&[3,1]);

assert_eq!(match_result.is_found_and_perfect(), false);
assert_eq!(match_result.is_found_and_excluded(), true);
assert_eq!(match_result.is_found(), true);
assert_eq!(match_result.payload(), Some(&vec![3]));
assert_eq!(match_result.excluded_elements(), &[1]);

[src]

Sets the payload of all subsets to None where the given payload does not fulfill the predicate

[src]

Executes the given mutable closure f on the value of each node.

[src]

Executes the given mutable closure f on the value of each node until the first closure returns false.

[src]

Executes the given mutable closure f on the payload of each node

[src]

Executes the given mutable closure f on the payload of each node until the first closure returns false.

[src]

Executes the given mutable closure f on the payload of each node

[src]

Executes the given mutable closure f on the payload of each node

[src]

Returns true if there are nodes and all of these have a payload set.

[src]

Returns true if there are no subsets or all of these have no payload set.

Example

An empty map has no values:

use subset_map::*;

let subset_map = SubsetMap::<u8, u8>::with_default(&[]);

assert_eq!(subset_map.no_subset_with_value(), true);

An map with a set entry has values:

use subset_map::*;

let subset_map = SubsetMap::<u8, u8>::with_default(&[1]);

assert_eq!(subset_map.no_subset_with_value(), false);

An non empty map where at least one subset has a value:

use subset_map::*;

let mut subset_map = SubsetMap::fill(&[1, 2], |c| c.len());

subset_map.filter_values(|p| *p == 2);

assert_eq!(subset_map.no_subset_with_value(), false);

An non empty map where no subset has a value:

use subset_map::*;

let mut subset_map = SubsetMap::<u8, u8>::with_default(&[1, 2]);

// Set all payloads to `None`
subset_map.filter_values(|p| false);

assert_eq!(subset_map.no_subset_with_value(), true);

[src]

Returns true if the map is empty and contains no subsets.

[src]

The number of subsets in this map

Trait Implementations

impl<E: Debug, P: Debug> Debug for SubsetMap<E, P>
[src]

[src]

Formats the value using the given formatter. Read more

impl<E: Clone, P: Clone> Clone for SubsetMap<E, P>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<E: PartialEq, P: PartialEq> PartialEq for SubsetMap<E, P>
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl<E: Eq, P: Eq> Eq for SubsetMap<E, P>
[src]

impl<E, P> Default for SubsetMap<E, P>
[src]

[src]

Returns the "default value" for a type. Read more

Auto Trait Implementations

impl<E, P> Send for SubsetMap<E, P> where
    E: Send,
    P: Send

impl<E, P> Sync for SubsetMap<E, P> where
    E: Sync,
    P: Sync