Changeset - r21351:95eabab86f13
[Not reviewed]
master
0 2 0
fonsinchen - 10 years ago 2014-04-08 19:28:14
fonsinchen@openttd.org
(svn r26448) -Fix [FS#5970]: Avoid division by 0 when scaling flow values.
2 files changed with 6 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/linkgraph/flowmapper.cpp
Show inline comments
 
@@ -40,26 +40,28 @@ void FlowMapper::Run(LinkGraphJob &job) 
 
				/* Prev node is origin. Simply add flow. */
 
				prev_node.Flows().AddFlow(origin, via, flow);
 
			}
 
		}
 
	}
 

	
 
	for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
 
		/* Remove local consumption shares marked as invalid. */
 
		Node node = job[node_id];
 
		FlowStatMap &flows = node.Flows();
 
		flows.FinalizeLocalConsumption(node.Station());
 
		if (this->scale) {
 
			/* Scale by time the graph has been running without being compressed. */
 
			uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression();
 
			/* Scale by time the graph has been running without being compressed. Add 1 to avoid
 
			 * division by 0 if spawn date == last compression date. This matches
 
			 * LinkGraph::Monthly(). */
 
			uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
 
			for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
 
				i->second.ScaleToMonthly(runtime);
 
			}
 
		}
 
		/* Clear paths. */
 
		PathList &paths = node.Paths();
 
		for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
 
			delete *i;
 
		}
 
		paths.clear();
 
	}
 
}
src/station_cmd.cpp
Show inline comments
 
@@ -4305,27 +4305,29 @@ void FlowStat::ReleaseShare(StationID st
 
			new_shares[flow + it->first] = it->second;
 
		} else {
 
			flow = 0;
 
		}
 
	}
 
	this->shares.swap(new_shares);
 
	assert(!this->shares.empty());
 
}
 

	
 
/**
 
 * Scale all shares from link graph's runtime to monthly values.
 
 * @param runtime Time the link graph has been running without compression.
 
 * @pre runtime must be greater than 0 as we don't want infinite flow values.
 
 */
 
void FlowStat::ScaleToMonthly(uint runtime)
 
{
 
	assert(runtime > 0);
 
	SharesMap new_shares;
 
	uint share = 0;
 
	for (SharesMap::iterator i = this->shares.begin(); i != this->shares.end(); ++i) {
 
		share = max(share + 1, i->first * 30 / runtime);
 
		new_shares[share] = i->second;
 
		if (this->unrestricted == i->first) this->unrestricted = share;
 
	}
 
	this->shares.swap(new_shares);
 
}
 

	
 
/**
 
 * Add some flow from "origin", going via "via".
0 comments (0 inline, 0 general)