// Apache License 2.0
// https://github.com/influxdata/tdigest
// Centroid average position of all points in a shape
export class Centroid {
    mean;
    weight;
    constructor(mean, weight) {
        this.mean = mean;
        this.weight = weight;
    }
    add(r) {
        if (r.weight < 0) {
            throw new Error('centroid weight cannot be less than zero');
        }
        if (this.weight !== 0) {
            this.weight += r.weight;
            this.mean += (r.weight * (r.mean - this.mean)) / this.weight;
        }
        else {
            this.weight = r.weight;
            this.mean = r.mean;
        }
    }
}
export function sortCentroidList(centroids) {
    centroids.sort((a, b) => a.mean - b.mean);
}
//# sourceMappingURL=centroid.js.map