c# - How to use SequenceEqual to compare coordinates? -


if nested int[] contains coordinates x, y, how compare them using sequenceequal?

the list group of coordinates. want check every other list see if have same number of coordinates , same coordinate values. if both match, want remove redundant one. otherwise, leave it.

  private list<list<int[]>> combinelist(list<list<int[]>> matches){         debug.log (matches.count());          foreach(list<int[]> tilegroup in matches){             foreach(list<int[]> other in matches){                 if(other == tilegroup) continue;                  if(sequenceequal(tilegroup, other)){                     matches.remove(other);                 }                  }         }          debug.log (matches.count());          return matches;     }      private bool sequenceequal(list<int[]> groupa, list<int[]> groupb){         if(groupa.count() == groupb.count()){             int = 0, j = 0;             dictionary<int, int[]>  dicta = new dictionary<int, int[]>(),                                      dictb = new dictionary<int, int[]>();                 foreach(int[] coordinate in groupa){                 dicta.add (i, coordinate);                 i++;             }              foreach(int[] coordinate in groupb){                 dictb.add (j, coordinate);                   j++;             }              return dicta.values.sequenceequal(dictb.values);         }           return false;     } 

probably quickest way implement iequalitycomparer<int[]>:

class intarrayequalitycomparer : iequalitycomparer<int[]> {     public bool equals(int[] x, int[] y)     {         if (referenceequals(x, y)) return true;         if (referenceequals(null, x)) return false;         if (referenceequals(null, y)) return false;         if (x.length != y.length) return false;         (var = 0; < x.length; i++)         {             if (x[i] != y[i]) return false;         }         return true;     }      public int gethashcode(int[] x)     {         if (x == null) return 0;         var hashcode = 0;         (var = 0; < x.length; i++)         {             hashcode = (32 * hashcode) + x[i];         }         return hashcode;     } } 

and use overloaded version of ienumerable<tsource>.sequenceequal:

private bool sequenceequal(list<int[]> groupa, list<int[]> groupb) {     if (referenceequals(groupa, groupb)) return true;     if (referenceequals(null, groupa)) return false;     if (referenceequals(null, groupb)) return false;     return groupa.sequenceequal(groupb, new intarrayequalitycomparer()); } 

in long run might beneficial create coordinates type implements iequatable<coordinates>, in case comparing 2 list<coordinates> objects.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -