class Permutation {
value;
string;
constructor(x) {
if (x instanceof Map) {
this.value = x
} else if (typeof x === 'string') {
this.value = x
.split(/[()]/).filter(s => s.length > 0)
.map(s => {if (/[ ,]/.test(s)) {return s.split(/[ ,]+/)} else {return Array.from(s)}})
.flatMap(a => {if (hasDuplicates(a)) {throw Error("duplicates in input")}; return new Map(zip(a, arrayRotate(a, 1)))})
.reduce(composeMap, new Map())
} else {
throw Error("unknown input")
}
this.string = printPermFromMap(this.value)
}
compose(q) {
return new Permutation(composeMap(this.value, q.value))
}
inverse() {
return new Permutation(new Map([...this.value.entries()].map(([k, v]) => [v, k])))
}
conj(q) {
if (conventionInverseLast) {
return q.compose(this).compose(q.inverse())
} else {
return q.inverse().compose(this).compose(q)
}
}
}