Answer the question
In order to leave comments, you need to log in
How to organize the optimal storage of errors in a hierarchical structure?
Hello everyone, I'm faced with the following problem, maybe someone has already solved it.
There is a hierarchical structure, each level has a list of "Markers". The marker can have 3 states (Error=0, Warning=1, Normal=2 - descending priority).
If there is a marker with an error at the lower level, then automatically higher levels become with an error. Markers can be "added" "removed" at any level.
It is necessary, taking any level of the hierarchy, to determine the state of the level.
PS
You can solve it head-on, each time calculating the state, but this will not work.
I also came up with a solution with storing data in a HashMap. The key is a hierarchy object, and the value is a list of ordered pairs of all children of the level below with their state, perhaps there is something more optimal.
Answer the question
In order to leave comments, you need to log in
How often do you have to calculate states and how many objects?
It is necessary to look at specific examples, but any optimization due to additional. dictionaries will require more memory. In my opinion, if the calculation of the state is a long operation, then you can do this, it will be more reliable to combine a subscription to the state change event of the child elements and the calculation of the general state on each change:
We subscribe the node to the "Change of State" event for child nodes. Each node propagates an event to its parent, up to the root. In each node, we store the attribute "isActualState", = "true" if the child nodes did NOT notify about the state change, = "false" if at least one of the child nodes did notify. After the event is generated, the state is recalculated with the state recalculated only in the branch where the changes actually took place (isActualState=false), and the current stored state will be used from all the others.
Something like this: pastebin.com/K6Z8JqVT
That is, the idea is to subscribe the node to the "marker" state change event when it is added to the node. In this case, the node propagates events to its parent (if any) up to the root. Each node keeps a compact set of state counters associated with its "markers" and the "markers" of its child nodes. To optimize something even more, in my opinion, it makes no sense, because. TreeMap (O(nlog(n)) keyed by state is already very fast.
The code is not thread-safe, of course.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question