Debian Codegraph @

src/utils/int_hash.rs

raw symbols SCIP
1 //! A simple hasher that literally just maps an int to itself.
2 //!
3 //! Originally based on <https://github.com/tetcoin/nohash/blob/master/src/lib.rs>.
4
5 use std::{
6 hash::{BuildHasherDefault, Hasher},
7 marker::PhantomData,
8 };
9
10 use indexmap::IndexMap;
11
12 type IntHasherState<K> = BuildHasherDefault<IntHasher<K>>;
13
14 /// A hash map that directly maps from an integer key to a value.
15 pub type IntHashMap<K, V> = std::collections::HashMap<K, V, IntHasherState<K>>;
16
17 /// A hash set that directly uses integer keys.
18 #[allow(dead_code)]
19 pub type IntHashSet<K> = std::collections::HashSet<K, IntHasherState<K>>;
20
21 /// An [`IndexMap`] wrapper such that it tracks insertion order, but uses
22 /// integer keys.
23 pub type IntIndexMap<K, V> = IndexMap<K, V, IntHasherState<K>>;
24
25 pub trait SupportedInt {}
26
27 impl SupportedInt for u8 {}
28 impl SupportedInt for u16 {}
29 impl SupportedInt for u32 {}
30 impl SupportedInt for u64 {}
31 impl SupportedInt for usize {}
32 impl SupportedInt for i8 {}
33 impl SupportedInt for i16 {}
34 impl SupportedInt for i32 {}
35 impl SupportedInt for i64 {}
36 impl SupportedInt for isize {}
37
38 #[derive(Default)]
39 pub struct IntHasher<T: SupportedInt> {
40 inner: u64,
41 _marker: PhantomData<T>,
42 }
43
44 impl<T: SupportedInt> Hasher for IntHasher<T> {
45 fn finish(&self) -> u64 {
46 self.inner
47 }
48
49 fn write(&mut self, _bytes: &[u8]) {
50 panic!("IntHasher does not support arbitrary writes")
51 }
52
53 fn write_u8(&mut self, i: u8) {
54 self.inner = i as u64;
55 }
56
57 fn write_u16(&mut self, i: u16) {
58 self.inner = i as u64;
59 }
60
61 fn write_u32(&mut self, i: u32) {
62 self.inner = i as u64;
63 }
64
65 fn write_u64(&mut self, i: u64) {
66 self.inner = i;
67 }
68
69 fn write_usize(&mut self, i: usize) {
70 self.inner = i as u64;
71 }
72
73 fn write_i8(&mut self, i: i8) {
74 self.inner = i as u64;
75 }
76
77 fn write_i16(&mut self, i: i16) {
78 self.inner = i as u64;
79 }
80
81 fn write_i32(&mut self, i: i32) {
82 self.inner = i as u64;
83 }
84
85 fn write_i64(&mut self, i: i64) {
86 self.inner = i as u64;
87 }
88
89 fn write_isize(&mut self, i: isize) {
90 self.inner = i as u64;
91 }
92 }
93
94 #[cfg(test)]
95 mod tests {
96 use super::*;
97
98 #[test]
99 fn test_u8() {
100 let mut hasher = IntHasher::<u8>::default();
101 hasher.write_u8(42);
102 assert_eq!(hasher.finish(), 42);
103 }
104
105 #[test]
106 fn test_u16() {
107 let mut hasher = IntHasher::<u16>::default();
108 hasher.write_u16(4242);
109 assert_eq!(hasher.finish(), 4242);
110 }
111
112 #[test]
113 fn test_u32() {
114 let mut hasher = IntHasher::<u32>::default();
115 hasher.write_u32(424242);
116 assert_eq!(hasher.finish(), 424242);
117 }
118
119 #[test]
120 fn test_u64() {
121 let mut hasher = IntHasher::<u64>::default();
122 hasher.write_u64(4242424242);
123 assert_eq!(hasher.finish(), 4242424242);
124 }
125
126 #[test]
127 fn test_usize() {
128 let mut hasher = IntHasher::<usize>::default();
129 hasher.write_usize(999999);
130 assert_eq!(hasher.finish(), 999999);
131 }
132
133 #[test]
134 fn test_i8() {
135 let mut hasher = IntHasher::<i8>::default();
136 hasher.write_i8(-42);
137 assert_eq!(hasher.finish(), -42_i8 as u64);
138 }
139
140 #[test]
141 fn test_i16() {
142 let mut hasher = IntHasher::<i16>::default();
143 hasher.write_i16(-4242);
144 assert_eq!(hasher.finish(), -4242_i64 as u64);
145 }
146
147 #[test]
148 fn test_i32() {
149 let mut hasher = IntHasher::<i32>::default();
150 hasher.write_i32(-424242);
151 assert_eq!(hasher.finish(), -424242_i64 as u64);
152 }
153
154 #[test]
155 fn test_i64() {
156 let mut hasher = IntHasher::<i64>::default();
157 hasher.write_i64(-4242424242);
158 assert_eq!(hasher.finish(), -4242424242_i64 as u64);
159 }
160
161 #[test]
162 fn test_isize() {
163 let mut hasher = IntHasher::<isize>::default();
164 hasher.write_isize(-424242);
165 assert_eq!(hasher.finish(), -424242_isize as u64);
166 }
167
168 #[test]
169 fn test_int_hash_map() {
170 let mut map = IntHashMap::<u32, &str>::default();
171 map.insert(1, "one");
172 map.insert(2, "two");
173 assert_eq!(map.get(&1), Some(&"one"));
174 assert_eq!(map.get(&2), Some(&"two"));
175 assert_eq!(map.get(&3), None);
176 }
177
178 #[test]
179 fn test_int_hash_set() {
180 let mut set = IntHashSet::<u32>::default();
181 set.insert(1);
182 set.insert(2);
183 assert!(set.contains(&1));
184 assert!(set.contains(&2));
185 assert!(!set.contains(&3));
186 }
187
188 #[test]
189 fn test_int_index_map() {
190 let mut map = IntIndexMap::<u32, &str>::default();
191 map.insert(1, "one");
192 map.insert(3, "three");
193 map.insert(2, "two");
194 assert_eq!(map.get(&1), Some(&"one"));
195 assert_eq!(map.get(&2), Some(&"two"));
196 assert_eq!(map.get(&3), Some(&"three"));
197 assert_eq!(map.get(&4), None);
198
199 assert_eq!(map.keys().cloned().collect::<Vec<_>>(), vec![1, 3, 2]);
200 assert_eq!(
201 map.values().cloned().collect::<Vec<_>>(),
202 vec!["one", "three", "two"]
203 );
204 }
205 }