开发者

A data structure that can represent multiple branching paths in a sequence

I am trying to implement a model for an input-output system, in where there are several different "calculators" doing different calculations with the inputs and returning one or more outputs (in my code, each calculator is an instance of a class). It's similar to a logic gate system except that here each Calculator may produce two or more outputs (the data is more complex than just numbers).

For this system, there will be at least one Calculator that will take only user input. The system will look for the first Calculator it finds that only receives user input (it will already be flagged as such) and start from there. Then, any outputs that it produces will go to other Calculators as inputs to them to do more calculations.

For any non-starting Calculator, it will not run until all Calculators pointing to it have finished their own run (in order to have all the inputs ready to process).

The number of inputs and outputs for each is arbitrary, but it will be very small (no more than 4 ins and 4 outs each). Such a system can be made so that it branches out to several Calculators but the final Calculator will always produce one output, which is the final result. Each output will go to exactly one Calculator (except for the final one).

Is there a graph-like data structure that can already apply what I described? The graph should represent a sequence like a linked list (since you go through a specific order for calculations), but it's also like a tree in that each node may point to several nodes, and in turn several nodes may point开发者_运维技巧 to it.

I was thinking of giving each Calculator an internal list of "Next" data which refers to the Calculators all outputs will map to. Any other ways to start this design?


The graph-like structure you're proposing is the adjacency list representation of the graph. When I've written code like this, I've found it convenient to have consumers point to producers and then work backward recursively from the output with an access pattern akin to a topological sort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜