예제 #1
0
파일: RelatedItems.cs 프로젝트: FizzyP/guru
        public ObjectForest <Item> getItemsAsForest(HashSet <Item> itemSet)
        {
            var forest = new ObjectForest <Item>();

            var rootNodes     = new HashSet <ObjectForestNode <Item> >();
            var itemToNodeMap = new Dictionary <Item, ObjectForestNode <Item> >();

            foreach (var i in itemSet)
            {
                var node = new ObjectForestNode <Item>();
                node.Value = i;
                rootNodes.Add(node);
                itemToNodeMap[i] = node;
            }

            foreach (var kv in dependencyMap)
            {
                var parent = kv.Key;
                foreach (var item in kv.Value)
                {
                    if (!itemSet.Contains(item))
                    {
                        continue;
                    }

                    var childNode = itemToNodeMap[item];
                    itemToNodeMap[parent].Children.Add(childNode);
                    //  Remember the child isn't a root
                    rootNodes.Remove(childNode);
                }
            }

            //  Write the root nodes
            foreach (var root in rootNodes)
            {
                forest.addRoot(root);
            }

            return(forest);
        }
예제 #2
0
 public void addRoot(ObjectForestNode <T> node)
 {
     Roots.Add(node);
 }