Struct SubsetMap

Source
pub struct SubsetMap<E, P> { /* private fields */ }
Expand description

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

Implementations§

Source§

impl<E, P> SubsetMap<E, P>
where E: Clone,

Source

pub fn new<F>(elements: &[E], init: F) -> SubsetMap<E, P>
where F: FnMut(&[E]) -> Option<P>,

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);
Source

pub fn fill<F>(elements: &[E], init: F) -> SubsetMap<E, P>
where F: FnMut(&[E]) -> P,

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);
Examples found in repository?
examples/walk.rs (lines 9-12)
5fn main() {
6    let elements: Vec<_> = (0..5).collect();
7
8    let mut n = 0;
9    let map = SubsetMap::fill(&elements, |_x| {
10        n += 1;
11        n
12    });
13
14    map.walk(|elements, payload| println!("{:?} -> {:?}", elements, payload))
15}
More examples
Hide additional examples
examples/show_operations.rs (lines 11-14)
6fn main() {
7    let elements: Vec<_> = (0..17).collect();
8    let start = Instant::now();
9
10    let mut n = 0;
11    let map = SubsetMap::fill(&elements, |_x| {
12        n += 1;
13        n
14    });
15
16    println!(
17        "[CREATE_SIMPLE]: Combinations: {}(Size: {}) -> Time: {:?}",
18        n,
19        map.size(),
20        start.elapsed()
21    );
22
23    drop(map);
24
25    let start = Instant::now();
26
27    let mut n = 0usize;
28    let mut combinations = Vec::new();
29    let map = SubsetMap::fill(&elements, |x| {
30        combinations.push(x.to_vec());
31        n += 1;
32        n
33    });
34
35    println!(
36        "[CREATE_FOR_QUERY]: Combinations: {}(Size: {}) -> Time: {:?}",
37        n,
38        map.size(),
39        start.elapsed()
40    );
41
42    let start = Instant::now();
43
44    let mut n = 0usize;
45    for combination in combinations {
46        if let Some(_p) = map.get(&combination) {
47            n += 1;
48        }
49    }
50
51    println!(
52        "[GET]: Combinations: {} -> Time: {:?} {}",
53        map.size(),
54        start.elapsed(),
55        n
56    );
57
58    drop(map);
59
60    let mut n = 0usize;
61    let mut combinations = Vec::new();
62    let map = SubsetMap::fill(&elements, |x| {
63        combinations.push(x.to_vec());
64        n += 1;
65        n
66    });
67
68    let start = Instant::now();
69
70    let mut n = 0usize;
71    for combination in combinations {
72        if let LookupResult::Perfect(Some(_p)) = map.lookup(&combination) {
73            n += 1;
74        }
75    }
76
77    println!(
78        "[LOOKUP]: Combinations: {} -> Time: {:?} {}",
79        map.size(),
80        start.elapsed(),
81        n
82    );
83
84    drop(map);
85
86    let mut n = 0usize;
87    let mut combinations = Vec::new();
88    let map = SubsetMap::fill(&elements, |x| {
89        combinations.push(x.to_vec());
90        n += 1;
91        n
92    });
93
94    let start = Instant::now();
95
96    let mut n = 0usize;
97    for combination in combinations {
98        if let FindResult::Perfect(_) = map.find(&combination) {
99            n += 1;
100        }
101    }
102
103    println!(
104        "[FIND]: Combinations: {} -> Time: {:?} {}",
105        map.size(),
106        start.elapsed(),
107        n
108    );
109}
Source

pub fn init<F, X>(elements: &[E], init: F) -> Result<SubsetMap<E, P>, X>
where F: FnMut(&[E]) -> Result<Option<P>, X>,

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!"));
Source

pub fn init_filled<F, X>(elements: &[E], init: F) -> Result<SubsetMap<E, P>, X>
where F: FnMut(&[E]) -> Result<P, X>,

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!"));
Source

pub fn with_value<F>(elements: &[E], init: F) -> SubsetMap<E, P>
where F: FnMut() -> P,

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);
Source

pub fn with_default(elements: &[E]) -> SubsetMap<E, P>
where P: Default,

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);
Source

pub fn get<'a>(&'a self, subset: &[E]) -> Option<&'a P>
where E: Eq,

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);
Examples found in repository?
examples/show_operations.rs (line 46)
6fn main() {
7    let elements: Vec<_> = (0..17).collect();
8    let start = Instant::now();
9
10    let mut n = 0;
11    let map = SubsetMap::fill(&elements, |_x| {
12        n += 1;
13        n
14    });
15
16    println!(
17        "[CREATE_SIMPLE]: Combinations: {}(Size: {}) -> Time: {:?}",
18        n,
19        map.size(),
20        start.elapsed()
21    );
22
23    drop(map);
24
25    let start = Instant::now();
26
27    let mut n = 0usize;
28    let mut combinations = Vec::new();
29    let map = SubsetMap::fill(&elements, |x| {
30        combinations.push(x.to_vec());
31        n += 1;
32        n
33    });
34
35    println!(
36        "[CREATE_FOR_QUERY]: Combinations: {}(Size: {}) -> Time: {:?}",
37        n,
38        map.size(),
39        start.elapsed()
40    );
41
42    let start = Instant::now();
43
44    let mut n = 0usize;
45    for combination in combinations {
46        if let Some(_p) = map.get(&combination) {
47            n += 1;
48        }
49    }
50
51    println!(
52        "[GET]: Combinations: {} -> Time: {:?} {}",
53        map.size(),
54        start.elapsed(),
55        n
56    );
57
58    drop(map);
59
60    let mut n = 0usize;
61    let mut combinations = Vec::new();
62    let map = SubsetMap::fill(&elements, |x| {
63        combinations.push(x.to_vec());
64        n += 1;
65        n
66    });
67
68    let start = Instant::now();
69
70    let mut n = 0usize;
71    for combination in combinations {
72        if let LookupResult::Perfect(Some(_p)) = map.lookup(&combination) {
73            n += 1;
74        }
75    }
76
77    println!(
78        "[LOOKUP]: Combinations: {} -> Time: {:?} {}",
79        map.size(),
80        start.elapsed(),
81        n
82    );
83
84    drop(map);
85
86    let mut n = 0usize;
87    let mut combinations = Vec::new();
88    let map = SubsetMap::fill(&elements, |x| {
89        combinations.push(x.to_vec());
90        n += 1;
91        n
92    });
93
94    let start = Instant::now();
95
96    let mut n = 0usize;
97    for combination in combinations {
98        if let FindResult::Perfect(_) = map.find(&combination) {
99            n += 1;
100        }
101    }
102
103    println!(
104        "[FIND]: Combinations: {} -> Time: {:?} {}",
105        map.size(),
106        start.elapsed(),
107        n
108    );
109}
Source

pub fn get_owned(&self, subset: &[E]) -> Option<P::Owned>
where E: Eq, P: ToOwned,

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.

Source

pub fn lookup<'a>(&'a self, subset: &[E]) -> LookupResult<'a, E, P>
where E: Eq,

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);
Examples found in repository?
examples/show_operations.rs (line 72)
6fn main() {
7    let elements: Vec<_> = (0..17).collect();
8    let start = Instant::now();
9
10    let mut n = 0;
11    let map = SubsetMap::fill(&elements, |_x| {
12        n += 1;
13        n
14    });
15
16    println!(
17        "[CREATE_SIMPLE]: Combinations: {}(Size: {}) -> Time: {:?}",
18        n,
19        map.size(),
20        start.elapsed()
21    );
22
23    drop(map);
24
25    let start = Instant::now();
26
27    let mut n = 0usize;
28    let mut combinations = Vec::new();
29    let map = SubsetMap::fill(&elements, |x| {
30        combinations.push(x.to_vec());
31        n += 1;
32        n
33    });
34
35    println!(
36        "[CREATE_FOR_QUERY]: Combinations: {}(Size: {}) -> Time: {:?}",
37        n,
38        map.size(),
39        start.elapsed()
40    );
41
42    let start = Instant::now();
43
44    let mut n = 0usize;
45    for combination in combinations {
46        if let Some(_p) = map.get(&combination) {
47            n += 1;
48        }
49    }
50
51    println!(
52        "[GET]: Combinations: {} -> Time: {:?} {}",
53        map.size(),
54        start.elapsed(),
55        n
56    );
57
58    drop(map);
59
60    let mut n = 0usize;
61    let mut combinations = Vec::new();
62    let map = SubsetMap::fill(&elements, |x| {
63        combinations.push(x.to_vec());
64        n += 1;
65        n
66    });
67
68    let start = Instant::now();
69
70    let mut n = 0usize;
71    for combination in combinations {
72        if let LookupResult::Perfect(Some(_p)) = map.lookup(&combination) {
73            n += 1;
74        }
75    }
76
77    println!(
78        "[LOOKUP]: Combinations: {} -> Time: {:?} {}",
79        map.size(),
80        start.elapsed(),
81        n
82    );
83
84    drop(map);
85
86    let mut n = 0usize;
87    let mut combinations = Vec::new();
88    let map = SubsetMap::fill(&elements, |x| {
89        combinations.push(x.to_vec());
90        n += 1;
91        n
92    });
93
94    let start = Instant::now();
95
96    let mut n = 0usize;
97    for combination in combinations {
98        if let FindResult::Perfect(_) = map.find(&combination) {
99            n += 1;
100        }
101    }
102
103    println!(
104        "[FIND]: Combinations: {} -> Time: {:?} {}",
105        map.size(),
106        start.elapsed(),
107        n
108    );
109}
Source

pub fn find<'a>(&'a self, subset: &[E]) -> FindResult<'a, E, P>
where E: Eq,

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]);
Examples found in repository?
examples/show_operations.rs (line 98)
6fn main() {
7    let elements: Vec<_> = (0..17).collect();
8    let start = Instant::now();
9
10    let mut n = 0;
11    let map = SubsetMap::fill(&elements, |_x| {
12        n += 1;
13        n
14    });
15
16    println!(
17        "[CREATE_SIMPLE]: Combinations: {}(Size: {}) -> Time: {:?}",
18        n,
19        map.size(),
20        start.elapsed()
21    );
22
23    drop(map);
24
25    let start = Instant::now();
26
27    let mut n = 0usize;
28    let mut combinations = Vec::new();
29    let map = SubsetMap::fill(&elements, |x| {
30        combinations.push(x.to_vec());
31        n += 1;
32        n
33    });
34
35    println!(
36        "[CREATE_FOR_QUERY]: Combinations: {}(Size: {}) -> Time: {:?}",
37        n,
38        map.size(),
39        start.elapsed()
40    );
41
42    let start = Instant::now();
43
44    let mut n = 0usize;
45    for combination in combinations {
46        if let Some(_p) = map.get(&combination) {
47            n += 1;
48        }
49    }
50
51    println!(
52        "[GET]: Combinations: {} -> Time: {:?} {}",
53        map.size(),
54        start.elapsed(),
55        n
56    );
57
58    drop(map);
59
60    let mut n = 0usize;
61    let mut combinations = Vec::new();
62    let map = SubsetMap::fill(&elements, |x| {
63        combinations.push(x.to_vec());
64        n += 1;
65        n
66    });
67
68    let start = Instant::now();
69
70    let mut n = 0usize;
71    for combination in combinations {
72        if let LookupResult::Perfect(Some(_p)) = map.lookup(&combination) {
73            n += 1;
74        }
75    }
76
77    println!(
78        "[LOOKUP]: Combinations: {} -> Time: {:?} {}",
79        map.size(),
80        start.elapsed(),
81        n
82    );
83
84    drop(map);
85
86    let mut n = 0usize;
87    let mut combinations = Vec::new();
88    let map = SubsetMap::fill(&elements, |x| {
89        combinations.push(x.to_vec());
90        n += 1;
91        n
92    });
93
94    let start = Instant::now();
95
96    let mut n = 0usize;
97    for combination in combinations {
98        if let FindResult::Perfect(_) = map.find(&combination) {
99            n += 1;
100        }
101    }
102
103    println!(
104        "[FIND]: Combinations: {} -> Time: {:?} {}",
105        map.size(),
106        start.elapsed(),
107        n
108    );
109}
Source

pub fn filter_values<F>(&mut self, predicate: F)
where F: FnMut(&P) -> bool,

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

Source

pub fn walk_values<F>(&self, f: F)
where F: FnMut(&P),

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

Source

pub fn walk_values_until<F>(&self, f: F)
where F: FnMut(&P) -> bool,

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

Source

pub fn walk_payloads<F>(&self, f: F)
where F: FnMut(Option<&P>),

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

Source

pub fn walk_payloads_until<F>(&self, f: F)
where F: FnMut(Option<&P>) -> bool,

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

Source

pub fn walk<F>(&self, f: F)
where F: FnMut(&[&E], Option<&P>),

Walk all elements with their payloads

Examples found in repository?
examples/walk.rs (line 14)
5fn main() {
6    let elements: Vec<_> = (0..5).collect();
7
8    let mut n = 0;
9    let map = SubsetMap::fill(&elements, |_x| {
10        n += 1;
11        n
12    });
13
14    map.walk(|elements, payload| println!("{:?} -> {:?}", elements, payload))
15}
Source

pub fn for_each_value<F>(&self, f: F)
where F: FnMut(&P),

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

Source

pub fn for_each_payload<F>(&self, f: F)
where F: FnMut(Option<&P>),

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

Source

pub fn all_subsets_have_values(&self) -> bool

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

Source

pub fn no_subset_with_value(&self) -> bool

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);
Source

pub fn is_empty(&self) -> bool

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

Source

pub fn size(&self) -> usize

The number of subsets in this map

Examples found in repository?
examples/show_operations.rs (line 19)
6fn main() {
7    let elements: Vec<_> = (0..17).collect();
8    let start = Instant::now();
9
10    let mut n = 0;
11    let map = SubsetMap::fill(&elements, |_x| {
12        n += 1;
13        n
14    });
15
16    println!(
17        "[CREATE_SIMPLE]: Combinations: {}(Size: {}) -> Time: {:?}",
18        n,
19        map.size(),
20        start.elapsed()
21    );
22
23    drop(map);
24
25    let start = Instant::now();
26
27    let mut n = 0usize;
28    let mut combinations = Vec::new();
29    let map = SubsetMap::fill(&elements, |x| {
30        combinations.push(x.to_vec());
31        n += 1;
32        n
33    });
34
35    println!(
36        "[CREATE_FOR_QUERY]: Combinations: {}(Size: {}) -> Time: {:?}",
37        n,
38        map.size(),
39        start.elapsed()
40    );
41
42    let start = Instant::now();
43
44    let mut n = 0usize;
45    for combination in combinations {
46        if let Some(_p) = map.get(&combination) {
47            n += 1;
48        }
49    }
50
51    println!(
52        "[GET]: Combinations: {} -> Time: {:?} {}",
53        map.size(),
54        start.elapsed(),
55        n
56    );
57
58    drop(map);
59
60    let mut n = 0usize;
61    let mut combinations = Vec::new();
62    let map = SubsetMap::fill(&elements, |x| {
63        combinations.push(x.to_vec());
64        n += 1;
65        n
66    });
67
68    let start = Instant::now();
69
70    let mut n = 0usize;
71    for combination in combinations {
72        if let LookupResult::Perfect(Some(_p)) = map.lookup(&combination) {
73            n += 1;
74        }
75    }
76
77    println!(
78        "[LOOKUP]: Combinations: {} -> Time: {:?} {}",
79        map.size(),
80        start.elapsed(),
81        n
82    );
83
84    drop(map);
85
86    let mut n = 0usize;
87    let mut combinations = Vec::new();
88    let map = SubsetMap::fill(&elements, |x| {
89        combinations.push(x.to_vec());
90        n += 1;
91        n
92    });
93
94    let start = Instant::now();
95
96    let mut n = 0usize;
97    for combination in combinations {
98        if let FindResult::Perfect(_) = map.find(&combination) {
99            n += 1;
100        }
101    }
102
103    println!(
104        "[FIND]: Combinations: {} -> Time: {:?} {}",
105        map.size(),
106        start.elapsed(),
107        n
108    );
109}

Trait Implementations§

Source§

impl<E: Clone, P: Clone> Clone for SubsetMap<E, P>

Source§

fn clone(&self) -> SubsetMap<E, P>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Debug, P: Debug> Debug for SubsetMap<E, P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E, P> Default for SubsetMap<E, P>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<E: PartialEq, P: PartialEq> PartialEq for SubsetMap<E, P>

Source§

fn eq(&self, other: &SubsetMap<E, P>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<E: Eq, P: Eq> Eq for SubsetMap<E, P>

Source§

impl<E, P> StructuralPartialEq for SubsetMap<E, P>

Auto Trait Implementations§

§

impl<E, P> Freeze for SubsetMap<E, P>

§

impl<E, P> RefUnwindSafe for SubsetMap<E, P>

§

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,

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.