From 5a965d21c7d5e8a7929de67ab10736e184c02e63 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Tue, 7 Jun 2022 21:29:06 +0200 Subject: [PATCH] replace vector copying with an iterator --- world/examples/economy_tree.rs | 2 +- world/src/site/economy/map_types.rs | 8 ++++---- world/src/site/economy/mod.rs | 21 +++++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/world/examples/economy_tree.rs b/world/examples/economy_tree.rs index 8273bcceac..1ac6c1a040 100644 --- a/world/examples/economy_tree.rs +++ b/world/examples/economy_tree.rs @@ -55,7 +55,7 @@ fn main() -> Result<(), std::io::Error> { )?; } } - for j in eco.get_orders_everyone().iter() { + for j in eco.get_orders_everyone() { writeln!( f, "{:?} -> Everyone [label=\"{:.1}\"];", diff --git a/world/src/site/economy/map_types.rs b/world/src/site/economy/map_types.rs index fd8810e8ba..54eed8800d 100644 --- a/world/src/site/economy/map_types.rs +++ b/world/src/site/economy/map_types.rs @@ -354,16 +354,16 @@ impl Labor { pub fn is_everyone(&self) -> bool { self.0 == DUMMY_LABOR.0 } - pub fn orders_everyone() -> Vec<(GoodIndex, f32)> { + pub fn orders_everyone() -> impl Iterator { LABOR .get(DUMMY_LABOR.0 as usize) - .map_or(Vec::new(), |l| l.orders.clone()) + .map_or([].iter(), |l| l.orders.iter()) } - pub fn orders(&self) -> Vec<(GoodIndex, f32)> { + pub fn orders(&self) -> impl Iterator { LABOR .get(self.0 as usize) - .map_or(Vec::new(), |l| l.orders.clone()) + .map_or([].iter(), |l| l.orders.iter()) } pub fn products(&self) -> (GoodIndex, f32) { diff --git a/world/src/site/economy/mod.rs b/world/src/site/economy/mod.rs index 16bb9aa629..60d900f160 100644 --- a/world/src/site/economy/mod.rs +++ b/world/src/site/economy/mod.rs @@ -221,8 +221,9 @@ impl Economy { fn get_orders(&self) -> &'static LaborMap> { lazy_static! { static ref ORDERS: LaborMap> = { - let mut res = LaborMap::default(); - res.iter_mut().for_each(|(i, e)| *e = i.orders()); + let mut res: LaborMap> = LaborMap::default(); + res.iter_mut() + .for_each(|(i, e)| e.extend(i.orders().copied())); res }; } @@ -230,7 +231,9 @@ impl Economy { } /// resources consumed by everyone (no matter which profession) - fn get_orders_everyone(&self) -> Vec<(GoodIndex, f32)> { Labor::orders_everyone() } + fn get_orders_everyone(&self) -> impl Iterator { + Labor::orders_everyone() + } fn get_production(&self) -> LaborMap<(GoodIndex, f32)> { // cache the site independent part of production @@ -582,7 +585,7 @@ impl Economy { assert!(next_demand[*good] >= 0.0); } } - for (good, amount) in self.get_orders_everyone().iter() { + for (good, amount) in self.get_orders_everyone() { next_demand[*good] += *amount * self.pop; assert!(next_demand[*good] >= 0.0); } @@ -802,7 +805,7 @@ impl Economy { demand[*good] += *amount * workers; } } - for (good, amount) in self.get_orders_everyone().iter() { + for (good, amount) in self.get_orders_everyone() { demand[*good] += *amount * self.pop; } if INTER_SITE_TRADE { @@ -1085,7 +1088,7 @@ impl Economy { } } // consume goods needed by everyone - for &(good, amount) in self.get_orders_everyone().iter() { + for &(good, amount) in self.get_orders_everyone() { let needed = amount * self.pop; let available = stocks_before[good]; self.stocks[good] = (self.stocks[good] - needed.min(available)).max(0.0); @@ -1142,7 +1145,7 @@ impl Economy { assert!(next_demand[*good] >= 0.0); } } - for (good, amount) in self.get_orders_everyone().iter() { + for (good, amount) in self.get_orders_everyone() { next_demand[*good] += *amount * self.pop; assert!(next_demand[*good] >= 0.0); } @@ -1426,7 +1429,9 @@ pub struct GraphInfo { impl GraphInfo { pub fn get_orders(&self) -> &'static LaborMap> { self.dummy.get_orders() } - pub fn get_orders_everyone(&self) -> Vec<(GoodIndex, f32)> { self.dummy.get_orders_everyone() } + pub fn get_orders_everyone(&self) -> impl Iterator { + self.dummy.get_orders_everyone() + } pub fn get_production(&self) -> LaborMap<(GoodIndex, f32)> { self.dummy.get_production() }