I have a simple custom class Point:
public class Point: IEqualityComparer<Point>
{
public double X;
public double Y;
public double Z;
private double[] startPointCoords;
public Point()
{
}
public Point(double[] pointArray)
{
this.startPointCoords = pointArray;
X = pointArray[0];
Y = pointArray[1];
Z = pointArray[2];
}
public bool Equals(Point x, Point y)
{
if(x.X == y.X && x.Y == y.Y && x.Z == y.Z)
{
return true;
}
return false;
}
public int GetHashCode(Point obj)
{
string xString = X.ToString().Replace(".", "");
string yString = Y.ToString().Replace(".", "");
string zString = Z.ToString().Replace(".", "");
int xInt = Convert.ToInt32(xString);
int yInt = Convert.ToInt32(yString);
int zInt = Convert.ToInt32(zString);
return xInt - yInt + zInt;
}
}
I am using this class in a Dictionary. I am checking for if the point instance has been added to the dictionary using:
if (!pointsToProcess.ContainsKey(startPoint))
{
pointsToProcess.Add(startPoint, startPoint);
}
I am debugging my code to make sure Equals is working correctly. My break point I have set in Point.Equals is never hit. I set a break point in Point.GetHashCode and it is never hit either. It seems like they are not being used.
I know that there are classes called Point in .Net. I am absolutely sure that all the Point that I have in my code is from my custom namespace.
Why would my Point.Equals and Point.GetHashCode not be reached when setting a break point?
The Equals(a, b) method is not hit by IEquatable, so you'll need to tailor it to suit the interface.
Try this one:
public class Point : IEquatable<Point>
{
public double X;
public double Y;
public double Z;
private double[] startPointCoords;
public Point()
{
}
public Point(double[] pointArray)
{
this.startPointCoords = pointArray;
X = pointArray[0];
Y = pointArray[1];
Z = pointArray[2];
}
public override bool Equals(object obj) => Equals(obj as Point);
public bool Equals(Point other)
{
if (other is null)
return false;
if (ReferenceEquals(this, other))
return true;
return this.X == other.X &&
this.Y == other.Y &&
this.Z == other.Z;
}
public override int GetHashCode()
{
string xString = X.ToString().Replace(".", "");
string yString = Y.ToString().Replace(".", "");
string zString = Z.ToString().Replace(".", "");
int xInt = Convert.ToInt32(xString);
int yInt = Convert.ToInt32(yString);
int zInt = Convert.ToInt32(zString);
return xInt - yInt + zInt;
}
}
Also there are a lot of ways to implement hashcodes in C# for custom objects. While not perfect, one simple way would be using anonymous object hashing:
public override int GetHashCode()
{
return new { X, Y, Z }.GetHashCode();
}
Related
The following method is from XUnit Assert class:
public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer);
And I am using it as:
IEnumerable<Decimal?> x = getXValues();
IEnumerable<Decimal?> y = getYValues();
Assert.Equal(x, y, new DecimalToleranceEqualityComparer(0.01m));
I am using an IEqualityComparer because is fine to consider 2.526 equal to 2.524.
I get an error because DecimalToleranceEqualityComparer is only for Decimal ...
x and y might have null values. DecimalToleranceEqualityComparer is:
public class DecimalToleranceEqualityComparer : IEqualityComparer<Decimal> {
private readonly Decimal _tolerance;
public DecimalToleranceEqualityComparer(Decimal tolerance) {
_tolerance = tolerance;
}
public Boolean Equals(Decimal x, Decimal y) {
return Math.Abs(x - y) <= _tolerance;
}
public Int32 GetHashCode(Decimal obj) {
return obj.GetHashCode();
}
}
I suppose if 2 values are nulls they should be consider equal ...
How to change the IEqualityComparer so that it handles nulls?
This code works for me. The real trick is in the imlementation of the Equals method. Also keep in mind the null check in the GetHashCode.
static void Main(string[] args)
{
IEnumerable<Decimal?> x = new List<Decimal?> { 1.51m, 3, null };
IEnumerable<Decimal?> y = new List<Decimal?> { 1.6m, 3, null };
Assert.Equal(x, y, new DecimalToleranceEqualityComparer(0.1m));
}
public class DecimalToleranceEqualityComparer : IEqualityComparer<Decimal?>
{
private readonly Decimal _tolerance;
public DecimalToleranceEqualityComparer(Decimal tolerance)
{
_tolerance = tolerance;
}
public Boolean Equals(Decimal? x, Decimal? y)
{
if (!x.HasValue && !y.HasValue)
{
// Both null -> they are equal
return true;
}
else if (!x.HasValue || !y.HasValue)
{
// One is null, other is not null -> not equal
return false;
}
else
{
// both have values -> run the actual comparison
return Math.Abs(x.Value - y.Value) <= _tolerance;
}
}
public Int32 GetHashCode(Decimal? obj)
{
if (obj.HasValue)
{
return obj.GetHashCode();
}
else
{
// Here decide what you need
return string.Empty.GetHashCode();
}
}
}
One option that comes to mind could be implementing new equality comparer for nullable decimal type IEqualityComparer<decimal?> which could use your existing DecimalToleranceEqualityComparer internally. Something like
public Boolean Equals(Decimal? x, Decimal? y) {
return (x.HasValue && y.HasValue)?
_decimalToleranceEqualityComparer.Equals(x.Value,y.Value)
: x == y;
}
You are supplying a list of Nullable<decimal>'s and your IEqualityComparer is expecting a list of Decimal's.
With a rewrite like this you should be fine:
public class DecimalToleranceEqualityComparer : IEqualityComparer<decimal?>
{
private readonly decimal _tolerance;
public DecimalToleranceEqualityComparer(decimal tolerance)
{
_tolerance = tolerance;
}
public bool Equals(decimal? x, decimal? y)
{
if (!x.HasValue && !y.HasValue) return true;
if (!x.HasValue || !y.HasValue) return false;
return Math.Abs(x.Value - y.Value) <= _tolerance;
}
public int GetHashCode(decimal? obj)
{
return obj.GetHashCode();
}
}
with ECPoint class :
public class ECPoint
{
private BigInteger p, x, y;
public BigInteger P
{
get { return p; }
set { p = value; }
}
public BigInteger X
{
get { return x; }
set { x = value; }
}
public BigInteger Y
{
get { return y; }
set { y = value; }
}
public ECPoint(BigInteger p, BigInteger x, BigInteger y)
{
this.p = p;
this.x = x;
this.y = y;
}
}
I have a dictionary in C#:
Dictionary<BigInteger, ECPoint> hashTable = new Dictionary<BigInteger, ECPoint>();
and an object of ECPoint Class :
ECPoint gamma = new ECPoint(p, Qx, Qy);
p,Qx,Qy are numerical values
And I did this test :
if (hashTable.ContainsValue(gamma))
{
BigInteger j = -1;
foreach (KeyValuePair<BigInteger, ECPoint> s in hashTable)
{
if (s.Value.X==gamma.X && s.Value.Y==gamma.Y)
{
j = s.Key;
return m*j;
}
}
}
The problem is this test has never given a true value, it is always false, so how to check if the dictionary hashTable contain the values of an object?. Someone can help me, thanks in advance, and I apologize for my English.
You can use FirstOrDefault() linq function to check and get value from hashTable so you don't have to iterate with foreach.
Your code will be like this:
var data = hashTable.FirstOrDefault(a => a.Value.X == gamma.X && a.Value.Y == gamma.Y);
if(data != null){
return data.Key * m;
}
You could either implement Equal and GetHashCode or turn ECPoint into a structure so the compare no longer uses a reference comparison.
How come if I comment out the second line when overriding Equals() like so:
public override bool Equals(object obj) {
if(object.ReferenceEquals(this, obj)) return true;
//if(obj == null) return false;
Person other = obj as Person;
if(other == null) return false;
return this.Name == other.Name;
}
I get a NullReferenceException? If I uncomment it, it'll work. Also I made sure that the obj argument wasn't a null, and it still does that.
Here is full code
namespace MyNameSpace{
class Person : IComparable<Person>{
public string Name { get; set; }
public Person(string name) {
Name = name;
}
public static bool operator <(Person x, Person y) {
return x.CompareTo(y) < 0;
}
public static bool operator >(Person x, Person y) {
return x.CompareTo(y) > 0;
}
public static bool operator ==(Person x, Person y) {
return x.Equals(y);
}
public static bool operator !=(Person x, Person y) {
return !x.Equals(y);
}
public override bool Equals(object obj) {
if(object.ReferenceEquals(this, obj)) return true;
//if(obj == null) return false;
Person other = obj as Person;
if(other == null) return false;
return this.Name == other.Name;
}
public int CompareTo(Person other) {
return this.Name.CompareTo(other.Name);
}
}
class Program {
static void Main(string[] args) {
Person one = new Person("one");
Person two = new Person("two");
Console.WriteLine(one == two);
}
}
}
I suspect you have a custom == operator on Person, which is being invoked by the line:
if(other == null) return false;
Which also hints that the operator is buggy and should be fixed.
Edit: and with your update: here is the buggy custom == operator:
public static bool operator ==(Person x, Person y) {
return x.Equals(y);
}
Using just:
public static bool operator ==(Person x, Person y) {
return Equals(x,y);
}
would solve that, along with:
public override bool Equals(object obj) {
if(obj == (object)this) return true; // ref equality, the cheap way
if(obj is Person) {
Person other = (Person)obj;
return this.Name == other.Name;
}
return false;
}
As a general rule for IComparable implementation, I would strongly recommend Eric Lippert's approach. It's very simple and helps a lot to not get NRE.
Basically, you don't call either Equals or == in any operator overload. You just call a unique static method that does all the job:
public int CompareTo(Natural x) { return CompareTo(this, x); }
public static bool operator <(Natural x, Natural y) { return CompareTo(x, y) < 0; }
public static bool operator >(Natural x, Natural y) { return CompareTo(x, y) > 0; }
public static bool operator <=(Natural x, Natural y) { return CompareTo(x, y) <= 0; }
public static bool operator >=(Natural x, Natural y) { return CompareTo(x, y) >= 0; }
public static bool operator ==(Natural x, Natural y) { return CompareTo(x, y) == 0; }
public static bool operator !=(Natural x, Natural y) { return CompareTo(x, y) != 0; }
public override bool Equals(object obj) { return CompareTo(this, obj as Natural) == 0; }
public bool Equals(Natural x) { return CompareTo(this, x) == 0; }
// negative means x < y
// positive means x > y
// zero means x == y
// two nulls are equal
// otherwise, null is always smaller
private static int CompareTo(Natural x, Natural y) {
if (ReferenceEquals(x, y))
return 0;
else if (ReferenceEquals(x, null))
return -1;
else if (ReferenceEquals(y, null))
return 1;
else if (ReferenceEquals(x, Zero))
return -1;
else if (ReferenceEquals(y, Zero))
return 1;
else if (x.head == y.head)
return CompareTo(x.tail, y.tail);
else if (x.head == ZeroBit)
return CompareTo(x.tail, y.tail) > 0 ? 1 : -1;
else
return CompareTo(x.tail, y.tail) < 0 ? -1 : 1;
}
I get it, I was recursively calling operator ==, and at some point i had operator == (x, y), where x and y were both null, and then I did x.Equals(), that's why it threw that exception.
I need your help. I am trying to get distinct values from List of objects.
My class looks like this:
class Chromosome
{
public bool[][] body { get; set; }
public double fitness { get; set; }
}
Now I have List<Chromosome> population. And now what I need is a way, how I can get new list: List<Chromosome> newGeneration. This new list will contain only unique chromosomes from original list - population.Chromosome is unique, when his whole body (which in this case is 2D bool array) is unique in comparison to the other chromosomes.
I know, that there is something like MoreLINQ, but I am not sure, whether I should use 3rd party code and I know that I should overwrite some methods, but I am kind of lost. So I would really appreciate some nice step by step description, that even idiot could accomplish :)
THX
First, implement the equality operator (this goes into class Chromosome):
public class Chromosome : IEquatable<Chromosome>
{
public bool[][] body { get; set; }
public double fitness { get; set; }
bool IEquatable<Chromosome>.Equals(Chromosome other)
{
// Compare fitness
if(fitness != other.fitness) return false;
// Make sure we don't get IndexOutOfBounds on one of them
if(body.Length != other.body.Length) return false;
for(var x = 0; x < body.Length; x++)
{
// IndexOutOfBounds on inner arrays
if(body[x].Length != other.body[x].Length) return false;
for(var y = 0; y < body[x].Length; y++)
// Compare bodies
if(body[x][y] != other.body[x][y]) return false;
}
// No difference found
return true;
}
// ReSharper's suggestion for equality members
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != this.GetType())
{
return false;
}
return this.Equals((Chromosome)obj);
}
public override int GetHashCode()
{
unchecked
{
return ((this.body != null ? this.body.GetHashCode() : 0) * 397) ^ this.fitness.GetHashCode();
}
}
}
Then, use Distinct:
var newGeneration = population.Distinct().ToList();
public class ChromosomeBodyComparer : IEqualityComparer<Chromosome>
{
private bool EqualValues(bool[][] left, bool[][] right)
{
if (left.Length != right.Length)
{
return false;
}
return left.Zip(right, (x, y) => x.SequenceEquals(y)).All();
}
public bool Equals(Chromosome left, Chromosome right)
{
return EqualValues(left.body, right.body)
}
//implementing GetHashCode is hard.
// here is a rubbish implementation.
public int GetHashCode(Chromosome c)
{
int numberOfBools = c.body.SelectMany(x => x).Count();
int numberOfTrues = c.body.SelectMany(x => x).Where(b => b).Count();
return (17 * numberOfBools) + (23 * numberOfTrues);
}
}
Called by:
List<Chromosome> nextGeneration = population
.Distinct(new ChromosomeBodyComparer())
.ToList();
I'd like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by "==", so it could (hopefully) be used as a key in dictionary.
The thing is I don't want to implement whole IList interface to my new class, but only add those two operators to existing array class.
I'm trying to do something like this:
class MyArray : Array<int>
But it's not working that way obviously ;).
Sorry if I'm unclear but I'm searching solution for hours now...
UPDATE:
I tried something like this:
class Zmienne : IEquatable<Zmienne>
{
public int[] x;
public Zmienne(int ilosc)
{
x = new int[ilosc];
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return base.Equals((Zmienne)obj);
}
public bool Equals(Zmienne drugie)
{
if (x.Length != drugie.x.Length)
return false;
else
{
for (int i = 0; i < x.Length; i++)
{
if (x[i] != drugie.x[i])
return false;
}
}
return true;
}
public override int GetHashCode()
{
int hash = x[0].GetHashCode();
for (int i = 1; i < x.Length; i++)
hash = hash ^ x[i].GetHashCode();
return hash;
}
}
Then use it like this:
Zmienne tab1 = new Zmienne(2);
Zmienne tab2 = new Zmienne(2);
tab1.x[0] = 1;
tab1.x[1] = 1;
tab2.x[0] = 1;
tab2.x[1] = 1;
if (tab1 == tab2)
Console.WriteLine("Works!");
And no effect. I'm not good with interfaces and overriding methods unfortunately :(. As for reason I'm trying to do it. I have some equations like:
x1 + x2 = 0.45
x1 + x4 = 0.2
x2 + x4 = 0.11
There are a lot more of them, and I need to for example add first equation to second and search all others to find out if there is any that matches the combination of x'es resulting in that adding.
Maybe I'm going in totally wrong direction?
For a single type, it is pretty easy to encapsulate, as below. Note that as a key you want to make it immutable too. If you want to use generics, it gets harder (ask for more info):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
static class Program {
static void Main() {
MyVector x = new MyVector(1, 2, 3), y = new MyVector(1, 2, 3),
z = new MyVector(4,5,6);
Console.WriteLine(x == y); // true
Console.WriteLine(x == z); // false
Console.WriteLine(object.Equals(x, y)); // true
Console.WriteLine(object.Equals(x, z)); // false
var comparer = EqualityComparer<MyVector>.Default;
Console.WriteLine(comparer.GetHashCode(x)); // should match y
Console.WriteLine(comparer.GetHashCode(y)); // should match x
Console.WriteLine(comparer.GetHashCode(z)); // *probably* different
Console.WriteLine(comparer.Equals(x,y)); // true
Console.WriteLine(comparer.Equals(x,z)); // false
MyVector sum = x + z;
Console.WriteLine(sum);
}
}
public sealed class MyVector : IEquatable<MyVector>, IEnumerable<int> {
private readonly int[] data;
public int this[int index] {
get { return data[index]; }
}
public MyVector(params int[] data) {
if (data == null) throw new ArgumentNullException("data");
this.data = (int[])data.Clone();
}
private int? hash;
public override int GetHashCode() {
if (hash == null) {
int result = 13;
for (int i = 0; i < data.Length; i++) {
result = (result * 7) + data[i];
}
hash = result;
}
return hash.GetValueOrDefault();
}
public int Length { get { return data.Length; } }
public IEnumerator<int> GetEnumerator() {
for (int i = 0; i < data.Length; i++) {
yield return data[i];
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public override bool Equals(object obj)
{
return this == (obj as MyVector);
}
public bool Equals(MyVector obj) {
return this == obj;
}
public override string ToString() {
StringBuilder sb = new StringBuilder("[");
if (data.Length > 0) sb.Append(data[0]);
for (int i = 1; i < data.Length; i++) {
sb.Append(',').Append(data[i]);
}
sb.Append(']');
return sb.ToString();
}
public static bool operator ==(MyVector x, MyVector y) {
if(ReferenceEquals(x,y)) return true;
if(ReferenceEquals(x,null) || ReferenceEquals(y,null)) return false;
if (x.hash.HasValue && y.hash.HasValue && // exploit known different hash
x.hash.GetValueOrDefault() != y.hash.GetValueOrDefault()) return false;
int[] xdata = x.data, ydata = y.data;
if(xdata.Length != ydata.Length) return false;
for(int i = 0 ; i < xdata.Length ; i++) {
if(xdata[i] != ydata[i]) return false;
}
return true;
}
public static bool operator != (MyVector x, MyVector y) {
return !(x==y);
}
public static MyVector operator +(MyVector x, MyVector y) {
if(x==null || y == null) throw new ArgumentNullException();
int[] xdata = x.data, ydata = y.data;
if(xdata.Length != ydata.Length) throw new InvalidOperationException("Length mismatch");
int[] result = new int[xdata.Length];
for(int i = 0 ; i < xdata.Length ; i++) {
result[i] = xdata[i] + ydata[i];
}
return new MyVector(result);
}
}
Its not permitted to extend the array class, see the reference: http://msdn.microsoft.com/en-us/library/system.array.aspx
You could either implement IList (which has the basic methods), or encapsulate an Array in your class and provide conversion operators.
Please let me know if you need more detail.
Can you not just use the List class? This already does what you want via the AddRange method.
implement the ienumerable interface