Dictionary<int [], bool> - compare values in the array, not reference? - c#

I am using dictionary for storing ID,otherID and bool value. Unfortunately it compares array reference, therefore I cannot use it.
Is there any way how to have an array as key but compare its values instead of reference?
Thanks

You can use the Comparer property of the dictionary to set it to a custom comparer created by you.
EDIT: actually the property is read-only, sorry. You should definitely use the proper constructor:
class IntArrayComparer : IEqualityComparer<int[]> {
public bool Equals(int[] x, int[] y) {
if (x.Length != y.Length) {
return false;
}
for (int i = 0; i < x.Length; ++i) {
if (x[i] != y[i]) {
return false;
}
}
return true;
}
public int GetHashCode(int[] obj) {
int ret = 0;
for (int i = 0; i < obj.Length; ++i) {
ret ^= obj[i].GetHashCode();
}
return ret;
}
}
static void Main(string[] args) {
Dictionary<int[], bool> dict = new Dictionary<int[], bool>(new IntArrayComparer());
}

You can try implementing IEqualityComparer<int[]> and then pass an instance of it to the proper constructor.

There are basically two ways of doing that:
Create a comparer that implements IEqualityComparable<int[]>, that you pass to the constructor of the dictionary.
Create a key class that encapsulates the integer array and implements IEquatable<T>.

There's nothing wrong with orsogufo's answer, but I wanted to point out that if you have .NET 3.5, you can implement an ArrayValueComparer with a lot less code, and at the same time make it generic, so it can compare the values of any type of array, and not just integer arrays. For that matter, you could easily make it work with any IEnumerable, and not just arrays.
using System.Collections.Generic;
using System.Linq;
class ArrayValueComparer<T> : IEqualityComparer<T[]>
{
public bool Equals(T[] x, T[] y)
{
return x.SequenceEqual(y, EqualityComparer<T>.Default);
}
public int GetHashCode(T[] obj)
{
return obj.Aggregate(0, (total, next) => total ^ next.GetHashCode());
}
}
static void Main(string[] args)
{
var dict = new Dictionary<int[], bool>(new ArrayValueComparer<int>());
}

Related

Primitives and IComparable interface

In Java if I wanna sort any data independent to its data type I would use Comparable interface and I found out we have similar interface in C#, so I have simple sorting algorithm:
public static void Sort(IComparable[] arr)
{
bool swap = true;
while (swap)
{
swap = false;
for (int i = 0; i < arr.Length - 1; i++)
{
if (Less(arr[i + 1], arr[i]))
{
Exchange(arr, i, i + 1);
swap = true;
}
}
}
}
So I can sort any objects that implements IComparable interface, but I can't sort any primitive datatypes and as I know C# doesn't have wrapper types for primitives like int -> Integer, but it has some structures like Int32 that actually implements IComparable interface but I still can't use it for some reason, for example:
static void Main(string[] args)
{
Int32[] ints = { 5, 2, 9, 0, 3};
BubbleSort.Sort(ints);
ReadKey();
}
I will get an error:
Error CS1503 Argument 1: cannot convert from 'int[]' to 'System.IComparable[]'
But if we check out metadata of Int32 we can see it implements IComparable
So,
What I don't know?
What's wrong here?
How can I make it better to sort any data?
An int[] isn't an IComparable[] - the latter is an array of references.
But there's a simple solution, which will also avoid boxing. Change your method declaration to be generic with a constraint on the type parameter:
public static void Sort<T>(IComparable<T>[] arr) where T : IComparable<T>
You may need to change your Exchange and Less methods as well - you haven't told us what that looks like. But they could (and probably should) be generic too. The method body of Sort shouldn't need to change, at that point.
If someone was interested:
public static class BubbleSort<T>
{
public static void Sort(T[] arr, Comparer<T> comparer = null)
{
Comparer<T> equaltyComparer = comparer ?? Comparer<T>.Default;
bool swap = true;
while (swap)
{
swap = false;
for (int i = 0; i < arr.Length - 1; i++)
{
if (Less(equaltyComparer, arr[i + 1], arr[i]))
{
Exchange(arr, i, i + 1);
swap = true;
}
}
}
}
private static bool Less(Comparer<T> comparer, T v, T w)
{
return comparer.Compare(v, w) < 0;
}
private static void Exchange(T[] arr, int i, int j)
{
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

How to create a sorting index in C#?

I have an array of values and I want to create a sorting index, i.e. an auxiliary array of integers that lists the element in sorted order by indirect addressing.
In other words,
I <= J -> Value[Index[I]] <= Value[Index[J]]
How can I define a comparator for the Sort method to achieve that ? The array of values must remain unchanged.
The easiest way to build such an index I see is to use LINQ:
var Index = Enumerable.Range(0, Value.Length).OrderBy(i => Value[i]).ToArray();
or if you insist on using Array.Sort, then you can use the overloads that accept Comparison<T> delegate:
var Index = new int[Value.Length];
for (int i = 0; i < Index.Length; i++)
Index[i] = i;
Array.Sort(Index, (a, b) => Comparer<Value_Type>.Default.Compare(Value[a], Value[b]));
where the Value_Type is the type of the Value array elements.
Another option (IMO the best) is to create a reusable generic comparer like this:
public static class Comparers
{
public static IComparer<int> CreateIndexComparer<T>(this IReadOnlyList<T> source, IComparer<T> comparer = null)
{
return new ListIndexComparer<T>(source, comparer);
}
private sealed class ListIndexComparer<T> : Comparer<int>
{
readonly IReadOnlyList<T> list;
readonly IComparer<T> comparer;
public ListIndexComparer(IReadOnlyList<T> list, IComparer<T> comparer = null)
{
this.list = list;
this.comparer = comparer ?? Comparer<T>.Default;
}
public override int Compare(int x, int y)
{
return x != y ? comparer.Compare(list[x], list[y]) : 0;
}
}
}
and use it with the Array.Sort overloads that accept IComparer<T>:
Array.Sort(Index, Value.CreateIndexComparer());

C# Enumerable.Take with default value

What is the best way to get exactly x values from an Enumerable in C#.
If i use Enumerable .Take() like this:
var myList = Enumerable.Range(0,10);
var result = myList.Take(20);
The result will only have 10 elements.
I want to fill the missing entries with a default value.
Something like this:
var myList = Enumerable.Range(0,10);
var result = myList.TakeOrDefault(20, default(int)); //Is there anything like this?
Is there such a function in C# and if not, what would be the best way to achieve this?
You could do something like:
var result = myList.Concat(Enumerable.Repeat(default(int), 20)).Take(20);
And it would be easy to turn this into an extension method:
public static IEnumerable<T> TakeOrDefault<T>(this IEnumerable<T> list, int count, T defaultValue)
{
return list.Concat(Enumerable.Repeat(defaultValue, count)).Take(count);
}
But there is a subtle gotcha here. This would work perfectly fine for value types, for a reference type, if your defaultValue isn't null, you are adding the same object multiple times. Which probably isn't want you want. For example, if you had this:
var result = myList.TakeOrDefault(20, new Foo());
You are going to add the same instance of Foo to pad your collection. To solve that problem, you'd need something like this:
public static IEnumerable<T> TakeOrDefault<T>(this IEnumerable<T> list, int count, Func<T> defaultFactory)
{
return list.Concat(Enumerable.Range(0, count).Select(i => defaultFactory())).Take(count);
}
Which you'd call like this:
var result = myList.TakeOrDefault(20, () => new Foo())
Of course, both methods can co-exist, so you could easily have:
// pad a list of ints with zeroes
var intResult = myIntList.TakeOrDefault(20, default(int));
// pad a list of objects with null
var objNullResult = myObjList.TakeOrDefault(20, (object)null);
// pad a list of Foo with new (separate) instances of Foo
var objPadNewResult = myFooList.TakeOrDefault(20, () => new Foo());
Its not there by default, but it's easy enough to write as an extension method
public static IEnumerable<T> TakeOrDefault<T>(this IEnumerable<T> items, int count, T defaultValue)
{
var i = 0;
foreach(var item in items)
{
i++;
yield return item;
if(i == count)
yield break;
}
while(i++<count)
{
yield return defaultValue;
}
}
Live example: http://rextester.com/XANF91263
What you're looking for is a general-purpose PadTo method, which extends the collection's length if needed using a given value.
public static IEnumerable<T> PadTo<T>(this IEnumerable<T> source, int len)
{
return source.PadTo(len, default(T));
}
public static IEnumerable<T> PadTo<T>(this IEnumerable<T> source, int len, T elem)
{
return source.PadTo(len, () => elem);
}
public static IEnumerable<T> PadTo<T>(this IEnumerable<T> source, int len, Func<T> elem)
{
int i = 0;
foreach(var t in source)
{
i++;
yield return t;
}
while(i++ < len)
yield return elem();
}
You can now express:
myList.Take(20).PadTo(20);
This is analogous to Scala's List[A].padTo
You could use Concat for this purpose. You can use a simple helper method to join this all together:
public IEnumerable<T> TakeSpawn(this IEnumerable<T> #this, int take, T defaultElement)
{
return #this.Concat(Enumerable.Repeat(defaultElement, take)).Take(take);
}
The idea is that you always append another enumerable on the end of the original enumerable, so if the input doesn't have enough elements, it will start enumerating from the Repeat.
There isn't anything in the .NET Framework, not that I'm aware of. This can be achieved easily using an extension method though and it works for all types if you supply a default value yourself:
public static class ListExtensions
{
public static IEnumerable<T> TakeOrDefault<T>(this List<T> list, int count, T defaultValue)
{
int missingItems = count - list.Count;
List<T> extra = new List<T>(missingItems);
for (int i = 0; i < missingItems; i++)
extra.Add(defaultValue);
return list.Take(count).Concat(extra);
}
}
I wrote a quick extension for this which depends on T being a value type.
public static class Extensions
{
public static IEnumerable<T> TakeOrDefault<T>(this IEnumerable<T> list, int totalElements)
{
List<T> finalList = list.ToList();
if (list.Count() < totalElements)
{
for (int i = list.Count(); i < totalElements; i++)
{
finalList.Add(Activator.CreateInstance<T>());
}
}
return finalList;
}
}
Why not just write an extension method that checks the count and returns the default value for remaining entries:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
List<int> values = new List<int>{1, 2, 3, 4};
IEnumerable<int> moreValues = values.TakeOrDefault(3, 100);
Console.WriteLine(moreValues.Count());
moreValues = values.TakeOrDefault(4, 100);
Console.WriteLine(moreValues.Count());
moreValues = values.TakeOrDefault(10, 100);
Console.WriteLine(moreValues.Count());
}
}
public static class ExtensionMethods
{
public static IEnumerable<T> TakeOrDefault<T>(this IEnumerable<T> enumerable, int count, T defaultValue)
{
int returnedCount = 0;
foreach (T variable in enumerable)
{
returnedCount++;
yield return variable;
if (returnedCount == count)
{
yield break;
}
}
if (returnedCount < count)
{
for (int i = returnedCount; i < count; i++)
{
yield return defaultValue;
}
}
}
}
}

Class<type> in C#

I have a class and want to work with it as Lists: e.g. List<int>, List<string>, ... , List<T>
I have a class Randomizor which will take the collection data type that will be shuffled. How can I do so?
class Randomizor<T>
{
public Randomizor()
{
}
public Array Shuffle(Array toShuffle)
{
}
}
Create a generic class like so:
class Randomizer<TList, TType> where TList : IList<TType>
{
public TList Randomize(TList list)
{
// ...
}
}
Or like so:
class Randomizer<T>
{
public IList<T> Randomize(IList<T> list)
{
// ...
}
}
Not very clear question... do you mean something like this?
public static class Randomizer<T>
{
public static T GetRandom(List<T> list)
{
T value = default(T);
// Perform some random logic.
return value;
}
}
EDIT: I found two superior impementations after a little digging so I would suggest those in preference.
An extension method for this purpose and already been suggested previously here
I include the code paraphrased to Shuffle below.
public static IEnumerable<T> Shuffle<T> (this IEnumerable<T> source)
{
Random random = new Random ();
T [] copy = source.ToArray ();
for (int i = copy.Length - 1; i >= 0; i--)
{
int index = random.Next (i + 1);
yield return copy [index];
copy [index] = copy [i];
}
}
And an interesting solution adapted from this linq approach
public static IEnumerable<T> Shuffle<T> (this IEnumerable<T> source)
{
Random random = new Random ();
return source.OrderBy(i => Random.Next()).AsEnumerable();
}
The orignal answer but slower than the edits
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> sequence)
{
Random random = new Random();
List<T> copy = sequence.ToList();
while (copy.Count > 0)
{
int index = random.Next(copy.Count);
yield return copy[index];
copy.RemoveAt(index);
}
}
If you like one of these you should up vote the linked answer.
If you are very concerned about randomness, you could upgrade to one of the RNG algorithms from the Crypto API and seed it with some non deterministic value, like somthing generated from recent mouse activity. I suspect that would be overkill and it would degrade performance.
class Randomizor<T>
{
public Randomizor()
{
}
public List<T> Shuffle(List<T> toShuffle)
{
}
}
class Randomizer<T>
{
public Randomizer(ICollection<T> collection)
{
//Do something with collection using T as the type of the elements
}
}
However you may want to go for a generic extension method
static class Randomizer
{
public static void Randomize<T>(this ICollection<T> collection)
{
//randomize the collection
}
}
and the usage:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
list.Randomize();
Maybe like this:
public List<T> Shuffle<T>(List<T> toShuffle)
{
return toShuffle.OrderBy(x => Guid.NewGuid()).ToList();
}
Or as an extension method
public static class Extensions
{
public static List<T> Shuffle<T>(this List<T> toShuffle)
{
return toShuffle.OrderBy(x => Guid.NewGuid()).ToList();
}
}

How can I compare two generic collections?

How can I compare two generic collections? Here's my attempt with two string arrays, but it doesn't return true.
namespace genericCollections
{
class Program
{
static void Main(string[] args)
{
string[] xx = new string[] { "gfdg", "gfgfd", "fgfgfd" };
string[] yy = new string[] { "gfdg", "gfgfd", "fgfgfd" };
Console.WriteLine(ComparerCollection(xx, yy).ToString());
Console.ReadKey();
}
static bool ComparerCollection<T>(ICollection<T> x, ICollection<T> y)
{
return x.Equals(y);
}
}
}
Call Enumerable.SequenceEqual:
bool arraysAreEqual = xx.SequenceEqual(yy);
From the MSDN documenation:
The default implementation of Equals
supports reference equality only, but
derived classes can override this
method to support value equality.
In your case xx and yy are two different instances of string[] so they are never equal.
You'd have to override the .Equal() method of the string[]
But you can simply solve it by looping through the entire collection
static bool CompareCollection<T>(ICollection<T> x, ICollection<T> y)
{
if (x.Length != y.Length)
return false;
for(int i = 0; i < x.Length,i++)
{
if (x[i] != y[i])
return false;
}
return true;
}
You can get elements that are in xx but not in xy by using LINQ:
var newInXY = xx.Except(xy);

Categories

Resources