binary-search.d.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. import type { SourceMapSegment, ReverseSegment } from './sourcemap-segment';
  2. export type MemoState = {
  3. lastKey: number;
  4. lastNeedle: number;
  5. lastIndex: number;
  6. };
  7. export declare let found: boolean;
  8. /**
  9. * A binary search implementation that returns the index if a match is found.
  10. * If no match is found, then the left-index (the index associated with the item that comes just
  11. * before the desired index) is returned. To maintain proper sort order, a splice would happen at
  12. * the next index:
  13. *
  14. * ```js
  15. * const array = [1, 3];
  16. * const needle = 2;
  17. * const index = binarySearch(array, needle, (item, needle) => item - needle);
  18. *
  19. * assert.equal(index, 0);
  20. * array.splice(index + 1, 0, needle);
  21. * assert.deepEqual(array, [1, 2, 3]);
  22. * ```
  23. */
  24. export declare function binarySearch(haystack: SourceMapSegment[] | ReverseSegment[], needle: number, low: number, high: number): number;
  25. export declare function upperBound(haystack: SourceMapSegment[] | ReverseSegment[], needle: number, index: number): number;
  26. export declare function lowerBound(haystack: SourceMapSegment[] | ReverseSegment[], needle: number, index: number): number;
  27. export declare function memoizedState(): MemoState;
  28. /**
  29. * This overly complicated beast is just to record the last tested line/column and the resulting
  30. * index, allowing us to skip a few tests if mappings are monotonically increasing.
  31. */
  32. export declare function memoizedBinarySearch(haystack: SourceMapSegment[] | ReverseSegment[], needle: number, state: MemoState, key: number): number;