sorted-array.ts 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as Impl from './impl/sorted-array';
  7. import { Interval } from './interval';
  8. namespace SortedArray {
  9. export const Empty: SortedArray = Impl.Empty as any;
  10. export const ofUnsortedArray: <T extends number = number>(xs: ArrayLike<number>) => SortedArray<T> = Impl.ofUnsortedArray as any;
  11. export const ofSingleton: <T extends number = number>(v: number) => SortedArray<T> = Impl.ofSingleton as any;
  12. export const ofSortedArray: <T extends number = number>(xs: ArrayLike<number>) => SortedArray<T> = Impl.ofSortedArray as any;
  13. /** create sorted array [min, max] (it DOES contain the max value) */
  14. export const ofRange: <T extends number = number>(min: T, max: T) => SortedArray<T> = Impl.ofRange as any;
  15. /** create sorted array [min, max) (it does NOT contain the max value) */
  16. export const ofBounds: <T extends number = number>(min: T, max: T) => SortedArray<T> = (min, max) => Impl.ofRange(min, max - 1) as any;
  17. export const is: <T extends number = number>(v: any) => v is SortedArray<T> = Impl.is as any;
  18. export const isRange: <T extends number = number>(array: ArrayLike<number>) => boolean = Impl.isRange as any;
  19. export const has: <T extends number = number>(array: SortedArray<T>, x: T) => boolean = Impl.has as any;
  20. /** Returns the index of `x` in `set` or -1 if not found. */
  21. export const indexOf: <T extends number = number>(array: SortedArray<T>, x: T) => number = Impl.indexOf as any;
  22. export const indexOfInInterval: <T extends number = number>(array: SortedArray<T>, x: number, bounds: Interval) => number = Impl.indexOfInInterval as any;
  23. export const indexOfInRange: <T extends number = number>(array: SortedArray<T>, x: number, start: number, end: number) => number = Impl.indexOfInRange as any;
  24. /** Returns `array[0]` */
  25. export const start: <T extends number = number>(array: SortedArray<T>) => T = Impl.start as any;
  26. /** Returns `array[array.length - 1] + 1` */
  27. export const end: <T extends number = number>(array: SortedArray<T>) => T = Impl.end as any;
  28. export const min: <T extends number = number>(array: SortedArray<T>) => T = Impl.min as any;
  29. export const max: <T extends number = number>(array: SortedArray<T>) => T = Impl.max as any;
  30. export const size: <T extends number = number>(array: SortedArray<T>) => number = Impl.size as any;
  31. export const hashCode: <T extends number = number>(array: SortedArray<T>) => number = Impl.hashCode as any;
  32. export const toString: <T extends number = number>(array: SortedArray<T>) => string = Impl.toString as any;
  33. export const areEqual: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areEqual as any;
  34. export const areIntersecting: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areIntersecting as any;
  35. export const isSubset: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.isSubset as any;
  36. export const union: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => SortedArray<T> = Impl.union as any;
  37. export const intersect: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => SortedArray<T> = Impl.intersect as any;
  38. export const subtract: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => SortedArray<T> = Impl.subtract as any;
  39. export const findPredecessorIndex: <T extends number = number>(array: SortedArray<T>, x: T) => number = Impl.findPredecessorIndex as any;
  40. export const findPredecessorIndexInInterval: <T extends number = number>(array: SortedArray<T>, x: T, bounds: Interval) => number = Impl.findPredecessorIndexInInterval as any;
  41. export const findRange: <T extends number = number>(array: SortedArray<T>, min: T, max: T) => Interval = Impl.findRange as any;
  42. export const intersectionSize: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => number = Impl.intersectionSize as any;
  43. export const deduplicate: <T extends number = number>(array: SortedArray<T>) => SortedArray<T> = Impl.deduplicate as any;
  44. /** Returns indices of xs in the array. E.g. indicesOf([10, 11, 12], [10, 12]) ==> [0, 2] */
  45. export const indicesOf: <T extends number = number, I extends number = number>(array: SortedArray<T>, xs: SortedArray<T>) => SortedArray<I> = Impl.indicesOf as any;
  46. }
  47. interface SortedArray<T extends number = number> extends ArrayLike<T> { '@type': 'int-sorted-array' }
  48. export { SortedArray };