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
use core::num::NonZeroUsize;

use serde::{Deserialize, Serialize};

use necsim_core_bond::PositiveF64;

use crate::cache::DirectMappedCache;

mod reporter;

pub mod individuals;
pub mod landscape;
pub mod monolithic;

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AbsoluteCapacity {
    pub capacity: NonZeroUsize,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RelativeCapacity {
    pub factor: PositiveF64,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum DedupCache {
    Absolute(AbsoluteCapacity),
    Relative(RelativeCapacity),
    None,
}

impl DedupCache {
    #[must_use]
    pub fn construct<T: core::hash::Hash + PartialEq>(
        self,
        workload: usize,
    ) -> DirectMappedCache<T> {
        DirectMappedCache::with_capacity(match self {
            DedupCache::Absolute(AbsoluteCapacity { capacity }) => capacity.get(),
            DedupCache::Relative(RelativeCapacity { factor }) => {
                #[allow(
                    clippy::cast_precision_loss,
                    clippy::cast_sign_loss,
                    clippy::cast_possible_truncation
                )]
                let capacity = ((workload as f64) * factor.get()) as usize;

                capacity
            },
            DedupCache::None => 0_usize,
        })
    }
}

#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum EventSlice {
    Absolute(AbsoluteCapacity),
    Relative(RelativeCapacity),
}

impl EventSlice {
    #[must_use]
    pub fn capacity(self, workload: usize) -> NonZeroUsize {
        match self {
            EventSlice::Absolute(AbsoluteCapacity { capacity }) => capacity,
            EventSlice::Relative(RelativeCapacity { factor }) => {
                #[allow(
                    clippy::cast_precision_loss,
                    clippy::cast_sign_loss,
                    clippy::cast_possible_truncation
                )]
                let capacity = ((workload as f64) * factor.get()) as usize;

                // Safety: max(c, 1) >= 1
                unsafe { NonZeroUsize::new_unchecked(capacity.max(1_usize)) }
            },
        }
    }
}