/// <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); } }
/// <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); } }