mirror of
https://github.com/italicsjenga/agb.git
synced 2025-02-23 22:58:18 +11:00
Add with_capacity method
This commit is contained in:
parent
c0d9f0ab41
commit
03f5cd0953
1 changed files with 16 additions and 1 deletions
|
@ -43,10 +43,16 @@ pub struct HashMap<K, V> {
|
||||||
|
|
||||||
impl<K, V> HashMap<K, V> {
|
impl<K, V> HashMap<K, V> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
Self::with_capacity(16)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
|
let next_power_of_2 = find_next_power_of_2(capacity);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
number_of_elements: 0,
|
number_of_elements: 0,
|
||||||
max_distance_to_initial_bucket: 0,
|
max_distance_to_initial_bucket: 0,
|
||||||
nodes: iter::repeat_with(|| None).take(16).collect(),
|
nodes: iter::repeat_with(|| None).take(next_power_of_2).collect(),
|
||||||
hasher: Default::default(),
|
hasher: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,6 +67,15 @@ const fn fast_mod(len: usize, hash: HashType) -> usize {
|
||||||
(hash as usize) & (len - 1)
|
(hash as usize) & (len - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fn find_next_power_of_2(n: usize) -> usize {
|
||||||
|
let mut next_power_of_2 = 1;
|
||||||
|
while next_power_of_2 <= n {
|
||||||
|
next_power_of_2 *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
next_power_of_2
|
||||||
|
}
|
||||||
|
|
||||||
impl<K, V> HashMap<K, V>
|
impl<K, V> HashMap<K, V>
|
||||||
where
|
where
|
||||||
K: Eq,
|
K: Eq,
|
||||||
|
|
Loading…
Add table
Reference in a new issue