using System.Collections.Concurrent; public class TreeNode { public int ID { get; set; } public int sInx { get; set; } public TreeNode parent { get; set; } public List children = new List(); public TreeNode(int ID){this.ID = ID;} } ConcurrentDictionary lookup = new ConcurrentDictionary(); ConcurrentDictionary rootNodes = new ConcurrentDictionary(); public void simulationStart() { lookup.Clear(); rootNodes.Clear(); tf.SetThreaded(true); tf.SetThreadedIterations(1); } public void simulationStep() { } public void simulationStepThreaded(int startInx, int endInx, int threadInx) { if (tf.GetThreadedIteration() == 0) { for (int i = startInx; i < endInx; i++) { int sInx = tf.GetSimIndex(i); if (tf.GetEventAge(sInx) == 0) { //// convert flat list of Ids/parentIds refs to nested tree structure //// int ID = tf.GetID(sInx); int parentID = tf.GetParentID(sInx); // add node to lookup or get from if already found as a parent TreeNode iNode = new TreeNode(ID); iNode = lookup.GetOrAdd(ID, iNode); // register simulation Index iNode.sInx = sInx; // check for valid parent if (parentID < 0) { // is a root node rootNodes.TryAdd(ID, iNode); } else { // is a child node - add or get parent node to/from lookup TreeNode parentNode = new TreeNode(parentID); parentNode = lookup.GetOrAdd(parentID, parentNode); // add node to parent children list parentNode.children.Add(iNode); // update parent property iNode.parent = parentNode; } } } } if (tf.GetThreadedIteration() == 1) { for (int i = startInx; i < endInx; i++) { int sInx = tf.GetSimIndex(i); if (tf.GetEventAge(sInx) == 0) { TreeNode value; //rootNodes.TryGetValue(tf.GetID(sInx),out value); lookup.TryGetValue(tf.GetID(sInx),out value); Print((value.parent!=null)? value.parent.ID : -1); } } } }