use super::{AssignStorageSiteTrait, HasIngestCapacityTrait, SteadyAlgo, TiltedAlgo};
use crate::_auxlib as aux;
#[allow(non_snake_case)]
pub fn has_ingest_capacity<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> bool {
let _0: Uint = Uint::zero();
let _1: Uint = Uint::one();
let _2: Uint = _1 + _1;
let _3: Uint = _2 + _1;
if S < _3 || S % _3 != _0 {
return false;
}
let third_S: Uint = S / _3;
let two_thirds_S: Uint = _2 * third_S;
let t_div_3: Uint = T / _3;
let has_capacity_1st = SteadyAlgo::has_ingest_capacity(
two_thirds_S,
t_div_3 * _2 + aux::from_bool::<Uint>(T % _3 > _0),
);
let has_capacity_2nd = T < _2 || TiltedAlgo::has_ingest_capacity(third_S, (T - _2) / _3);
has_capacity_1st && has_capacity_2nd
}
#[allow(non_snake_case)]
pub fn _assign_storage_site<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> Uint {
debug_assert!(has_ingest_capacity(S, T));
let _1: Uint = Uint::one();
let _2: Uint = _1 + _1;
let _3: Uint = _2 + _1;
let third_S: Uint = S / _3;
let two_thirds_S: Uint = _2 * third_S;
let remainder: Uint = T % _3;
if remainder < _2 {
let adj_T: Uint = (T / _3) * _2 + remainder;
let site: Uint = SteadyAlgo::_assign_storage_site(two_thirds_S, adj_T);
if site == two_thirds_S {
S
} else {
site
}
} else {
let adj_T: Uint = T / _3;
let site: Uint = TiltedAlgo::_assign_storage_site(third_S, adj_T);
if site == third_S {
S
} else {
two_thirds_S + site
}
}
}
#[allow(non_snake_case)]
pub fn assign_storage_site<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> Option<Uint> {
let k: Uint = _assign_storage_site(S, T);
if k == S {
None
} else {
Some(k)
}
}
pub struct Algo;
#[allow(non_snake_case)]
impl crate::dstream::HasIngestCapacityTrait for Algo {
fn has_ingest_capacity<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> bool {
has_ingest_capacity::<Uint>(S, T)
}
}
#[allow(non_snake_case)]
impl crate::dstream::AssignStorageSiteTrait for Algo {
fn _assign_storage_site<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> Uint {
_assign_storage_site::<Uint>(S, T)
}
fn assign_storage_site<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> Option<Uint> {
assign_storage_site::<Uint>(S, T)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_smoke_has_ingest_capacity() {
has_ingest_capacity::<u32>(12, 5);
}
#[test]
fn test_smoke_assign_storage_site() {
assign_storage_site::<u32>(12, 5);
}
#[test]
fn test_smoke_impl_assign_storage_site() {
_assign_storage_site::<u32>(12, 5);
}
}