AbstractSim: convert the {get,put}_material functions to use only Index

This commit is contained in:
2022-07-29 14:43:59 -07:00
parent f4ac5de099
commit 7e452f508f
3 changed files with 21 additions and 14 deletions

View File

@@ -334,11 +334,11 @@ impl<R: Real, M: Material<R> + Send + Sync> AbstractSim for SimState<R, M> {
self.step_no
}
fn put_material<C: Coord, M2: Into<Self::Material>>(&mut self, pos: C, mat: M2) {
*self.get_mat_mut(pos) = mat.into();
fn put_material_index(&mut self, pos: Index, mat: M) {
*self.get_mat_mut(pos) = mat;
}
fn get_material<C: Coord>(&self, pos: C) -> &M {
fn get_material_index(&self, pos: Index) -> &M {
self.get_material(pos)
}

View File

@@ -112,19 +112,28 @@ impl<R: Real> CellStateWithM<R> {
pub trait AbstractSim: Sync {
type Real: Real;
type Material;
fn meta(&self) -> SimMeta<f32>;
fn step_no(&self) -> u64;
fn sample(&self, pos: Meters) -> Sample;
fn get_material_index(&self, at: Index) -> &Self::Material;
fn put_material_index(&mut self, at: Index, m: Self::Material);
fn step_multiple<S: AbstractStimulus>(&mut self, num_steps: u32, s: &S);
/// Take a "snapshot" of the simulation, dropping all material-specific information.
fn to_static(&self) -> StaticSim;
fn to_generic(&self) -> GenericSim<Self::Real>;
fn put_material<C: Coord, M: Into<Self::Material>>(&mut self, pos: C, mat: M);
fn get_material<C: Coord>(&self, pos: C) -> &Self::Material;
fn step_multiple<S: AbstractStimulus>(&mut self, num_steps: u32, s: &S);
//--- HELPER METHODS below (derived) ---//
fn get_material<C: Coord>(&self, pos: C) -> &Self::Material {
self.get_material_index(pos.to_index(self.feature_size()))
}
fn put_material<C: Coord, M: Into<Self::Material>>(&mut self, pos: C, mat: M) {
self.put_material_index(pos.to_index(self.feature_size()), mat.into())
}
fn step(&mut self) {
// XXX: try not to exercise this path! NoopStimulus is probably a lot of waste.
self.step_multiple(1, &NoopStimulus);

View File

@@ -85,15 +85,13 @@ where
self.step_no
}
fn put_material<C: Coord, M2: Into<Self::Material>>(&mut self, pos: C, mat: M2) {
let pos_sim = pos.to_index(self.feature_size());
let flat_idx = self.flat_index(pos_sim).unwrap();
self.mat[flat_idx] = mat.into();
fn put_material_index(&mut self, pos: Index, mat: M) {
let flat_idx = self.flat_index(pos).unwrap();
self.mat[flat_idx] = mat;
}
fn get_material<C: Coord>(&self, pos: C) -> &Self::Material {
let pos_sim = pos.to_index(self.feature_size());
let flat_idx = self.flat_index(pos_sim).unwrap();
fn get_material_index(&self, pos: Index) -> &M {
let flat_idx = self.flat_index(pos).unwrap();
&self.mat[flat_idx]
}