/// <summary> /// Merge all the members of the partitions labeled by any of the labels in sourceLabels /// into the partition indicated by targetLabel. /// </summary> /// <param name="targetLabel">Move members into this labeled partition.</param> /// <param name="sourceLabels">Move members out of these labeled partitions.</param> /// <returns>True if at least one point was added to the targetLabel's partition. /// False if the target partition did not increase in size.</returns> public bool Merge(TLabel targetLabel, IEnumerable <TLabel> sourceLabels) { var targetPartition = EnsurePartition(targetLabel); var startingSize = targetPartition.Count; foreach (var sourceLabel in sourceLabels.Where(sLabel => !sLabel.Equals(targetLabel))) { ISet <TPoint> singleSourcePoints; if (!LabelToPoints.TryGetValue(sourceLabel, out singleSourcePoints)) { continue; } // Add to LabelToPoints under new targetLabel targetPartition.UnionWith(singleSourcePoints); // Remove from LabelToPoints under old sourceLabel. LabelToPoints.Remove(sourceLabel); foreach (var p in singleSourcePoints) { PointToLabel[p] = targetLabel; } } return(startingSize < targetPartition.Count); }
private ISet <TPoint> EnsurePartition(TLabel classLabel) { ISet <TPoint> partition; if (LabelToPoints.TryGetValue(classLabel, out partition)) { return(partition); } partition = new HashSet <TPoint>(); LabelToPoints[classLabel] = partition; return(partition); }