splice-buffer.d.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Some of the internal operations of micromark do lots of editing
  3. * operations on very large arrays. This runs into problems with two
  4. * properties of most circa-2020 JavaScript interpreters:
  5. *
  6. * - Array-length modifications at the high end of an array (push/pop) are
  7. * expected to be common and are implemented in (amortized) time
  8. * proportional to the number of elements added or removed, whereas
  9. * other operations (shift/unshift and splice) are much less efficient.
  10. * - Function arguments are passed on the stack, so adding tens of thousands
  11. * of elements to an array with `arr.push(...newElements)` will frequently
  12. * cause stack overflows. (see <https://stackoverflow.com/questions/22123769/rangeerror-maximum-call-stack-size-exceeded-why>)
  13. *
  14. * SpliceBuffers are an implementation of gap buffers, which are a
  15. * generalization of the "queue made of two stacks" idea. The splice buffer
  16. * maintains a cursor, and moving the cursor has cost proportional to the
  17. * distance the cursor moves, but inserting, deleting, or splicing in
  18. * new information at the cursor is as efficient as the push/pop operation.
  19. * This allows for an efficient sequence of splices (or pushes, pops, shifts,
  20. * or unshifts) as long such edits happen at the same part of the array or
  21. * generally sweep through the array from the beginning to the end.
  22. *
  23. * The interface for splice buffers also supports large numbers of inputs by
  24. * passing a single array argument rather passing multiple arguments on the
  25. * function call stack.
  26. *
  27. * @template T
  28. * Item type.
  29. */
  30. export class SpliceBuffer<T> {
  31. /**
  32. * @param {ReadonlyArray<T> | null | undefined} [initial]
  33. * Initial items (optional).
  34. * @returns
  35. * Splice buffer.
  36. */
  37. constructor(initial?: ReadonlyArray<T> | null | undefined);
  38. /** @type {Array<T>} */
  39. left: Array<T>;
  40. /** @type {Array<T>} */
  41. right: Array<T>;
  42. /**
  43. * Array access;
  44. * does not move the cursor.
  45. *
  46. * @param {number} index
  47. * Index.
  48. * @return {T}
  49. * Item.
  50. */
  51. get(index: number): T;
  52. /**
  53. * The length of the splice buffer, one greater than the largest index in the
  54. * array.
  55. */
  56. get length(): number;
  57. /**
  58. * Remove and return `list[0]`;
  59. * moves the cursor to `0`.
  60. *
  61. * @returns {T | undefined}
  62. * Item, optional.
  63. */
  64. shift(): T | undefined;
  65. /**
  66. * Slice the buffer to get an array;
  67. * does not move the cursor.
  68. *
  69. * @param {number} start
  70. * Start.
  71. * @param {number | null | undefined} [end]
  72. * End (optional).
  73. * @returns {Array<T>}
  74. * Array of items.
  75. */
  76. slice(start: number, end?: number | null | undefined): Array<T>;
  77. /**
  78. * Mimics the behavior of Array.prototype.splice() except for the change of
  79. * interface necessary to avoid segfaults when patching in very large arrays.
  80. *
  81. * This operation moves cursor is moved to `start` and results in the cursor
  82. * placed after any inserted items.
  83. *
  84. * @param {number} start
  85. * Start;
  86. * zero-based index at which to start changing the array;
  87. * negative numbers count backwards from the end of the array and values
  88. * that are out-of bounds are clamped to the appropriate end of the array.
  89. * @param {number | null | undefined} [deleteCount=0]
  90. * Delete count (default: `0`);
  91. * maximum number of elements to delete, starting from start.
  92. * @param {Array<T> | null | undefined} [items=[]]
  93. * Items to include in place of the deleted items (default: `[]`).
  94. * @return {Array<T>}
  95. * Any removed items.
  96. */
  97. splice(start: number, deleteCount?: number | null | undefined, items?: Array<T> | null | undefined): Array<T>;
  98. /**
  99. * Remove and return the highest-numbered item in the array, so
  100. * `list[list.length - 1]`;
  101. * Moves the cursor to `length`.
  102. *
  103. * @returns {T | undefined}
  104. * Item, optional.
  105. */
  106. pop(): T | undefined;
  107. /**
  108. * Inserts a single item to the high-numbered side of the array;
  109. * moves the cursor to `length`.
  110. *
  111. * @param {T} item
  112. * Item.
  113. * @returns {undefined}
  114. * Nothing.
  115. */
  116. push(item: T): undefined;
  117. /**
  118. * Inserts many items to the high-numbered side of the array.
  119. * Moves the cursor to `length`.
  120. *
  121. * @param {Array<T>} items
  122. * Items.
  123. * @returns {undefined}
  124. * Nothing.
  125. */
  126. pushMany(items: Array<T>): undefined;
  127. /**
  128. * Inserts a single item to the low-numbered side of the array;
  129. * Moves the cursor to `0`.
  130. *
  131. * @param {T} item
  132. * Item.
  133. * @returns {undefined}
  134. * Nothing.
  135. */
  136. unshift(item: T): undefined;
  137. /**
  138. * Inserts many items to the low-numbered side of the array;
  139. * moves the cursor to `0`.
  140. *
  141. * @param {Array<T>} items
  142. * Items.
  143. * @returns {undefined}
  144. * Nothing.
  145. */
  146. unshiftMany(items: Array<T>): undefined;
  147. /**
  148. * Move the cursor to a specific position in the array. Requires
  149. * time proportional to the distance moved.
  150. *
  151. * If `n < 0`, the cursor will end up at the beginning.
  152. * If `n > length`, the cursor will end up at the end.
  153. *
  154. * @param {number} n
  155. * Position.
  156. * @return {undefined}
  157. * Nothing.
  158. */
  159. setCursor(n: number): undefined;
  160. }
  161. //# sourceMappingURL=splice-buffer.d.ts.map