function weightedQuantileF(data, weights, accessor) {
const values = accessor ? Float64Array.from(data, (d, i) => accessor(d, i, data)) : Float64Array.from(data);
weights = typeof weights === "function" ? Float64Array.from(data, weights) : Float64Array.from(weights);
const indices = Int32Array.from(values, (_, i) => i)
.sort((i, j) => d3.ascending(values[i], values[j])),
cs = d3.cumsum(indices, i => weights[i]);
const wq = (q) => {
const r = cs[cs.length - 1] * q,
i = d3.bisectLeft(cs, r),
mix = r == cs[i] && i < values.length - 1,
select = mix ? [indices[i], indices[i + 1]] : [indices[i], indices[i]],
value = mix ? (values[select[0]] + values[select[1]]) / 2 : values[select[0]];
return { value, select };
}
wq.invert = (value) => (cs[d3.bisector((i) => values[i]).left(indices, value)-1] ?? 0) / cs[cs.length - 1];
return wq;
}