Пример #1
0
        float FindIntervalSize(List <float> S)
        {
            float intervalsize = (S.Max() - S.Min()) / S.Count;
            var   T            = new List <float>(S);

            while (T.Count > 0)
            {
                Buckets buckets = new Buckets(intervalsize);
                while (T.Count > 0 && !buckets.HasBucketLargerThan(Math.Sqrt(S.Count)))
                {
                    float r = T[T.Count - 1];
                    T.RemoveAt(T.Count - 1);
                    int bucket = buckets.Bucket(r);
                    buckets.Add(bucket, r);
                }

                intervalsize = buckets.BucketsWithMoreThanOneItem()
                               .Select(items => FindIntervalSize(items))
                               .Union(new[] { intervalsize })
                               .Min();
            }

            Buckets morebuckets = new Buckets(intervalsize);

            S.ForEach(r => morebuckets.Add(morebuckets.Bucket(r), r));
            intervalsize = morebuckets.BucketsWithMoreThanOneItem()
                           .Select(items => FindIntervalSize(items))
                           .Union(new[] { intervalsize })
                           .Min();

            return(intervalsize);
        }
Пример #2
0
        public PointF ClosestFloats(List <float> floats)
        {
            float intervalsize = FindIntervalSize(floats);
            var   buckets      = new Buckets(2.0f * intervalsize);

            floats.ForEach(r => buckets.Add(buckets.Bucket(r), r));
            var ordered = buckets.GetBuckets();
            var pair    = ordered.SelectMany(i => buckets.CandidatesInBucket(i))
                          .OrderBy(p => Math.Abs(p.first - p.second))
                          .First();

            return(new PointF(pair.first, pair.second));
        }
        float FindIntervalSize(List<float> S)
        {
            float intervalsize = (S.Max() - S.Min()) / S.Count;
            var T = new List<float>(S);
            while (T.Count > 0)
            {
                Buckets buckets = new Buckets(intervalsize);
                while (T.Count > 0 && !buckets.HasBucketLargerThan(Math.Sqrt(S.Count)))
                {
                    float r = T[T.Count - 1];
                    T.RemoveAt(T.Count - 1);
                    int bucket = buckets.Bucket(r);
                    buckets.Add(bucket, r);
                }

                intervalsize = buckets.BucketsWithMoreThanOneItem()
                    .Select(items => FindIntervalSize(items))
                    .Union(new[] { intervalsize })
                    .Min();
            }

            Buckets morebuckets = new Buckets(intervalsize);
            S.ForEach(r => morebuckets.Add(morebuckets.Bucket(r), r));
            intervalsize = morebuckets.BucketsWithMoreThanOneItem()
                .Select(items => FindIntervalSize(items))
                .Union(new[] { intervalsize })
                .Min();

            return intervalsize;
        }
        public PointF ClosestFloats(List<float> floats)
        {
            float intervalsize = FindIntervalSize(floats);
            var buckets = new Buckets(2.0f * intervalsize);
            floats.ForEach(r => buckets.Add(buckets.Bucket(r), r));
            var ordered = buckets.GetBuckets();
            var pair = ordered.SelectMany(i => buckets.CandidatesInBucket(i))
                .OrderBy(p => Math.Abs(p.first - p.second))
                .First();

            return new PointF(pair.first, pair.second);
        }