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
extern crate smallvec; pub mod bucket; pub mod hash; pub mod kv; use std::marker::PhantomData; pub use bucket::{ArrayBucket, ArrayBucketList, Bucket, BucketList, SmallVecBucket, SmallVecBucketList}; pub use hash::{DefaultHasher, Hash, Hasher}; pub use kv::{Key, Value}; pub struct PrimitiveMap< K: Key, V: Value, B: Bucket<K, V>, BL: BucketList<K, V, Bucket = B>, H: Hasher<K>, > { buckets: BL, _marker: PhantomData<(K, V, B, H)>, } impl<K, V, B, BL, H> PrimitiveMap<K, V, B, BL, H> where K: Key, V: Value, B: Bucket<K, V>, BL: BucketList<K, V, Bucket = B>, H: Hasher<K>, { pub fn custom(buckets: BL, _hasher: H) -> Self { PrimitiveMap { buckets, _marker: PhantomData, } } pub fn insert(&mut self, key: K, value: V) { let addr = self.get_addr(key); let bucket = self.buckets.get_mut(addr); bucket.push(key, value) } pub fn get(&self, key: K) -> Option<V> { let addr = self.get_addr(key); let bucket = &self.buckets.get(addr); bucket.get(key) } fn get_addr(&self, key: K) -> usize { let hash = H::hash(key); H::compress(hash, self.buckets.len()) } } impl<K, V> PrimitiveMap<K, V, SmallVecBucket<K, V>, SmallVecBucketList<K, V>, DefaultHasher<K>> where K: Key, V: Value, DefaultHasher<K>: Hasher<K>, { pub fn dynamic() -> Self { PrimitiveMap::custom(SmallVecBucketList::empty(), DefaultHasher::default()) } } impl<K, V> PrimitiveMap<K, V, ArrayBucket<K, V>, ArrayBucketList<K, V>, DefaultHasher<K>> where K: Key, V: Value, DefaultHasher<K>: Hasher<K>, { pub fn fixed() -> Self { PrimitiveMap::custom(ArrayBucketList::empty(), DefaultHasher::default()) } } impl<K, V, H> PrimitiveMap<K, V, SmallVecBucket<K, V>, SmallVecBucketList<K, V>, H> where K: Key, V: Value, H: Hasher<K>, { pub fn dynamic_with_hasher(hasher: H) -> Self { PrimitiveMap::custom(SmallVecBucketList::empty(), hasher) } } impl<K, V, H> PrimitiveMap<K, V, ArrayBucket<K, V>, ArrayBucketList<K, V>, H> where K: Key, V: Value, H: Hasher<K>, { pub fn fixed_with_hasher(hasher: H) -> Self { PrimitiveMap::custom(ArrayBucketList::empty(), hasher) } } #[cfg(test)] mod tests { use super::*; #[test] fn create_dynamic() { <PrimitiveMap<u8, u32, _, _, _>>::dynamic(); } #[test] fn create_fixed() { <PrimitiveMap<u8, u32, _, _, _>>::fixed(); } #[test] fn default_hasher() { <PrimitiveMap<u8, u8, _, _, _>>::dynamic(); <PrimitiveMap<u16, u8, _, _, _>>::dynamic(); <PrimitiveMap<u32, u8, _, _, _>>::dynamic(); <PrimitiveMap<u64, u8, _, _, _>>::dynamic(); <PrimitiveMap<usize, u8, _, _, _>>::dynamic(); <PrimitiveMap<i8, u8, _, _, _>>::dynamic(); <PrimitiveMap<i16, u8, _, _, _>>::dynamic(); <PrimitiveMap<i32, u8, _, _, _>>::dynamic(); } #[test] fn insert_dynamic() { let mut map = PrimitiveMap::dynamic(); map.insert(0u8, 10u32); } #[test] fn insert_fixed() { let mut map: PrimitiveMap<_, _, _, _, DefaultHasher<_>> = PrimitiveMap::fixed(); map.insert(0u16, 10u32); } #[test] fn get_empty_dynamic() { let map = PrimitiveMap::dynamic(); assert_eq!(map.get(0u32), None::<u32>); } #[test] fn get_empty_fixed() { let map = PrimitiveMap::fixed(); assert_eq!(map.get(0u32), None::<u32>); } #[test] fn insert_and_get_dynamic() { let mut map = PrimitiveMap::dynamic(); map.insert(0i8, 10u32); assert_eq!(map.get(0i8), Some(10u32)); } #[test] fn insert_and_get_fixed() { let mut map = PrimitiveMap::fixed(); map.insert(0i16, 10u32); assert_eq!(map.get(0i16), Some(10u32)); } #[test] fn insert_saturate_buckets_dynamic() { let mut map = PrimitiveMap::dynamic(); for i in 0..10000 { map.insert(i, 10u32); } } }