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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Defines the [NumMap] struct which acts as if all unmapped keys have a value of 0.
//! 
//! Author --- daniel.bechaz@gmail.com  
//! Last Moddified --- 2019-03-31

#![deny(missing_docs,)]
#![cfg_attr(feature = "map_get_key_value", feature(map_get_key_value,),)]

use std::{
  hash::*, iter::*, fmt,
  ops::{Deref, DerefMut,},
  borrow::{Borrow, BorrowMut,},
  convert::{AsRef, AsMut,},
  collections::{hash_map::RandomState, HashMap,},
  cmp::Ordering,
};

mod number;
mod iter;

pub use self::{number::*, iter::*,};

/// A map of numbers where all keys are considered mapped but 0 values are not stored.
pub struct NumMap<K, V, S = RandomState,>(HashMap<K, V::NonZero, S>,)
  where V: Number;

impl<K, V,> NumMap<K, V, RandomState,>
  where K: Hash + Eq, V: Number, {
  /// Creates an empty `NumMap`.
  /// 
  /// The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let mut map: NumMap<&str, i32> = NumMap::new();
  #[inline]
  pub fn new() -> Self { HashMap::new().into() }
  /// Creates an empty `NumMap` with the specified capacity.
  /// 
  /// The `NumMap` will be able to hold at least capacity elements without reallocating.
  /// If capacity is 0, the hash map will not allocate.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let map: NumMap<&str, i32> = NumMap::with_capacity(10);
  /// ```
  #[inline]
  pub fn with_capacity(capactiy: usize,) -> Self { HashMap::with_capacity(capactiy,).into() }
}

impl<K, V, S,> NumMap<K, V, S,>
  where V: Number, {
  /// An iterator over all the key/value pairs present in this `NumMap`.
  #[inline]
  pub fn iter(&self,) -> Iter<K, V,> {
    Iter(self.0.iter().map(|(k, v,): (&K, &V::NonZero,),| (k, v.get(),),))
  }
}

impl<K, V, S,> NumMap<K, V, S,>
  where K: Eq + Hash, V: Number, S: BuildHasher, {
  /// Creates an empty `NumMap` which will use the given hash builder to hash keys.
  /// 
  /// The created map has the default initial capacity.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// use std::collections::hash_map::RandomState;
  /// 
  /// let s = RandomState::new();
  /// let mut map = NumMap::<i32, i32,>::with_hasher(s);
  /// 
  /// map.set(1, 2);
  /// ```
  /// 
  /// # Warnings
  /// 
  /// `hash_builder` is normally randomly generated, and is designed to allow `NumMap`s
  /// 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,) -> Self { HashMap::with_hasher(hash_builder,).into() }
  /// Creates an empty `NumMap` with the specified capacity, using hash_builder 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.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// use std::collections::hash_map::RandomState;
  /// 
  /// let s = RandomState::new();
  /// let mut map = NumMap::<i32, i32,>::with_capacity_and_hasher(10, s);
  /// 
  /// map.set(1, 2);
  /// ```
  /// 
  /// # Warning
  /// 
  /// `hash_builder` is normally randomly generated, and is designed to allow `NumMap`s
  /// 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,) -> Self {
    HashMap::with_capacity_and_hasher(capacity, hash_builder,).into()
  }
  /// Returns the value mapped to the corresponding key.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let mut map = NumMap::<i32, i32,>::new();
  /// 
  /// map.set(1, 2);
  /// assert_eq!(map.get(&1), 2);
  /// assert_eq!(map.get(&2), 0);
  /// ```
  #[inline]
  pub fn get<Q,>(&self, k: &Q,) -> V
    where K: Borrow<Q>, Q: Hash + Eq + ?Sized, {
    self.0.get(k,).map(|v,| v.get(),).unwrap_or(V::ZERO,)
  }
  /// Returns the key-value pair corresponding to the supplied key.
  /// 
  /// The supplied key may be any borrowed form of the map's key type, but `Hash` and
  /// `Eq` on the borrowed form must match those for the key type.
  /// 
  /// Enabled with feature "map_get_key_value".
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// #![feature(map_get_key_value)]
  /// 
  /// use nummap::NumMap;
  /// 
  /// let mut map = NumMap::new();
  /// 
  /// map.set(1, 2);
  /// assert_eq!(map.get_key_value(&1), (&1, 2));
  /// assert_eq!(map.get_key_value(&2), (&2, 0));
  /// ```
  #[cfg(feature = "map_get_key_value",)]
  #[inline]
  pub fn get_key_value<'a,>(&'a self, k: &'a K,) -> (&'a K, V) {
    self.0.get_key_value(k,).map(|(k, v,),| (k, v.get(),),).unwrap_or((k, V::ZERO,),)
  }
  /// Updates the value mapped to the corresponding key and returns the old value.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let mut map = NumMap::<i32, i32,>::new();
  /// 
  /// assert_eq!(map.set(1, 2), 0);
  /// assert_eq!(map.set(1, 0), 2);
  /// ```
  pub fn set(&mut self, k: K, v: V,) -> V {
    match V::NonZero::new(v,) {
      Some(v) => self.0.insert(k, v,),
      None => self.0.remove(&k,)
    }.map(V::NonZero::get,).unwrap_or(V::ZERO,)
  }
  /// Updates the value mapped to the corresponding key and returns the old value.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// use std::num::NonZeroU32;
  /// 
  /// let mut map = NumMap::<u32, u32,>::new();
  /// let two = NonZeroU32::new(2,).unwrap();
  /// 
  /// assert_eq!(map.insert(1, two), 0);
  /// ```
  #[inline]
  pub fn insert(&mut self, k: K, v: V::NonZero,) -> V {
    self.0.insert(k, v,).map(V::NonZero::get,).unwrap_or(V::ZERO,)
  }
  /// Removes and returns the value mapped to the corresponding key.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let mut map = NumMap::<i32, i32,>::new();
  /// 
  /// assert_eq!(map.remove(&1), 0);
  /// assert_eq!(map.set(1, 2), 0);
  /// assert_eq!(map.remove(&1), 2);
  /// ```
  #[inline]
  pub fn remove<Q,>(&mut self, k: &Q,) -> V
    where K: Borrow<Q>, Q: Eq + Hash + ?Sized, {
    self.0.remove(k,).map(V::NonZero::get,).unwrap_or(V::ZERO,)
  }
  /// Removes and returns both the key and the value mapped to the key.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let mut map = NumMap::<i32, i32,>::new();
  /// 
  /// assert_eq!(map.remove_entry(1), (1, 0,));
  /// assert_eq!(map.set(1, 2), 0);
  /// assert_eq!(map.remove_entry(1), (1, 2,));
  /// ```
  #[inline]
  pub fn remove_entry(&mut self, k: K,) -> (K, V) {
    self.0.remove_entry(&k,).map(|(k, v,),| (k, v.get(),),).unwrap_or((k, V::ZERO,),)
  }
  /// Retains only the elements specified by the predicate.
  /// 
  /// In other words, remove all pairs (k, v) such that f(&k,&mut v) returns false.
  /// 
  /// # Examples
  /// 
  /// ```rust
  /// use nummap::NumMap;
  /// 
  /// let mut map: NumMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
  /// 
  /// map.retain(|&k, _| k % 2 == 1);
  /// assert_eq!(map.len(), 4);
  /// ```
  #[inline]
  pub fn retain<F,>(&mut self, mut f: F,)
    where F: FnMut(&K, V,) -> bool, {
    self.0.retain(move |k, v,| f(k, v.get(),),)
  }
}

impl<K, V, S,> Clone for NumMap<K, V, S,>
  where V: Number, HashMap<K, V::NonZero, S>: Clone, {
  #[inline]
  fn clone(&self,) -> Self { self.0.clone().into() }
}

impl<K, V, S,> From<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where V: Number, {
  #[inline]
  fn from(from: HashMap<K, V::NonZero, S>,) -> Self { NumMap(from,) }
}

impl<K, V, S,> Default for NumMap<K, V, S,>
  where V: Number, HashMap<K, V::NonZero, S>: Default, {
  #[inline]
  fn default() -> Self { HashMap::default().into() }
}

impl<K, V, S,> PartialEq<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where V: Number, HashMap<K, V::NonZero, S>: PartialEq, {
  #[inline]
  fn eq(&self, rhs: &HashMap<K, V::NonZero, S>,) -> bool { self.0 == *rhs }
}

impl<K, V, S,> PartialEq for NumMap<K, V, S,>
  where V: Number, Self: PartialEq<HashMap<K, V::NonZero, S>>, {
  #[inline]
  fn eq(&self, rhs: &Self,) -> bool { *self == rhs.0 }
}

impl<K, V, S,> Eq for NumMap<K, V, S,>
  where V: Number, HashMap<K, V::NonZero, S>: Eq, {}

/// Compares the two sets; sets are only considered `Greater` or `Less` if **ALL** of the
/// mapping are `Greater` or `Less` (`Equal` is ignored).
impl<K, V, S,> PartialOrd<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where K: Eq + Hash, V: Number, S: BuildHasher,
    Self: PartialEq<HashMap<K, V::NonZero, S>>, {
  fn partial_cmp(&self, rhs: &HashMap<K, V::NonZero, S>,) -> Option<Ordering> {
    let mut lhs_iter;
    let mut rhs_iter;
    //Iterate over all values from the largest set.
    let iter: &mut dyn Iterator<Item = Ordering> = if self.len() >= rhs.len() {
      lhs_iter = self.0.iter().map(|(k, v,),| rhs.get(k,)
        .map(move |rhs,| v.get().cmp(&rhs.get(),),)
        .unwrap_or(Ordering::Greater,),
      );

      &mut lhs_iter
    } else {
      rhs_iter = rhs.iter().map(|(k, rhs,),| self.0.get(k,)
        .map(move |v,| v.get().cmp(&rhs.get(),),)
        .unwrap_or(Ordering::Less,),
      );

      &mut rhs_iter
    };
    //Filter out all equal values.
    let mut iter = iter.filter(move |o,| o != &Ordering::Equal,);
    let ord = match iter.next() {
      Some(ord) => ord,
      //If there is no unequal values the sets are equal.
      None => return Some(Ordering::Equal),
    };

    //If all orderings are aligned the sets are ordered.
    if iter.all(move |o,| ord == o,) { return Some(ord) }
    else { None }
  }
}

impl<K, V, S,> PartialOrd for NumMap<K, V, S,>
  where K: Eq + Hash, V: Number, S: BuildHasher,
    Self: PartialOrd<HashMap<K, V::NonZero, S>>, {
  #[inline]
  fn partial_cmp(&self, rhs: &Self,) -> Option<Ordering> { self.partial_cmp(&rhs.0,) }
}

impl<K, V, S, A,> FromIterator<A> for NumMap<K, V, S,>
  where K: Hash + Eq, V: Number, S: Default + BuildHasher, A: Into<(K, V,)>, {
  fn from_iter<Iter,>(iter: Iter,) -> Self
    where Iter: IntoIterator<Item = A>, {
    iter.into_iter()
    .map(A::into,)
    .filter_map(|(k, v,),| V::NonZero::new(v,).map(move |v,| (k, v,),),)
    .collect::<HashMap<_, _, S,>>().into()
  }
}

impl<'a, K, V, S,> Extend<(&'a K, &'a V,)> for NumMap<K, V, S,>
  where K: 'a + Hash + Eq + Copy, V: 'a + Number, S: Default + BuildHasher, {
  #[inline]
  fn extend<Iter,>(&mut self, iter: Iter,)
    where Iter: IntoIterator<Item = (&'a K, &'a V,)>, {
    self.extend(iter.into_iter()
      .map(|(&k, &v,),| (k, v,),),
    )
  }
}

impl<K, V, S,> Extend<(K, V,)> for NumMap<K, V, S,>
  where K: Hash + Eq, V: Number, S: Default + BuildHasher, {
  fn extend<Iter,>(&mut self, iter: Iter,)
    where Iter: IntoIterator<Item = (K, V,)>, {
    self.0.extend(
      iter.into_iter()
      .filter_map(|(k, v,),| V::NonZero::new(v,).map(move |v,| (k, v,),),),
    )
  }
}

impl<K, V, S,> IntoIterator for NumMap<K, V, S,>
  where V: Number, {
  type Item = (K, V,);
  type IntoIter = IntoIter<K, V,>;

  #[inline]
  fn into_iter(self,) -> Self::IntoIter {
    IntoIter(self.0.into_iter().map(|(k, v,): (K, V::NonZero,),| (k, v.get(),),),)
  }
}

impl<K, V, S,> Deref for NumMap<K, V, S,>
  where V: Number, {
  type Target = HashMap<K, V::NonZero, S>;

  #[inline]
  fn deref(&self,) -> &Self::Target { &self.0 }
}

impl<K, V, S,> DerefMut for NumMap<K, V, S,>
  where V: Number, {
  #[inline]
  fn deref_mut(&mut self,) -> &mut Self::Target { &mut self.0 }
}

impl<K, V, S,> Borrow<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where V: Number, {
  #[inline]
  fn borrow(&self,) -> &HashMap<K, V::NonZero, S> { &self.0 }
}

impl<K, V, S,> BorrowMut<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where V: Number, {
  #[inline]
  fn borrow_mut(&mut self,) -> &mut HashMap<K, V::NonZero, S> { &mut self.0 }
}

impl<K, V, S,> AsRef<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where V: Number, {
  #[inline]
  fn as_ref(&self,) -> &HashMap<K, V::NonZero, S> { &self.0 }
}

impl<K, V, S,> AsMut<HashMap<K, V::NonZero, S>> for NumMap<K, V, S,>
  where V: Number, {
  #[inline]
  fn as_mut(&mut self,) -> &mut HashMap<K, V::NonZero, S> { &mut self.0 }
}

impl<K, V, S,> fmt::Debug for NumMap<K, V, S,>
  where V: Number, HashMap<K, V::NonZero, S>: fmt::Debug, {
  #[inline]
  fn fmt(&self, fmt: &mut fmt::Formatter,) -> fmt::Result { self.0.fmt(fmt,) }
}

#[cfg(test,)]
mod tests {
  use super::*;

  #[test]
  fn test_cmp() {
    let one = (1..=10).map(|x,| (x, x,),).collect::<NumMap<_, _,>>();
    let two = (1..=10).map(|x,| (x, 2 * x,),).collect::<NumMap<_, _,>>();
    let neg_two = (1..=10).map(|x,| (x, -2 * x,),).collect::<NumMap<_, _,>>();

    assert!(one < two);
    assert!(one > neg_two);
    assert!(two > neg_two);
    assert!(one == one);
    assert!(one >= one);
    assert!(one <= one);
    assert!(neg_two == neg_two);
    assert!(neg_two >= neg_two);
    assert!(neg_two <= neg_two);
  }
}