c# - Group objects by equality -


I have a collection of such objects, which I would like to compare to similarity using a method that looks like this Is:

bool areEqual (MyObject O1, MyObject O2);

What would be the most favorable way to group all equal objects? The clear answer collection will compare everything with all other objects, but it will affect performance (N ^ N, I believe). Edit:

Edit:

I probably should name my object MyObject because I can not modify its implementation (and it does not apply to Icompowered It means that I would probably use the ICR solution

You need to compare each object with each other object, each group with each group (Like the first object in the group) and if there is no new match (or if it is the first item).

Something like this might appear:

  public static IEnumerable & lt; IEnumerable & lt; T & gt; & Gt; Group & lt; T & gt; (IEnumerable & lt; T & gt; Items) where T: IEquatable & lt; T & gt; {IList & lt; IList & lt; T & gt; & Gt; Group = new list & lt; IList & lt; T & gt; & Gt; (); Foreign objects (TT in items) {Bull found group = false; Foreign (LILIT & lt; T & gt; Group in Groups) {Debug.Assert (group.Count ()> = 1); If (group [0] .Equals (t)) {group.Add (t); FoundGroup = true; break; }} If (! Found group) {IList & lt; T & gt; NewGroup = New List & lt; T & gt; () {T}; Groups.Add (newGroup); }} Foreach (IList & lt; T & gt; Groups in groups) {yield returns group; }}  

This is, of course, already done for you in Linq, which people have told about how to use them. I just wanted to show that the algorithm could be a bit better than comparing every item of each item.

The N.B algorithm relies on the assumption that the equality relationship is permeable - that is, if a is equal to b, and B is equal to c, then one is equal to c. Although I'm pretty sure how you will group non-infected objects.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -