Struct HashCounter

Source
pub struct HashCounter<T> { /* private fields */ }

Implementations§

Source§

impl<T> HashCounter<T>

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn clear(&mut self)

Source

pub fn keys(&self) -> Keys<'_, T, usize>

Source

pub fn values(&self) -> Values<'_, T, usize>

Source

pub fn iter(&self) -> Iter<'_, T, usize>

Examples found in repository?
crates/competitive/src/data_structure/counter.rs (line 19)
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        f.debug_map().entries(self.iter()).finish()
20    }
Source

pub fn drain(&mut self) -> Drain<'_, T, usize>

Source§

impl<T> HashCounter<T>
where T: Eq + Hash,

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn get(&self, item: &T) -> usize

Source

pub fn add(&mut self, item: T)

Examples found in repository?
crates/competitive/src/data_structure/counter.rs (line 106)
104    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
105        for item in iter {
106            self.add(item)
107        }
108    }
Source

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    }
Source

pub fn remove(&mut self, item: &T) -> bool

Source

pub fn remove_count(&mut self, item: &T, count: usize) -> usize

Examples found in repository?
crates/competitive/src/data_structure/counter.rs (line 77)
76    pub fn remove(&mut self, item: &T) -> bool {
77        self.remove_count(item, 1) == 1
78    }
Source

pub fn append(&mut self, other: &mut Self)

Trait Implementations§

Source§

impl<T: Clone> Clone for HashCounter<T>

Source§

fn clone(&self) -> HashCounter<T>

Returns a duplicate 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<T> Debug for HashCounter<T>
where T: Debug,

Source§

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

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

impl<T> Default for HashCounter<T>
where T: Eq + Hash,

Source§

fn default() -> Self

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

impl<T> Extend<(T, usize)> for HashCounter<T>
where T: Eq + Hash,

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

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>
where T: Eq + Hash,

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

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>
where T: Eq + Hash,

Source§

fn from_iter<I: IntoIterator<Item = (T, usize)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> FromIterator<T> for HashCounter<T>
where T: Eq + Hash,

Source§

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> 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.