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
use core::{
    marker::PhantomData,
    num::{NonZeroU32, NonZeroU64},
};

use necsim_core::{
    cogs::{Habitat, MathsCore, RngCore, UniformlySampleableHabitat},
    landscape::{IndexedLocation, LandscapeExtent, Location},
};
use necsim_core_bond::{OffByOneU32, OffByOneU64};

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct NonSpatialHabitat<M: MathsCore> {
    #[cfg_attr(feature = "cuda", cuda(embed))]
    extent: LandscapeExtent,
    deme: NonZeroU32,
    marker: PhantomData<M>,
}

impl<M: MathsCore> NonSpatialHabitat<M> {
    #[must_use]
    #[debug_ensures(
        ret.get_total_habitat() == old(
            OffByOneU64::from(area.0) * OffByOneU64::from(area.1) * OffByOneU64::from(deme)
        ),
        "creates a habitat with community size area.0 * area.1 * deme"
    )]
    pub fn new(area: (OffByOneU32, OffByOneU32), deme: NonZeroU32) -> Self {
        Self {
            extent: LandscapeExtent::new(Location::new(0, 0), area.0, area.1),
            deme,
            marker: PhantomData::<M>,
        }
    }

    pub(super) fn new_with_offset(
        offset: Location,
        area: (OffByOneU32, OffByOneU32),
        deme: NonZeroU32,
    ) -> Self {
        Self {
            extent: LandscapeExtent::new(offset, area.0, area.1),
            deme,
            marker: PhantomData::<M>,
        }
    }

    #[must_use]
    pub fn get_deme(&self) -> NonZeroU32 {
        self.deme
    }
}

impl<M: MathsCore> Clone for NonSpatialHabitat<M> {
    fn clone(&self) -> Self {
        Self {
            extent: self.extent.clone(),
            deme: self.deme,
            marker: PhantomData::<M>,
        }
    }
}

#[contract_trait]
impl<M: MathsCore> Habitat<M> for NonSpatialHabitat<M> {
    type LocationIterator<'a> = impl Iterator<Item = Location>;

    #[must_use]
    fn is_finite(&self) -> bool {
        true
    }

    #[must_use]
    fn get_extent(&self) -> &LandscapeExtent {
        &self.extent
    }

    #[must_use]
    fn get_total_habitat(&self) -> OffByOneU64 {
        OffByOneU64::from(self.extent.width())
            * OffByOneU64::from(self.extent.height())
            * OffByOneU64::from(self.deme)
    }

    #[must_use]
    fn get_habitat_at_location(&self, _location: &Location) -> u32 {
        self.deme.get()
    }

    #[must_use]
    fn map_indexed_location_to_u64_injective(&self, indexed_location: &IndexedLocation) -> u64 {
        (u64::from(
            indexed_location
                .location()
                .y()
                .wrapping_sub(self.extent.origin().y()),
        ) * u64::from(self.extent.width())
            + u64::from(
                indexed_location
                    .location()
                    .x()
                    .wrapping_sub(self.extent.origin().x()),
            ))
            * u64::from(self.deme.get())
            + u64::from(indexed_location.index())
    }

    #[must_use]
    fn iter_habitable_locations(&self) -> Self::LocationIterator<'_> {
        self.get_extent().iter()
    }
}

#[contract_trait]
impl<M: MathsCore, G: RngCore<M>> UniformlySampleableHabitat<M, G> for NonSpatialHabitat<M> {
    #[must_use]
    #[inline]
    fn sample_habitable_indexed_location(&self, rng: &mut G) -> IndexedLocation {
        use necsim_core::cogs::RngSampler;

        let habitat_index_max =
            self.extent.width().get() * self.extent.height().get() * u64::from(self.deme.get());

        // Safety: habitat width, height, and deme are all > 0
        let mut dispersal_target_index =
            rng.sample_index_u64(unsafe { NonZeroU64::new_unchecked(habitat_index_max) });
        #[allow(clippy::cast_possible_truncation)]
        let index = (dispersal_target_index % u64::from(self.deme.get())) as u32;
        dispersal_target_index /= u64::from(self.deme.get());

        #[allow(clippy::cast_possible_truncation)]
        IndexedLocation::new(
            Location::new(
                self.extent
                    .origin()
                    .x()
                    .wrapping_add((dispersal_target_index % self.extent.width().get()) as u32),
                self.extent
                    .origin()
                    .y()
                    .wrapping_add((dispersal_target_index / self.extent.width().get()) as u32),
            ),
            index,
        )
    }
}