public static IList <TNODE> FromParentChildRelationship <TROW, TKEY, TNODE>( IEnumerable <TROW> rows, GetNodeKey <TROW, TKEY> getchildkey, GetNodeKey <TROW, TKEY> getparentkey, CreateNewNode <TROW, TNODE> createnewnode, AddChild <TNODE> addchild) where TKEY : System.IComparable <TKEY> { var pairs = from row in rows let childkey = getchildkey(row) select new { childkey, row }; // first create the nodes for each distinct key var node_dic = new Dictionary <TKEY, TNODE>(); // assign create child nodes foreach (var pair in pairs) { if (node_dic.ContainsKey(pair.childkey)) { // no need to create child key } else { // need to add the child key var new_node = createnewnode(pair.row); node_dic[pair.childkey] = new_node; } } var items = from row in rows let parentkey = getparentkey(row) let childkey = getchildkey(row) let childnode = node_dic[childkey] select new { parentkey, childkey, childnode }; foreach (var item in items) { if (item.parentkey.CompareTo(item.childkey) == 0) { throw new System.ArgumentException("Parent and Child Keys cannot be the same"); } if (node_dic.ContainsKey(item.parentkey)) { var parentnode = node_dic[item.parentkey]; addchild(parentnode, item.childnode); } } var nodes = node_dic.Values.ToList(); return(nodes); }
void ExportResource() { createNewNode = new CreateNewNode(CreateNew_v3); PrepareTextures = new PrepareAsset(PrepareTextureAsset_v3); PrepareObjects = new PrepareAsset(PrepareObjectAsset_v3); }
public static IList <NODET> FromDepths <T, NODET>(IEnumerable <T> items, GetNodeDepth <T> getdepth, CreateNewNode <T, NODET> new_node, AddChild <NODET> addchild) { var root_nodes = new List <NODET>(); var stack_nodes = new Stack <NODET>(); var stack_depth = new Stack <int>(); foreach (var item in items) { var item_depth = getdepth(item); if (item_depth < 0) { throw new System.ArgumentException("getdepth returned negative value"); } var item_node = new_node(item); // Very important that these two stacks have the same number of items if (stack_depth.Count != stack_nodes.Count) { throw new System.InvalidOperationException("Stacks out of sync"); } // Remove all nodes on the stack that are too deep to be a parent while (stack_nodes.Count > 0) { if (stack_depth.Peek() >= item_depth) { stack_nodes.Pop(); stack_depth.Pop(); } else { break; } } // if there's any node in the stack, it means the top of the stack has a node that can serce as a parent if (stack_nodes.Count > 0) { var cur_node = stack_nodes.Peek(); int cur_depth = stack_depth.Peek(); if (item_depth > cur_depth) { addchild(cur_node, item_node); } } // Add the current node as a potential parent of the next item that will be processed stack_nodes.Push(item_node); stack_depth.Push(item_depth); // if there is only a single node in the stack that means that this // node is is a root node if (stack_nodes.Count == 1) { root_nodes.Add(item_node); } } return(root_nodes); }