/// <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); }
/// <summary> /// Remove a point from its class. /// </summary> /// <param name="p">Point to remove.</param> /// <returns>True if the point was removed, false if it was not previously a member of any class.</returns> public bool Remove(TPoint p) { TLabel label; if (!PointToLabel.TryGetValue(p, out label)) { return(false); } PointToLabel.Remove(p); var oldPoints = LabelToPoints[label]; var didRemove = oldPoints.Remove(p); if (oldPoints.Count == 0) { LabelToPoints.Remove(label); } return(didRemove); }