PackedArrayContext.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. "use strict";
  2. /*
  3. * This is a TypeScript port of the original Java version, which was written by
  4. * Gil Tene as described in
  5. * https://github.com/HdrHistogram/HdrHistogram
  6. * and released to the public domain, as explained at
  7. * http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.PackedArrayContext = exports.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY = void 0;
  11. /**
  12. * A packed-value, sparse array context used for storing 64 bit signed values.
  13. *
  14. * An array context is optimised for tracking sparsely set (as in mostly zeros) values that tend to not make
  15. * use pof the full 64 bit value range even when they are non-zero. The array context's internal representation
  16. * is such that the packed value at each virtual array index may be represented by 0-8 bytes of actual storage.
  17. *
  18. * An array context encodes the packed values in 8 "set trees" with each set tree representing one byte of the
  19. * packed value at the virtual index in question. The {@link #getPackedIndex(int, int, boolean)} method is used
  20. * to look up the byte-index corresponding to the given (set tree) value byte of the given virtual index, and can
  21. * be used to add entries to represent that byte as needed. As a succesful {@link #getPackedIndex(int, int, boolean)}
  22. * may require a resizing of the array, it can throw a {@link ResizeException} to indicate that the requested
  23. * packed index cannot be found or added without a resize of the physical storage.
  24. *
  25. */
  26. exports.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY = 16;
  27. const MAX_SUPPORTED_PACKED_COUNTS_ARRAY_LENGTH = Math.pow(2, 13) - 1; //(Short.MAX_VALUE / 4); TODO ALEX why ???
  28. const SET_0_START_INDEX = 0;
  29. const NUMBER_OF_SETS = 8;
  30. const LEAF_LEVEL_SHIFT = 3;
  31. const NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET = 0;
  32. const NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS = 1;
  33. const PACKED_ARRAY_GROWTH_INCREMENT = 16;
  34. const PACKED_ARRAY_GROWTH_FRACTION_POW2 = 4;
  35. const { pow, ceil, log2, max } = Math;
  36. const bitCount = (n) => {
  37. var bits = 0;
  38. while (n !== 0) {
  39. bits += bitCount32(n | 0);
  40. n /= 0x100000000;
  41. }
  42. return bits;
  43. };
  44. const bitCount32 = (n) => {
  45. n = n - ((n >> 1) & 0x55555555);
  46. n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
  47. return (((n + (n >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
  48. };
  49. class PackedArrayContext {
  50. constructor(virtualLength, initialPhysicalLength) {
  51. this.populatedShortLength = 0;
  52. this.topLevelShift = Number.MAX_VALUE; // Make it non-sensical until properly initialized.
  53. this.physicalLength = Math.max(initialPhysicalLength, exports.MINIMUM_INITIAL_PACKED_ARRAY_CAPACITY);
  54. this.isPacked =
  55. this.physicalLength <= MAX_SUPPORTED_PACKED_COUNTS_ARRAY_LENGTH;
  56. if (!this.isPacked) {
  57. this.physicalLength = virtualLength;
  58. }
  59. this.array = new ArrayBuffer(this.physicalLength * 8);
  60. this.initArrayViews(this.array);
  61. this.init(virtualLength);
  62. }
  63. initArrayViews(array) {
  64. this.byteArray = new Uint8Array(array);
  65. this.shortArray = new Uint16Array(array);
  66. this.longArray = new Float64Array(array);
  67. }
  68. init(virtualLength) {
  69. if (!this.isPacked) {
  70. // Deal with non-packed context init:
  71. this.virtualLength = virtualLength;
  72. return;
  73. }
  74. this.populatedShortLength = SET_0_START_INDEX + 8;
  75. // Populate empty root entries, and point to them from the root indexes:
  76. for (let i = 0; i < NUMBER_OF_SETS; i++) {
  77. this.setAtShortIndex(SET_0_START_INDEX + i, 0);
  78. }
  79. this.setVirtualLength(virtualLength);
  80. }
  81. clear() {
  82. this.byteArray.fill(0);
  83. this.init(this.virtualLength);
  84. }
  85. copyAndIncreaseSize(newPhysicalArrayLength, newVirtualArrayLength) {
  86. const ctx = new PackedArrayContext(newVirtualArrayLength, newPhysicalArrayLength);
  87. if (this.isPacked) {
  88. ctx.populateEquivalentEntriesWithEntriesFromOther(this);
  89. }
  90. return ctx;
  91. }
  92. getPopulatedShortLength() {
  93. return this.populatedShortLength;
  94. }
  95. getPopulatedLongLength() {
  96. return (this.getPopulatedShortLength() + 3) >> 2; // round up
  97. }
  98. setAtByteIndex(byteIndex, value) {
  99. this.byteArray[byteIndex] = value;
  100. }
  101. getAtByteIndex(byteIndex) {
  102. return this.byteArray[byteIndex];
  103. }
  104. /**
  105. * add a byte value to a current byte value in the array
  106. * @param byteIndex index of byte value to add to
  107. * @param valueToAdd byte value to add
  108. * @return the afterAddValue. ((afterAddValue & 0x100) != 0) indicates a carry.
  109. */
  110. addAtByteIndex(byteIndex, valueToAdd) {
  111. const newValue = this.byteArray[byteIndex] + valueToAdd;
  112. this.byteArray[byteIndex] = newValue;
  113. return newValue;
  114. }
  115. setPopulatedLongLength(newPopulatedLongLength) {
  116. this.populatedShortLength = newPopulatedLongLength << 2;
  117. }
  118. getVirtualLength() {
  119. return this.virtualLength;
  120. }
  121. length() {
  122. return this.physicalLength;
  123. }
  124. setAtShortIndex(shortIndex, value) {
  125. this.shortArray[shortIndex] = value;
  126. }
  127. setAtLongIndex(longIndex, value) {
  128. this.longArray[longIndex] = value;
  129. }
  130. getAtShortIndex(shortIndex) {
  131. return this.shortArray[shortIndex];
  132. }
  133. getIndexAtShortIndex(shortIndex) {
  134. return this.shortArray[shortIndex];
  135. }
  136. setPackedSlotIndicators(entryIndex, newPackedSlotIndicators) {
  137. this.setAtShortIndex(entryIndex + NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET, newPackedSlotIndicators);
  138. }
  139. getPackedSlotIndicators(entryIndex) {
  140. return (this.shortArray[entryIndex + NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET] &
  141. 0xffff);
  142. }
  143. getIndexAtEntrySlot(entryIndex, slot) {
  144. return this.getAtShortIndex(entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + slot);
  145. }
  146. setIndexAtEntrySlot(entryIndex, slot, newIndexValue) {
  147. this.setAtShortIndex(entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + slot, newIndexValue);
  148. }
  149. expandArrayIfNeeded(entryLengthInLongs) {
  150. const currentLength = this.length();
  151. if (currentLength < this.getPopulatedLongLength() + entryLengthInLongs) {
  152. const growthIncrement = max(entryLengthInLongs, PACKED_ARRAY_GROWTH_INCREMENT, this.getPopulatedLongLength() >> PACKED_ARRAY_GROWTH_FRACTION_POW2);
  153. this.resizeArray(currentLength + growthIncrement);
  154. }
  155. }
  156. newEntry(entryLengthInShorts) {
  157. // Add entry at the end of the array:
  158. const newEntryIndex = this.populatedShortLength;
  159. this.expandArrayIfNeeded((entryLengthInShorts >> 2) + 1);
  160. this.populatedShortLength = newEntryIndex + entryLengthInShorts;
  161. for (let i = 0; i < entryLengthInShorts; i++) {
  162. this.setAtShortIndex(newEntryIndex + i, -1); // Poison value -1. Must be overriden before reads
  163. }
  164. return newEntryIndex;
  165. }
  166. newLeafEntry() {
  167. // Add entry at the end of the array:
  168. let newEntryIndex;
  169. newEntryIndex = this.getPopulatedLongLength();
  170. this.expandArrayIfNeeded(1);
  171. this.setPopulatedLongLength(newEntryIndex + 1);
  172. this.setAtLongIndex(newEntryIndex, 0);
  173. return newEntryIndex;
  174. }
  175. /**
  176. * Consolidate entry with previous entry verison if one exists
  177. *
  178. * @param entryIndex The shortIndex of the entry to be consolidated
  179. * @param previousVersionIndex the index of the previous version of the entry
  180. */
  181. consolidateEntry(entryIndex, previousVersionIndex) {
  182. const previousVersionPackedSlotsIndicators = this.getPackedSlotIndicators(previousVersionIndex);
  183. // Previous version exists, needs consolidation
  184. const packedSlotsIndicators = this.getPackedSlotIndicators(entryIndex);
  185. const insertedSlotMask = packedSlotsIndicators ^ previousVersionPackedSlotsIndicators; // the only bit that differs
  186. const slotsBelowBitNumber = packedSlotsIndicators & (insertedSlotMask - 1);
  187. const insertedSlotIndex = bitCount(slotsBelowBitNumber);
  188. const numberOfSlotsInEntry = bitCount(packedSlotsIndicators);
  189. // Copy the entry slots from previous version, skipping the newly inserted slot in the target:
  190. let sourceSlot = 0;
  191. for (let targetSlot = 0; targetSlot < numberOfSlotsInEntry; targetSlot++) {
  192. if (targetSlot !== insertedSlotIndex) {
  193. const indexAtSlot = this.getIndexAtEntrySlot(previousVersionIndex, sourceSlot);
  194. if (indexAtSlot !== 0) {
  195. this.setIndexAtEntrySlot(entryIndex, targetSlot, indexAtSlot);
  196. }
  197. sourceSlot++;
  198. }
  199. }
  200. }
  201. /**
  202. * Expand entry as indicated.
  203. *
  204. * @param existingEntryIndex the index of the entry
  205. * @param entryPointerIndex index to the slot pointing to the entry (needs to be fixed up)
  206. * @param insertedSlotIndex realtive [packed] index of slot being inserted into entry
  207. * @param insertedSlotMask mask value fo slot being inserted
  208. * @param nextLevelIsLeaf the level below this one is a leaf level
  209. * @return the updated index of the entry (-1 if epansion failed due to conflict)
  210. * @throws RetryException if expansion fails due to concurrent conflict, and caller should try again.
  211. */
  212. expandEntry(existingEntryIndex, entryPointerIndex, insertedSlotIndex, insertedSlotMask, nextLevelIsLeaf) {
  213. let packedSlotIndicators = this.getAtShortIndex(existingEntryIndex) & 0xffff;
  214. packedSlotIndicators |= insertedSlotMask;
  215. const numberOfslotsInExpandedEntry = bitCount(packedSlotIndicators);
  216. if (insertedSlotIndex >= numberOfslotsInExpandedEntry) {
  217. throw new Error("inserted slot index is out of range given provided masks");
  218. }
  219. const expandedEntryLength = numberOfslotsInExpandedEntry + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS;
  220. // Create new next-level entry to refer to from slot at this level:
  221. let indexOfNewNextLevelEntry = 0;
  222. if (nextLevelIsLeaf) {
  223. indexOfNewNextLevelEntry = this.newLeafEntry(); // Establish long-index to new leaf entry
  224. }
  225. else {
  226. // TODO: Optimize this by creating the whole sub-tree here, rather than a step that will immediaterly expand
  227. // Create a new 1 word (empty, no slots set) entry for the next level:
  228. indexOfNewNextLevelEntry = this.newEntry(NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS); // Establish short-index to new leaf entry
  229. this.setPackedSlotIndicators(indexOfNewNextLevelEntry, 0);
  230. }
  231. const insertedSlotValue = indexOfNewNextLevelEntry;
  232. const expandedEntryIndex = this.newEntry(expandedEntryLength);
  233. // populate the packed indicators word:
  234. this.setPackedSlotIndicators(expandedEntryIndex, packedSlotIndicators);
  235. // Populate the inserted slot with the index of the new next level entry:
  236. this.setIndexAtEntrySlot(expandedEntryIndex, insertedSlotIndex, insertedSlotValue);
  237. this.setAtShortIndex(entryPointerIndex, expandedEntryIndex);
  238. this.consolidateEntry(expandedEntryIndex, existingEntryIndex);
  239. return expandedEntryIndex;
  240. }
  241. //
  242. // ###### ######## ######## ## ## ### ## ## #### ## ## ######## ######## ## ##
  243. // ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ## ##
  244. // ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ## ##
  245. // ## #### ###### ## ## ## ## ## ## ## ## ## ## ## ## ## ###### ###
  246. // ## ## ## ## ## ## ######### ## ## ## ## #### ## ## ## ## ##
  247. // ## ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ##
  248. // ###### ######## ## ### ## ## ######## ## #### ## ## ######## ######## ## ##
  249. //
  250. getRootEntry(setNumber, insertAsNeeded = false) {
  251. const entryPointerIndex = SET_0_START_INDEX + setNumber;
  252. let entryIndex = this.getIndexAtShortIndex(entryPointerIndex);
  253. if (entryIndex == 0) {
  254. if (!insertAsNeeded) {
  255. return 0; // Index does not currently exist in packed array;
  256. }
  257. entryIndex = this.newEntry(NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS);
  258. // Create a new empty (no slots set) entry for the next level:
  259. this.setPackedSlotIndicators(entryIndex, 0);
  260. this.setAtShortIndex(entryPointerIndex, entryIndex);
  261. }
  262. return entryIndex;
  263. }
  264. /**
  265. * Get the byte-index (into the packed array) corresponding to a given (set tree) value byte of given virtual index.
  266. * Inserts new set tree nodes as needed if indicated.
  267. *
  268. * @param setNumber The set tree number (0-7, 0 corresponding with the LSByte set tree)
  269. * @param virtualIndex The virtual index into the PackedArray
  270. * @param insertAsNeeded If true, will insert new set tree nodes as needed if they do not already exist
  271. * @return the byte-index corresponding to the given (set tree) value byte of the given virtual index
  272. */
  273. getPackedIndex(setNumber, virtualIndex, insertAsNeeded) {
  274. if (virtualIndex >= this.virtualLength) {
  275. throw new Error(`Attempting access at index ${virtualIndex}, beyond virtualLength ${this.virtualLength}`);
  276. }
  277. let entryPointerIndex = SET_0_START_INDEX + setNumber; // TODO init needed ?
  278. let entryIndex = this.getRootEntry(setNumber, insertAsNeeded);
  279. if (entryIndex == 0) {
  280. return -1; // Index does not currently exist in packed array;
  281. }
  282. // Work down the levels of non-leaf entries:
  283. for (let indexShift = this.topLevelShift; indexShift >= LEAF_LEVEL_SHIFT; indexShift -= 4) {
  284. const nextLevelIsLeaf = indexShift === LEAF_LEVEL_SHIFT;
  285. // Target is a packedSlotIndicators entry
  286. const packedSlotIndicators = this.getPackedSlotIndicators(entryIndex);
  287. const slotBitNumber = (virtualIndex / pow(2, indexShift)) & 0xf; //(virtualIndex >>> indexShift) & 0xf;
  288. const slotMask = 1 << slotBitNumber;
  289. const slotsBelowBitNumber = packedSlotIndicators & (slotMask - 1);
  290. const slotNumber = bitCount(slotsBelowBitNumber);
  291. if ((packedSlotIndicators & slotMask) === 0) {
  292. // The entryIndex slot does not have the contents we want
  293. if (!insertAsNeeded) {
  294. return -1; // Index does not currently exist in packed array;
  295. }
  296. // Expand the entry, adding the index to new entry at the proper slot:
  297. entryIndex = this.expandEntry(entryIndex, entryPointerIndex, slotNumber, slotMask, nextLevelIsLeaf);
  298. }
  299. // Next level's entry pointer index is in the appropriate slot in in the entries array in this entry:
  300. entryPointerIndex =
  301. entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + slotNumber;
  302. entryIndex = this.getIndexAtShortIndex(entryPointerIndex);
  303. }
  304. // entryIndex is the long-index of a leaf entry that contains the value byte for the given set
  305. const byteIndex = (entryIndex << 3) + (virtualIndex & 0x7); // Determine byte index offset within leaf entry
  306. return byteIndex;
  307. }
  308. determineTopLevelShiftForVirtualLength(virtualLength) {
  309. const sizeMagnitude = ceil(log2(virtualLength));
  310. const eightsSizeMagnitude = sizeMagnitude - 3;
  311. let multipleOfFourSizeMagnitude = ceil(eightsSizeMagnitude / 4) * 4;
  312. multipleOfFourSizeMagnitude = max(multipleOfFourSizeMagnitude, 8);
  313. const topLevelShiftNeeded = multipleOfFourSizeMagnitude - 4 + 3;
  314. return topLevelShiftNeeded;
  315. }
  316. setVirtualLength(virtualLength) {
  317. if (!this.isPacked) {
  318. throw new Error("Should never be adjusting the virtual size of a non-packed context");
  319. }
  320. this.topLevelShift = this.determineTopLevelShiftForVirtualLength(virtualLength);
  321. this.virtualLength = virtualLength;
  322. }
  323. getTopLevelShift() {
  324. return this.topLevelShift;
  325. }
  326. //
  327. // ## ## ######## ####### ######## ## ## ## ### ######## ########
  328. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  329. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  330. // ### ####### ######## ## ## ######## ## ## ## ## ## ## ######
  331. // ## ## ## ## ## ## ## ## ## ######### ## ##
  332. // ## ## ## ## ## ## ## ## ## ## ## ## ##
  333. // ## ## ## ####### ## ####### ######## ## ## ## ########
  334. //
  335. resizeArray(newLength) {
  336. const tmp = new Uint8Array(newLength * 8);
  337. tmp.set(this.byteArray);
  338. this.array = tmp.buffer;
  339. this.initArrayViews(this.array);
  340. this.physicalLength = newLength;
  341. }
  342. populateEquivalentEntriesWithEntriesFromOther(other) {
  343. if (this.virtualLength < other.getVirtualLength()) {
  344. throw new Error("Cannot populate array of smaller virtual length");
  345. }
  346. for (let i = 0; i < NUMBER_OF_SETS; i++) {
  347. const otherEntryIndex = other.getAtShortIndex(SET_0_START_INDEX + i);
  348. if (otherEntryIndex == 0)
  349. continue; // No tree to duplicate
  350. let entryIndexPointer = SET_0_START_INDEX + i;
  351. for (let i = this.topLevelShift; i > other.topLevelShift; i -= 4) {
  352. // for each inserted level:
  353. // Allocate entry in other:
  354. const sizeOfEntry = NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + 1;
  355. const newEntryIndex = this.newEntry(sizeOfEntry);
  356. // Link new level in.
  357. this.setAtShortIndex(entryIndexPointer, newEntryIndex);
  358. // Populate new level entry, use pointer to slot 0 as place to populate under:
  359. this.setPackedSlotIndicators(newEntryIndex, 0x1); // Slot 0 populated
  360. entryIndexPointer =
  361. newEntryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS; // Where the slot 0 index goes.
  362. }
  363. this.copyEntriesAtLevelFromOther(other, otherEntryIndex, entryIndexPointer, other.topLevelShift);
  364. }
  365. }
  366. copyEntriesAtLevelFromOther(other, otherLevelEntryIndex, levelEntryIndexPointer, otherIndexShift) {
  367. const nextLevelIsLeaf = otherIndexShift == LEAF_LEVEL_SHIFT;
  368. const packedSlotIndicators = other.getPackedSlotIndicators(otherLevelEntryIndex);
  369. const numberOfSlots = bitCount(packedSlotIndicators);
  370. const sizeOfEntry = NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + numberOfSlots;
  371. const entryIndex = this.newEntry(sizeOfEntry);
  372. this.setAtShortIndex(levelEntryIndexPointer, entryIndex);
  373. this.setAtShortIndex(entryIndex + NON_LEAF_ENTRY_SLOT_INDICATORS_OFFSET, packedSlotIndicators);
  374. for (let i = 0; i < numberOfSlots; i++) {
  375. if (nextLevelIsLeaf) {
  376. // Make leaf in other:
  377. const leafEntryIndex = this.newLeafEntry();
  378. this.setIndexAtEntrySlot(entryIndex, i, leafEntryIndex);
  379. // OPTIM
  380. // avoid iteration on all the values of the source ctx
  381. const otherNextLevelEntryIndex = other.getIndexAtEntrySlot(otherLevelEntryIndex, i);
  382. this.longArray[leafEntryIndex] =
  383. other.longArray[otherNextLevelEntryIndex];
  384. }
  385. else {
  386. const otherNextLevelEntryIndex = other.getIndexAtEntrySlot(otherLevelEntryIndex, i);
  387. this.copyEntriesAtLevelFromOther(other, otherNextLevelEntryIndex, entryIndex + NON_LEAF_ENTRY_HEADER_SIZE_IN_SHORTS + i, otherIndexShift - 4);
  388. }
  389. }
  390. }
  391. getAtUnpackedIndex(index) {
  392. return this.longArray[index];
  393. }
  394. setAtUnpackedIndex(index, newValue) {
  395. this.longArray[index] = newValue;
  396. }
  397. lazysetAtUnpackedIndex(index, newValue) {
  398. this.longArray[index] = newValue;
  399. }
  400. incrementAndGetAtUnpackedIndex(index) {
  401. this.longArray[index]++;
  402. return this.longArray[index];
  403. }
  404. addAndGetAtUnpackedIndex(index, valueToAdd) {
  405. this.longArray[index] += valueToAdd;
  406. return this.longArray[index];
  407. }
  408. //
  409. // ######## ####### ###### ######## ######## #### ## ## ######
  410. // ## ## ## ## ## ## ## ## ## ### ## ## ##
  411. // ## ## ## ## ## ## ## ## #### ## ##
  412. // ## ## ## ####### ###### ## ######## ## ## ## ## ## ####
  413. // ## ## ## ## ## ## ## ## ## #### ## ##
  414. // ## ## ## ## ## ## ## ## ## ## ### ## ##
  415. // ## ####### ###### ## ## ## #### ## ## ######
  416. //
  417. nonLeafEntryToString(entryIndex, indexShift, indentLevel) {
  418. let output = "";
  419. for (let i = 0; i < indentLevel; i++) {
  420. output += " ";
  421. }
  422. try {
  423. const packedSlotIndicators = this.getPackedSlotIndicators(entryIndex);
  424. output += `slotIndiators: 0x${toHex(packedSlotIndicators)}, prevVersionIndex: 0: [ `;
  425. const numberOfslotsInEntry = bitCount(packedSlotIndicators);
  426. for (let i = 0; i < numberOfslotsInEntry; i++) {
  427. output += this.getIndexAtEntrySlot(entryIndex, i);
  428. if (i < numberOfslotsInEntry - 1) {
  429. output += ", ";
  430. }
  431. }
  432. output += ` ] (indexShift = ${indexShift})\n`;
  433. const nextLevelIsLeaf = indexShift == LEAF_LEVEL_SHIFT;
  434. for (let i = 0; i < numberOfslotsInEntry; i++) {
  435. const nextLevelEntryIndex = this.getIndexAtEntrySlot(entryIndex, i);
  436. if (nextLevelIsLeaf) {
  437. output += this.leafEntryToString(nextLevelEntryIndex, indentLevel + 4);
  438. }
  439. else {
  440. output += this.nonLeafEntryToString(nextLevelEntryIndex, indexShift - 4, indentLevel + 4);
  441. }
  442. }
  443. }
  444. catch (ex) {
  445. output += `Exception thrown at nonLeafEnty at index ${entryIndex} with indexShift ${indexShift}\n`;
  446. }
  447. return output;
  448. }
  449. leafEntryToString(entryIndex, indentLevel) {
  450. let output = "";
  451. for (let i = 0; i < indentLevel; i++) {
  452. output += " ";
  453. }
  454. try {
  455. output += "Leaf bytes : ";
  456. for (let i = 0; i < 8; i++) {
  457. output += `0x${toHex(this.byteArray[entryIndex * 8 + i])} `;
  458. }
  459. output += "\n";
  460. }
  461. catch (ex) {
  462. output += `Exception thrown at leafEnty at index ${entryIndex}\n`;
  463. }
  464. return output;
  465. }
  466. toString() {
  467. let output = "PackedArrayContext:\n";
  468. if (!this.isPacked) {
  469. return output + "Context is unpacked:\n"; // unpackedToString();
  470. }
  471. for (let setNumber = 0; setNumber < NUMBER_OF_SETS; setNumber++) {
  472. try {
  473. const entryPointerIndex = SET_0_START_INDEX + setNumber;
  474. const entryIndex = this.getIndexAtShortIndex(entryPointerIndex);
  475. output += `Set ${setNumber}: root = ${entryIndex} \n`;
  476. if (entryIndex == 0)
  477. continue;
  478. output += this.nonLeafEntryToString(entryIndex, this.topLevelShift, 4);
  479. }
  480. catch (ex) {
  481. output += `Exception thrown in set ${setNumber}%d\n`;
  482. }
  483. }
  484. //output += recordedValuesToString();
  485. return output;
  486. }
  487. }
  488. exports.PackedArrayContext = PackedArrayContext;
  489. const toHex = (n) => {
  490. return Number(n)
  491. .toString(16)
  492. .padStart(2, "0");
  493. };
  494. //# sourceMappingURL=PackedArrayContext.js.map