2014-09-07 3 views
-2

У меня есть 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.

+4

Есть ли причина, вы исключили 'list2.Except (песни1) .ToList()'? –

+2

'if (latCompare == 0) return 0;' Это скорее всего противоположность того, что вы намеревались сделать. – Rotem

+0

Что является большим в этом случае? (В большой для 'HashSet' * довольно * большой на современных машинах) – Carsten

ответ

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; 
     } 
Смежные вопросы