2020-10-27 18:08:49 +08:00
|
|
|
module es {
|
2020-11-27 11:07:43 +08:00
|
|
|
export class EqualityComparer<T> implements IEqualityComparer<T> {
|
|
|
|
|
public static default<T>() {
|
2020-10-27 18:08:49 +08:00
|
|
|
return new EqualityComparer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 11:07:43 +08:00
|
|
|
protected constructor() { }
|
2020-10-27 18:08:49 +08:00
|
|
|
|
2020-11-27 11:07:43 +08:00
|
|
|
public equals(x: T, y: T): boolean {
|
|
|
|
|
if (typeof x["equals"] == 'function') {
|
2020-10-27 18:08:49 +08:00
|
|
|
return x["equals"](y);
|
|
|
|
|
} else {
|
|
|
|
|
return x === y;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-27 11:07:43 +08:00
|
|
|
|
|
|
|
|
public getHashCode(o: T): number {
|
|
|
|
|
if (typeof o == 'number') {
|
|
|
|
|
return this._getHashCodeForNumber(o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof o == 'string') {
|
|
|
|
|
return this._getHashCodeForString(o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let hashCode = 385229220;
|
|
|
|
|
this.forOwn(o, (value) => {
|
|
|
|
|
if (typeof value == 'number') {
|
|
|
|
|
hashCode += this._getHashCodeForNumber(value);
|
|
|
|
|
} else if (typeof value == 'string') {
|
|
|
|
|
hashCode += this._getHashCodeForString(value);
|
|
|
|
|
} else if (typeof value == 'object') {
|
|
|
|
|
this.forOwn(value, () => {
|
|
|
|
|
hashCode += this.getHashCode(value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _getHashCodeForNumber(n: number): number {
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _getHashCodeForString(s: string): number {
|
|
|
|
|
let hashCode = 385229220;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < s.length; i++) {
|
|
|
|
|
hashCode = (hashCode * -1521134295) ^ s.charCodeAt(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private forOwn(object, iteratee){
|
|
|
|
|
object = Object(object);
|
|
|
|
|
Object.keys(object).forEach((key) => iteratee(object[key], key, object));
|
|
|
|
|
}
|
2020-10-27 18:08:49 +08:00
|
|
|
}
|
|
|
|
|
}
|