Click here to Skip to main content
15,867,935 members
Articles / Programming Languages / C#
Tip/Trick

SetComparers: A variety of comparers for collections of objects in C#

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
23 Nov 2021MIT 8.5K   95   5   8
Easily compare collections for equality
There are the occasional situations, particularly when running complicated algorithms where it can be useful to compare entire collections for equality. This project contains comparers for various types of collections.

Introduction

When implementing a mathematical algorithm in code there are many situations where you need to compare sets or collections for equality. One example is the powerset construction algorithm used to convert NFA state machines into DFA state machines. Yet another example is in building LALR(1) parsing tables.

Using this Mess

Using this is just a matter of dropping SetComparers.cs into your project and then using SetComparers:

C#
using SetComparers;
...
var x = new int[] { 1, 2, 3 };
var y = new int[] { 3, 2, 1 };
Console.WriteLine("OrderedSetComparer<int>.Default.Compare(x, y) = {0}", 
    OrderedSetComparer<int>.Default.Compare(x, y));
Console.WriteLine("OrderedSetComparer<int>.Default.Compare(y, x) = {0}", 
    OrderedSetComparer<int>.Default.Compare(y, x));
Console.WriteLine();

Console.WriteLine("OrderedSetEqualityComparer<int>.Default.Equals(x, y) = {0}", 
    OrderedSetEqualityComparer<int>.Default.Equals(x, y));
Console.WriteLine();

Console.WriteLine("UniqueUnorderedSetEqualityComparer<int>.Default.Equals(x, y) = {0}", 
    UniqueUnorderedSetEqualityComparer<int>.Default.Equals(x, y));

You can also use the equality comparers with Dictionary<> and HashSet<> classes so that you can use collections as keys:

C#
var d = new Dictionary<ICollection<int>, string>(
    OrderedSetEqualityComparer<int>.Default);
d.Add(new int[] { 1, 2, 3 }, "foo");
d.Add(new int[] { 3, 2, 1 }, "bar");
Console.WriteLine("d[new int[] { 1, 2, 3 }] = " 
    + d[new int[] { 1, 2, 3 }].ToString());
Console.WriteLine("d[new int[] { 3, 2, 1 }] = " 
    + d[new int[] { 3, 2, 1 }].ToString());

Performance

This is not nearly as performant as it could be if you were using dedicated sets that were intended for this, although it will use the optimized set comparisons on ISet<> instances. If you want better throughput you should use something like my optimized comparable collections.

History

  • 23rd November, 2021 - Initial submission

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
Suggestionuse HashSet<> Pin
sx200825-Nov-21 6:38
sx200825-Nov-21 6:38 
GeneralRe: use HashSet<> Pin
honey the codewitch25-Nov-21 8:51
mvahoney the codewitch25-Nov-21 8:51 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA24-Nov-21 4:43
professionalȘtefan-Mihai MOGA24-Nov-21 4:43 
QuestionWhat do you mean by "equality" ? Pin
PIEBALDconsult23-Nov-21 4:54
mvePIEBALDconsult23-Nov-21 4:54 
AnswerRe: What do you mean by "equality" ? Pin
honey the codewitch23-Nov-21 4:58
mvahoney the codewitch23-Nov-21 4:58 
GeneralRe: What do you mean by "equality" ? Pin
PIEBALDconsult23-Nov-21 5:10
mvePIEBALDconsult23-Nov-21 5:10 
GeneralRe: What do you mean by "equality" ? Pin
honey the codewitch23-Nov-21 5:17
mvahoney the codewitch23-Nov-21 5:17 
AnswerRe: What do you mean by "equality" ? Pin
honey the codewitch23-Nov-21 5:00
mvahoney the codewitch23-Nov-21 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.