export class Pair {
x: A
y: B
constructor(x: A, y: B) {
this.x = x
this.y = y
}
toKey() {
return `Pair(${this.x}, ${this.y})`
}
equals(other: Pair) {
return this.x === other.x && this.y === other.y
}
static fromKey(key: string) {
// Parse the Pair(x, y) format
const match = key.match(/^Pair\((.*), (.*)\)$/)
if (!match) {
throw new Error(`Invalid Pair key format: ${key}`)
}
const [, x, y] = match
return new Pair(x as A, y as B)
}
static key(x: X, y: Y) {
return new Pair(x, y).toKey()
}
}