Пример #1
0
        /// <summary>
        /// Returns the first pair of sequences that are in conflict with each other.
        /// </summary>
        /// <returns>The <see cref="List{T}" />.</returns>
        public IList <IEnumerable <T> > GetConflicts()
        {
            var conflictLists = new List <IEnumerable <T> >();

            if (UsesTracking)
            {
                if (firstConflict != null)
                {
                    conflictLists.Add(Sequences.First(s => !CanSort(s)));
                    conflictLists.Add(firstConflict);
                }
                return(conflictLists);
            }
            else
            {
                var newSort = new TopologicalSort <T>();

                var sequences = (Sequences as IList <IEnumerable <T> >);

                for (var i = 0; i < sequences.Count; i++)
                {
                    newSort.Add(sequences[i]);

                    if (newSort.Sort() != null)
                    {
                        continue;
                    }

                    newSort = new TopologicalSort <T>();
                    newSort.Add(sequences[i]);

                    for (var j = 0; j < i; j++)
                    {
                        newSort.Add(sequences[j]);

                        if (newSort.Sort() != null)
                        {
                            continue;
                        }

                        conflictLists.Add(sequences[j].ToList());
                        conflictLists.Add(sequences[i].ToList());

                        break;
                    }

                    break;
                }

                return(conflictLists);
            }
        }
Пример #2
0
        /// <summary>
        /// The can sort.
        /// </summary>
        /// <param name="sequence">
        /// The sub list.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool CanSort(IEnumerable <T> sequence)
        {
            if (sequence == null)
            {
                return(false);
            }

            if (UsesTracking)
            {
                var tempInto = new HashSet <int>();
                var tempSeen = new HashSet <int>();
                var tempDict = new Dictionary <T, int>(nodesDict);

                foreach (var node in sequence.Reverse())
                {
                    var key = NodeKeySafe(node, tempDict);

                    if (tempSeen.Contains(key))
                    {
                        return(false);
                    }
                    else
                    {
                        tempSeen.Add(key);
                    }

                    if (key < transInto.Count)
                    {
                        tempInto.UnionWith(transInto[key]);
                        tempInto.Remove(key);
                    }
                }

                tempInto.IntersectWith(tempSeen);

                return(!tempInto.Any());
            }
            else
            {
                var newSort = new TopologicalSort <T>(this, false, false);
                newSort.Add(sequence);

                return(newSort.Sort() != null);
            }
        }
Пример #3
0
        public TopologicalSort(TopologicalSort <T> baseSort, bool?usesPriority = null, bool?usesTracking = null)
        {
            nodesDict = new Dictionary <T, int>(baseSort.nodesDict);
            nodesList = new List <T>(baseSort.nodesList);

            edgesInto = new List <HashSet <int> >(baseSort.edgesInto.Select(hash => new HashSet <int>(hash)));
            edgesFrom = new List <HashSet <int> >(baseSort.edgesFrom.Select(hash => new HashSet <int>(hash)));

            UsesPriority = usesPriority ?? baseSort.UsesPriority;
            UsesTracking = usesTracking ?? baseSort.UsesTracking;

            if (UsesTracking)
            {
                transInto = new List <HashSet <int> >(baseSort.transInto.Select(hash => new HashSet <int>(hash)));
                transFrom = new List <HashSet <int> >(baseSort.transFrom.Select(hash => new HashSet <int>(hash)));
            }

            Sequences = baseSort.Sequences.ToList();
        }