1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 import { Vector } from '../vector';
19 import { MapRow, StructRow } from '../vector/row';
20 import { compareArrayLike } from '../util/buffer';
21 import { BigInt, BigIntAvailable } from './compat';
24 type RangeLike = { length: number; stride?: number };
26 type ClampThen<T extends RangeLike> = (source: T, index: number) => any;
28 type ClampRangeThen<T extends RangeLike> = (source: T, offset: number, length: number) => any;
30 export function clampIndex<T extends RangeLike>(source: T, index: number): number;
31 export function clampIndex<T extends RangeLike, N extends ClampThen<T> = ClampThen<T>>(source: T, index: number, then: N): ReturnType<N>;
33 export function clampIndex<T extends RangeLike, N extends ClampThen<T> = ClampThen<T>>(source: T, index: number, then?: N) {
34 const length = source.length;
35 const adjust = index > -1 ? index : (length + (index % length));
36 return then ? then(source, adjust) : adjust;
41 export function clampRange<T extends RangeLike>(source: T, begin: number | undefined, end: number | undefined): [number, number];
42 export function clampRange<T extends RangeLike, N extends ClampRangeThen<T> = ClampRangeThen<T>>(source: T, begin: number | undefined, end: number | undefined, then: N): ReturnType<N>;
44 export function clampRange<T extends RangeLike, N extends ClampRangeThen<T> = ClampRangeThen<T>>(source: T, begin: number | undefined, end: number | undefined, then?: N) {
46 // Adjust args similar to Array.prototype.slice. Normalize begin/end to
47 // clamp between 0 and length, and wrap around on negative indices, e.g.
48 // slice(-1, 5) or slice(5, -1)
49 const { length: len = 0 } = source;
50 let lhs = typeof begin !== 'number' ? 0 : begin;
51 let rhs = typeof end !== 'number' ? len : end;
52 // wrap around on negative start/end positions
53 (lhs < 0) && (lhs = ((lhs % len) + len) % len);
54 (rhs < 0) && (rhs = ((rhs % len) + len) % len);
56 (rhs < lhs) && (tmp = lhs, lhs = rhs, rhs = tmp);
57 // ensure rhs <= length
58 (rhs > len) && (rhs = len);
60 return then ? then(source, lhs, rhs) : [lhs, rhs];
63 const big0 = BigIntAvailable ? BigInt(0) : 0;
64 const isNaNFast = (value: any) => value !== value;
67 export function createElementComparator(search: any) {
68 const typeofSearch = typeof search;
70 if (typeofSearch !== 'object' || search === null) {
72 if (isNaNFast(search)) {
75 return typeofSearch !== 'bigint'
76 ? (value: any) => value === search
77 : (value: any) => (big0 + value) === search;
80 if (search instanceof Date) {
81 const valueOfSearch = search.valueOf();
82 return (value: any) => value instanceof Date ? (value.valueOf() === valueOfSearch) : false;
84 // Compare TypedArrays
85 if (ArrayBuffer.isView(search)) {
86 return (value: any) => value ? compareArrayLike(search, value) : false;
88 // Compare Maps and Rows
89 if (search instanceof Map) { return creatMapComparator(search); }
90 // Compare Array-likes
91 if (Array.isArray(search)) { return createArrayLikeComparator(search); }
93 if (search instanceof Vector) { return createVectorComparator(search); }
94 // Compare non-empty Objects
95 return createObjectComparator(search);
99 function createArrayLikeComparator(lhs: ArrayLike<any>) {
100 const comparators = [] as ((x: any) => boolean)[];
101 for (let i = -1, n = lhs.length; ++i < n;) {
102 comparators[i] = createElementComparator(lhs[i]);
104 return createSubElementsComparator(comparators);
108 function creatMapComparator(lhs: Map<any, any>) {
110 const comparators = [] as ((x: any) => boolean)[];
111 lhs.forEach((v) => comparators[++i] = createElementComparator(v));
112 return createSubElementsComparator(comparators);
116 function createVectorComparator(lhs: Vector<any>) {
117 const comparators = [] as ((x: any) => boolean)[];
118 for (let i = -1, n = lhs.length; ++i < n;) {
119 comparators[i] = createElementComparator(lhs.get(i));
121 return createSubElementsComparator(comparators);
125 function createObjectComparator(lhs: any) {
126 const keys = Object.keys(lhs);
127 // Only compare non-empty Objects
128 if (keys.length === 0) { return () => false; }
129 const comparators = [] as ((x: any) => boolean)[];
130 for (let i = -1, n = keys.length; ++i < n;) {
131 comparators[i] = createElementComparator(lhs[keys[i]]);
133 return createSubElementsComparator(comparators, keys);
136 function createSubElementsComparator(comparators: ((x: any) => boolean)[], keys?: Iterable<string>) {
137 return (rhs: any) => {
138 if (!rhs || typeof rhs !== 'object') {
141 switch (rhs.constructor) {
142 case Array: return compareArray(comparators, rhs);
146 return compareObject(comparators, rhs, rhs.keys());
148 case undefined: // support `Object.create(null)` objects
149 return compareObject(comparators, rhs, keys || Object.keys(rhs));
151 return rhs instanceof Vector ? compareVector(comparators, rhs) : false;
155 function compareArray(comparators: ((x: any) => boolean)[], arr: any[]) {
156 const n = comparators.length;
157 if (arr.length !== n) { return false; }
158 for (let i = -1; ++i < n;) {
159 if (!(comparators[i](arr[i]))) { return false; }
164 function compareVector(comparators: ((x: any) => boolean)[], vec: Vector) {
165 const n = comparators.length;
166 if (vec.length !== n) { return false; }
167 for (let i = -1; ++i < n;) {
168 if (!(comparators[i](vec.get(i)))) { return false; }
173 function compareObject(comparators: ((x: any) => boolean)[], obj: Map<any, any>, keys: Iterable<string>) {
175 const lKeyItr = keys[Symbol.iterator]();
176 const rKeyItr = obj instanceof Map ? obj.keys() : Object.keys(obj)[Symbol.iterator]();
177 const rValItr = obj instanceof Map ? obj.values() : Object.values(obj)[Symbol.iterator]();
180 const n = comparators.length;
181 let rVal = rValItr.next();
182 let lKey = lKeyItr.next();
183 let rKey = rKeyItr.next();
185 for (; i < n && !lKey.done && !rKey.done && !rVal.done;
186 ++i, lKey = lKeyItr.next(), rKey = rKeyItr.next(), rVal = rValItr.next()) {
187 if (lKey.value !== rKey.value || !comparators[i](rVal.value)) {
191 if (i === n && lKey.done && rKey.done && rVal.done) {
194 lKeyItr.return && lKeyItr.return();
195 rKeyItr.return && rKeyItr.return();
196 rValItr.return && rValItr.return();