Exemple #1
0
        /// <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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
 /// <summary>
 /// Counts the number of partitions whose size equals or exceeds the given number.
 /// </summary>
 /// <param name="minSize">Minimum size of partitions to be counted.</param>
 /// <returns>The number of partitions that are large enough.</returns>
 public int NumLargePartitions(int minSize)
 {
     return(LabelToPoints.Count(p => p.Value.Count >= minSize));
 }