Created
December 20, 2022 21:45
-
-
Save dhable/f23b5cc5e346ee1b1bbe2fda1c73e2cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct WeightedValueGen<'a, T, const N: usize> { | |
values: [&'a T; N], | |
weights: [f32; N], | |
} | |
impl<'a, T, const N: usize> WeightedValueGen<'a, T, N> { | |
#[allow(dead_code, unused_mut)] | |
fn new(input: &'a [(T, f32); N]) -> Self { | |
let mut values = unsafe { | |
let mut values: [mem::MaybeUninit<&T>; N] = mem::MaybeUninit::uninit().assume_init(); | |
for i in 0..values.len() { | |
std::ptr::write(values[i].as_mut_ptr(), &input[i].0); | |
} | |
(&*(&mem::MaybeUninit::new(values) as *const _ as *const mem::MaybeUninit<_>)) | |
.assume_init_read() | |
}; | |
let weights = build_weight_arr(input); | |
Self { values, weights } | |
} | |
#[allow(dead_code)] | |
pub fn get_value(&self) -> &T { | |
let idx = get_rand_index(rnd_point, &self.weights); | |
&self.values[idx] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment