1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
//! This module contains an implementation of a multi-set supporting a threshold //! operation. //! //! The concept of threshold is explained in detail in [this blog post](https://vitorenes.org/post/2018/11/threshold-union/). //! //! # Examples //! ``` //! use std::iter::FromIterator; //! use threshold::*; //! //! let mut mset = MultiSet::new(); //! //! mset.add(vec![(17, 1), (23, 1)]); //! assert_eq!(mset.threshold(1), vec![&17, &23]); //! //! mset.add(vec![(17, 1), (42, 3)]); //! assert_eq!(mset.threshold(1), vec![&17, &23, &42]); //! assert_eq!(mset.threshold(2), vec![&17, &42]); //! assert_eq!(mset.threshold(3), vec![&42]); //! ``` use crate::Count; use std::collections::btree_map::{self, BTreeMap}; use std::iter::FromIterator; #[derive(Debug, Clone, PartialEq, Eq)] pub struct MultiSet<E: Ord, C: Count> { /// Associate a count to each element occurrences: BTreeMap<E, C>, } impl<E: Ord, C: Count> MultiSet<E, C> { /// Returns a new `MultiSet` instance. #[allow(clippy::new_without_default)] pub fn new() -> Self { MultiSet { occurrences: BTreeMap::new(), } } /// Creates a new `MultiSet` from an iterator of tuples (elem, elem count). /// /// # Examples /// ``` /// use threshold::*; /// /// let mset = MultiSet::from(vec![(17, 1), (23, 2)]); /// assert_eq!(mset.count(&17), 1); /// assert_eq!(mset.count(&23), 2); /// ``` pub fn from<I: IntoIterator<Item = (E, C)>>(iter: I) -> Self { MultiSet { occurrences: BTreeMap::from_iter(iter), } } /// Adds several elements (each with an associated count) to the `MultiSet`. /// /// # Examples /// ``` /// use threshold::*; /// /// let mut mset = MultiSet::new(); /// assert_eq!(mset.count(&17), 0); /// /// mset.add(vec![(17, 1), (23, 2)]); /// assert_eq!(mset.count(&17), 1); /// assert_eq!(mset.count(&23), 2); /// ``` pub fn add<I: IntoIterator<Item = (E, C)>>(&mut self, iter: I) { for (elem, by) in iter { self.add_elem(elem, by); } } /// Adds a single element (with an associated count) to the `MultiSet`. /// /// # Examples /// ``` /// use threshold::*; /// /// let mut mset = MultiSet::new(); /// assert_eq!(mset.count(&17), 0); /// /// mset.add_elem(17, 2); /// assert_eq!(mset.count(&17), 2); /// ``` pub fn add_elem(&mut self, elem: E, by: C) { // increase element count let count = self.occurrences.entry(elem).or_insert_with(Count::zero); count.add(by); } /// Returns the `Count` of an element. /// /// # Examples /// ``` /// use threshold::*; /// /// let mut mset = MultiSet::new(); /// assert_eq!(mset.count(&17), 0); /// /// mset.add(vec![(17, 1), (23, 1)]); /// assert_eq!(mset.count(&17), 1); /// assert_eq!(mset.count(&23), 1); /// assert_eq!(mset.count(&42), 0); /// /// mset.add(vec![(17, 1), (42, 1)]); /// assert_eq!(mset.count(&17), 2); /// assert_eq!(mset.count(&23), 1); /// assert_eq!(mset.count(&42), 1); /// assert_eq!(mset.count(&108), 0); /// ``` pub fn count(&self, elem: &E) -> C { self.occurrences .get(elem) .map_or(Count::zero(), |&count| count) } /// Returns a sorted (ASC) double ended iterator. pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&E, &C)> { self.occurrences.iter() } } impl<E: Ord> MultiSet<E, u64> { /// Returns the elements in the `MultiSet` such that its multiplicity is /// bigger or equal than a given [threshold](https://vitorenes.org/post/2018/11/threshold-union/). /// /// # Examples /// ``` /// use threshold::*; /// /// let mut mset = MultiSet::new(); /// let empty: Vec<&u64> = Vec::new(); /// assert_eq!(mset.threshold(1), empty); /// /// mset.add(vec![(17, 1), (23, 1)]); /// assert_eq!(mset.threshold(1), vec![&17, &23]); /// assert_eq!(mset.threshold(2), empty); /// /// mset.add(vec![(17, 1), (42, 3)]); /// assert_eq!(mset.threshold(1), vec![&17, &23, &42]); /// assert_eq!(mset.threshold(2), vec![&17, &42]); /// assert_eq!(mset.threshold(3), vec![&42]); /// assert_eq!(mset.threshold(4), empty); /// ``` pub fn threshold(&self, threshold: u64) -> Vec<&E> { self.occurrences .iter() .filter(|(_, &count)| count >= threshold) .map(|(elem, _)| elem) .collect() } } pub struct IntoIter<E: Ord, C: Count>(btree_map::IntoIter<E, C>); impl<E: Ord, C: Count> Iterator for IntoIter<E, C> { type Item = (E, C); fn next(&mut self) -> Option<Self::Item> { self.0.next() } } impl<E: Ord, C: Count> IntoIterator for MultiSet<E, C> { type Item = (E, C); type IntoIter = IntoIter<E, C>; /// Returns a `MultiSet` into iterator. /// /// # Examples /// ``` /// use threshold::*; /// /// let elems_count = vec![("A", 2), ("B", 1)]; /// let mset = MultiSet::from(elems_count); /// /// let mut iter = mset.into_iter(); /// assert_eq!(Some(("A", 2)), iter.next()); /// assert_eq!(Some(("B", 1)), iter.next()); /// assert_eq!(None, iter.next()); /// ``` fn into_iter(self) -> Self::IntoIter { IntoIter(self.occurrences.into_iter()) } }