pub struct HashCounter<T> { /* private fields */ }
Implementations§
Source§impl<T> HashCounter<T>
impl<T> HashCounter<T>
Source§impl<T> HashCounter<T>
impl<T> HashCounter<T>
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn get(&self, item: &T) -> usize
Sourcepub fn add_count(&mut self, item: T, count: usize)
pub fn add_count(&mut self, item: T, count: usize)
Examples found in repository?
crates/competitive/src/data_structure/counter.rs (line 71)
70 pub fn add(&mut self, item: T) {
71 self.add_count(item, 1)
72 }
73 pub fn add_count(&mut self, item: T, count: usize) {
74 *self.map.entry(item).or_default() += count;
75 }
76 pub fn remove(&mut self, item: &T) -> bool {
77 self.remove_count(item, 1) == 1
78 }
79 pub fn remove_count(&mut self, item: &T, count: usize) -> usize {
80 if let Some(cnt) = self.map.get_mut(item) {
81 if *cnt <= count {
82 let cnt = *cnt;
83 self.map.remove(item);
84 cnt
85 } else {
86 *cnt -= count;
87 count
88 }
89 } else {
90 0
91 }
92 }
93 pub fn append(&mut self, other: &mut Self) {
94 if self.map.len() < other.map.len() {
95 std::mem::swap(self, other);
96 }
97 self.extend(other.map.drain());
98 }
99}
100impl<T> Extend<T> for HashCounter<T>
101where
102 T: Eq + Hash,
103{
104 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
105 for item in iter {
106 self.add(item)
107 }
108 }
109}
110impl<T> Extend<(T, usize)> for HashCounter<T>
111where
112 T: Eq + Hash,
113{
114 fn extend<I: IntoIterator<Item = (T, usize)>>(&mut self, iter: I) {
115 for (item, count) in iter {
116 self.add_count(item, count)
117 }
118 }
pub fn remove(&mut self, item: &T) -> bool
Sourcepub fn remove_count(&mut self, item: &T, count: usize) -> usize
pub fn remove_count(&mut self, item: &T, count: usize) -> usize
pub fn append(&mut self, other: &mut Self)
Trait Implementations§
Source§impl<T: Clone> Clone for HashCounter<T>
impl<T: Clone> Clone for HashCounter<T>
Source§fn clone(&self) -> HashCounter<T>
fn clone(&self) -> HashCounter<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<T> Debug for HashCounter<T>where
T: Debug,
impl<T> Debug for HashCounter<T>where
T: Debug,
Source§impl<T> Default for HashCounter<T>
impl<T> Default for HashCounter<T>
Source§impl<T> Extend<(T, usize)> for HashCounter<T>
impl<T> Extend<(T, usize)> for HashCounter<T>
Source§fn extend<I: IntoIterator<Item = (T, usize)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (T, usize)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<T> Extend<T> for HashCounter<T>
impl<T> Extend<T> for HashCounter<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<T> FromIterator<(T, usize)> for HashCounter<T>
impl<T> FromIterator<(T, usize)> for HashCounter<T>
Source§impl<T> FromIterator<T> for HashCounter<T>
impl<T> FromIterator<T> for HashCounter<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a value from an iterator. Read more
Auto Trait Implementations§
impl<T> Freeze for HashCounter<T>
impl<T> RefUnwindSafe for HashCounter<T>where
T: RefUnwindSafe,
impl<T> Send for HashCounter<T>where
T: Send,
impl<T> Sync for HashCounter<T>where
T: Sync,
impl<T> Unpin for HashCounter<T>where
T: Unpin,
impl<T> UnwindSafe for HashCounter<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more