mlb_api/requests/stats/wrappers/
map.rs1use crate::stats::{SingletonSplitStat, Stat};
2use derive_more::{Deref, DerefMut};
3use fxhash::FxHashMap;
4use std::convert::Infallible;
5use std::fmt::{Debug, Formatter};
6use std::hash::Hash;
7
8#[derive(Deref, DerefMut)]
9pub struct Map<T: SingletonSplitStat, A: MapKey<T>> {
10 inner: FxHashMap<A::Key, T>,
11}
12
13impl<T: SingletonSplitStat, A: MapKey<T>> Debug for Map<T, A> {
14 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
15 self.inner.fmt(f)
16 }
17}
18
19impl<T: SingletonSplitStat, A: MapKey<T>> PartialEq for Map<T, A> {
20 fn eq(&self, other: &Self) -> bool {
21 self.inner == other.inner
22 }
23}
24
25impl<T: SingletonSplitStat, A: MapKey<T>> Eq for Map<T, A> {}
26
27impl<T: SingletonSplitStat, A: MapKey<T>> Clone for Map<T, A> {
28 fn clone(&self) -> Self {
29 Self {
30 inner: self.inner.clone(),
31 }
32 }
33}
34
35impl<T: SingletonSplitStat, A: MapKey<T>> Default for Map<T, A> {
36 fn default() -> Self {
37 Self { inner: FxHashMap::default() }
38 }
39}
40
41impl<T: SingletonSplitStat, A: MapKey<T>> Stat for Map<T, A> {
42 type Split = T;
43 type TryFromSplitError = Infallible;
44
45 fn from_splits(splits: impl Iterator<Item=T>) -> Result<Self, Self::TryFromSplitError>
46 where
47 Self: Sized
48 {
49 let mut this = Self::default();
50 for split in splits {
51 let id: A::Key = A::get_key(&split);
52 let id = id.clone();
53 this.inner.entry(id.clone()).insert_entry(split);
54 }
55 Ok(this)
56 }
57}
58
59#[derive(Deref, DerefMut)]
60pub struct Map2D<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> {
61 inner: FxHashMap<A::Key, FxHashMap<B::Key, T>>,
62}
63
64impl<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> Clone for Map2D<T, A, B> {
65 fn clone(&self) -> Self {
66 Self {
67 inner: self.inner.clone(),
68 }
69 }
70}
71
72impl<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> Debug for Map2D<T, A, B> {
73 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74 self.inner.fmt(f)
75 }
76}
77
78impl<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> PartialEq for Map2D<T, A, B> {
79 fn eq(&self, other: &Self) -> bool {
80 self.inner == other.inner
81 }
82}
83
84impl<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> Eq for Map2D<T, A, B> {}
85
86impl<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> Default for Map2D<T, A, B> {
87 fn default() -> Self {
88 Self { inner: FxHashMap::default() }
89 }
90}
91
92impl<T: SingletonSplitStat, A: MapKey<T>, B: MapKey<T>> Stat for Map2D<T, A, B> {
93 type Split = T;
94 type TryFromSplitError = Infallible;
95
96 fn from_splits(splits: impl Iterator<Item=Self::Split>) -> Result<Self, Self::TryFromSplitError>
97 where
98 Self: Sized
99 {
100 let mut this = Self::default();
101 for split in splits {
102 let id: A::Key = A::get_key(&split);
103 let inner_id: B::Key = B::get_key(&split);
104 this.inner.entry(id).or_insert_with(FxHashMap::default).entry(inner_id.clone()).insert_entry(split);
105 }
106 Ok(this)
107 }
108}
109
110pub trait MapKey<T> {
111 type Key: Hash + Clone + Debug + Eq;
112
113 fn get_key(this: &T) -> Self::Key;
114}