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
use alloc::vec::Vec;
use core::{fmt, marker::PhantomData};
use necsim_core_bond::NonNegativeF64;

use necsim_core::{
    event::{PackedEvent, TypedEvent},
    impl_report,
    reporter::Reporter,
};

use necsim_partitioning_core::LocalPartition;

use super::WaterLevelReporterProxy;

#[allow(clippy::module_name_repetitions)]
pub struct LiveWaterLevelReporterProxy<'l, 'p, R: Reporter, P: LocalPartition<'p, R>> {
    water_level: NonNegativeF64,
    slow_events: Vec<PackedEvent>,
    fast_events: Vec<PackedEvent>,

    local_partition: &'l mut P,
    _marker: PhantomData<(&'p (), R)>,
}

impl<'l, 'p, R: Reporter, P: LocalPartition<'p, R>> fmt::Debug
    for LiveWaterLevelReporterProxy<'l, 'p, R, P>
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        struct EventBufferLen(usize);

        impl fmt::Debug for EventBufferLen {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "Vec<PackedEvent; {}>", self.0)
            }
        }

        fmt.debug_struct(stringify!(LiveWaterLevelReporterProxy))
            .field("water_level", &self.water_level)
            .field("slow_events", &EventBufferLen(self.slow_events.len()))
            .field("fast_events", &EventBufferLen(self.fast_events.len()))
            .finish()
    }
}

impl<'l, 'p, R: Reporter, P: LocalPartition<'p, R>> Reporter
    for LiveWaterLevelReporterProxy<'l, 'p, R, P>
{
    impl_report!(speciation(&mut self, speciation: MaybeUsed<R::ReportSpeciation>) {
        if speciation.event_time < self.water_level {
            self.slow_events.push(speciation.clone().into());
        } else {
            self.fast_events.push(speciation.clone().into());
        }
    });

    impl_report!(dispersal(&mut self, dispersal: MaybeUsed<R::ReportDispersal>) {
        if dispersal.event_time < self.water_level {
            self.slow_events.push(dispersal.clone().into());
        } else {
            self.fast_events.push(dispersal.clone().into());
        }
    });

    impl_report!(progress(&mut self, _progress: Ignored) {});
}

#[contract_trait]
impl<'l, 'p, R: Reporter, P: LocalPartition<'p, R>> WaterLevelReporterProxy<'l, 'p, R, P>
    for LiveWaterLevelReporterProxy<'l, 'p, R, P>
{
    fn new(capacity: usize, local_partition: &'l mut P) -> Self {
        info!("Events will be reported using the live water-level algorithm ...");

        Self {
            water_level: NonNegativeF64::zero(),
            slow_events: Vec::with_capacity(capacity),
            fast_events: Vec::with_capacity(capacity),

            local_partition,
            _marker: PhantomData::<(&'p (), R)>,
        }
    }

    fn water_level(&self) -> NonNegativeF64 {
        self.water_level
    }

    fn advance_water_level(&mut self, water_level: NonNegativeF64) {
        // Report all events below the water level in sorted order
        self.slow_events.sort_unstable();

        for event in self.slow_events.drain(..) {
            match event.into() {
                TypedEvent::Speciation(event) => {
                    self.local_partition
                        .get_reporter()
                        .report_speciation(&event.into());
                },
                TypedEvent::Dispersal(event) => {
                    self.local_partition
                        .get_reporter()
                        .report_dispersal(&event.into());
                },
            }
        }

        // Update the water level
        self.water_level = water_level;

        // Move fast events below the new water level into slow events
        self.slow_events.extend(
            self.fast_events
                .extract_if(|event| event.event_time() < water_level),
        );
    }

    fn local_partition(&mut self) -> &mut P {
        self.local_partition
    }
}

impl<'l, 'p, R: Reporter, P: LocalPartition<'p, R>> Drop
    for LiveWaterLevelReporterProxy<'l, 'p, R, P>
{
    fn drop(&mut self) {
        // Report all events below the water level in sorted order
        self.slow_events.sort_unstable();

        for event in self.slow_events.drain(..) {
            match event.into() {
                TypedEvent::Speciation(event) => {
                    self.local_partition
                        .get_reporter()
                        .report_speciation(&event.into());
                },
                TypedEvent::Dispersal(event) => {
                    self.local_partition
                        .get_reporter()
                        .report_dispersal(&event.into());
                },
            }
        }

        // Report all events above the water level in sorted order
        self.fast_events.sort_unstable();

        for event in self.fast_events.drain(..) {
            match event.into() {
                TypedEvent::Speciation(event) => {
                    self.local_partition
                        .get_reporter()
                        .report_speciation(&event.into());
                },
                TypedEvent::Dispersal(event) => {
                    self.local_partition
                        .get_reporter()
                        .report_dispersal(&event.into());
                },
            }
        }
    }
}