Beispiel #1
0
        private static ImmutableSortedDictionary <int, List <TTreeNode> > GetNodeLevels <TValue, TTree, TTreeNode>(ITree <TValue, TTree, TTreeNode> tree)
            where TTree : ITree <TValue, TTree, TTreeNode>
            where TTreeNode : ITreeNode <TValue, TTree, TTreeNode>
        {
            var result = new FlexibleListDictionary <int, TTreeNode>();

            Iterate(tree.Root, 0);

            return(result.ToImmutableSortedDictionary());

            void Iterate(TTreeNode node, int depth)
            {
                result[depth].Add(node);

                depth++;
                foreach (var child in node.Children)
                {
                    Iterate(child, depth);
                }
            }
        }
Beispiel #2
0
            public int FindUnbalancedWeight()
            {
                var totalWeights = new FlexibleListDictionary <int, ProgramTreeNode>();

                foreach (var current in programTree.TraverseBottomLevelOrderNodes())
                {
                    foreach (var c in current.Children)
                    {
                        totalWeights[c.TotalWeight].Add(c);
                    }

                    if (totalWeights.Count > 1)
                    {
                        var keys = totalWeights.Keys;

                        int             correctWeight          = 0;
                        int             wrongWeight            = 0;
                        ProgramTreeNode wronglyWeightedProgram = null;

                        foreach (int weight in keys)
                        {
                            if (totalWeights[weight].Count == 1)
                            {
                                wronglyWeightedProgram = totalWeights[weight].First();
                                wrongWeight            = weight;
                            }
                            else
                            {
                                correctWeight = weight;
                            }
                        }

                        return(wronglyWeightedProgram.Value.Weight + (correctWeight - wrongWeight));
                    }

                    totalWeights.Clear();
                }

                return(-1);
            }
Beispiel #3
0
            public int GetCountAfterRemovingCollsions()
            {
                var remaining        = new HashSet <MovableParticle>(particles.Select(MovableParticle.FromParticle));
                int previousCount    = remaining.Count;
                int ignoredParticles = 0;

                var locations          = new FlexibleDictionary <Location3D, MovableParticle>();
                int nonCollidingRounds = 0;

                while (remaining.Any())
                {
                    nonCollidingRounds++;

                    // ToArray is sadly necessary
                    foreach (var particle in remaining.ToArray())
                    {
                        particle.Iterate();
                        var position = particle.Position;

                        // Identify collision and remove the colliding particles
                        if (!locations.TryAdd(position, particle))
                        {
                            remaining.Remove(locations[position]);
                            remaining.Remove(particle);
                            nonCollidingRounds = 0;
                        }
                    }

                    locations.Clear();

                    const int collisionRoundThreshold = 12;

                    // No collisions in the past rounds; must evaluate the relationships between the particles
                    // to ignore the ones that will never collide
                    if (nonCollidingRounds > collisionRoundThreshold)
                    {
                        var approachMap = new FlexibleListDictionary <MovableParticle, MovableParticle>();

                        var remainingArray = remaining.ToArray();
                        for (int i = 0; i < remainingArray.Length; i++)
                        {
                            var a = remainingArray[i];

                            for (int j = i + 1; j < remainingArray.Length; j++)
                            {
                                var b = remainingArray[j];

                                if (!a.Approaches(b))
                                {
                                    continue;
                                }

                                approachMap[a].Add(b);
                                approachMap[b].Add(a);
                            }
                        }

                        // Eliminate disconnected particles
                        foreach (var particle in remainingArray)
                        {
                            if (approachMap.ContainsKey(particle))
                            {
                                continue;
                            }

                            remaining.Remove(particle);
                            ignoredParticles++;
                        }

                        // Reset the counter since the particles were reduced
                        nonCollidingRounds = 0;
                    }
                }

                return(ignoredParticles);
            }