|
1 | 1 | use alloc::{vec, vec::Vec}; |
2 | | -use core::num::{NonZeroU128, NonZeroU64, NonZeroUsize}; |
3 | 2 |
|
4 | 3 | use hashbrown::HashMap; |
5 | 4 |
|
6 | | -use necsim_core::cogs::{ |
7 | | - distribution::{IndexU128, IndexU64, IndexUsize, Length}, |
8 | | - Backup, DistributionSampler, Rng, RngCore, SeedableRng, |
9 | | -}; |
| 5 | +use necsim_core::cogs::{Backup, SeedableRng}; |
10 | 6 | use necsim_core_bond::{NonNegativeF64, PositiveF64}; |
11 | | -use necsim_core_maths::MathsCore; |
12 | 7 |
|
13 | 8 | use crate::cogs::{ |
14 | 9 | maths::intrinsics::IntrinsicsMathsCore, |
15 | 10 | rng::{simple::SimpleRng, wyhash::WyHash}, |
16 | 11 | }; |
17 | 12 |
|
18 | 13 | use super::{ |
19 | | - super::decompose_weight, DynamicAliasMethodIndexedSampler, EventLocation, |
20 | | - RejectionSamplingGroup, |
| 14 | + super::{decompose_weight, tests::DummyRng}, |
| 15 | + DynamicAliasMethodIndexedSampler, EventLocation, RejectionSamplingGroup, |
21 | 16 | }; |
22 | 17 |
|
23 | 18 | #[test] |
@@ -1077,151 +1072,3 @@ fn debug_display_sampler() { |
1077 | 1072 | "DynamicAliasMethodIndexedSampler { exponents: [2, 1], total_weight: 20.0 }" |
1078 | 1073 | ); |
1079 | 1074 | } |
1080 | | - |
1081 | | -// GRCOV_EXCL_START |
1082 | | -#[derive(Debug, serde::Serialize, serde::Deserialize)] |
1083 | | -struct DummyRng(Vec<f64>); |
1084 | | - |
1085 | | -impl DummyRng { |
1086 | | - fn new(mut vec: Vec<f64>) -> Self { |
1087 | | - vec.reverse(); |
1088 | | - |
1089 | | - Self(vec) |
1090 | | - } |
1091 | | - |
1092 | | - fn sample_f64(&mut self) -> f64 { |
1093 | | - self.0.pop().unwrap() |
1094 | | - } |
1095 | | -} |
1096 | | - |
1097 | | -impl RngCore for DummyRng { |
1098 | | - type Seed = [u8; 0]; |
1099 | | - |
1100 | | - #[must_use] |
1101 | | - fn from_seed(_seed: Self::Seed) -> Self { |
1102 | | - Self(Vec::new()) |
1103 | | - } |
1104 | | - |
1105 | | - #[must_use] |
1106 | | - fn sample_u64(&mut self) -> u64 { |
1107 | | - #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] |
1108 | | - { |
1109 | | - ((self.sample_f64() / f64::from_bits(0x3CA0_0000_0000_0000_u64)) as u64) << 11 |
1110 | | - } |
1111 | | - } |
1112 | | -} |
1113 | | - |
1114 | | -impl Rng<IntrinsicsMathsCore> for DummyRng { |
1115 | | - type Generator = Self; |
1116 | | - type Sampler = DummyDistributionSamplers; |
1117 | | - |
1118 | | - fn generator(&mut self) -> &mut Self::Generator { |
1119 | | - self |
1120 | | - } |
1121 | | - |
1122 | | - fn map_generator<F: FnOnce(Self::Generator) -> Self::Generator>(self, map: F) -> Self { |
1123 | | - map(self) |
1124 | | - } |
1125 | | - |
1126 | | - fn with<F: FnOnce(&mut Self::Generator, &Self::Sampler) -> Q, Q>(&mut self, inner: F) -> Q { |
1127 | | - let samplers = DummyDistributionSamplers; |
1128 | | - |
1129 | | - inner(self, &samplers) |
1130 | | - } |
1131 | | -} |
1132 | | - |
1133 | | -struct DummyDistributionSamplers; |
1134 | | - |
1135 | | -impl DistributionSampler<IntrinsicsMathsCore, DummyRng, DummyDistributionSamplers, IndexUsize> |
1136 | | - for DummyDistributionSamplers |
1137 | | -{ |
1138 | | - type ConcreteSampler = Self; |
1139 | | - |
1140 | | - fn concrete(&self) -> &Self::ConcreteSampler { |
1141 | | - self |
1142 | | - } |
1143 | | - |
1144 | | - fn sample_distribution( |
1145 | | - &self, |
1146 | | - rng: &mut DummyRng, |
1147 | | - _samplers: &DummyDistributionSamplers, |
1148 | | - Length(length): Length<NonZeroUsize>, |
1149 | | - ) -> usize { |
1150 | | - let u01 = rng.sample_f64(); |
1151 | | - |
1152 | | - // Safety: U[0, 1) * length in [0, 2^[32/64]) is a valid [u32/u64] |
1153 | | - // since (1 - 2^-53) * 2^[32/64] <= (2^[32/64] - 1) |
1154 | | - #[allow(clippy::cast_precision_loss)] |
1155 | | - let index = unsafe { |
1156 | | - IntrinsicsMathsCore::floor(u01 * (length.get() as f64)).to_int_unchecked::<usize>() |
1157 | | - }; |
1158 | | - |
1159 | | - if cfg!(target_pointer_width = "32") { |
1160 | | - // Note: [0, 2^32) is losslessly represented in f64 |
1161 | | - index |
1162 | | - } else { |
1163 | | - // Note: Ensure index < length despite |
1164 | | - // usize->f64->usize precision loss |
1165 | | - index.min(length.get() - 1) |
1166 | | - } |
1167 | | - } |
1168 | | -} |
1169 | | - |
1170 | | -impl DistributionSampler<IntrinsicsMathsCore, DummyRng, DummyDistributionSamplers, IndexU64> |
1171 | | - for DummyDistributionSamplers |
1172 | | -{ |
1173 | | - type ConcreteSampler = Self; |
1174 | | - |
1175 | | - fn concrete(&self) -> &Self::ConcreteSampler { |
1176 | | - self |
1177 | | - } |
1178 | | - |
1179 | | - fn sample_distribution( |
1180 | | - &self, |
1181 | | - rng: &mut DummyRng, |
1182 | | - _samplers: &DummyDistributionSamplers, |
1183 | | - Length(length): Length<NonZeroU64>, |
1184 | | - ) -> u64 { |
1185 | | - let u01 = rng.sample_f64(); |
1186 | | - |
1187 | | - // Safety: U[0, 1) * length in [0, 2^64) is a valid u64 |
1188 | | - // since (1 - 2^-53) * 2^64 <= (2^64 - 1) |
1189 | | - #[allow(clippy::cast_precision_loss)] |
1190 | | - let index = unsafe { |
1191 | | - IntrinsicsMathsCore::floor(u01 * (length.get() as f64)).to_int_unchecked::<u64>() |
1192 | | - }; |
1193 | | - |
1194 | | - // Note: Ensure index < length despite u64->f64->u64 precision loss |
1195 | | - index.min(length.get() - 1) |
1196 | | - } |
1197 | | -} |
1198 | | - |
1199 | | -impl DistributionSampler<IntrinsicsMathsCore, DummyRng, DummyDistributionSamplers, IndexU128> |
1200 | | - for DummyDistributionSamplers |
1201 | | -{ |
1202 | | - type ConcreteSampler = Self; |
1203 | | - |
1204 | | - fn concrete(&self) -> &Self::ConcreteSampler { |
1205 | | - self |
1206 | | - } |
1207 | | - |
1208 | | - fn sample_distribution( |
1209 | | - &self, |
1210 | | - rng: &mut DummyRng, |
1211 | | - _samplers: &DummyDistributionSamplers, |
1212 | | - Length(length): Length<NonZeroU128>, |
1213 | | - ) -> u128 { |
1214 | | - let u01 = rng.sample_f64(); |
1215 | | - |
1216 | | - // Safety: U[0, 1) * length in [0, 2^128) is a valid u128 |
1217 | | - // since (1 - 2^-53) * 2^128 <= (2^128 - 1) |
1218 | | - #[allow(clippy::cast_precision_loss)] |
1219 | | - let index = unsafe { |
1220 | | - IntrinsicsMathsCore::floor(u01 * (length.get() as f64)).to_int_unchecked::<u128>() |
1221 | | - }; |
1222 | | - |
1223 | | - // Note: Ensure index < length despite u128->f64->u128 precision loss |
1224 | | - index.min(length.get() - 1) |
1225 | | - } |
1226 | | -} |
1227 | | -// GRCOV_EXCL_STOP |
0 commit comments