-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
115 lines (102 loc) · 2.68 KB
/
main.rs
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
use ahash::AHasher;
use hashbrown::HashMap;
pub struct BuildAHasher {
seed: u64,
}
impl BuildAHasher {
pub fn new(seed: u64) -> Self {
Self { seed }
}
}
impl std::hash::BuildHasher for BuildAHasher {
type Hasher = AHasher;
fn build_hasher(&self) -> Self::Hasher {
AHasher::new_with_key(self.seed)
}
}
#[test]
fn test_with_capacity() {
let capacity: usize = 28;
let hash_seed: u64 = 4774451669087367725;
let mut map = HashMap::with_capacity_and_hasher(
capacity,
BuildAHasher::new(hash_seed),
);
let items: Vec<(u16, u16)> = vec![
(1988, 29987),
(2666, 27242),
(6040, 2394),
(25752, 61248),
(27146, 27242),
(27241, 27242),
(27242, 27242),
(27243, 27242),
(27285, 27242),
(27331, 27242),
(28712, 1989),
(29517, 57394),
(32582, 1480),
(34410, 27242),
(35690, 26931),
(38250, 27242),
(39274, 15180),
(44843, 27864),
(48680, 48830),
(56389, 27242),
(57394, 52917),
(61248, 34543),
(61510, 51837),
(63016, 47943)
];
for (k, v) in items {
map.insert(k, v);
eprintln!("inserted {}\tcapacity = {}", k, map.capacity());
}
map.remove(&29517);
eprintln!("removed 29517\tcapacity = {}", map.capacity());
let previous_capacity = map.capacity();
map.clear();
assert_eq!(map.capacity(), previous_capacity, "map.capacity() != previous_capacity");
}
#[test]
fn test_reserve() {
let capacity: usize = 28;
let hash_seed: u64 = 4774451669087367725;
let mut map = HashMap::with_hasher(BuildAHasher::new(hash_seed));
map.reserve(capacity);
let items: Vec<(u16, u16)> = vec![
(1988, 29987),
(2666, 27242),
(6040, 2394),
(25752, 61248),
(27146, 27242),
(27241, 27242),
(27242, 27242),
(27243, 27242),
(27285, 27242),
(27331, 27242),
(28712, 1989),
(29517, 57394),
(32582, 1480),
(34410, 27242),
(35690, 26931),
(38250, 27242),
(39274, 15180),
(44843, 27864),
(48680, 48830),
(56389, 27242),
(57394, 52917),
(61248, 34543),
(61510, 51837),
(63016, 47943)
];
for (k, v) in items {
map.insert(k, v);
eprintln!("inserted {}\tcapacity = {}", k, map.capacity());
}
map.remove(&29517);
eprintln!("removed 29517\tcapacity = {}", map.capacity());
let previous_capacity = map.capacity();
map.clear();
assert_eq!(map.capacity(), previous_capacity, "map.capacity() != previous_capacity");
}