replace vector copying with an iterator

This commit is contained in:
Christof Petig 2022-06-07 21:29:06 +02:00
parent 39fafe646c
commit 5a965d21c7
3 changed files with 18 additions and 13 deletions

View File

@ -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!( writeln!(
f, f,
"{:?} -> Everyone [label=\"{:.1}\"];", "{:?} -> Everyone [label=\"{:.1}\"];",

View File

@ -354,16 +354,16 @@ impl Labor {
pub fn is_everyone(&self) -> bool { self.0 == DUMMY_LABOR.0 } pub fn is_everyone(&self) -> bool { self.0 == DUMMY_LABOR.0 }
pub fn orders_everyone() -> Vec<(GoodIndex, f32)> { pub fn orders_everyone() -> impl Iterator<Item = &'static (GoodIndex, f32)> {
LABOR LABOR
.get(DUMMY_LABOR.0 as usize) .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<Item = &'static (GoodIndex, f32)> {
LABOR LABOR
.get(self.0 as usize) .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) { pub fn products(&self) -> (GoodIndex, f32) {

View File

@ -221,8 +221,9 @@ impl Economy {
fn get_orders(&self) -> &'static LaborMap<Vec<(GoodIndex, f32)>> { fn get_orders(&self) -> &'static LaborMap<Vec<(GoodIndex, f32)>> {
lazy_static! { lazy_static! {
static ref ORDERS: LaborMap<Vec<(GoodIndex, f32)>> = { static ref ORDERS: LaborMap<Vec<(GoodIndex, f32)>> = {
let mut res = LaborMap::default(); let mut res: LaborMap<Vec<(GoodIndex, f32)>> = LaborMap::default();
res.iter_mut().for_each(|(i, e)| *e = i.orders()); res.iter_mut()
.for_each(|(i, e)| e.extend(i.orders().copied()));
res res
}; };
} }
@ -230,7 +231,9 @@ impl Economy {
} }
/// resources consumed by everyone (no matter which profession) /// 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<Item = &'static (GoodIndex, f32)> {
Labor::orders_everyone()
}
fn get_production(&self) -> LaborMap<(GoodIndex, f32)> { fn get_production(&self) -> LaborMap<(GoodIndex, f32)> {
// cache the site independent part of production // cache the site independent part of production
@ -582,7 +585,7 @@ impl Economy {
assert!(next_demand[*good] >= 0.0); 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; next_demand[*good] += *amount * self.pop;
assert!(next_demand[*good] >= 0.0); assert!(next_demand[*good] >= 0.0);
} }
@ -802,7 +805,7 @@ impl Economy {
demand[*good] += *amount * workers; 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; demand[*good] += *amount * self.pop;
} }
if INTER_SITE_TRADE { if INTER_SITE_TRADE {
@ -1085,7 +1088,7 @@ impl Economy {
} }
} }
// consume goods needed by everyone // 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 needed = amount * self.pop;
let available = stocks_before[good]; let available = stocks_before[good];
self.stocks[good] = (self.stocks[good] - needed.min(available)).max(0.0); self.stocks[good] = (self.stocks[good] - needed.min(available)).max(0.0);
@ -1142,7 +1145,7 @@ impl Economy {
assert!(next_demand[*good] >= 0.0); 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; next_demand[*good] += *amount * self.pop;
assert!(next_demand[*good] >= 0.0); assert!(next_demand[*good] >= 0.0);
} }
@ -1426,7 +1429,9 @@ pub struct GraphInfo {
impl GraphInfo { impl GraphInfo {
pub fn get_orders(&self) -> &'static LaborMap<Vec<(GoodIndex, f32)>> { self.dummy.get_orders() } pub fn get_orders(&self) -> &'static LaborMap<Vec<(GoodIndex, f32)>> { 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<Item = &'static (GoodIndex, f32)> {
self.dummy.get_orders_everyone()
}
pub fn get_production(&self) -> LaborMap<(GoodIndex, f32)> { self.dummy.get_production() } pub fn get_production(&self) -> LaborMap<(GoodIndex, f32)> { self.dummy.get_production() }