为什么算法不起作用?

时间:2014-09-07 10:06:22

标签: c# algorithm

我有2个系列,我想获得第二个系列的元素,这些元素在第一个系列中不存在。由于两个集合的大小,我创建了一个特殊的算法(下面是示例项目的链接):

    /// <summary>
    /// Object-shell of point with coordinates
    /// </summary>
    class Point
    {
        public Point(string coordinates)
        {
            var parts = coordinates.Split(' ');
            Latitude = double.Parse(parts[0]);
            Longitude = double.Parse(parts[1]);
        }

        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    class PointComparer : IComparer<Point>
    {
        private const double Eps = 1e-4;

        /// <summary>
        /// Before compare latitude, than longitude
        /// </summary>
        public int Compare(Point first, Point second)
        {
            var latCompare = Compare(first.Latitude, second.Latitude);

            if (latCompare == 0)
                return 0;

            return Compare(first.Longitude, second.Longitude);
        }

        /// <summary>
        /// Compare two double numbers with specified occuracy
        /// </summary>
        private int Compare(double first, double second)
        {
            var temp = first - second;

            if (Math.Abs(temp) < Eps)
                return 0;

            if (temp < 0)
                return -1;

            return 1;
        }
    }

    /// <summary>
    /// Find objects of 2 collection, which don't exist in 1 collection
    /// For example f({1,2,3}, {1,2,4}) = {4}
    /// </summary>
    private static IEnumerable<T> GetNewT<T>(IEnumerable<T> first, IEnumerable<T> second, IComparer<T> comparer)
    {
        //Sort both collections
        var sortedFirst = first.OrderBy(i => i, comparer).ToList();
        var sortedSecond = second.OrderBy(i => i, comparer).ToList();

        //Because of 2 collection was sorted after we find item of second collection in 'i' place of first collection,
        //we can to find next item of second collection after 'i' place of first collection.
        int startSearchIndex = 0;

        //Try to find object of seconed collection in first collectio via Binary Search
        foreach (var item in sortedSecond)
        {
            var countToSearch = sortedFirst.Count - startSearchIndex;
            var findResultIndex = sortedFirst.BinarySearch(startSearchIndex, countToSearch, item, comparer);

            if (findResultIndex >= 0) //Find it!
            {
                startSearchIndex = findResultIndex;
            }
            else
            {
                yield return item;
            }
        }
    }

所以它不起作用,但我找不到任何错误。 There is a link to example project with example data to see problem.

1 个答案:

答案 0 :(得分:1)

Rotem在此代码中发现错误: 必须有:

        /// <summary>
        /// Before compare latitude, than longitude
        /// </summary>
        public int Compare(Point first, Point second)
        {
            var latCompare = Compare(first.Latitude, second.Latitude);

            if (latCompare == 0)
                return Compare(first.Longitude, second.Longitude);

            return latCompare;
        }