I’m trying to build a sequence that determines the order to destroy objects. We can assume there are no cycles. If an object A uses an object B during its (A’s) construction, then object B should still be available during object A’s destruction. Thus the desired order of destruction is A, B. If another object C uses object B during its (C’s) construction as well, then the desired order is A, C, B. In general, as long as an object X is only destroyed after all other objects that used that object during their construction, the destruction is safe.
If our destruction order so far is AECDBF, and we now are given an X (we never know before hand what order the construction will initially happen in, it’s discovered on the fly), that uses C and F during its construction, then we can get a new safe order by putting X before whichever is currently earlier in the list, C or F (happens to be C). So the new order would be ABXCDEF.
In the context of the X example, a linked list seems unsuitable because a lot of linear scanning would be involved to determine which is earlier, C or F. An array will mean slow insertions which is going to be one of the more common operations. A priority queue doesn’t really have a suitable interface, there’s no, “Insert this item before whichever one of these items is earliest” (we don’t know the right priority before hand to make sure it’s inserted before the lower priority element and without disturbing other entries).
All objects are constructed, desired order is computed, and the sequence will be iterated once and destructed in order. No other operations need to be done (in fact, after using whatever data structure to determine the order, it could be copied into a flat array and discarded).
Edit: Just to clarify, the first time an object is used is when it is constructed. So if A uses B, then E uses B, when E tries to use B it has already been created. This means a stack won’t give the desired order. AB will become ABE when we want AEB.
Edit2: I’m trying to build the order ‘as I go’ to keep the algorithm in place. I would prefer to avoid building up a large intermediate structure and then converting that to a final structure.
Edit3: I made this too complicated ;p
Since dependencies are always initialised before the objects that depend on them, and remain available until after such objects are destroyed, it should always be safe to destroy objects in strictly reverse order of initialisation. So all you need is a linked list to which you prepend objects as they are initialised and walk on destruction, and for each object to request initialisation of all its dependencies that have not yet been initialised before it initialises itself.
So for initialisation of each object:
and for destruction, just walk the linked list from the front forwards (or pop items off stack until empty), destroying as you go. The example in your first paragraph initialised in order B, A, C would thus be destroyed in order C, A, B – which is safe; the example in your edit would be initialised in order B, A, E (not A, B, E since A depends on B), and thus destroyed in order E, A, B, which is also safe.