Codechange: [Linkgraph] Drop node/edge wrappers from LinkGraphJob.
This commit is contained in:
@@ -28,7 +28,7 @@ extern LinkGraphJobPool _link_graph_job_pool;
|
||||
* Class for calculation jobs to be run on link graphs.
|
||||
*/
|
||||
class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool>{
|
||||
private:
|
||||
public:
|
||||
/**
|
||||
* Demand between two nodes.
|
||||
*/
|
||||
@@ -41,13 +41,46 @@ private:
|
||||
* Annotation for a link graph edge.
|
||||
*/
|
||||
struct EdgeAnnotation {
|
||||
const LinkGraph::BaseEdge &base; ///< Reference to the edge that is annotated.
|
||||
|
||||
uint flow; ///< Planned flow over this edge.
|
||||
|
||||
EdgeAnnotation(const LinkGraph::BaseEdge &base) : base(base), flow(0) {}
|
||||
|
||||
/**
|
||||
* Get the total flow on the edge.
|
||||
* @return Flow.
|
||||
*/
|
||||
uint Flow() const { return this->flow; }
|
||||
|
||||
/**
|
||||
* Add some flow.
|
||||
* @param flow Flow to be added.
|
||||
*/
|
||||
void AddFlow(uint flow) { this->flow += flow; }
|
||||
|
||||
/**
|
||||
* Remove some flow.
|
||||
* @param flow Flow to be removed.
|
||||
*/
|
||||
void RemoveFlow(uint flow)
|
||||
{
|
||||
assert(flow <= this->flow);
|
||||
this->flow -= flow;
|
||||
}
|
||||
|
||||
friend inline bool operator <(NodeID dest, const EdgeAnnotation &rhs)
|
||||
{
|
||||
return dest < rhs.base.dest_node;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Annotation for a link graph node.
|
||||
*/
|
||||
struct NodeAnnotation {
|
||||
const LinkGraph::BaseNode &base; ///< Reference to the node that is annotated.
|
||||
|
||||
uint undelivered_supply; ///< Amount of supply that hasn't been distributed yet.
|
||||
PathList paths; ///< Paths through this node, sorted so that those with flow == 0 are in the back.
|
||||
FlowStatMap flows; ///< Planned flows to other nodes.
|
||||
@@ -55,13 +88,73 @@ private:
|
||||
std::vector<EdgeAnnotation> edges; ///< Annotations for all edges originating at this node.
|
||||
std::vector<DemandAnnotation> demands; ///< Annotations for the demand to all other nodes.
|
||||
|
||||
NodeAnnotation(const LinkGraph::BaseNode &node, size_t size) : undelivered_supply(node.supply), paths(), flows()
|
||||
NodeAnnotation(const LinkGraph::BaseNode &node, size_t size) : base(node), undelivered_supply(node.supply), paths(), flows()
|
||||
{
|
||||
this->edges.resize(node.edges.size());
|
||||
this->edges.reserve(node.edges.size());
|
||||
for (auto &e : node.edges) this->edges.emplace_back(e);
|
||||
this->demands.resize(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an edge starting at this node.
|
||||
* @param to Remote end of the edge.
|
||||
* @return Edge between this node and "to".
|
||||
*/
|
||||
EdgeAnnotation &operator[](NodeID to)
|
||||
{
|
||||
auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
|
||||
assert(it != this->edges.end());
|
||||
return *it;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an edge starting at this node.
|
||||
* @param to Remote end of the edge.
|
||||
* @return Edge between this node and "to".
|
||||
*/
|
||||
const EdgeAnnotation &operator[](NodeID to) const
|
||||
{
|
||||
auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
|
||||
assert(it != this->edges.end());
|
||||
return *it;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transport demand between end the points of the edge.
|
||||
* @return Demand.
|
||||
*/
|
||||
uint DemandTo(NodeID to) const { return this->demands[to].demand; }
|
||||
|
||||
/**
|
||||
* Get the transport demand that hasn't been satisfied by flows, yet.
|
||||
* @return Unsatisfied demand.
|
||||
*/
|
||||
uint UnsatisfiedDemandTo(NodeID to) const { return this->demands[to].unsatisfied_demand; }
|
||||
|
||||
/**
|
||||
* Satisfy some demand.
|
||||
* @param demand Demand to be satisfied.
|
||||
*/
|
||||
void SatisfyDemandTo(NodeID to, uint demand)
|
||||
{
|
||||
assert(demand <= this->demands[to].unsatisfied_demand);
|
||||
this->demands[to].unsatisfied_demand -= demand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deliver some supply, adding demand to the respective edge.
|
||||
* @param to Destination for supply.
|
||||
* @param amount Amount of supply to be delivered.
|
||||
*/
|
||||
void DeliverSupply(NodeID to, uint amount)
|
||||
{
|
||||
this->undelivered_supply -= amount;
|
||||
this->demands[to].demand += amount;
|
||||
this->demands[to].unsatisfied_demand += amount;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
typedef std::vector<NodeAnnotation> NodeAnnotationVector;
|
||||
|
||||
friend SaveLoadTable GetLinkGraphJobDesc();
|
||||
@@ -81,199 +174,6 @@ protected:
|
||||
void SpawnThread();
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* A job edge. Wraps a link graph edge and an edge annotation. The
|
||||
* annotation can be modified, the edge is constant.
|
||||
*/
|
||||
class Edge : public LinkGraph::ConstEdge {
|
||||
private:
|
||||
EdgeAnnotation &anno; ///< Annotation being wrapped.
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param edge Link graph edge to be wrapped.
|
||||
* @param anno Annotation to be wrapped.
|
||||
*/
|
||||
Edge(const LinkGraph::BaseEdge &edge, EdgeAnnotation &anno) :
|
||||
LinkGraph::ConstEdge(edge), anno(anno) {}
|
||||
|
||||
/**
|
||||
* Get the total flow on the edge.
|
||||
* @return Flow.
|
||||
*/
|
||||
uint Flow() const { return this->anno.flow; }
|
||||
|
||||
/**
|
||||
* Add some flow.
|
||||
* @param flow Flow to be added.
|
||||
*/
|
||||
void AddFlow(uint flow) { this->anno.flow += flow; }
|
||||
|
||||
/**
|
||||
* Remove some flow.
|
||||
* @param flow Flow to be removed.
|
||||
*/
|
||||
void RemoveFlow(uint flow)
|
||||
{
|
||||
assert(flow <= this->anno.flow);
|
||||
this->anno.flow -= flow;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Iterator for job edges.
|
||||
*/
|
||||
class EdgeIterator : public LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator> {
|
||||
span<EdgeAnnotation> base_anno; ///< Array of annotations to be (indirectly) iterated.
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param base Array of edges to be iterated.
|
||||
* @param base_anno Array of annotations to be iterated.
|
||||
* @param current Start offset of iteration.
|
||||
*/
|
||||
EdgeIterator(span<const LinkGraph::BaseEdge> base, span<EdgeAnnotation> base_anno, bool end) :
|
||||
LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator>(base, end),
|
||||
base_anno(base_anno) {}
|
||||
|
||||
EdgeIterator() :
|
||||
LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator>(span<const LinkGraph::BaseEdge>(), true),
|
||||
base_anno() {}
|
||||
|
||||
/**
|
||||
* Dereference.
|
||||
* @return Pair of the edge currently pointed to and the ID of its
|
||||
* other end.
|
||||
*/
|
||||
std::pair<NodeID, Edge> operator*() const
|
||||
{
|
||||
return std::pair<NodeID, Edge>(this->base[this->current].dest_node, Edge(this->base[this->current], this->base_anno[this->current]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereference. Has to be repeated here as operator* is different than
|
||||
* in LinkGraph::EdgeWrapper.
|
||||
* @return Fake pointer to pair of NodeID/Edge.
|
||||
*/
|
||||
FakePointer operator->() const {
|
||||
return FakePointer(this->operator*());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Link graph job node. Wraps a constant link graph node and a modifiable
|
||||
* node annotation.
|
||||
*/
|
||||
class Node : public LinkGraph::ConstNode {
|
||||
private:
|
||||
NodeAnnotation &node_anno; ///< Annotation being wrapped.
|
||||
span<EdgeAnnotation> edge_annos; ///< Edge annotations belonging to this node.
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param lgj Job to take the node from.
|
||||
* @param node ID of the node.
|
||||
*/
|
||||
Node (LinkGraphJob *lgj, NodeID node) :
|
||||
LinkGraph::ConstNode(&lgj->link_graph, node),
|
||||
node_anno(lgj->nodes[node]), edge_annos(lgj->nodes[node].edges)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Retrieve an edge starting at this node. Mind that this returns an
|
||||
* object, not a reference.
|
||||
* @param to Remote end of the edge.
|
||||
* @return Edge between this node and "to".
|
||||
*/
|
||||
Edge operator[](NodeID to) const
|
||||
{
|
||||
assert(this->HasEdgeTo(to));
|
||||
auto index = std::distance(this->node.edges.begin(), this->GetEdge(to));
|
||||
return Edge(this->node.edges[index], this->edge_annos[index]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator for the "begin" of the edge array. Only edges with capacity
|
||||
* are iterated. The others are skipped.
|
||||
* @return Iterator pointing to the first edge.
|
||||
*/
|
||||
EdgeIterator Begin() const { return EdgeIterator(this->node.edges, this->edge_annos, false); }
|
||||
|
||||
/**
|
||||
* Iterator for the "end" of the edge array. Only edges with capacity
|
||||
* are iterated. The others are skipped.
|
||||
* @return Iterator pointing beyond the last edge.
|
||||
*/
|
||||
EdgeIterator End() const { return EdgeIterator(this->node.edges, this->edge_annos, true); }
|
||||
|
||||
/**
|
||||
* Get amount of supply that hasn't been delivered, yet.
|
||||
* @return Undelivered supply.
|
||||
*/
|
||||
uint UndeliveredSupply() const { return this->node_anno.undelivered_supply; }
|
||||
|
||||
/**
|
||||
* Get the flows running through this node.
|
||||
* @return Flows.
|
||||
*/
|
||||
FlowStatMap &Flows() { return this->node_anno.flows; }
|
||||
|
||||
/**
|
||||
* Get a constant version of the flows running through this node.
|
||||
* @return Flows.
|
||||
*/
|
||||
const FlowStatMap &Flows() const { return this->node_anno.flows; }
|
||||
|
||||
/**
|
||||
* Get the paths this node is part of. Paths are always expected to be
|
||||
* sorted so that those with flow == 0 are in the back of the list.
|
||||
* @return Paths.
|
||||
*/
|
||||
PathList &Paths() { return this->node_anno.paths; }
|
||||
|
||||
/**
|
||||
* Get a constant version of the paths this node is part of.
|
||||
* @return Paths.
|
||||
*/
|
||||
const PathList &Paths() const { return this->node_anno.paths; }
|
||||
|
||||
/**
|
||||
* Get the transport demand between end the points of the edge.
|
||||
* @return Demand.
|
||||
*/
|
||||
uint DemandTo(NodeID to) const { return this->node_anno.demands[to].demand; }
|
||||
|
||||
/**
|
||||
* Get the transport demand that hasn't been satisfied by flows, yet.
|
||||
* @return Unsatisfied demand.
|
||||
*/
|
||||
uint UnsatisfiedDemandTo(NodeID to) const { return this->node_anno.demands[to].unsatisfied_demand; }
|
||||
|
||||
/**
|
||||
* Satisfy some demand.
|
||||
* @param demand Demand to be satisfied.
|
||||
*/
|
||||
void SatisfyDemandTo(NodeID to, uint demand)
|
||||
{
|
||||
assert(demand <= this->node_anno.demands[to].unsatisfied_demand);
|
||||
this->node_anno.demands[to].unsatisfied_demand -= demand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deliver some supply, adding demand to the respective edge.
|
||||
* @param to Destination for supply.
|
||||
* @param amount Amount of supply to be delivered.
|
||||
*/
|
||||
void DeliverSupply(NodeID to, uint amount)
|
||||
{
|
||||
this->node_anno.undelivered_supply -= amount;
|
||||
this->node_anno.demands[to].demand += amount;
|
||||
this->node_anno.demands[to].unsatisfied_demand += amount;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Bare constructor, only for save/load. link_graph, join_date and actually
|
||||
* settings have to be brutally const-casted in order to populate them.
|
||||
@@ -337,7 +237,7 @@ public:
|
||||
* @param num ID of the node.
|
||||
* @return the Requested node.
|
||||
*/
|
||||
inline Node operator[](NodeID num) { return Node(this, num); }
|
||||
inline NodeAnnotation &operator[](NodeID num) { return this->nodes[num]; }
|
||||
|
||||
/**
|
||||
* Get the size of the underlying link graph.
|
||||
|
Reference in New Issue
Block a user