@bazilio-san/af-stream
Version:
Data stream from database table
139 lines • 4.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findSlotFloat = exports.findSlotFloatBINARY = exports.findSlotFloatRIGHT = exports.findSlotFloatLEFT = exports.lte = exports.gte = exports.lt = exports.gt = exports.eq = void 0;
const eq = (a, b, SIGMA) => Math.abs(a - b) <= SIGMA;
exports.eq = eq;
const gt = (a, b, SIGMA) => a > b + SIGMA;
exports.gt = gt;
const lt = (a, b, SIGMA) => a < b - SIGMA;
exports.lt = lt;
const gte = (a, b, SIGMA) => a >= b - SIGMA;
exports.gte = gte;
const lte = (a, b, SIGMA) => a <= b + SIGMA;
exports.lte = lte;
const findSlotFloatLEFT = (arr, x, SIGMA) => {
const lastIndex = arr.length - 1;
if (lastIndex < 0) {
return [null, null, null];
}
const index = arr.findIndex((v) => (0, exports.gte)(v, x, SIGMA));
if (index > -1) {
if ((0, exports.eq)(arr[index], x, SIGMA)) {
return [
index === 0 ? null : index - 1,
index,
index === lastIndex ? null : index + 1,
];
} // x < arr[index]
return [
index === 0 ? null : index - 1,
null,
index,
];
}
// index === -1: X больше наибольшего
return [lastIndex, null, null];
};
exports.findSlotFloatLEFT = findSlotFloatLEFT;
const findSlotFloatRIGHT = (arr, x, SIGMA) => {
const { length } = arr;
if (!length) {
return [null, null, null];
}
let pos = length;
const lastIndex = length - 1;
while (--pos >= 0) {
const v = arr[pos];
if ((0, exports.eq)(v, x, SIGMA)) {
return [
pos === 0 ? null : pos - 1,
pos,
pos === lastIndex ? null : pos + 1,
];
}
if ((0, exports.lt)(v, x, SIGMA)) {
return [
pos,
null,
pos === lastIndex ? null : pos + 1,
];
}
}
return [null, null, 0];
};
exports.findSlotFloatRIGHT = findSlotFloatRIGHT;
const findSlotFloatBINARY = (arr, x, SIGMA) => {
const { length } = arr;
if (!length) {
return [null, null, null];
}
const lastIndex = length - 1;
let start = 0;
let end = length;
if ((0, exports.lt)(x, arr[0], SIGMA)) {
return [null, null, 0];
}
if ((0, exports.eq)(x, arr[0], SIGMA)) {
return [null, 0, length > 1 ? 1 : null];
}
if ((0, exports.gt)(x, arr[lastIndex], SIGMA)) {
return [lastIndex, null, null];
}
if ((0, exports.eq)(x, arr[lastIndex], SIGMA)) {
return [lastIndex ? lastIndex - 1 : null, lastIndex, null];
}
let mid = 0;
while (start <= end) {
mid = Math.floor((start + end) / 2);
if ((0, exports.eq)(arr[mid], x, SIGMA)) {
return [
mid === 0 ? null : mid - 1,
mid,
mid === length - 1 ? null : mid + 1,
];
}
if ((0, exports.gt)(arr[mid], x, SIGMA)) {
end = mid - 1;
}
else {
start = mid + 1;
}
}
if ((0, exports.lt)(arr[mid], x, SIGMA)) {
return [
mid,
null,
length > 1 ? mid + 1 : null,
];
}
return [
length > 1 ? mid - 1 : null,
null,
mid,
];
};
exports.findSlotFloatBINARY = findSlotFloatBINARY;
/**
* Searches for a place for a float number (x) in an array of float numbers (arr) sorted in ascending order.
* Returns an array of 3 indices:
[
<index of nearest smaller number | null>,
<index of number equal to desired | null>,
<index of nearest larger number | null>
]
Numbers are compared using SIGMA: if the numbers differ by less than SIGMA, then they are considered equal.
*/
const findSlotFloat = (arr, x, SIGMA) => {
const { length } = arr;
if (!length) {
return [null, null, null];
}
if (length > 2000) {
return (0, exports.findSlotFloatBINARY)(arr, x, SIGMA);
}
return ((arr[length - 1] - arr[0]) / 2) < x
? (0, exports.findSlotFloatRIGHT)(arr, x, SIGMA)
: (0, exports.findSlotFloatLEFT)(arr, x, SIGMA);
};
exports.findSlotFloat = findSlotFloat;
//# sourceMappingURL=find-slot-float.js.map