tdigest-kt
Version:
port of latest (5/2019) TDigest algorithm to kotlin multiplatform
1,332 lines • 127 kB
JavaScript
(function (_, Kotlin, $module$kotlinx_atomicfu, $module$bytebuffer, $module$long) {
'use strict';
var $$importsForInline$$ = _.$$importsForInline$$ || (_.$$importsForInline$$ = {});
var toByte = Kotlin.toByte;
var Kind_INTERFACE = Kotlin.Kind.INTERFACE;
var shuffle = Kotlin.kotlin.collections.shuffle_9jeydg$;
var IllegalStateException_init = Kotlin.kotlin.IllegalStateException_init_pdl1vj$;
var Kind_OBJECT = Kotlin.Kind.OBJECT;
var Random = Kotlin.kotlin.random.Random_za3lpa$;
var Kind_CLASS = Kotlin.Kind.CLASS;
var ArrayList_init = Kotlin.kotlin.collections.ArrayList_init_287e2$;
var Math_0 = Math;
var StringBuilder_init = Kotlin.kotlin.text.StringBuilder_init;
var joinToString = Kotlin.kotlin.collections.joinToString_raq4np$;
var toString = Kotlin.toString;
var joinToString_0 = Kotlin.kotlin.collections.joinToString_vk9fgb$;
var ensureNotNull = Kotlin.ensureNotNull;
var UnsupportedOperationException_init = Kotlin.kotlin.UnsupportedOperationException_init_pdl1vj$;
var MutableIterator = Kotlin.kotlin.collections.MutableIterator;
var AbstractCollection = Kotlin.kotlin.collections.AbstractCollection;
var copyOf = Kotlin.kotlin.collections.copyOf_xgrzbe$;
var copyOf_0 = Kotlin.kotlin.collections.copyOf_c03ot6$;
var copyOf_1 = Kotlin.kotlin.collections.copyOf_8ujjk8$;
var UnsupportedOperationException_init_0 = Kotlin.kotlin.UnsupportedOperationException_init;
var IllegalStateException_init_0 = Kotlin.kotlin.IllegalStateException_init;
var IllegalArgumentException_init = Kotlin.kotlin.IllegalArgumentException_init;
var MutableList = Kotlin.kotlin.collections.MutableList;
var throwCCE = Kotlin.throwCCE;
var kotlin_js_internal_DoubleCompanionObject = Kotlin.kotlin.js.internal.DoubleCompanionObject;
var Unit = Kotlin.kotlin.Unit;
var IllegalArgumentException_init_0 = Kotlin.kotlin.IllegalArgumentException_init_pdl1vj$;
var L0 = Kotlin.Long.ZERO;
var atomic = $module$kotlinx_atomicfu.kotlinx.atomicfu.atomic$int$;
var Comparable = Kotlin.kotlin.Comparable;
var numberToInt = Kotlin.numberToInt;
var copyOf_2 = Kotlin.kotlin.collections.copyOf_mrm5p$;
var AssertionError_init = Kotlin.kotlin.AssertionError_init;
var kotlin_js_internal_ByteCompanionObject = Kotlin.kotlin.js.internal.ByteCompanionObject;
var abs = Kotlin.kotlin.math.abs_za3lpa$;
var AssertionError_init_0 = Kotlin.kotlin.AssertionError_init_s8jyv4$;
var List = Kotlin.kotlin.collections.List;
var isNaN_0 = Kotlin.kotlin.isNaN_yrwdxr$;
var listOf = Kotlin.kotlin.collections.listOf_mh5how$;
var NotImplementedError = Kotlin.kotlin.NotImplementedError;
var reverse = Kotlin.kotlin.collections.reverse_vvxzk3$;
var println = Kotlin.kotlin.io.println_s8jyv4$;
var Enum = Kotlin.kotlin.Enum;
var throwISE = Kotlin.throwISE;
var toShort = Kotlin.toShort;
var math = Kotlin.kotlin.math;
var endsWith = Kotlin.kotlin.text.endsWith_7epoxm$;
var arrayCopy = Kotlin.kotlin.collections.arrayCopy;
var defineInlineFunction = Kotlin.defineInlineFunction;
var wrapFunction = Kotlin.wrapFunction;
AbstractTDigest.prototype = Object.create(TDigest.prototype);
AbstractTDigest.prototype.constructor = AbstractTDigest;
AVLGroupTree_init$ObjectLiteral.prototype = Object.create(IntAVLTree.prototype);
AVLGroupTree_init$ObjectLiteral.prototype.constructor = AVLGroupTree_init$ObjectLiteral;
AVLGroupTree.prototype = Object.create(AbstractCollection.prototype);
AVLGroupTree.prototype.constructor = AVLGroupTree;
AVLTreeDigest.prototype = Object.create(AbstractTDigest.prototype);
AVLTreeDigest.prototype.constructor = AVLTreeDigest;
MergingDigest$centroids$ObjectLiteral.prototype = Object.create(AbstractCollection.prototype);
MergingDigest$centroids$ObjectLiteral.prototype.constructor = MergingDigest$centroids$ObjectLiteral;
MergingDigest$Encoding.prototype = Object.create(Enum.prototype);
MergingDigest$Encoding.prototype.constructor = MergingDigest$Encoding;
MergingDigest.prototype = Object.create(AbstractTDigest.prototype);
MergingDigest.prototype.constructor = MergingDigest;
ScaleFunction.prototype = Object.create(Enum.prototype);
ScaleFunction.prototype.constructor = ScaleFunction;
ScaleFunction$K_0.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_0.prototype.constructor = ScaleFunction$K_0;
ScaleFunction$K_1.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_1.prototype.constructor = ScaleFunction$K_1;
ScaleFunction$K_1_FAST.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_1_FAST.prototype.constructor = ScaleFunction$K_1_FAST;
ScaleFunction$K_2.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_2.prototype.constructor = ScaleFunction$K_2;
ScaleFunction$K_3.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_3.prototype.constructor = ScaleFunction$K_3;
ScaleFunction$K_2_NO_NORM.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_2_NO_NORM.prototype.constructor = ScaleFunction$K_2_NO_NORM;
ScaleFunction$K_3_NO_NORM.prototype = Object.create(ScaleFunction.prototype);
ScaleFunction$K_3_NO_NORM.prototype.constructor = ScaleFunction$K_3_NO_NORM;
function BinaryInput() {
}
BinaryInput.prototype.readBoolean = function () {
return this.readByte() === toByte(0) ? false : true;
};
BinaryInput.$metadata$ = {
kind: Kind_INTERFACE,
simpleName: 'BinaryInput',
interfaces: []
};
function BinaryOutput() {
}
BinaryOutput.prototype.writeBoolean_6taknv$ = function (v) {
this.writeByte_s8j3t7$(v ? 1 : 0);
};
BinaryOutput.$metadata$ = {
kind: Kind_INTERFACE,
simpleName: 'BinaryOutput',
interfaces: []
};
function AbstractTDigest() {
AbstractTDigest$Companion_getInstance();
TDigest.call(this);
this.gen_8be2vx$ = Random(0);
this.isRecording_zamskc$_0 = false;
}
Object.defineProperty(AbstractTDigest.prototype, 'isRecording', {
get: function () {
return this.isRecording_zamskc$_0;
},
set: function (isRecording) {
this.isRecording_zamskc$_0 = isRecording;
}
});
AbstractTDigest.prototype.recordAllData = function () {
this.isRecording = true;
return this;
};
AbstractTDigest.prototype.addSample = function (x) {
this.addWeightedSample(x, 1);
};
AbstractTDigest.prototype.addOtherDigest = function (other) {
var tmp$, tmp$_0;
var tmp = ArrayList_init();
tmp$ = other.centroids().iterator();
while (tmp$.hasNext()) {
var centroid = tmp$.next();
tmp.add_11rb$(centroid);
}
shuffle(tmp, this.gen_8be2vx$);
tmp$_0 = tmp.iterator();
while (tmp$_0.hasNext()) {
var centroid_0 = tmp$_0.next();
this.add_9eljot$(centroid_0.mean(), centroid_0.count(), centroid_0);
}
};
AbstractTDigest.prototype.createCentroid_12fank$ = function (mean, id) {
return Centroid_init_2(mean, id, this.isRecording);
};
function AbstractTDigest$Companion() {
AbstractTDigest$Companion_instance = this;
}
AbstractTDigest$Companion.prototype.weightedAverage_kn9dxl$ = function (x1, w1, x2, w2) {
var tmp$;
if (x1 <= x2) {
tmp$ = this.weightedAverageSorted_0(x1, w1, x2, w2);
}
else {
tmp$ = this.weightedAverageSorted_0(x2, w2, x1, w1);
}
return tmp$;
};
AbstractTDigest$Companion.prototype.weightedAverageSorted_0 = function (x1, w1, x2, w2) {
mpassert(x1 <= x2);
var x = (x1 * w1 + x2 * w2) / (w1 + w2);
var b = Math_0.min(x, x2);
return Math_0.max(x1, b);
};
AbstractTDigest$Companion.prototype.interpolate_hln2n9$ = function (x, x0, x1) {
return (x - x0) / (x1 - x0);
};
AbstractTDigest$Companion.prototype.encode_ydee4o$ = function (buf, n) {
var n_0 = n;
var k = 0;
while (n_0 < 0 || n_0 > 127) {
var b = toByte(128 | 127 & n_0);
buf.writeByte_s8j3t7$(b);
n_0 = n_0 >>> 7;
k = k + 1 | 0;
if (k >= 6) {
throw IllegalStateException_init('Size is implausibly large');
}
}
buf.writeByte_s8j3t7$(toByte(n_0));
};
AbstractTDigest$Companion.prototype.decode_51dx0l$ = function (buf) {
var v = buf.readByte();
var z = 127 & v;
var shift = 7;
while ((v & 128) !== 0) {
if (shift > 28) {
throw IllegalStateException_init('Shift too large in decode');
}
v = buf.readByte();
z = z + ((v & 127) << shift) | 0;
shift = shift + 7 | 0;
}
return z;
};
AbstractTDigest$Companion.prototype.quantile_m467d5$ = function (index, previousIndex, nextIndex, previousMean, nextMean) {
var delta = nextIndex - previousIndex;
var previousWeight = (nextIndex - index) / delta;
var nextWeight = (index - previousIndex) / delta;
return previousMean * previousWeight + nextMean * nextWeight;
};
AbstractTDigest$Companion.$metadata$ = {
kind: Kind_OBJECT,
simpleName: 'Companion',
interfaces: []
};
var AbstractTDigest$Companion_instance = null;
function AbstractTDigest$Companion_getInstance() {
if (AbstractTDigest$Companion_instance === null) {
new AbstractTDigest$Companion();
}
return AbstractTDigest$Companion_instance;
}
AbstractTDigest.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'AbstractTDigest',
interfaces: [TDigest]
};
function AVLGroupTree(record) {
if (record === void 0)
record = false;
AbstractCollection.call(this);
this.centroid_0 = 0;
this.count_0 = 0;
this.data_0 = null;
this.centroids_0 = null;
this.counts_0 = null;
this.datas_0 = null;
this.aggregatedCounts_0 = null;
this.tree_0 = null;
this.tree_0 = new AVLGroupTree_init$ObjectLiteral(this);
this.centroids_0 = new Float64Array(this.tree_0.capacity());
this.counts_0 = new Int32Array(this.tree_0.capacity());
this.aggregatedCounts_0 = new Int32Array(this.tree_0.capacity());
if (record) {
var datas = Kotlin.newArray(this.tree_0.capacity(), null);
this.datas_0 = datas;
}
}
AVLGroupTree.prototype.toString = function () {
var tmp$, tmp$_0;
var sb = StringBuilder_init();
sb.append_gw00v9$('centroids=' + toString((tmp$ = this.centroids_0) != null ? joinToString(tmp$) : null) + ';');
sb.append_gw00v9$('counts=' + toString((tmp$_0 = this.counts_0) != null ? joinToString_0(tmp$_0) : null) + ';');
return sb.toString();
};
Object.defineProperty(AVLGroupTree.prototype, 'size', {
get: function () {
return this.tree_0.size();
}
});
AVLGroupTree.prototype.prev_za3lpa$ = function (node) {
return this.tree_0.prev_za3lpa$(node);
};
AVLGroupTree.prototype.next_za3lpa$ = function (node) {
return this.tree_0.next_za3lpa$(node);
};
AVLGroupTree.prototype.mean_za3lpa$ = function (node) {
return ensureNotNull(this.centroids_0)[node];
};
AVLGroupTree.prototype.count_za3lpa$ = function (node) {
return ensureNotNull(this.counts_0)[node];
};
AVLGroupTree.prototype.data_za3lpa$ = function (node) {
return this.datas_0 == null ? null : ensureNotNull(this.datas_0)[node];
};
AVLGroupTree.prototype.add_lswtur$ = function (centroid, count, data) {
this.centroid_0 = centroid;
this.count_0 = count;
this.data_0 = data;
this.tree_0.add();
};
AVLGroupTree.prototype.add_lv3ymp$ = function (centroid) {
this.add_lswtur$(centroid.mean(), centroid.count(), centroid.data());
return true;
};
AVLGroupTree.prototype.update_yovgz0$ = function (node, centroid, count, data, forceInPlace) {
if (centroid === ensureNotNull(this.centroids_0)[node] || forceInPlace) {
ensureNotNull(this.centroids_0)[node] = centroid;
ensureNotNull(this.counts_0)[node] = count;
if (this.datas_0 != null) {
ensureNotNull(this.datas_0)[node] = data;
}
}
else {
this.centroid_0 = centroid;
this.count_0 = count;
this.data_0 = data;
this.tree_0.update_za3lpa$(node);
}
};
AVLGroupTree.prototype.remove_za3lpa$ = function (node) {
this.tree_0.remove_za3lpa$(node);
};
AVLGroupTree.prototype.floor_14dthe$ = function (centroid) {
var floor = IntAVLTree$Companion_getInstance().NIL;
var node = this.tree_0.root();
while (node !== IntAVLTree$Companion_getInstance().NIL) {
var cmp = Kotlin.compareTo(centroid, this.mean_za3lpa$(node));
if (cmp <= 0) {
node = this.tree_0.left_za3lpa$(node);
}
else {
floor = node;
node = this.tree_0.right_za3lpa$(node);
}
}
return floor;
};
AVLGroupTree.prototype.floorSum_s8cxhz$ = function (sum) {
var sum_0 = sum;
var floor = IntAVLTree$Companion_getInstance().NIL;
var node = this.tree_0.root();
while (node !== IntAVLTree$Companion_getInstance().NIL) {
var left = this.tree_0.left_za3lpa$(node);
var leftCount = Kotlin.Long.fromInt(ensureNotNull(this.aggregatedCounts_0)[left]);
if (leftCount.compareTo_11rb$(sum_0) <= 0) {
floor = node;
sum_0 = sum_0.subtract(leftCount.add(Kotlin.Long.fromInt(this.count_za3lpa$(node))));
node = this.tree_0.right_za3lpa$(node);
}
else {
node = this.tree_0.left_za3lpa$(node);
}
}
return floor;
};
AVLGroupTree.prototype.first = function () {
return this.tree_0.first_za3lpa$(this.tree_0.root());
};
AVLGroupTree.prototype.last = function () {
return this.tree_0.last_za3lpa$(this.tree_0.root());
};
AVLGroupTree.prototype.headSum_za3lpa$ = function (node) {
var left = this.tree_0.left_za3lpa$(node);
var sum = Kotlin.Long.fromInt(ensureNotNull(this.aggregatedCounts_0)[left]);
var n = node;
var p = this.tree_0.parent_za3lpa$(node);
while (p !== IntAVLTree$Companion_getInstance().NIL) {
if (n === this.tree_0.right_za3lpa$(p)) {
var leftP = this.tree_0.left_za3lpa$(p);
sum = sum.add(Kotlin.Long.fromInt(ensureNotNull(this.counts_0)[p] + ensureNotNull(this.aggregatedCounts_0)[leftP] | 0));
}
n = p;
p = this.tree_0.parent_za3lpa$(n);
}
return sum;
};
AVLGroupTree.prototype.iterator = function () {
return this.iterator_0(this.first());
};
function AVLGroupTree$iterator$ObjectLiteral(this$AVLGroupTree, closure$startNode) {
this.this$AVLGroupTree = this$AVLGroupTree;
this.nextNode = closure$startNode;
}
AVLGroupTree$iterator$ObjectLiteral.prototype.hasNext = function () {
return this.nextNode !== IntAVLTree$Companion_getInstance().NIL;
};
AVLGroupTree$iterator$ObjectLiteral.prototype.next = function () {
var tmp$;
var next = Centroid_init_0(this.this$AVLGroupTree.mean_za3lpa$(this.nextNode), this.this$AVLGroupTree.count_za3lpa$(this.nextNode));
var data = this.this$AVLGroupTree.data_za3lpa$(this.nextNode);
if (data != null) {
tmp$ = data.iterator();
while (tmp$.hasNext()) {
var x = tmp$.next();
next.insertData_14dthe$(x);
}
}
this.nextNode = this.this$AVLGroupTree.tree_0.next_za3lpa$(this.nextNode);
return next;
};
AVLGroupTree$iterator$ObjectLiteral.prototype.remove = function () {
throw UnsupportedOperationException_init('Read-only iterator');
};
AVLGroupTree$iterator$ObjectLiteral.$metadata$ = {
kind: Kind_CLASS,
interfaces: [MutableIterator]
};
AVLGroupTree.prototype.iterator_0 = function (startNode) {
return new AVLGroupTree$iterator$ObjectLiteral(this, startNode);
};
AVLGroupTree.prototype.sum = function () {
return ensureNotNull(this.aggregatedCounts_0)[this.tree_0.root()];
};
AVLGroupTree.prototype.checkBalance = function () {
this.tree_0.checkBalance_za3lpa$(this.tree_0.root());
};
AVLGroupTree.prototype.checkAggregates = function () {
this.checkAggregates_0(this.tree_0.root());
};
AVLGroupTree.prototype.checkAggregates_0 = function (node) {
mpassert(ensureNotNull(this.aggregatedCounts_0)[node] === (ensureNotNull(this.counts_0)[node] + ensureNotNull(this.aggregatedCounts_0)[this.tree_0.left_za3lpa$(node)] + ensureNotNull(this.aggregatedCounts_0)[this.tree_0.right_za3lpa$(node)] | 0));
if (node !== IntAVLTree$Companion_getInstance().NIL) {
this.checkAggregates_0(this.tree_0.left_za3lpa$(node));
this.checkAggregates_0(this.tree_0.right_za3lpa$(node));
}
};
function AVLGroupTree_init$ObjectLiteral(this$AVLGroupTree, initialCapacity) {
this.this$AVLGroupTree = this$AVLGroupTree;
IntAVLTree.call(this, initialCapacity);
}
AVLGroupTree_init$ObjectLiteral.prototype.resize_za3lpa$ = function (newCapacity) {
IntAVLTree.prototype.resize_za3lpa$.call(this, newCapacity);
this.this$AVLGroupTree.centroids_0 = copyOf(ensureNotNull(this.this$AVLGroupTree.centroids_0), newCapacity);
this.this$AVLGroupTree.counts_0 = copyOf_0(ensureNotNull(this.this$AVLGroupTree.counts_0), newCapacity);
this.this$AVLGroupTree.aggregatedCounts_0 = copyOf_0(ensureNotNull(this.this$AVLGroupTree.aggregatedCounts_0), newCapacity);
if (this.this$AVLGroupTree.datas_0 != null) {
this.this$AVLGroupTree.datas_0 = copyOf_1(ensureNotNull(this.this$AVLGroupTree.datas_0), newCapacity);
}
};
AVLGroupTree_init$ObjectLiteral.prototype.merge_za3lpa$ = function (node) {
throw UnsupportedOperationException_init_0();
};
AVLGroupTree_init$ObjectLiteral.prototype.copy_za3lpa$ = function (node) {
ensureNotNull(this.this$AVLGroupTree.centroids_0)[node] = this.this$AVLGroupTree.centroid_0;
ensureNotNull(this.this$AVLGroupTree.counts_0)[node] = this.this$AVLGroupTree.count_0;
if (this.this$AVLGroupTree.datas_0 != null) {
if (this.this$AVLGroupTree.data_0 == null) {
if (this.this$AVLGroupTree.count_0 !== 1) {
throw IllegalStateException_init_0();
}
this.this$AVLGroupTree.data_0 = ArrayList_init();
ensureNotNull(this.this$AVLGroupTree.data_0).add_11rb$(this.this$AVLGroupTree.centroid_0);
}
ensureNotNull(this.this$AVLGroupTree.datas_0)[node] = ensureNotNull(this.this$AVLGroupTree.data_0);
}
};
AVLGroupTree_init$ObjectLiteral.prototype.compare_za3lpa$ = function (node) {
var tmp$;
if (this.this$AVLGroupTree.centroid_0 < ensureNotNull(this.this$AVLGroupTree.centroids_0)[node]) {
tmp$ = -1;
}
else {
tmp$ = 1;
}
return tmp$;
};
AVLGroupTree_init$ObjectLiteral.prototype.fixAggregates_za3lpa$ = function (node) {
IntAVLTree.prototype.fixAggregates_za3lpa$.call(this, node);
ensureNotNull(this.this$AVLGroupTree.aggregatedCounts_0)[node] = ensureNotNull(this.this$AVLGroupTree.counts_0)[node] + ensureNotNull(this.this$AVLGroupTree.aggregatedCounts_0)[this.left_za3lpa$(node)] + ensureNotNull(this.this$AVLGroupTree.aggregatedCounts_0)[this.right_za3lpa$(node)] | 0;
};
AVLGroupTree_init$ObjectLiteral.$metadata$ = {
kind: Kind_CLASS,
interfaces: [IntAVLTree]
};
AVLGroupTree.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'AVLGroupTree',
interfaces: [AbstractCollection]
};
function AVLTreeDigest() {
AVLTreeDigest$Companion_getInstance();
this.compression_0 = 0;
this.summary_0 = null;
this.count_0 = L0;
}
AVLTreeDigest.prototype.toString = function () {
var tmp$, tmp$_0;
return (tmp$_0 = (tmp$ = this.summary_0) != null ? tmp$.toString() : null) != null ? tmp$_0 : '';
};
AVLTreeDigest.prototype.recordAllData = function () {
if (ensureNotNull(this.summary_0).size !== 0) {
throw IllegalStateException_init('Can only ask to record added data on an empty summary');
}
this.summary_0 = new AVLGroupTree(true);
return AbstractTDigest.prototype.recordAllData.call(this);
};
AVLTreeDigest.prototype.centroidCount = function () {
return ensureNotNull(this.summary_0).size;
};
AVLTreeDigest.prototype.add_9eljot$ = function (x, w, base) {
if (x !== base.mean() || w !== base.count()) {
throw IllegalArgumentException_init();
}
this.add_lswtur$(x, w, base.data());
};
AVLTreeDigest.prototype.addWeightedSample = function (x, w) {
var tmp$;
this.add_lswtur$(x, w, (tmp$ = null) == null || Kotlin.isType(tmp$, MutableList) ? tmp$ : throwCCE());
};
AVLTreeDigest.prototype.add_ixnl3q$ = function (others) {
var tmp$, tmp$_0;
tmp$ = others.iterator();
while (tmp$.hasNext()) {
var other = tmp$.next();
var a = this.min;
var b = other.min;
var tmp$_1 = Math_0.min(a, b);
var a_0 = this.max;
var b_0 = other.max;
this.setMinMax_sdh6z7$(tmp$_1, Math_0.max(a_0, b_0));
tmp$_0 = other.centroids().iterator();
while (tmp$_0.hasNext()) {
var centroid = tmp$_0.next();
this.add_lswtur$(centroid.mean(), centroid.count(), this.isRecording ? centroid.data() : null);
}
}
};
AVLTreeDigest.prototype.add_lswtur$ = function (x, w, data) {
this.checkValue_tq0o01$(x);
if (x < this.min) {
this.min = x;
}
if (x > this.max) {
this.max = x;
}
var start = {v: ensureNotNull(this.summary_0).floor_14dthe$(x)};
if (start.v === IntAVLTree$Companion_getInstance().NIL) {
start.v = ensureNotNull(this.summary_0).first();
}
if (start.v === IntAVLTree$Companion_getInstance().NIL) {
mpassert(ensureNotNull(this.summary_0).size === 0);
ensureNotNull(this.summary_0).add_lswtur$(x, w, data);
this.count_0 = Kotlin.Long.fromInt(w);
}
else {
var minDistance = {v: kotlin_js_internal_DoubleCompanionObject.MAX_VALUE};
var lastNeighbor = {v: IntAVLTree$Companion_getInstance().NIL};
var neighbor = start.v;
while (neighbor !== IntAVLTree$Companion_getInstance().NIL) {
var x_0 = ensureNotNull(this.summary_0).mean_za3lpa$(neighbor) - x;
var z = Math_0.abs(x_0);
if (z < minDistance.v) {
start.v = neighbor;
minDistance.v = z;
}
else if (z > minDistance.v) {
lastNeighbor.v = neighbor;
break;
}
neighbor = ensureNotNull(this.summary_0).next_za3lpa$(neighbor);
}
var closest = IntAVLTree$Companion_getInstance().NIL;
var n = 0.0;
var neighbor_0 = start.v;
while (neighbor_0 !== lastNeighbor.v) {
var tmp$ = minDistance.v;
var x_1 = ensureNotNull(this.summary_0).mean_za3lpa$(neighbor_0) - x;
mpassert(tmp$ === Math_0.abs(x_1));
var q0 = ensureNotNull(this.summary_0).headSum_za3lpa$(neighbor_0).toNumber() / this.count_0.toNumber();
var q1 = q0 + ensureNotNull(this.summary_0).count_za3lpa$(neighbor_0) / this.count_0.toNumber();
var tmp$_0 = this.count_0.toNumber();
var a = this.scale.max_yvo9jy$(q0, this.compression_0, this.count_0.toNumber());
var b = this.scale.max_yvo9jy$(q1, this.compression_0, this.count_0.toNumber());
var k = tmp$_0 * Math_0.min(a, b);
if ((ensureNotNull(this.summary_0).count_za3lpa$(neighbor_0) + w | 0) <= k) {
n = n + 1;
if (this.gen_8be2vx$.nextDouble() < 1 / n) {
closest = neighbor_0;
}
}
neighbor_0 = ensureNotNull(this.summary_0).next_za3lpa$(neighbor_0);
}
if (closest === IntAVLTree$Companion_getInstance().NIL) {
ensureNotNull(this.summary_0).add_lswtur$(x, w, data);
}
else {
var centroid = ensureNotNull(this.summary_0).mean_za3lpa$(closest);
var count = ensureNotNull(this.summary_0).count_za3lpa$(closest);
var d = ensureNotNull(this.summary_0).data_za3lpa$(closest);
if (d != null) {
if (w === 1) {
d.add_11rb$(x);
}
else {
d.addAll_brywnq$(ensureNotNull(data));
}
}
centroid = AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(centroid, count, x, w);
count = count + w | 0;
ensureNotNull(this.summary_0).update_yovgz0$(closest, centroid, count, d, false);
}
this.count_0 = this.count_0.add(Kotlin.Long.fromInt(w));
if (ensureNotNull(this.summary_0).size > 20 * this.compression_0) {
this.compress();
}
}
};
AVLTreeDigest.prototype.updateSample_lu1900$ = function (oldValue, newValue) {
this.checkValue_tq0o01$(oldValue);
this.checkValue_tq0o01$(newValue);
if (oldValue > this.max || oldValue < this.min) {
throw IllegalArgumentException_init_0('oldValue not in range');
}
var x = oldValue;
var start = {v: ensureNotNull(this.summary_0).floor_14dthe$(x)};
if (start.v === IntAVLTree$Companion_getInstance().NIL) {
start.v = ensureNotNull(this.summary_0).first();
}
var minDistance = {v: kotlin_js_internal_DoubleCompanionObject.MAX_VALUE};
var lastNeighbor = {v: IntAVLTree$Companion_getInstance().NIL};
var neighbor = start.v;
while (neighbor !== IntAVLTree$Companion_getInstance().NIL) {
var x_0 = ensureNotNull(this.summary_0).mean_za3lpa$(neighbor) - x;
var z = Math_0.abs(x_0);
if (z < minDistance.v) {
start.v = neighbor;
minDistance.v = z;
}
else if (z > minDistance.v) {
lastNeighbor.v = neighbor;
break;
}
neighbor = ensureNotNull(this.summary_0).next_za3lpa$(neighbor);
}
var closest = IntAVLTree$Companion_getInstance().NIL;
var n = 0.0;
var neighbor_0 = start.v;
while (neighbor_0 !== lastNeighbor.v) {
var tmp$ = minDistance.v;
var x_1 = ensureNotNull(this.summary_0).mean_za3lpa$(neighbor_0) - x;
mpassert(tmp$ === Math_0.abs(x_1));
n = n + 1;
if (this.gen_8be2vx$.nextDouble() < 1 / n) {
closest = neighbor_0;
}
neighbor_0 = ensureNotNull(this.summary_0).next_za3lpa$(neighbor_0);
}
mpassert(closest !== IntAVLTree$Companion_getInstance().NIL);
var centroid_closest = ensureNotNull(this.summary_0).mean_za3lpa$(closest);
var count_closest = ensureNotNull(this.summary_0).count_za3lpa$(closest);
var d_closest = ensureNotNull(this.summary_0).data_za3lpa$(closest);
if (d_closest != null) {
d_closest.remove_11rb$(x);
}
if (count_closest > 1) {
centroid_closest = AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(centroid_closest, count_closest, x, -1.0);
count_closest = count_closest + -1 | 0;
ensureNotNull(this.summary_0).update_yovgz0$(closest, centroid_closest, count_closest, d_closest, false);
}
else {
mpassert(minDistance.v === 0.0);
ensureNotNull(this.summary_0).remove_za3lpa$(closest);
}
this.count_0 = this.count_0.add(Kotlin.Long.fromInt(-1));
if (ensureNotNull(this.summary_0).size > 20 * this.compression_0) {
this.compress();
}
this.add_lswtur$(newValue, 1, null);
};
AVLTreeDigest.prototype.compress = function () {
if (ensureNotNull(this.summary_0).size <= 1) {
return;
}
var n0 = 0.0;
var k0 = this.count_0.toNumber() * this.scale.max_yvo9jy$(n0 / this.count_0.toNumber(), this.compression_0, this.count_0.toNumber());
var node = ensureNotNull(this.summary_0).first();
var w0 = ensureNotNull(this.summary_0).count_za3lpa$(node);
var n1 = n0 + ensureNotNull(this.summary_0).count_za3lpa$(node);
var w1 = 0;
var k1;
while (node !== IntAVLTree$Companion_getInstance().NIL) {
var after = ensureNotNull(this.summary_0).next_za3lpa$(node);
while (after !== IntAVLTree$Companion_getInstance().NIL) {
w1 = ensureNotNull(this.summary_0).count_za3lpa$(after);
k1 = this.count_0.toNumber() * this.scale.max_yvo9jy$((n1 + w1) / this.count_0.toNumber(), this.compression_0, this.count_0.toNumber());
var tmp$ = w0 + w1 | 0;
var a = k0;
if (tmp$ > Math_0.min(a, k1)) {
break;
}
else {
var mean = AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(ensureNotNull(this.summary_0).mean_za3lpa$(node), w0, ensureNotNull(this.summary_0).mean_za3lpa$(after), w1);
var d1 = ensureNotNull(this.summary_0).data_za3lpa$(node);
var d2 = ensureNotNull(this.summary_0).data_za3lpa$(after);
if (d1 != null && d2 != null) {
d1.addAll_brywnq$(d2);
}
ensureNotNull(this.summary_0).update_yovgz0$(node, mean, w0 + w1 | 0, d1, true);
var tmp = ensureNotNull(this.summary_0).next_za3lpa$(after);
ensureNotNull(this.summary_0).remove_za3lpa$(after);
after = tmp;
n1 += w1;
w0 = w0 + w1 | 0;
}
}
node = after;
if (node !== IntAVLTree$Companion_getInstance().NIL) {
n0 = n1;
k0 = this.count_0.toNumber() * this.scale.max_yvo9jy$(n0 / this.count_0.toNumber(), this.compression_0, this.count_0.toNumber());
w0 = w1;
n1 = n0 + w0;
}
}
};
AVLTreeDigest.prototype.size = function () {
return this.count_0;
};
AVLTreeDigest.prototype.cdf_14dthe$ = function (x) {
var values = this.summary_0;
if (ensureNotNull(values).size === 0) {
return kotlin_js_internal_DoubleCompanionObject.NaN;
}
else if (values.size === 1) {
return x < values.mean_za3lpa$(values.first()) ? 0.0 : x > values.mean_za3lpa$(values.first()) ? 1.0 : 0.5;
}
else {
if (x < this.min) {
return 0.0;
}
else if (x === this.min) {
return 0.5 / this.size().toNumber();
}
mpassert(x > this.min);
if (x > this.max) {
return 1.0;
}
else if (x === this.max) {
var n = this.size();
return (n.toNumber() - 0.5) / n.toNumber();
}
mpassert(x < this.max);
var first = values.first();
var firstMean = values.mean_za3lpa$(first);
if (x > this.min && x < firstMean) {
return this.interpolateTail_0(values, x, first, firstMean, this.min);
}
var last = values.last();
var lastMean = values.mean_za3lpa$(last);
if (x < this.max && x > lastMean) {
return 1 - this.interpolateTail_0(values, x, last, lastMean, this.max);
}
mpassert(values.size >= 2);
mpassert(x >= firstMean);
mpassert(x <= lastMean);
var it = values.iterator();
var a = it.next();
var aMean = a.mean();
var aWeight = a.count();
if (x === aMean) {
return aWeight / 2.0 / this.size().toNumber();
}
mpassert(x > aMean);
var b = it.next();
var bMean = b.mean();
var bWeight = b.count();
mpassert(bMean >= aMean);
var weightSoFar = 0.0;
while (bWeight > 0) {
mpassert(x > aMean);
if (x === bMean) {
mpassert(bMean > aMean);
weightSoFar += aWeight;
while (it.hasNext()) {
b = it.next();
if (x === b.mean()) {
bWeight += b.count();
}
else {
break;
}
}
return (weightSoFar + aWeight + bWeight / 2.0) / this.size().toNumber();
}
mpassert(x < bMean || x > bMean);
if (x < bMean) {
mpassert(aMean < bMean);
if (aWeight === 1.0) {
if (bWeight === 1.0) {
return (weightSoFar + 1.0) / this.size().toNumber();
}
else {
var partialWeight = (x - aMean) / (bMean - aMean) * bWeight / 2.0;
return (weightSoFar + 1.0 + partialWeight) / this.size().toNumber();
}
}
else if (bWeight === 1.0) {
var partialWeight_0 = (x - aMean) / (bMean - aMean) * aWeight / 2.0;
return (weightSoFar + aWeight / 2.0 + partialWeight_0) / this.size().toNumber();
}
else {
var partialWeight_1 = (x - aMean) / (bMean - aMean) * (aWeight + bWeight) / 2.0;
return (weightSoFar + aWeight / 2.0 + partialWeight_1) / this.size().toNumber();
}
}
weightSoFar += aWeight;
mpassert(x > bMean);
if (it.hasNext()) {
aMean = bMean;
aWeight = bWeight;
b = it.next();
bMean = b.mean();
bWeight = b.count();
mpassert(bMean >= aMean);
}
else {
bWeight = 0.0;
}
}
throw IllegalStateException_init('Ran out of centroids');
}
};
AVLTreeDigest.prototype.interpolateTail_0 = function (values, x, node, mean, extremeValue) {
var count = values.count_za3lpa$(node);
mpassert(count > 1);
if (count === 2) {
return 1.0 / this.size().toNumber();
}
else {
var weight = count / 2.0 - 1;
var partialWeight = (extremeValue - x) / (extremeValue - mean) * weight;
return (partialWeight + 1.0) / this.size().toNumber();
}
};
AVLTreeDigest.prototype.quantile = function (q) {
var tmp$;
if (q < 0 || q > 1) {
throw IllegalArgumentException_init_0('q should be in [0,1], got ' + q);
}
var values = this.summary_0;
if (ensureNotNull(values).size === 0) {
return kotlin_js_internal_DoubleCompanionObject.NaN;
}
else if (values.size === 1) {
return values.iterator().next().mean();
}
var index = q * this.count_0.toNumber();
if (index < 1) {
return this.min;
}
if (index >= this.count_0.subtract(Kotlin.Long.fromInt(1)).toNumber()) {
return this.max;
}
var currentNode = values.first();
var currentWeight = values.count_za3lpa$(currentNode);
if (currentWeight === 2 && index <= 2) {
return 2 * values.mean_za3lpa$(currentNode) - this.min;
}
if (values.count_za3lpa$(values.last()) === 2 && index > this.count_0.subtract(Kotlin.Long.fromInt(2)).toNumber()) {
return 2 * values.mean_za3lpa$(values.last()) - this.max;
}
var weightSoFar = currentWeight / 2.0;
if (index < weightSoFar) {
return AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(this.min, weightSoFar - index, values.mean_za3lpa$(currentNode), index - 1);
}
tmp$ = values.size - 1 | 0;
for (var i = 0; i < tmp$; i++) {
var nextNode = values.next_za3lpa$(currentNode);
var nextWeight = values.count_za3lpa$(nextNode);
var dw = (currentWeight + nextWeight | 0) / 2.0;
if (index < weightSoFar + dw) {
var leftExclusion = 0.0;
var rightExclusion = 0.0;
if (currentWeight === 1) {
if (index < weightSoFar + 0.5) {
return values.mean_za3lpa$(currentNode);
}
else {
leftExclusion = 0.5;
}
}
if (nextWeight === 1) {
if (index >= weightSoFar + dw - 0.5) {
return values.mean_za3lpa$(nextNode);
}
else {
rightExclusion = 0.5;
}
}
mpassert(leftExclusion + rightExclusion < 1);
mpassert(dw > 1);
var w1 = index - weightSoFar - leftExclusion;
var w2 = weightSoFar + dw - index - rightExclusion;
return AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(values.mean_za3lpa$(currentNode), w2, values.mean_za3lpa$(nextNode), w1);
}
weightSoFar += dw;
currentNode = nextNode;
currentWeight = nextWeight;
}
mpassert(currentWeight > 1);
mpassert(index - weightSoFar < ((currentWeight / 2 | 0) - 1 | 0));
mpassert(this.count_0.toNumber() - weightSoFar > 0.5);
var w1_0 = index - weightSoFar;
var w2_0 = this.count_0.toNumber() - 1.0 - index;
return AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(values.mean_za3lpa$(currentNode), w2_0, this.max, w1_0);
};
AVLTreeDigest.prototype.centroids = function () {
return ensureNotNull(this.summary_0);
};
AVLTreeDigest.prototype.compression = function () {
return this.compression_0;
};
AVLTreeDigest.prototype.byteSize = function () {
this.compress();
return 32 + (ensureNotNull(this.summary_0).size * 12 | 0) | 0;
};
function AVLTreeDigest$smallByteSize$lambda(this$AVLTreeDigest, closure$res) {
return function ($receiver) {
this$AVLTreeDigest.asSmallBytes($receiver);
closure$res.v = $receiver.size;
return Unit;
};
}
AVLTreeDigest.prototype.smallByteSize = function () {
var bound = this.byteSize();
var res = {v: 0};
var buf = buildBinaryOutput(bound, AVLTreeDigest$smallByteSize$lambda(this, res));
buf.release();
return res.v;
};
AVLTreeDigest.prototype.asBytes = function (buf) {
var tmp$, tmp$_0;
buf.writeInt_za3lpa$(AVLTreeDigest$Companion_getInstance().VERBOSE_ENCODING_0);
buf.writeDouble_14dthe$(this.min);
buf.writeDouble_14dthe$(this.max);
buf.writeDouble_14dthe$(this.compression());
buf.writeInt_za3lpa$(ensureNotNull(this.summary_0).size);
tmp$ = ensureNotNull(this.summary_0).iterator();
while (tmp$.hasNext()) {
var centroid = tmp$.next();
buf.writeDouble_14dthe$(centroid.mean());
}
tmp$_0 = ensureNotNull(this.summary_0).iterator();
while (tmp$_0.hasNext()) {
var centroid_0 = tmp$_0.next();
buf.writeInt_za3lpa$(centroid_0.count());
}
};
AVLTreeDigest.prototype.asSmallBytes = function (buf) {
var tmp$, tmp$_0;
buf.writeInt_za3lpa$(AVLTreeDigest$Companion_getInstance().SMALL_ENCODING_0);
buf.writeDouble_14dthe$(this.min);
buf.writeDouble_14dthe$(this.max);
buf.writeDouble_14dthe$(this.compression());
buf.writeInt_za3lpa$(ensureNotNull(this.summary_0).size);
var x = 0.0;
tmp$ = ensureNotNull(this.summary_0).iterator();
while (tmp$.hasNext()) {
var centroid = tmp$.next();
var delta = centroid.mean() - x;
x = centroid.mean();
buf.writeFloat_mx4ult$(delta);
}
tmp$_0 = ensureNotNull(this.summary_0).iterator();
while (tmp$_0.hasNext()) {
var centroid_0 = tmp$_0.next();
var n = centroid_0.count();
AbstractTDigest$Companion_getInstance().encode_ydee4o$(buf, n);
}
};
function AVLTreeDigest$Companion() {
AVLTreeDigest$Companion_instance = this;
this.VERBOSE_ENCODING_0 = 1;
this.SMALL_ENCODING_0 = 2;
}
AVLTreeDigest$Companion.prototype.fromBytes = function (buf) {
var encoding = buf.readInt();
if (encoding === this.VERBOSE_ENCODING_0) {
var min = buf.readDouble();
var max = buf.readDouble();
var compression = buf.readDouble();
var r = AVLTreeDigest_init(compression);
r.setMinMax_sdh6z7$(min, max);
var n = buf.readInt();
var means = new Float64Array(n);
for (var i = 0; i < n; i++) {
means[i] = buf.readDouble();
}
for (var i_0 = 0; i_0 < n; i_0++) {
r.addWeightedSample(means[i_0], buf.readInt());
}
return r;
}
else if (encoding === this.SMALL_ENCODING_0) {
var min_0 = buf.readDouble();
var max_0 = buf.readDouble();
var compression_0 = buf.readDouble();
var r_0 = AVLTreeDigest_init(compression_0);
r_0.setMinMax_sdh6z7$(min_0, max_0);
var n_0 = buf.readInt();
var means_0 = new Float64Array(n_0);
var x = 0.0;
for (var i_1 = 0; i_1 < n_0; i_1++) {
var delta = buf.readFloat();
x += delta;
means_0[i_1] = x;
}
for (var i_2 = 0; i_2 < n_0; i_2++) {
var z = AbstractTDigest$Companion_getInstance().decode_51dx0l$(buf);
r_0.addWeightedSample(means_0[i_2], z);
}
return r_0;
}
else {
throw IllegalStateException_init('Invalid format for serialized histogram');
}
};
AVLTreeDigest$Companion.$metadata$ = {
kind: Kind_OBJECT,
simpleName: 'Companion',
interfaces: []
};
var AVLTreeDigest$Companion_instance = null;
function AVLTreeDigest$Companion_getInstance() {
if (AVLTreeDigest$Companion_instance === null) {
new AVLTreeDigest$Companion();
}
return AVLTreeDigest$Companion_instance;
}
AVLTreeDigest.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'AVLTreeDigest',
interfaces: [AbstractTDigest]
};
function AVLTreeDigest_init(compression, $this) {
$this = $this || Object.create(AVLTreeDigest.prototype);
AbstractTDigest.call($this);
AVLTreeDigest.call($this);
$this.compression_0 = compression;
$this.summary_0 = new AVLGroupTree(false);
return $this;
}
function Centroid(record) {
Centroid$Companion_getInstance();
this.centroid_0 = 0.0;
this.count_0 = 0;
this.id_0 = 0;
this.actualData_0 = null;
this.id_0 = Centroid$Companion_getInstance().uniqueCount_0.getAndIncrement$atomicfu();
if (record) {
this.actualData_0 = ArrayList_init();
}
}
Centroid.prototype.start_0 = function (x, w, id) {
this.id_0 = id;
this.add_12fank$(x, w);
};
Centroid.prototype.add_12fank$ = function (x, w) {
if (this.actualData_0 != null) {
ensureNotNull(this.actualData_0).add_11rb$(x);
}
this.count_0 = this.count_0 + w | 0;
this.centroid_0 += w * (x - this.centroid_0) / this.count_0;
};
Centroid.prototype.mean = function () {
return this.centroid_0;
};
Centroid.prototype.count = function () {
return this.count_0;
};
Centroid.prototype.id = function () {
return this.id_0;
};
Centroid.prototype.toString = function () {
return 'Centroid{' + 'centroid=' + toString(this.centroid_0) + ', count=' + toString(this.count_0) + String.fromCharCode(125);
};
Centroid.prototype.hashCode = function () {
return this.id_0;
};
Centroid.prototype.compareTo_11rb$ = function (o) {
var r = Kotlin.compareTo(this.centroid_0, o.centroid_0);
if (r === 0) {
r = this.id_0 - o.id_0 | 0;
}
return r;
};
Centroid.prototype.data = function () {
return this.actualData_0;
};
Centroid.prototype.insertData_14dthe$ = function (x) {
if (this.actualData_0 == null) {
this.actualData_0 = ArrayList_init();
}
ensureNotNull(this.actualData_0).add_11rb$(x);
};
Centroid.prototype.add_a3f1dg$ = function (x, w, data) {
var tmp$;
if (this.actualData_0 != null) {
if (data != null) {
tmp$ = data.iterator();
while (tmp$.hasNext()) {
var old = tmp$.next();
ensureNotNull(this.actualData_0).add_11rb$(old);
}
}
else {
ensureNotNull(this.actualData_0).add_11rb$(x);
}
}
this.centroid_0 = AbstractTDigest$Companion_getInstance().weightedAverage_kn9dxl$(this.centroid_0, this.count_0, x, w);
this.count_0 = this.count_0 + w | 0;
};
function Centroid$Companion() {
Centroid$Companion_instance = this;
this.uniqueCount_0 = atomic(1);
}
Centroid$Companion.prototype.createWeighted_a3f1dg$ = function (x, w, data) {
var r = new Centroid(data != null);
r.add_a3f1dg$(x, w, data);
return r;
};
Centroid$Companion.$metadata$ = {
kind: Kind_OBJECT,
simpleName: 'Companion',
interfaces: []
};
var Centroid$Companion_instance = null;
function Centroid$Companion_getInstance() {
if (Centroid$Companion_instance === null) {
new Centroid$Companion();
}
return Centroid$Companion_instance;
}
Centroid.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'Centroid',
interfaces: [Comparable]
};
function Centroid_init(x, $this) {
$this = $this || Object.create(Centroid.prototype);
Centroid.call($this, false);
$this.start_0(x, 1, Centroid$Companion_getInstance().uniqueCount_0.getAndIncrement$atomicfu());
return $this;
}
function Centroid_init_0(x, w, $this) {
$this = $this || Object.create(Centroid.prototype);
Centroid.call($this, false);
$this.start_0(x, w, Centroid$Companion_getInstance().uniqueCount_0.getAndIncrement$atomicfu());
return $this;
}
function Centroid_init_1(x, w, id, $this) {
$this = $this || Object.create(Centroid.prototype);
Centroid.call($this, false);
$this.start_0(x, w, id);
return $this;
}
function Centroid_init_2(x, id, record, $this) {
$this = $this || Object.create(Centroid.prototype);
Centroid.call($this, record);
$this.start_0(x, 1, id);
return $this;
}
function Centroid_init_3(x, w, data, $this) {
$this = $this || Object.create(Centroid.prototype);
Centroid_init_0(x, w, $this);
$this.actualData_0 = data;
return $this;
}
function Dist() {
Dist_instance = this;
}
Dist.prototype.cdf_4avpt5$ = function (x, data) {
var tmp$;
var n1 = 0;
var n2 = 0;
for (tmp$ = 0; tmp$ !== data.length; ++tmp$) {
var v = data[tmp$];
n1 = n1 + (v < x ? 1 : 0) | 0;
n2 = n2 + (v === x ? 1 : 0) | 0;
}
return (n1 + n2 / 2.0) / data.length;
};
Dist.prototype.cdf_7si1j9$ = function (x, data) {
var tmp$;
var n1 = 0;
var n2 = 0;
tmp$ = data.iterator();
while (tmp$.hasNext()) {
var v = tmp$.next();
n1 = n1 + (v < x ? 1 : 0) | 0;
n2 = n2 + (v === x ? 1 : 0) | 0;
}
return (n1 + n2 / 2.0) / data.size;
};
Dist.prototype.quantile_4avpt5$ = function (q, data) {
var n = data.length;
if (n === 0) {
return kotlin_js_internal_DoubleCompanionObject.NaN;
}
var index = q * n;
if (index < 0) {
index = 0.0;
}
if (index > (n - 1 | 0)) {
index = n - 1 | 0;
}
var x = index;
return data[numberToInt(Math_0.floor(x))];
};
Dist.prototype.quantile_9iknyd$ = function (q, data) {
var n = data.size;
if (n === 0) {
return kotlin_js_internal_DoubleCompanionObject.NaN;
}
var index = q * n;
if (index < 0) {
index = 0.0;
}
if (index > (n - 1 | 0)) {
index = n - 1 | 0;
}
var x = index;
return data.get_za3lpa$(numberToInt(Math_0.floor(x)));
};
Dist.$metadata$ = {
kind: Kind_OBJECT,
simpleName: 'Dist',
interfaces: []
};
var Dist_instance = null;
function Dist_getInstance() {
if (Dist_instance === null) {
new Dist();
}
return Dist_instance;
}
function IntAVLTree(initialCapacity) {
IntAVLTree$Companion_getInstance();
if (initialCapacity === void 0)
initialCapacity = 16;
this.nodeAllocator_0 = null;
this.troot_0 = 0;
this.parent_0 = null;
this.left_0 = null;
this.right_0 = null;
this.depth_0 = null;
this.nodeAllocator_0 = new IntAVLTree$NodeAllocator();
this.troot_0 = IntAVLTree$Companion_getInstance().NIL;
this.parent_0 = new Int32Array(initialCapacity);
this.left_0 = new Int32Array(initialCapacity);
this.right_0 = new Int32Array(initialCapacity);
this.depth_0 = new Int8Array(initialCapacity);
}
IntAVLTree.prototype.root = function () {
return this.troot_0;
};
IntAVLTree.prototype.capacity = function () {
return this.parent_0.length;
};
IntAVLTree.prototype.resize_za3lpa$ = function (newCapacity) {
this.parent_0 = copyOf_0(this.parent_0, newCapacity);
this.left_0 = copyOf_0(this.left_0, newCapacity);
this.right_0 = copyOf_0(this.right_0, newCapacity);
this.depth_0 = copyOf_2(this.depth_0, newCapacity);
};
IntAVLTree.prototype.size = function () {
return this.nodeAllocator_0.size_8be2vx$();
};
IntAVLTree.prototype.parent_za3lpa$ = function (node) {
return this.parent_0[node];
};
IntAVLTree.prototype.left_za3lpa$ = function (node) {
return this.left_0[node];
};
IntAVLTree.prototype.right_za3lpa$ = function (node) {
return this.right_0[node];
};
IntAVLTree.prototype.depth_za3lpa$ = function (node) {
return this.depth_0[node];
};
IntAVLTree.prototype.first_za3lpa$ = function (node) {
var node_0 = node;
if (node_0 === IntAVLTree$Companion_getInstance().NIL) {
return IntAVLTree$Companion_getInstance().NIL;
}
while (true) {
var left = this.left_za3lpa$(node_0);
if (left === IntAVLTree$Companion_getInstance().NIL) {
break;
}
node_0 = left;
}
return node_0;
};
IntAVLTree.prototype.last_za3lpa$ = function (node) {
var node_0 = node;
while (true) {
var right = this.right_za3lpa$(node_0);
if (right === IntAVLTree$Companion_getInstance().NIL) {
break;
}
node_0 = right;
}
return node_0;
};
IntAVLTree.prototype.next_za3lpa$ = function (node) {
var node_0 = node;
var right = this.right_za3lpa$(node_0);
if (right !== IntAVLTree$Companion_getInstance().NIL) {
return this.first_za3lpa$(right);
}
else {
var parent = this.parent_za3lpa$(node_0);
while (parent !== IntAVLTree$Companion_getInstance().NIL && node_0 === this.right_za3lpa$(parent)) {
node_0 = parent;
parent = this.parent_za3lpa$(parent);
}
return parent;
}
};
IntAVLTree.prototype.prev_za3lpa$ = function (node) {
var node_0 = node;
var left = this.left_za3lpa$(node_0);
if (left !== IntAVLTree$Companion_getInstance().NIL) {
return this.last_za3lpa$(left);
}
else {
var parent = this.parent_za3lpa$(node_0);
while (parent !== IntAVLTree$Companion_getInstance().NIL && node_0 === this.left_za3lpa$(parent)) {
node_0 = parent;
parent = this.parent_za3lpa$(parent);
}
return parent;
}
};
IntAVLTree.prototype.add = function () {
if (this.troot_0 === IntAVLTree$Companion_getInstance().NIL) {
this.troot_0 = this.nodeAllocator_0.newNode_8be2vx$();
this.copy_za3lpa$(this.troot_0);
this.fixAggregates_za3lpa$(this.troot_0);
return true;
}
else {
var node = this.troot_0;
mpassert(this.parent_za3lpa$(this.troot_0) === IntAVLTree$Companion_getInstance().NIL);
var parent;
var cmp;
do {
cmp = this.compare_za3lpa$(node);
if (cmp < 0) {
parent = node;
node = this.left_za3lpa$(node);
}
else if (cmp > 0) {
parent = node;
node = this.right_za3lpa$(node);
}
else {
this.merge_za3lpa$(node);
return false;
}
}
while (node !== IntAVLTre