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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! The [`Frequency`][frequency] trait represents types that keep track of the 
//! observed counts of items. The [`HashMapFrequency`][hashmapfrequency] type
//! is a frequency counter backed by a [`HashMap`][hashmap].
//!
//! # Usage
//! Add [`frequency`][freq_crate] and `frequency-hashmap` to your `Cargo.toml`:
//!
//! ```
//! [dependencies]
//! frequency = "^1.0.0"
//! frequency-hashmap = "^1.0.0"
//! ```
//!
//! Import both [`Frequency`][frequency] and 
//! [`HashMapFrequency`][hashmapfrequency]:
//!
//! ```
//! extern crate frequency;
//! extern crate frequency_hashmap;
//!
//! use frequency::Frequency;
//! use frequency_hashmap::HashMapFrequency;
//! ```
//!
//! [freq_crate]: https://docs.rs/frequency/~1/
//! [frequency]: https://docs.rs/frequency/~1/frequency/trait.Frequency.html
//! [hashmapfrequency]: struct.HashMapFrequency.html
//! [hashmap]: https://doc.rust-lang.org/std/collections/struct.HashMap.html

extern crate frequency;
extern crate fnv;
extern crate num_traits;

use frequency::Frequency;
use fnv::FnvBuildHasher;
use num_traits::{Num, One, Zero};
use std::hash::Hash;
use std::iter::FromIterator;
use std::hash::BuildHasher;
use std::collections::hash_map::{Iter, Keys, Values, HashMap};

/// A frequency counter backed by a [`HashMap`][hashmap].
/// [hashmap]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
pub struct HashMapFrequency<T, N=usize, S=FnvBuildHasher>
    where T: Hash + Eq,
          N: Num,
          S: BuildHasher
{
    frequency: HashMap<T, N, S>,
}

impl<'t, T, N, S> Frequency<'t, T> for HashMapFrequency<T, N, S>
    where T: 't + Eq + Hash,
          N: 't + Num + Clone,
          S: 't + BuildHasher
{
    type N      = N;
    type Iter   = Iter<'t, T, Self::N>;
    type Items  = Keys<'t, T, Self::N>;
    type Counts = Values<'t, T, Self::N>;

    #[inline]
    fn count(&self, key: &T) -> Self::N {
        self.frequency.get(key).cloned().unwrap_or_else(Zero::zero)
    }

    #[inline]
    fn increment(&mut self, value: T) {
        let value = self.frequency.entry(value).or_insert_with(Zero::zero);
        *value = value.clone() + One::one();
    }

    #[inline]
    fn iter(&'t self) -> Self::Iter {
        self.frequency.iter()
    }

    #[inline]
    fn items(&'t self) -> Self::Items {
        self.frequency.keys()
    }

    #[inline]
    fn counts(&'t self) -> Self::Counts {
        self.frequency.values()
    }
}

impl<T, N, S> HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num,
          S: BuildHasher + Default
{
    /// Creates an empty `HashMapFrequency`, a frequency counter backed
    /// by a HashMap.
    #[inline]
    pub fn new() -> HashMapFrequency<T, N, S> {
        Default::default()
    }

    /// Creates an empty `HashMapFrequency`, a frequency counter backed
    /// by a HashMap with the specified capacity.
    ///
    /// The hash map will be able to hold at least `capacity` elements without
    /// reallocating. If `capacity` is 0, the hash map will not allocate.
    /// ```
    #[inline]
    pub fn with_capacity(capacity: usize) -> HashMapFrequency<T, N, S> {
        HashMapFrequency::with_capacity_and_hasher(capacity, Default::default())
    }
}

impl<T, N, S> HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num,
          S: BuildHasher
{
    /// Creates an empty `HashMapFrequency`, a frequency counter backed
    /// by a HashMap which will use the given hash builder to hash
    /// keys.
    ///
    /// The created map has the default initial capacity.
    ///
    /// Warning: `hash_builder` is normally randomly generated, and
    /// is designed to allow HashMaps to be resistant to attacks that
    /// cause many collisions and very poor performance. Setting it
    /// manually using this function can expose a DoS attack vector.
    /// ```
    #[inline]
    pub fn with_hasher(hash_builder: S) -> HashMapFrequency<T, N, S> {
        HashMapFrequency {
            frequency: HashMap::with_hasher(hash_builder)
        }
    }

    /// Creates an empty `HashMapFrequency`, a frequency counter backed
    /// by a HashMap with the specified capacity, using `hasher`
    /// to hash the keys.
    ///
    /// The hash map will be able to hold at least `capacity` elements without
    /// reallocating. If `capacity` is 0, the hash map will not allocate.
    /// Warning: `hasher` is normally randomly generated, and
    /// is designed to allow HashMaps to be resistant to attacks that
    /// cause many collisions and very poor performance. Setting it
    /// manually using this function can expose a DoS attack vector.
    #[inline]
    pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMapFrequency<T, N, S> {
        HashMapFrequency {
            frequency: HashMap::with_capacity_and_hasher(capacity, hash_builder)
        }
    }

    /// Returns the number of elements that have been counted.
    #[inline]
    pub fn len(&self) -> usize {
        self.frequency.len()
    }

    /// Returns true if the counter contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Reserves capacity for at least `additional` more elements to be inserted
    /// in the `HashMap` backing this frequency counter. The collection may 
    /// reserve more space to avoid frequent reallocations.
    ///
    /// # Panics
    ///
    /// Panics if the new allocation size overflows [`usize`].
    ///
    /// [`usize`]: ../../std/primitive.usize.html
    /// ```
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.frequency.reserve(additional)
    }
}

impl<T, N, S> Default for HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num,
          S: BuildHasher + Default
{
    /// Creates an empty `HashMapFrequency<T, V, S>`, a frequency counter backed
    /// by a HashMap with the `Default` value for the hasher.
    fn default() -> HashMapFrequency<T, N, S> {
        HashMapFrequency::with_hasher(Default::default())
    }
}

impl<T, N, S> Extend<T> for HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num + Clone,
          S: BuildHasher
{
    fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
        // Keys may be already present or show multiple times in the iterator.
        // Reserve the entire hint lower bound if the map is empty.
        // Otherwise reserve half the hint (rounded up), so the map
        // will only resize twice in the worst case.
        let iter = iter.into_iter();
        let reserve = if self.is_empty() {
            iter.size_hint().0
        } else {
            (iter.size_hint().0 + 1) / 2
        };
        self.reserve(reserve);
        for k in iter {
            self.increment(k);
        }
    }
}

impl<T, N, S> FromIterator<T> for HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num + Clone,
          S: BuildHasher + Default
{
    fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
        let mut frequency = HashMapFrequency::default();
        frequency.extend(iter);
        frequency
    }
}

impl<'t, T, N, S> IntoIterator for &'t HashMapFrequency<T, N, S>
    where T: 't + Eq + Hash,
          N: 't + Num + Clone,
          S: 't + BuildHasher
{
    type Item = <<HashMapFrequency<T, N, S> as Frequency<'t, T>>::Iter as Iterator>::Item;
    type IntoIter = <HashMapFrequency<T, N, S> as Frequency<'t, T>>::Iter;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<T, N, S> AsRef<HashMap<T, N, S>> for HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num,
          S: BuildHasher
{
    fn as_ref(&self) -> &HashMap<T, N, S> {
        &self.frequency
    }
}

impl<T, N, S> AsMut<HashMap<T, N, S>> for HashMapFrequency<T, N, S>
    where T: Eq + Hash,
          N: Num,
          S: BuildHasher
{
    fn as_mut(&mut self) -> &mut HashMap<T, N, S> {
        &mut self.frequency
    }
}