I'm currently trying to do the following. I am using a Set to store objects. Now as Typescript uses SameZeroEquality for comparing Objects the default has will not work.
So I am trying to build an AdvancedSet that defines a custom has method. So far it looks somewhat like that
interface AdvancedSetOptions<T> {
equal?: (a: T, b: T) => boolean;
}
const defaultOptions: AdvancedSetOptions<any> = {
}
export class AdvancedSet<T> extends Set<T>{
constructor(Options: AdvancedSetOptions<T> = defaultOptions) {
super();
super.has = Options.equal ? this.getHasFromValidator(Options.equal) : super.has;
}
private getHasFromValidator(validator: ((a: T, b: T) => boolean)): (value: T) => boolean {
return function(value: T): boolean {
let found = false;
let iterator = this.values();
let entry = iterator.next();
while (entry.value) {
if (entry.value) {
if (validator(entry.value, value)) {
found = true;
break;
}
}
entry = iterator.next();
}
return found;
}
}
}
Now this will obviously not work as i am iterating through the set which will be painfully slow and I could have used an array in the first place.
Basically I will have to rewrite getHash and equals to achieve somthing that's working. Is this possible at all with set or do I basically have to build a class HashTable from scratch?