Skip to content
Snippets Groups Projects
Commit c0144d82 authored by Alexander Rose's avatar Alexander Rose
Browse files

SortedArray: add .isRange, improve .areEqual

parent de3e819b
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ export const ofBounds = I.ofBounds;
export function ofSortedArray(xs: Nums): OrderedSetImpl {
if (!xs.length) return Empty;
// check if the array is just a range
if (xs[xs.length - 1] - xs[0] + 1 === xs.length) return I.ofRange(xs[0], xs[xs.length - 1]);
if (S.isRange(xs)) return I.ofRange(xs[0], xs[xs.length - 1]);
return xs as any;
}
......
......@@ -22,6 +22,7 @@ export function ofRange(min: number, max: number) {
return ret;
}
export function is(xs: any): xs is Nums { return xs && (Array.isArray(xs) || !!xs.buffer); }
export function isRange(xs: Nums) { return xs[xs.length - 1] - xs[0] + 1 === xs.length; }
export function start(xs: Nums) { return xs[0]; }
export function end(xs: Nums) { return xs[xs.length - 1] + 1; }
......@@ -61,6 +62,7 @@ export function areEqual(a: Nums, b: Nums) {
if (a === b) return true;
const aSize = a.length;
if (aSize !== b.length || a[0] !== b[0] || a[aSize - 1] !== b[aSize - 1]) return false;
if (isRange(a)) return true;
for (let i = 0; i < aSize; i++) {
if (a[i] !== b[i]) return false;
}
......@@ -340,7 +342,7 @@ export function deduplicate(xs: Nums) {
}
export function indicesOf(a: Nums, b: Nums): Nums {
if (a === b) return ofSortedArray(createRangeArray(0, a.length - 1));
if (areEqual(a, b)) return ofSortedArray(createRangeArray(0, a.length - 1));
const { startI: sI, startJ: sJ, endI, endJ } = getSuitableIntersectionRange(a, b);
let i = sI, j = sJ;
......
......@@ -17,6 +17,7 @@ namespace SortedArray {
/** create sorted array [min, max) (it does NOT contain the max value) */
export const ofBounds: <T extends number = number>(min: T, max: T) => SortedArray<T> = (min, max) => Impl.ofRange(min, max - 1) as any;
export const is: <T extends number = number>(v: any) => v is SortedArray<T> = Impl.is as any;
export const isRange: <T extends number = number>(array: ArrayLike<number>) => boolean = Impl.isRange as any;
export const has: <T extends number = number>(array: SortedArray<T>, x: T) => boolean = Impl.has as any;
/** Returns the index of `x` in `set` or -1 if not found. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment