sorted-array.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Copyright (c) 2017 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 has: <T extends number = number>(array: SortedArray<T>, x: T) => boolean = Impl.has as any;
  19. /** Returns the index of `x` in `set` or -1 if not found. */
  20. export const indexOf: <T extends number = number>(array: SortedArray<T>, x: T) => number = Impl.indexOf as any;
  21. export const indexOfInInterval: <T extends number = number>(array: SortedArray<T>, x: number, bounds: Interval) => number = Impl.indexOfInInterval as any;
  22. // array[0]
  23. export const start: <T extends number = number>(array: SortedArray<T>) => T = Impl.start as any;
  24. // array[array.length - 1] + 1
  25. export const end: <T extends number = number>(array: SortedArray<T>) => T = Impl.end as any;
  26. export const min: <T extends number = number>(array: SortedArray<T>) => T = Impl.min as any;
  27. export const max: <T extends number = number>(array: SortedArray<T>) => T = Impl.max as any;
  28. export const size: <T extends number = number>(array: SortedArray<T>) => number = Impl.size as any;
  29. export const hashCode: <T extends number = number>(array: SortedArray<T>) => number = Impl.hashCode as any;
  30. export const areEqual: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areEqual as any;
  31. export const areIntersecting: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.areIntersecting as any;
  32. export const isSubset: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => boolean = Impl.isSubset as any;
  33. export const union: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => SortedArray<T> = Impl.union as any;
  34. export const intersect: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => SortedArray<T> = Impl.intersect as any;
  35. export const subtract: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => SortedArray<T> = Impl.subtract as any;
  36. export const findPredecessorIndex: <T extends number = number>(array: SortedArray<T>, x: T) => number = Impl.findPredecessorIndex as any;
  37. export const findPredecessorIndexInInterval: <T extends number = number>(array: SortedArray<T>, x: T, bounds: Interval) => number = Impl.findPredecessorIndexInInterval as any;
  38. export const findRange: <T extends number = number>(array: SortedArray<T>, min: T, max: T) => Interval = Impl.findRange as any;
  39. export const intersectionSize: <T extends number = number>(a: SortedArray<T>, b: SortedArray<T>) => number = Impl.intersectionSize as any;
  40. export const deduplicate: <T extends number = number>(array: SortedArray<T>) => SortedArray<T> = Impl.deduplicate as any;
  41. /** Returns indices of xs in the array. E.g. indicesOf([10, 11, 12], [10, 12]) ==> [0, 2] */
  42. export const indicesOf: <T extends number = number, I extends number = number>(array: SortedArray<T>, xs: SortedArray<T>) => SortedArray<I> = Impl.indicesOf as any;
  43. }
  44. interface SortedArray<T extends number = number> extends ArrayLike<T> { '@type': 'int-sorted-array' }
  45. export default SortedArray