private void CleanRedundant(GetCollection getCollection, CleanInternal cleanInternal) { Action cleanupActions = null; foreach (SortedTreeNode <IdType, DataType> node in NodesToSort.Values) { IList <IdType> collection = getCollection.Invoke(node); if (collection.Count < 2) { continue; } foreach (IdType d1 in collection) { if (!NodesToSort.TryGetValue(d1, out SortedTreeNode <IdType, DataType> nodeD1)) { continue; } foreach (IdType d2 in collection) { if (d1.Equals(d2)) { continue; } if (!NodesToSort.TryGetValue(d2, out SortedTreeNode <IdType, DataType> nodeD2)) { continue; } if (HasSortPreferenceWith(nodeD1, getCollection, nodeD2)) { // d2 can be removed from entries that already have d1 IdType d3 = d2; cleanupActions += () => CleanupRedundant(d1, getCollection, d3, cleanInternal); } else if (HasSortPreferenceWith(nodeD2, getCollection, nodeD1)) { // d1 can be removed from entries that already have d2 cleanupActions += () => CleanupRedundant(d2, getCollection, d1, cleanInternal); } } } } cleanupActions?.Invoke(); }
private void CleanupRedundant(IdType ifThisIsPresent, GetCollection inThisCollection, IdType thenRemoveThis, CleanInternal andPassItAlong) { foreach (SortedTreeNode <IdType, DataType> cleanupNode in NodesToSort.Values) { IList <IdType> collectionToClean = inThisCollection(cleanupNode); if (collectionToClean.Contains(ifThisIsPresent) && collectionToClean.Remove(thenRemoveThis)) { andPassItAlong.Invoke(thenRemoveThis); } } }