Updated the files.

This commit is contained in:
Batuhan Berk Başoğlu 2024-02-08 19:38:41 -05:00
parent 1553e6b971
commit 753967d4f5
23418 changed files with 3784666 additions and 0 deletions

View file

@ -0,0 +1,55 @@
/**
* A Packed array of signed 64 bit values, and supports {@link #get get()}, {@link #set set()},
* {@link #add add()} and {@link #increment increment()} operations on the logical contents of the array.
*
* An {@link PackedLongArray} Uses {@link PackedArrayContext} to track
* the array's logical contents. Contexts may be switched when a context requires resizing
* to complete logical array operations (get, set, add, increment). Contexts are
* established and used within critical sections in order to facilitate concurrent
* implementors.
*
*/
export declare class PackedArray {
private arrayContext;
constructor(virtualLength: number, initialPhysicalLength?: number);
setVirtualLength(newVirtualArrayLength: number): void;
/**
* Get value at virtual index in the array
* @param index the virtual array index
* @return the array value at the virtual index given
*/
get(index: number): number;
/**
* Increment value at a virrual index in the array
* @param index virtual index of value to increment
*/
increment(index: number): void;
private safeGetPackedIndexgetPackedIndex;
/**
* Add to a value at a virtual index in the array
* @param index the virtual index of the value to be added to
* @param value the value to add
*/
add(index: number, value: number): void;
/**
* Set the value at a virtual index in the array
* @param index the virtual index of the value to set
* @param value the value to set
*/
set(index: number, value: number): void;
/**
* Get the current physical length (in longs) of the array's backing storage
* @return the current physical length (in longs) of the array's current backing storage
*/
getPhysicalLength(): number;
/**
* Get the (virtual) length of the array
* @return the (virtual) length of the array
*/
length(): number;
/**
* Clear the array contents
*/
clear(): void;
toString(): string;
}

View file

@ -0,0 +1 @@
export {};

View file

@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*
* This is a TypeScript port of the original Java version, which was written by
* Gil Tene as described in
* https://github.com/HdrHistogram/HdrHistogram
* and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const fc = require("fast-check");
const PackedArray_1 = require("./PackedArray");
const runFromStryker = __dirname.includes("stryker");
const runnerOptions = {
numRuns: runFromStryker ? 10 : 1000,
verbose: true
};
describe("Packed array", () => {
it("should store data as a regular sparse array", () => {
const SIZE = 1000;
fc.assert(fc.property(arbData(SIZE), entries => {
const packedArray = new PackedArray_1.PackedArray(SIZE + 1);
const sparseArray = new Array();
entries.forEach(([index, value]) => packedArray.add(index, value));
entries.forEach(([index, value]) => {
if (sparseArray[index]) {
sparseArray[index] = sparseArray[index] + value;
}
else {
sparseArray[index] = value;
}
});
return entries.every(([index]) => sparseArray[index] === packedArray.get(index));
}), runnerOptions);
});
});
const arbData = (size) => fc.array(fc.tuple(fc.integer(1, size), fc.integer(1, 100000000)), 1, size);
//# sourceMappingURL=PackedArray.fc.spec.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"PackedArray.fc.spec.js","sourceRoot":"","sources":["../../src/packedarray/PackedArray.fc.spec.ts"],"names":[],"mappings":";;AAAA;;;;;;GAMG;AACH,iCAAiC;AACjC,+CAA4C;AAE5C,MAAM,cAAc,GAAG,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;AAErD,MAAM,aAAa,GAAG;IACpB,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI;IACnC,OAAO,EAAE,IAAI;CACd,CAAC;AAEF,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,IAAI,GAAG,IAAI,CAAC;QAClB,EAAE,CAAC,MAAM,CACP,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,EAAE;YACnC,MAAM,WAAW,GAAG,IAAI,yBAAW,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;YAC9C,MAAM,WAAW,GAAG,IAAI,KAAK,EAAE,CAAC;YAChC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;YACnE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE;gBACjC,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;oBACtB,WAAW,CAAC,KAAK,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;iBACjD;qBAAM;oBACL,WAAW,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;iBAC5B;YACH,CAAC,CAAC,CAAC;YACH,OAAO,OAAO,CAAC,KAAK,CAClB,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,KAAK,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAC3D,CAAC;QACJ,CAAC,CAAC,EACF,aAAa,CACd,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE,CAC/B,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC"}

View file

@ -0,0 +1,181 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PackedArray = void 0;
/*
* This is a TypeScript port of the original Java version, which was written by
* Gil Tene as described in
* https://github.com/HdrHistogram/HdrHistogram
* and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const PackedArrayContext_1 = require("./PackedArrayContext");
const NUMBER_OF_SETS = 8;
const { pow, floor } = Math;
/**
* A Packed array of signed 64 bit values, and supports {@link #get get()}, {@link #set set()},
* {@link #add add()} and {@link #increment increment()} operations on the logical contents of the array.
*
* An {@link PackedLongArray} Uses {@link PackedArrayContext} to track
* the array's logical contents. Contexts may be switched when a context requires resizing
* to complete logical array operations (get, set, add, increment). Contexts are
* established and used within critical sections in order to facilitate concurrent
* implementors.
*
*/
class PackedArray {
constructor(virtualLength, initialPhysicalLength = PackedArrayContext_1.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY) {
this.arrayContext = new PackedArrayContext_1.PackedArrayContext(virtualLength, initialPhysicalLength);
}
setVirtualLength(newVirtualArrayLength) {
if (newVirtualArrayLength < this.length()) {
throw new Error("Cannot set virtual length, as requested length " +
newVirtualArrayLength +
" is smaller than the current virtual length " +
this.length());
}
const currentArrayContext = this.arrayContext;
if (currentArrayContext.isPacked &&
currentArrayContext.determineTopLevelShiftForVirtualLength(newVirtualArrayLength) == currentArrayContext.getTopLevelShift()) {
// No changes to the array context contents is needed. Just change the virtual length.
currentArrayContext.setVirtualLength(newVirtualArrayLength);
return;
}
this.arrayContext = currentArrayContext.copyAndIncreaseSize(this.getPhysicalLength(), newVirtualArrayLength);
}
/**
* Get value at virtual index in the array
* @param index the virtual array index
* @return the array value at the virtual index given
*/
get(index) {
let value = 0;
for (let byteNum = 0; byteNum < NUMBER_OF_SETS; byteNum++) {
let byteValueAtPackedIndex = 0;
// Deal with unpacked context:
if (!this.arrayContext.isPacked) {
return this.arrayContext.getAtUnpackedIndex(index);
}
// Context is packed:
const packedIndex = this.arrayContext.getPackedIndex(byteNum, index, false);
if (packedIndex < 0) {
return value;
}
byteValueAtPackedIndex =
this.arrayContext.getAtByteIndex(packedIndex) * pow(2, byteNum << 3);
value += byteValueAtPackedIndex;
}
return value;
}
/**
* Increment value at a virrual index in the array
* @param index virtual index of value to increment
*/
increment(index) {
this.add(index, 1);
}
safeGetPackedIndexgetPackedIndex(setNumber, virtualIndex) {
//do {
//try {
return this.arrayContext.getPackedIndex(setNumber, virtualIndex, true);
/*} catch (ex) {
if (ex instanceof ResizeError) {
this.arrayContext.resizeArray(ex.newSize);
} else {
throw ex;
}
}*/
//} while (true);
}
/**
* Add to a value at a virtual index in the array
* @param index the virtual index of the value to be added to
* @param value the value to add
*/
add(index, value) {
let remainingValueToAdd = value;
for (let byteNum = 0, byteShift = 0; byteNum < NUMBER_OF_SETS; byteNum++, byteShift += 8) {
// Deal with unpacked context:
if (!this.arrayContext.isPacked) {
this.arrayContext.addAndGetAtUnpackedIndex(index, value);
return;
}
// Context is packed:
const packedIndex = this.safeGetPackedIndexgetPackedIndex(byteNum, index);
const byteToAdd = remainingValueToAdd & 0xff;
const afterAddByteValue = this.arrayContext.addAtByteIndex(packedIndex, byteToAdd);
// Reduce remaining value to add by amount just added:
remainingValueToAdd -= byteToAdd;
remainingValueToAdd = remainingValueToAdd / pow(2, 8);
// Account for carry:
remainingValueToAdd += floor(afterAddByteValue / pow(2, 8));
if (remainingValueToAdd == 0) {
return; // nothing to add to higher magnitudes
}
}
}
/**
* Set the value at a virtual index in the array
* @param index the virtual index of the value to set
* @param value the value to set
*/
set(index, value) {
let bytesAlreadySet = 0;
let valueForNextLevels = value;
for (let byteNum = 0; byteNum < NUMBER_OF_SETS; byteNum++) {
// Establish context within: critical section
// Deal with unpacked context:
if (!this.arrayContext.isPacked) {
this.arrayContext.setAtUnpackedIndex(index, value);
return;
}
// Context is packed:
if (valueForNextLevels == 0) {
// Special-case zeros to avoid inflating packed array for no reason
const packedIndex = this.arrayContext.getPackedIndex(byteNum, index, false);
if (packedIndex < 0) {
return; // no need to create entries for zero values if they don't already exist
}
}
// Make sure byte is populated:
const packedIndex = this.arrayContext.getPackedIndex(byteNum, index, true);
// Determine value to write, and prepare for next levels
const byteToWrite = valueForNextLevels & 0xff;
valueForNextLevels = floor(valueForNextLevels / pow(2, 8));
if (byteNum < bytesAlreadySet) {
// We want to avoid writing to the same byte twice when not doing so for the
// entire 64 bit value atomically, as doing so opens a race with e.g. concurrent
// adders. So dobn't actually write the byte if has been written before.
continue;
}
this.arrayContext.setAtByteIndex(packedIndex, byteToWrite);
bytesAlreadySet++;
}
}
/**
* Get the current physical length (in longs) of the array's backing storage
* @return the current physical length (in longs) of the array's current backing storage
*/
getPhysicalLength() {
return this.arrayContext.physicalLength;
}
/**
* Get the (virtual) length of the array
* @return the (virtual) length of the array
*/
length() {
return this.arrayContext.getVirtualLength();
}
/**
* Clear the array contents
*/
clear() {
this.arrayContext.clear();
}
toString() {
let output = "PackedArray:\n";
output += this.arrayContext.toString();
return output;
}
}
exports.PackedArray = PackedArray;
//# sourceMappingURL=PackedArray.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"PackedArray.js","sourceRoot":"","sources":["../../src/packedarray/PackedArray.ts"],"names":[],"mappings":"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"}

View file

@ -0,0 +1 @@
export {};

View file

@ -0,0 +1,142 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*
* This is a TypeScript port of the original Java version, which was written by
* Gil Tene as described in
* https://github.com/HdrHistogram/HdrHistogram
* and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const PackedArrayContext_1 = require("./PackedArrayContext");
const PackedArray_1 = require("./PackedArray");
const { pow } = Math;
describe("Packed array context", () => {
it("Should initialize array", () => {
const ctx = new PackedArrayContext_1.PackedArrayContext(1024, 128);
expect(ctx.isPacked).toBe(true);
expect(ctx.getPopulatedShortLength()).toBeGreaterThan(0);
});
});
describe("Packed array", () => {
it("Should initialize array", () => {
const array = new PackedArray_1.PackedArray(1024, 128);
expect(array.getPhysicalLength()).toBe(128);
expect(array.length()).toBe(1024);
});
it("Should retrieve data stored in array", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
// when
array.set(16, 1);
array.set(12, 42);
// then
expect(array.get(12)).toBe(42);
expect(array.get(16)).toBe(1);
});
it("Should resize array when storing data", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
// when
array.set(12, 361);
// then
const storedData = array.get(12);
expect(storedData).toBe(361);
});
it("Should retrieve big numbers stored in array", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
// when
array.set(12, Math.pow(2, 16) + 1);
// then
const storedData = array.get(12);
expect(storedData).toBe(Math.pow(2, 16) + 1);
});
it("Should copy data when resizing array", () => {
const array = new PackedArray_1.PackedArray(1024);
for (let value = 1; value <= 272; value++) {
array.set(value, value);
}
expect(array.get(1)).toBe(1);
expect(array.get(255)).toBe(255);
expect(array.get(272)).toBe(272);
});
it("Should increment data stored in array", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
array.set(16, 1);
// when
array.add(16, 41);
// then
expect(array.get(16)).toBe(42);
});
it("Should increment data stored in array with big numbers", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
array.set(16, 42);
// when
array.add(16, pow(2, 33));
// then
expect(array.get(16)).toBe(pow(2, 33) + 42);
});
it("Should increment data stored in array with big numbers when a resize is needed", () => {
// given
const array = new PackedArray_1.PackedArray(10000, 16);
array.set(6144, 243);
array.set(60, 243);
array.set(1160, 243);
// when
array.add(6144, 25);
// then
expect(array.get(6144)).toBe(268);
});
it("Should increment data stored in array with big numbers", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
array.set(16, 42);
// when
array.add(16, pow(2, 33));
// then
expect(array.get(16)).toBe(pow(2, 33) + 42);
});
it("Should clear data stored in array", () => {
// given
const array = new PackedArray_1.PackedArray(1024, 16);
array.set(16, 42);
// when
array.clear();
// then
expect(array.get(16)).toBe(0);
});
it("Should resize array when virtual length change", () => {
// given
const array = new PackedArray_1.PackedArray(16, 16);
array.set(7, 42);
// when
array.setVirtualLength(pow(2, 20));
array.add(pow(2, 19), 42);
// then
expect(array.get(7)).toBe(42);
expect(array.get(pow(2, 19))).toBe(42);
});
it("should handle properly big numbers", () => {
// given
const array = new PackedArray_1.PackedArray(45056, 16);
// when
array.set(32768, 1);
// then
expect(array.get(32768)).toBe(1);
expect(array.get(0)).toBe(0);
});
});
describe("Unpacked array", () => {
it("Should increment data stored in array", () => {
// given
const array = new PackedArray_1.PackedArray(1024, pow(2, 20));
array.set(16, 1);
// when
array.add(16, 41);
// then
expect(array.get(16)).toBe(42);
});
});
//# sourceMappingURL=PackedArray.spec.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,101 @@
/**
* A packed-value, sparse array context used for storing 64 bit signed values.
*
* An array context is optimised for tracking sparsely set (as in mostly zeros) values that tend to not make
* use pof the full 64 bit value range even when they are non-zero. The array context's internal representation
* is such that the packed value at each virtual array index may be represented by 0-8 bytes of actual storage.
*
* An array context encodes the packed values in 8 "set trees" with each set tree representing one byte of the
* packed value at the virtual index in question. The {@link #getPackedIndex(int, int, boolean)} method is used
* to look up the byte-index corresponding to the given (set tree) value byte of the given virtual index, and can
* be used to add entries to represent that byte as needed. As a succesful {@link #getPackedIndex(int, int, boolean)}
* may require a resizing of the array, it can throw a {@link ResizeException} to indicate that the requested
* packed index cannot be found or added without a resize of the physical storage.
*
*/
export declare const MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY = 16;
export declare class PackedArrayContext {
readonly isPacked: boolean;
physicalLength: number;
private array;
private byteArray;
private shortArray;
private longArray;
private populatedShortLength;
private virtualLength;
private topLevelShift;
constructor(virtualLength: number, initialPhysicalLength: number);
private initArrayViews;
private init;
clear(): void;
copyAndIncreaseSize(newPhysicalArrayLength: number, newVirtualArrayLength: number): PackedArrayContext;
getPopulatedShortLength(): number;
getPopulatedLongLength(): number;
setAtByteIndex(byteIndex: number, value: number): void;
getAtByteIndex(byteIndex: number): number;
/**
* add a byte value to a current byte value in the array
* @param byteIndex index of byte value to add to
* @param valueToAdd byte value to add
* @return the afterAddValue. ((afterAddValue & 0x100) != 0) indicates a carry.
*/
addAtByteIndex(byteIndex: number, valueToAdd: number): number;
setPopulatedLongLength(newPopulatedLongLength: number): void;
getVirtualLength(): number;
length(): number;
setAtShortIndex(shortIndex: number, value: number): void;
setAtLongIndex(longIndex: number, value: number): void;
getAtShortIndex(shortIndex: number): number;
getIndexAtShortIndex(shortIndex: number): number;
setPackedSlotIndicators(entryIndex: number, newPackedSlotIndicators: number): void;
getPackedSlotIndicators(entryIndex: number): number;
private getIndexAtEntrySlot;
setIndexAtEntrySlot(entryIndex: number, slot: number, newIndexValue: number): void;
private expandArrayIfNeeded;
private newEntry;
private newLeafEntry;
/**
* Consolidate entry with previous entry verison if one exists
*
* @param entryIndex The shortIndex of the entry to be consolidated
* @param previousVersionIndex the index of the previous version of the entry
*/
private consolidateEntry;
/**
* Expand entry as indicated.
*
* @param existingEntryIndex the index of the entry
* @param entryPointerIndex index to the slot pointing to the entry (needs to be fixed up)
* @param insertedSlotIndex realtive [packed] index of slot being inserted into entry
* @param insertedSlotMask mask value fo slot being inserted
* @param nextLevelIsLeaf the level below this one is a leaf level
* @return the updated index of the entry (-1 if epansion failed due to conflict)
* @throws RetryException if expansion fails due to concurrent conflict, and caller should try again.
*/
expandEntry(existingEntryIndex: number, entryPointerIndex: number, insertedSlotIndex: number, insertedSlotMask: number, nextLevelIsLeaf: boolean): number;
getRootEntry(setNumber: number, insertAsNeeded?: boolean): number;
/**
* Get the byte-index (into the packed array) corresponding to a given (set tree) value byte of given virtual index.
* Inserts new set tree nodes as needed if indicated.
*
* @param setNumber The set tree number (0-7, 0 corresponding with the LSByte set tree)
* @param virtualIndex The virtual index into the PackedArray
* @param insertAsNeeded If true, will insert new set tree nodes as needed if they do not already exist
* @return the byte-index corresponding to the given (set tree) value byte of the given virtual index
*/
getPackedIndex(setNumber: number, virtualIndex: number, insertAsNeeded: boolean): number;
determineTopLevelShiftForVirtualLength(virtualLength: number): number;
setVirtualLength(virtualLength: number): void;
getTopLevelShift(): number;
resizeArray(newLength: number): void;
private populateEquivalentEntriesWithEntriesFromOther;
private copyEntriesAtLevelFromOther;
getAtUnpackedIndex(index: number): number;
setAtUnpackedIndex(index: number, newValue: number): void;
lazysetAtUnpackedIndex(index: number, newValue: number): void;
incrementAndGetAtUnpackedIndex(index: number): number;
addAndGetAtUnpackedIndex(index: number, valueToAdd: number): number;
private nonLeafEntryToString;
private leafEntryToString;
toString(): string;
}

View file

@ -0,0 +1,494 @@
"use strict";
/*
* This is a TypeScript port of the original Java version, which was written by
* Gil Tene as described in
* https://github.com/HdrHistogram/HdrHistogram
* and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PackedArrayContext = exports.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY = void 0;
/**
* A packed-value, sparse array context used for storing 64 bit signed values.
*
* An array context is optimised for tracking sparsely set (as in mostly zeros) values that tend to not make
* use pof the full 64 bit value range even when they are non-zero. The array context's internal representation
* is such that the packed value at each virtual array index may be represented by 0-8 bytes of actual storage.
*
* An array context encodes the packed values in 8 "set trees" with each set tree representing one byte of the
* packed value at the virtual index in question. The {@link #getPackedIndex(int, int, boolean)} method is used
* to look up the byte-index corresponding to the given (set tree) value byte of the given virtual index, and can
* be used to add entries to represent that byte as needed. As a succesful {@link #getPackedIndex(int, int, boolean)}
* may require a resizing of the array, it can throw a {@link ResizeException} to indicate that the requested
* packed index cannot be found or added without a resize of the physical storage.
*
*/
exports.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY = 16;
const MAX_SUPPORTED_PACKED_COUNTS_ARRAY_LENGTH = Math.pow(2, 13) - 1; //(Short.MAX_VALUE / 4); TODO ALEX why ???
const SET_0_START_INDEX = 0;
const NUMBER_OF_SETS = 8;
const LEAF_LEVEL_SHIFT = 3;
const NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET = 0;
const NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS = 1;
const PACKED_ARRAY_GROWTH_INCREMENT = 16;
const PACKED_ARRAY_GROWTH_FRACTION_POW2 = 4;
const { pow, ceil, log2, max } = Math;
const bitCount = (n) => {
var bits = 0;
while (n !== 0) {
bits += bitCount32(n | 0);
n /= 0x100000000;
}
return bits;
};
const bitCount32 = (n) => {
n = n - ((n >> 1) & 0x55555555);
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
return (((n + (n >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
};
class PackedArrayContext {
constructor(virtualLength, initialPhysicalLength) {
this.populatedShortLength = 0;
this.topLevelShift = Number.MAX_VALUE; // Make it non-sensical until properly initialized.
this.physicalLength = Math.max(initialPhysicalLength, exports.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY);
this.isPacked =
this.physicalLength <= MAX_SUPPORTED_PACKED_COUNTS_ARRAY_LENGTH;
if (!this.isPacked) {
this.physicalLength = virtualLength;
}
this.array = new ArrayBuffer(this.physicalLength * 8);
this.initArrayViews(this.array);
this.init(virtualLength);
}
initArrayViews(array) {
this.byteArray = new Uint8Array(array);
this.shortArray = new Uint16Array(array);
this.longArray = new Float64Array(array);
}
init(virtualLength) {
if (!this.isPacked) {
// Deal with non-packed context init:
this.virtualLength = virtualLength;
return;
}
this.populatedShortLength = SET_0_START_INDEX + 8;
// Populate empty root entries, and point to them from the root indexes:
for (let i = 0; i < NUMBER_OF_SETS; i++) {
this.setAtShortIndex(SET_0_START_INDEX + i, 0);
}
this.setVirtualLength(virtualLength);
}
clear() {
this.byteArray.fill(0);
this.init(this.virtualLength);
}
copyAndIncreaseSize(newPhysicalArrayLength, newVirtualArrayLength) {
const ctx = new PackedArrayContext(newVirtualArrayLength, newPhysicalArrayLength);
if (this.isPacked) {
ctx.populateEquivalentEntriesWithEntriesFromOther(this);
}
return ctx;
}
getPopulatedShortLength() {
return this.populatedShortLength;
}
getPopulatedLongLength() {
return (this.getPopulatedShortLength() + 3) >> 2; // round up
}
setAtByteIndex(byteIndex, value) {
this.byteArray[byteIndex] = value;
}
getAtByteIndex(byteIndex) {
return this.byteArray[byteIndex];
}
/**
* add a byte value to a current byte value in the array
* @param byteIndex index of byte value to add to
* @param valueToAdd byte value to add
* @return the afterAddValue. ((afterAddValue & 0x100) != 0) indicates a carry.
*/
addAtByteIndex(byteIndex, valueToAdd) {
const newValue = this.byteArray[byteIndex] + valueToAdd;
this.byteArray[byteIndex] = newValue;
return newValue;
}
setPopulatedLongLength(newPopulatedLongLength) {
this.populatedShortLength = newPopulatedLongLength << 2;
}
getVirtualLength() {
return this.virtualLength;
}
length() {
return this.physicalLength;
}
setAtShortIndex(shortIndex, value) {
this.shortArray[shortIndex] = value;
}
setAtLongIndex(longIndex, value) {
this.longArray[longIndex] = value;
}
getAtShortIndex(shortIndex) {
return this.shortArray[shortIndex];
}
getIndexAtShortIndex(shortIndex) {
return this.shortArray[shortIndex];
}
setPackedSlotIndicators(entryIndex, newPackedSlotIndicators) {
this.setAtShortIndex(entryIndex + NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET, newPackedSlotIndicators);
}
getPackedSlotIndicators(entryIndex) {
return (this.shortArray[entryIndex + NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET] &
0xffff);
}
getIndexAtEntrySlot(entryIndex, slot) {
return this.getAtShortIndex(entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + slot);
}
setIndexAtEntrySlot(entryIndex, slot, newIndexValue) {
this.setAtShortIndex(entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + slot, newIndexValue);
}
expandArrayIfNeeded(entryLengthInLongs) {
const currentLength = this.length();
if (currentLength < this.getPopulatedLongLength() + entryLengthInLongs) {
const growthIncrement = max(entryLengthInLongs, PACKED_ARRAY_GROWTH_INCREMENT, this.getPopulatedLongLength() >> PACKED_ARRAY_GROWTH_FRACTION_POW2);
this.resizeArray(currentLength + growthIncrement);
}
}
newEntry(entryLengthInShorts) {
// Add entry at the end of the array:
const newEntryIndex = this.populatedShortLength;
this.expandArrayIfNeeded((entryLengthInShorts >> 2) + 1);
this.populatedShortLength = newEntryIndex + entryLengthInShorts;
for (let i = 0; i < entryLengthInShorts; i++) {
this.setAtShortIndex(newEntryIndex + i, -1); // Poison value -1. Must be overriden before reads
}
return newEntryIndex;
}
newLeafEntry() {
// Add entry at the end of the array:
let newEntryIndex;
newEntryIndex = this.getPopulatedLongLength();
this.expandArrayIfNeeded(1);
this.setPopulatedLongLength(newEntryIndex + 1);
this.setAtLongIndex(newEntryIndex, 0);
return newEntryIndex;
}
/**
* Consolidate entry with previous entry verison if one exists
*
* @param entryIndex The shortIndex of the entry to be consolidated
* @param previousVersionIndex the index of the previous version of the entry
*/
consolidateEntry(entryIndex, previousVersionIndex) {
const previousVersionPackedSlotsIndicators = this.getPackedSlotIndicators(previousVersionIndex);
// Previous version exists, needs consolidation
const packedSlotsIndicators = this.getPackedSlotIndicators(entryIndex);
const insertedSlotMask = packedSlotsIndicators ^ previousVersionPackedSlotsIndicators; // the only bit that differs
const slotsBelowBitNumber = packedSlotsIndicators & (insertedSlotMask - 1);
const insertedSlotIndex = bitCount(slotsBelowBitNumber);
const numberOfSlotsInEntry = bitCount(packedSlotsIndicators);
// Copy the entry slots from previous version, skipping the newly inserted slot in the target:
let sourceSlot = 0;
for (let targetSlot = 0; targetSlot < numberOfSlotsInEntry; targetSlot++) {
if (targetSlot !== insertedSlotIndex) {
const indexAtSlot = this.getIndexAtEntrySlot(previousVersionIndex, sourceSlot);
if (indexAtSlot !== 0) {
this.setIndexAtEntrySlot(entryIndex, targetSlot, indexAtSlot);
}
sourceSlot++;
}
}
}
/**
* Expand entry as indicated.
*
* @param existingEntryIndex the index of the entry
* @param entryPointerIndex index to the slot pointing to the entry (needs to be fixed up)
* @param insertedSlotIndex realtive [packed] index of slot being inserted into entry
* @param insertedSlotMask mask value fo slot being inserted
* @param nextLevelIsLeaf the level below this one is a leaf level
* @return the updated index of the entry (-1 if epansion failed due to conflict)
* @throws RetryException if expansion fails due to concurrent conflict, and caller should try again.
*/
expandEntry(existingEntryIndex, entryPointerIndex, insertedSlotIndex, insertedSlotMask, nextLevelIsLeaf) {
let packedSlotIndicators = this.getAtShortIndex(existingEntryIndex) & 0xffff;
packedSlotIndicators |= insertedSlotMask;
const numberOfslotsInExpandedEntry = bitCount(packedSlotIndicators);
if (insertedSlotIndex >= numberOfslotsInExpandedEntry) {
throw new Error("inserted slot index is out of range given provided masks");
}
const expandedEntryLength = numberOfslotsInExpandedEntry + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS;
// Create new next-level entry to refer to from slot at this level:
let indexOfNewNextLevelEntry = 0;
if (nextLevelIsLeaf) {
indexOfNewNextLevelEntry = this.newLeafEntry(); // Establish long-index to new leaf entry
}
else {
// TODO: Optimize this by creating the whole sub-tree here, rather than a step that will immediaterly expand
// Create a new 1 word (empty, no slots set) entry for the next level:
indexOfNewNextLevelEntry = this.newEntry(NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS); // Establish short-index to new leaf entry
this.setPackedSlotIndicators(indexOfNewNextLevelEntry, 0);
}
const insertedSlotValue = indexOfNewNextLevelEntry;
const expandedEntryIndex = this.newEntry(expandedEntryLength);
// populate the packed indicators word:
this.setPackedSlotIndicators(expandedEntryIndex, packedSlotIndicators);
// Populate the inserted slot with the index of the new next level entry:
this.setIndexAtEntrySlot(expandedEntryIndex, insertedSlotIndex, insertedSlotValue);
this.setAtShortIndex(entryPointerIndex, expandedEntryIndex);
this.consolidateEntry(expandedEntryIndex, existingEntryIndex);
return expandedEntryIndex;
}
//
// ###### ######## ######## ## ## ### ## ## #### ## ## ######## ######## ## ##
// ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ## ##
// ## #### ###### ## ## ## ## ## ## ## ## ## ## ## ## ## ###### ###
// ## ## ## ## ## ## ######### ## ## ## ## #### ## ## ## ## ##
// ## ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ##
// ###### ######## ## ### ## ## ######## ## #### ## ## ######## ######## ## ##
//
getRootEntry(setNumber, insertAsNeeded = false) {
const entryPointerIndex = SET_0_START_INDEX + setNumber;
let entryIndex = this.getIndexAtShortIndex(entryPointerIndex);
if (entryIndex == 0) {
if (!insertAsNeeded) {
return 0; // Index does not currently exist in packed array;
}
entryIndex = this.newEntry(NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS);
// Create a new empty (no slots set) entry for the next level:
this.setPackedSlotIndicators(entryIndex, 0);
this.setAtShortIndex(entryPointerIndex, entryIndex);
}
return entryIndex;
}
/**
* Get the byte-index (into the packed array) corresponding to a given (set tree) value byte of given virtual index.
* Inserts new set tree nodes as needed if indicated.
*
* @param setNumber The set tree number (0-7, 0 corresponding with the LSByte set tree)
* @param virtualIndex The virtual index into the PackedArray
* @param insertAsNeeded If true, will insert new set tree nodes as needed if they do not already exist
* @return the byte-index corresponding to the given (set tree) value byte of the given virtual index
*/
getPackedIndex(setNumber, virtualIndex, insertAsNeeded) {
if (virtualIndex >= this.virtualLength) {
throw new Error(`Attempting access at index ${virtualIndex}, beyond virtualLength ${this.virtualLength}`);
}
let entryPointerIndex = SET_0_START_INDEX + setNumber; // TODO init needed ?
let entryIndex = this.getRootEntry(setNumber, insertAsNeeded);
if (entryIndex == 0) {
return -1; // Index does not currently exist in packed array;
}
// Work down the levels of non-leaf entries:
for (let indexShift = this.topLevelShift; indexShift >= LEAF_LEVEL_SHIFT; indexShift -= 4) {
const nextLevelIsLeaf = indexShift === LEAF_LEVEL_SHIFT;
// Target is a packedSlotIndicators entry
const packedSlotIndicators = this.getPackedSlotIndicators(entryIndex);
const slotBitNumber = (virtualIndex / pow(2, indexShift)) & 0xf; //(virtualIndex >>> indexShift) & 0xf;
const slotMask = 1 << slotBitNumber;
const slotsBelowBitNumber = packedSlotIndicators & (slotMask - 1);
const slotNumber = bitCount(slotsBelowBitNumber);
if ((packedSlotIndicators & slotMask) === 0) {
// The entryIndex slot does not have the contents we want
if (!insertAsNeeded) {
return -1; // Index does not currently exist in packed array;
}
// Expand the entry, adding the index to new entry at the proper slot:
entryIndex = this.expandEntry(entryIndex, entryPointerIndex, slotNumber, slotMask, nextLevelIsLeaf);
}
// Next level's entry pointer index is in the appropriate slot in in the entries array in this entry:
entryPointerIndex =
entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + slotNumber;
entryIndex = this.getIndexAtShortIndex(entryPointerIndex);
}
// entryIndex is the long-index of a leaf entry that contains the value byte for the given set
const byteIndex = (entryIndex << 3) + (virtualIndex & 0x7); // Determine byte index offset within leaf entry
return byteIndex;
}
determineTopLevelShiftForVirtualLength(virtualLength) {
const sizeMagnitude = ceil(log2(virtualLength));
const eightsSizeMagnitude = sizeMagnitude - 3;
let multipleOfFourSizeMagnitude = ceil(eightsSizeMagnitude / 4) * 4;
multipleOfFourSizeMagnitude = max(multipleOfFourSizeMagnitude, 8);
const topLevelShiftNeeded = multipleOfFourSizeMagnitude - 4 + 3;
return topLevelShiftNeeded;
}
setVirtualLength(virtualLength) {
if (!this.isPacked) {
throw new Error("Should never be adjusting the virtual size of a non-packed context");
}
this.topLevelShift = this.determineTopLevelShiftForVirtualLength(virtualLength);
this.virtualLength = virtualLength;
}
getTopLevelShift() {
return this.topLevelShift;
}
//
// ## ## ######## ####### ######## ## ## ## ### ######## ########
// ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
// ### ####### ######## ## ## ######## ## ## ## ## ## ## ######
// ## ## ## ## ## ## ## ## ## ######### ## ##
// ## ## ## ## ## ## ## ## ## ## ## ## ##
// ## ## ## ####### ## ####### ######## ## ## ## ########
//
resizeArray(newLength) {
const tmp = new Uint8Array(newLength * 8);
tmp.set(this.byteArray);
this.array = tmp.buffer;
this.initArrayViews(this.array);
this.physicalLength = newLength;
}
populateEquivalentEntriesWithEntriesFromOther(other) {
if (this.virtualLength < other.getVirtualLength()) {
throw new Error("Cannot populate array of smaller virtual length");
}
for (let i = 0; i < NUMBER_OF_SETS; i++) {
const otherEntryIndex = other.getAtShortIndex(SET_0_START_INDEX + i);
if (otherEntryIndex == 0)
continue; // No tree to duplicate
let entryIndexPointer = SET_0_START_INDEX + i;
for (let i = this.topLevelShift; i > other.topLevelShift; i -= 4) {
// for each inserted level:
// Allocate entry in other:
const sizeOfEntry = NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + 1;
const newEntryIndex = this.newEntry(sizeOfEntry);
// Link new level in.
this.setAtShortIndex(entryIndexPointer, newEntryIndex);
// Populate new level entry, use pointer to slot 0 as place to populate under:
this.setPackedSlotIndicators(newEntryIndex, 0x1); // Slot 0 populated
entryIndexPointer =
newEntryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS; // Where the slot 0 index goes.
}
this.copyEntriesAtLevelFromOther(other, otherEntryIndex, entryIndexPointer, other.topLevelShift);
}
}
copyEntriesAtLevelFromOther(other, otherLevelEntryIndex, levelEntryIndexPointer, otherIndexShift) {
const nextLevelIsLeaf = otherIndexShift == LEAF_LEVEL_SHIFT;
const packedSlotIndicators = other.getPackedSlotIndicators(otherLevelEntryIndex);
const numberOfSlots = bitCount(packedSlotIndicators);
const sizeOfEntry = NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + numberOfSlots;
const entryIndex = this.newEntry(sizeOfEntry);
this.setAtShortIndex(levelEntryIndexPointer, entryIndex);
this.setAtShortIndex(entryIndex + NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET, packedSlotIndicators);
for (let i = 0; i < numberOfSlots; i++) {
if (nextLevelIsLeaf) {
// Make leaf in other:
const leafEntryIndex = this.newLeafEntry();
this.setIndexAtEntrySlot(entryIndex, i, leafEntryIndex);
// OPTIM
// avoid iteration on all the values of the source ctx
const otherNextLevelEntryIndex = other.getIndexAtEntrySlot(otherLevelEntryIndex, i);
this.longArray[leafEntryIndex] =
other.longArray[otherNextLevelEntryIndex];
}
else {
const otherNextLevelEntryIndex = other.getIndexAtEntrySlot(otherLevelEntryIndex, i);
this.copyEntriesAtLevelFromOther(other, otherNextLevelEntryIndex, entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + i, otherIndexShift - 4);
}
}
}
getAtUnpackedIndex(index) {
return this.longArray[index];
}
setAtUnpackedIndex(index, newValue) {
this.longArray[index] = newValue;
}
lazysetAtUnpackedIndex(index, newValue) {
this.longArray[index] = newValue;
}
incrementAndGetAtUnpackedIndex(index) {
this.longArray[index]++;
return this.longArray[index];
}
addAndGetAtUnpackedIndex(index, valueToAdd) {
this.longArray[index] += valueToAdd;
return this.longArray[index];
}
//
// ######## ####### ###### ######## ######## #### ## ## ######
// ## ## ## ## ## ## ## ## ## ### ## ## ##
// ## ## ## ## ## ## ## ## #### ## ##
// ## ## ## ####### ###### ## ######## ## ## ## ## ## ####
// ## ## ## ## ## ## ## ## ## #### ## ##
// ## ## ## ## ## ## ## ## ## ## ### ## ##
// ## ####### ###### ## ## ## #### ## ## ######
//
nonLeafEntryToString(entryIndex, indexShift, indentLevel) {
let output = "";
for (let i = 0; i < indentLevel; i++) {
output += " ";
}
try {
const packedSlotIndicators = this.getPackedSlotIndicators(entryIndex);
output += `slotIndiators: 0x${toHex(packedSlotIndicators)}, prevVersionIndex: 0: [ `;
const numberOfslotsInEntry = bitCount(packedSlotIndicators);
for (let i = 0; i < numberOfslotsInEntry; i++) {
output += this.getIndexAtEntrySlot(entryIndex, i);
if (i < numberOfslotsInEntry - 1) {
output += ", ";
}
}
output += ` ] (indexShift = ${indexShift})\n`;
const nextLevelIsLeaf = indexShift == LEAF_LEVEL_SHIFT;
for (let i = 0; i < numberOfslotsInEntry; i++) {
const nextLevelEntryIndex = this.getIndexAtEntrySlot(entryIndex, i);
if (nextLevelIsLeaf) {
output += this.leafEntryToString(nextLevelEntryIndex, indentLevel + 4);
}
else {
output += this.nonLeafEntryToString(nextLevelEntryIndex, indexShift - 4, indentLevel + 4);
}
}
}
catch (ex) {
output += `Exception thrown at nonLeafEnty at index ${entryIndex} with indexShift ${indexShift}\n`;
}
return output;
}
leafEntryToString(entryIndex, indentLevel) {
let output = "";
for (let i = 0; i < indentLevel; i++) {
output += " ";
}
try {
output += "Leaf bytes : ";
for (let i = 0; i < 8; i++) {
output += `0x${toHex(this.byteArray[entryIndex * 8 + i])} `;
}
output += "\n";
}
catch (ex) {
output += `Exception thrown at leafEnty at index ${entryIndex}\n`;
}
return output;
}
toString() {
let output = "PackedArrayContext:\n";
if (!this.isPacked) {
return output + "Context is unpacked:\n"; // unpackedToString();
}
for (let setNumber = 0; setNumber < NUMBER_OF_SETS; setNumber++) {
try {
const entryPointerIndex = SET_0_START_INDEX + setNumber;
const entryIndex = this.getIndexAtShortIndex(entryPointerIndex);
output += `Set ${setNumber}: root = ${entryIndex} \n`;
if (entryIndex == 0)
continue;
output += this.nonLeafEntryToString(entryIndex, this.topLevelShift, 4);
}
catch (ex) {
output += `Exception thrown in set ${setNumber}%d\n`;
}
}
//output += recordedValuesToString();
return output;
}
}
exports.PackedArrayContext = PackedArrayContext;
const toHex = (n) => {
return Number(n)
.toString(16)
.padStart(2, "0");
};
//# sourceMappingURL=PackedArrayContext.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,4 @@
export declare class ResizeError {
newSize: number;
constructor(newSize: number);
}

View file

@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResizeError = void 0;
/*
* This is a TypeScript port of the original Java version, which was written by
* Gil Tene as described in
* https://github.com/HdrHistogram/HdrHistogram
* and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
class ResizeError {
constructor(newSize) {
this.newSize = newSize;
}
}
exports.ResizeError = ResizeError;
//# sourceMappingURL=ResizeError.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"ResizeError.js","sourceRoot":"","sources":["../../src/packedarray/ResizeError.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,MAAa,WAAW;IACtB,YAAmB,OAAe;QAAf,YAAO,GAAP,OAAO,CAAQ;IAAG,CAAC;CACvC;AAFD,kCAEC"}