refine Updatable concept

This commit is contained in:
Quaternions 2024-02-01 00:09:30 -08:00
parent fa97ab8c6d
commit 9291b77679
3 changed files with 18 additions and 74 deletions

View File

@ -49,22 +49,10 @@ pub struct StageUpdate{
ordered_checkpoints:HashMap<CheckpointId,ModelId>, ordered_checkpoints:HashMap<CheckpointId,ModelId>,
unordered_checkpoints:HashSet<ModelId>, unordered_checkpoints:HashSet<ModelId>,
} }
impl Updatable<&StageUpdate> for Stage{ impl Updatable<StageUpdate> for Stage{
fn insert(&mut self,update:&StageUpdate){ fn update(&mut self,update:StageUpdate){
for (&checkpoint,&model) in &update.ordered_checkpoints{ self.ordered_checkpoints.extend(update.ordered_checkpoints);
self.ordered_checkpoints.insert(checkpoint,model); self.unordered_checkpoints.extend(update.unordered_checkpoints);
}
for &checkpoint in &update.unordered_checkpoints{
self.unordered_checkpoints.insert(checkpoint);
}
}
fn remove(&mut self,update:&StageUpdate){
for (checkpoint,_) in &update.ordered_checkpoints{
self.ordered_checkpoints.remove(checkpoint);
}
for model in &update.unordered_checkpoints{
self.unordered_checkpoints.remove(model);
}
} }
} }
@ -132,38 +120,16 @@ pub struct ModeUpdate{
elements:HashMap<ModelId,StageElement>, elements:HashMap<ModelId,StageElement>,
jump_limit:HashMap<ModelId,u32>, jump_limit:HashMap<ModelId,u32>,
} }
impl Updatable<&ModeUpdate> for Mode{ impl Updatable<ModeUpdate> for Mode{
fn insert(&mut self,update:&ModeUpdate){ fn update(&mut self,update:ModeUpdate){
for (&model,&zone) in &update.zones{ self.zones.extend(update.zones);
self.zones.insert(model,zone); for (stage,stage_update) in update.stages{
}
for (stage,stage_update) in &update.stages{
if let Some(stage)=self.stages.get_mut(stage.0 as usize){ if let Some(stage)=self.stages.get_mut(stage.0 as usize){
stage.insert(stage_update); stage.update(stage_update);
} }
} }
for (&model,stage_element) in &update.elements{ self.elements.extend(update.elements);
self.elements.insert(model,stage_element.clone()); self.jump_limit.extend(update.jump_limit);
}
for (&model,&limit) in &update.jump_limit{
self.jump_limit.insert(model,limit);
}
}
fn remove(&mut self,update:&ModeUpdate){
for (model,_) in &update.zones{
self.zones.remove(model);
}
for (stage,stage_update) in &update.stages{
if let Some(stage)=self.stages.get_mut(stage.0 as usize){
stage.remove(stage_update);
}
}
for (model,_) in &update.elements{
self.elements.remove(model);
}
for (model,_) in &update.jump_limit{
self.jump_limit.remove(model);
}
} }
} }
impl ModeUpdate{ impl ModeUpdate{
@ -206,18 +172,11 @@ impl Modes{
pub struct ModesUpdate{ pub struct ModesUpdate{
modes:HashMap<ModeId,ModeUpdate>, modes:HashMap<ModeId,ModeUpdate>,
} }
impl Updatable<&ModesUpdate> for Modes{ impl Updatable<ModesUpdate> for Modes{
fn insert(&mut self,update:&ModesUpdate){ fn update(&mut self,update:ModesUpdate){
for (mode,mode_update) in &update.modes{ for (mode,mode_update) in update.modes{
if let Some(mode)=self.modes.get_mut(mode.0 as usize){ if let Some(mode)=self.modes.get_mut(mode.0 as usize){
mode.insert(mode_update); mode.update(mode_update);
}
}
}
fn remove(&mut self,update:&ModesUpdate){
for (mode,mode_update) in &update.modes{
if let Some(mode)=self.modes.get_mut(mode.0 as usize){
mode.remove(mode_update);
} }
} }
} }

View File

@ -62,17 +62,8 @@ pub struct Models{
models:HashMap<ModelId,Model>, models:HashMap<ModelId,Model>,
} }
impl Updatable<Models> for Models{ impl Updatable<Models> for Models{
fn insert(&mut self,update:Models){ fn update(&mut self,update:Models){
self.indexed_models.extend(update.indexed_models); self.indexed_models.extend(update.indexed_models);
self.models.extend(update.models); self.models.extend(update.models);
} }
fn remove(&mut self,update:Models){
for (indexed_model_id,_) in &update.indexed_models{
self.indexed_models.remove(indexed_model_id);
}
for (model_id,_) in &update.models{
self.models.remove(model_id);
}
todo!("stop cloning models for remove");
}
} }

View File

@ -1,10 +1,4 @@
pub trait Updatable<Updater>{ pub trait Updatable<Updater>{
fn insert(&mut self,update:Updater);
fn remove(&mut self,update:Updater);
}
//what if do like this
//*
pub trait Updatable2<Updater>{
fn update(&mut self,update:Updater); fn update(&mut self,update:Updater);
} }
#[derive(Clone,Copy,Hash,Eq,PartialEq)] #[derive(Clone,Copy,Hash,Eq,PartialEq)]
@ -37,14 +31,14 @@ struct OuterUpdate{
//#[updatable(Update)] //#[updatable(Update)]
//inners:std::collections::HashMap<InnerId,InnerUpdate>, //inners:std::collections::HashMap<InnerId,InnerUpdate>,
} }
impl Updatable2<InnerUpdate> for Inner{ impl Updatable<InnerUpdate> for Inner{
fn update(&mut self,update:InnerUpdate){ fn update(&mut self,update:InnerUpdate){
if let Some(enabled)=update.enabled{ if let Some(enabled)=update.enabled{
self.enabled=enabled; self.enabled=enabled;
} }
} }
} }
impl Updatable2<OuterUpdate> for Outer{ impl Updatable<OuterUpdate> for Outer{
fn update(&mut self,update:OuterUpdate){ fn update(&mut self,update:OuterUpdate){
for (id,up) in update.inners{ for (id,up) in update.inners{
match up{ match up{