]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/js/src/visitor/indexof.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / visitor / indexof.ts
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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
16 // under the License.
17
18 import { Data } from '../data';
19 import { Type } from '../enum';
20 import { Visitor } from '../visitor';
21 import { VectorType } from '../interfaces';
22 import { getBool, BitIterator } from '../util/bit';
23 import { createElementComparator } from '../util/vector';
24 import {
25 DataType, Dictionary,
26 Bool, Null, Utf8, Binary, Decimal, FixedSizeBinary, List, FixedSizeList, Map_, Struct,
27 Float, Float16, Float32, Float64,
28 Int, Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64,
29 Date_, DateDay, DateMillisecond,
30 Interval, IntervalDayTime, IntervalYearMonth,
31 Time, TimeSecond, TimeMillisecond, TimeMicrosecond, TimeNanosecond,
32 Timestamp, TimestampSecond, TimestampMillisecond, TimestampMicrosecond, TimestampNanosecond,
33 Union, DenseUnion, SparseUnion,
34 } from '../type';
35
36 /** @ignore */
37 export interface IndexOfVisitor extends Visitor {
38 visit<T extends VectorType> (node: T, value: T['TValue'] | null, index?: number): number;
39 visitMany <T extends VectorType> (nodes: T[], values: (T['TValue'] | null)[], indices: (number | undefined)[]): number[];
40 getVisitFn<T extends Type> (node: T): (vector: VectorType<T>, value: VectorType<T>['TValue'] | null, index?: number) => number;
41 getVisitFn<T extends DataType>(node: VectorType<T> | Data<T> | T): (vector: VectorType<T>, value: T['TValue'] | null, index?: number) => number;
42 visitNull <T extends Null> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
43 visitBool <T extends Bool> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
44 visitInt <T extends Int> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
45 visitInt8 <T extends Int8> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
46 visitInt16 <T extends Int16> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
47 visitInt32 <T extends Int32> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
48 visitInt64 <T extends Int64> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
49 visitUint8 <T extends Uint8> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
50 visitUint16 <T extends Uint16> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
51 visitUint32 <T extends Uint32> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
52 visitUint64 <T extends Uint64> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
53 visitFloat <T extends Float> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
54 visitFloat16 <T extends Float16> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
55 visitFloat32 <T extends Float32> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
56 visitFloat64 <T extends Float64> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
57 visitUtf8 <T extends Utf8> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
58 visitBinary <T extends Binary> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
59 visitFixedSizeBinary <T extends FixedSizeBinary> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
60 visitDate <T extends Date_> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
61 visitDateDay <T extends DateDay> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
62 visitDateMillisecond <T extends DateMillisecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
63 visitTimestamp <T extends Timestamp> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
64 visitTimestampSecond <T extends TimestampSecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
65 visitTimestampMillisecond <T extends TimestampMillisecond>(vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
66 visitTimestampMicrosecond <T extends TimestampMicrosecond>(vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
67 visitTimestampNanosecond <T extends TimestampNanosecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
68 visitTime <T extends Time> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
69 visitTimeSecond <T extends TimeSecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
70 visitTimeMillisecond <T extends TimeMillisecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
71 visitTimeMicrosecond <T extends TimeMicrosecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
72 visitTimeNanosecond <T extends TimeNanosecond> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
73 visitDecimal <T extends Decimal> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
74 visitList <T extends List> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
75 visitStruct <T extends Struct> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
76 visitUnion <T extends Union> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
77 visitDenseUnion <T extends DenseUnion> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
78 visitSparseUnion <T extends SparseUnion> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
79 visitDictionary <T extends Dictionary> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
80 visitInterval <T extends Interval> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
81 visitIntervalDayTime <T extends IntervalDayTime> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
82 visitIntervalYearMonth <T extends IntervalYearMonth> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
83 visitFixedSizeList <T extends FixedSizeList> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
84 visitMap <T extends Map_> (vector: VectorType<T>, value: T['TValue'] | null, index?: number): number;
85 }
86
87 /** @ignore */
88 export class IndexOfVisitor extends Visitor {}
89
90 /** @ignore */
91 function nullIndexOf(vector: VectorType<Null>, searchElement?: null) {
92 // if you're looking for nulls and the vector isn't empty, we've got 'em!
93 return searchElement === null && vector.length > 0 ? 0 : -1;
94 }
95
96 /** @ignore */
97 function indexOfNull<T extends DataType>(vector: VectorType<T>, fromIndex?: number): number {
98 const { nullBitmap } = vector.data;
99 if (!nullBitmap || vector.nullCount <= 0) {
100 return -1;
101 }
102 let i = 0;
103 for (const isValid of new BitIterator(nullBitmap, vector.data.offset + (fromIndex || 0), vector.length, nullBitmap, getBool)) {
104 if (!isValid) { return i; }
105 ++i;
106 }
107 return -1;
108 }
109
110 /** @ignore */
111 function indexOfValue<T extends DataType>(vector: VectorType<T>, searchElement?: T['TValue'] | null, fromIndex?: number): number {
112 if (searchElement === undefined) { return -1; }
113 if (searchElement === null) { return indexOfNull(vector, fromIndex); }
114 const compare = createElementComparator(searchElement);
115 for (let i = (fromIndex || 0) - 1, n = vector.length; ++i < n;) {
116 if (compare(vector.get(i))) {
117 return i;
118 }
119 }
120 return -1;
121 }
122
123 /** @ignore */
124 function indexOfUnion<T extends DataType>(vector: VectorType<T>, searchElement?: T['TValue'] | null, fromIndex?: number): number {
125 // Unions are special -- they do have a nullBitmap, but so can their children.
126 // If the searchElement is null, we don't know whether it came from the Union's
127 // bitmap or one of its childrens'. So we don't interrogate the Union's bitmap,
128 // since that will report the wrong index if a child has a null before the Union.
129 const compare = createElementComparator(searchElement);
130 for (let i = (fromIndex || 0) - 1, n = vector.length; ++i < n;) {
131 if (compare(vector.get(i))) {
132 return i;
133 }
134 }
135 return -1;
136 }
137
138 IndexOfVisitor.prototype.visitNull = nullIndexOf;
139 IndexOfVisitor.prototype.visitBool = indexOfValue;
140 IndexOfVisitor.prototype.visitInt = indexOfValue;
141 IndexOfVisitor.prototype.visitInt8 = indexOfValue;
142 IndexOfVisitor.prototype.visitInt16 = indexOfValue;
143 IndexOfVisitor.prototype.visitInt32 = indexOfValue;
144 IndexOfVisitor.prototype.visitInt64 = indexOfValue;
145 IndexOfVisitor.prototype.visitUint8 = indexOfValue;
146 IndexOfVisitor.prototype.visitUint16 = indexOfValue;
147 IndexOfVisitor.prototype.visitUint32 = indexOfValue;
148 IndexOfVisitor.prototype.visitUint64 = indexOfValue;
149 IndexOfVisitor.prototype.visitFloat = indexOfValue;
150 IndexOfVisitor.prototype.visitFloat16 = indexOfValue;
151 IndexOfVisitor.prototype.visitFloat32 = indexOfValue;
152 IndexOfVisitor.prototype.visitFloat64 = indexOfValue;
153 IndexOfVisitor.prototype.visitUtf8 = indexOfValue;
154 IndexOfVisitor.prototype.visitBinary = indexOfValue;
155 IndexOfVisitor.prototype.visitFixedSizeBinary = indexOfValue;
156 IndexOfVisitor.prototype.visitDate = indexOfValue;
157 IndexOfVisitor.prototype.visitDateDay = indexOfValue;
158 IndexOfVisitor.prototype.visitDateMillisecond = indexOfValue;
159 IndexOfVisitor.prototype.visitTimestamp = indexOfValue;
160 IndexOfVisitor.prototype.visitTimestampSecond = indexOfValue;
161 IndexOfVisitor.prototype.visitTimestampMillisecond = indexOfValue;
162 IndexOfVisitor.prototype.visitTimestampMicrosecond = indexOfValue;
163 IndexOfVisitor.prototype.visitTimestampNanosecond = indexOfValue;
164 IndexOfVisitor.prototype.visitTime = indexOfValue;
165 IndexOfVisitor.prototype.visitTimeSecond = indexOfValue;
166 IndexOfVisitor.prototype.visitTimeMillisecond = indexOfValue;
167 IndexOfVisitor.prototype.visitTimeMicrosecond = indexOfValue;
168 IndexOfVisitor.prototype.visitTimeNanosecond = indexOfValue;
169 IndexOfVisitor.prototype.visitDecimal = indexOfValue;
170 IndexOfVisitor.prototype.visitList = indexOfValue;
171 IndexOfVisitor.prototype.visitStruct = indexOfValue;
172 IndexOfVisitor.prototype.visitUnion = indexOfValue;
173 IndexOfVisitor.prototype.visitDenseUnion = indexOfUnion;
174 IndexOfVisitor.prototype.visitSparseUnion = indexOfUnion;
175 IndexOfVisitor.prototype.visitDictionary = indexOfValue;
176 IndexOfVisitor.prototype.visitInterval = indexOfValue;
177 IndexOfVisitor.prototype.visitIntervalDayTime = indexOfValue;
178 IndexOfVisitor.prototype.visitIntervalYearMonth = indexOfValue;
179 IndexOfVisitor.prototype.visitFixedSizeList = indexOfValue;
180 IndexOfVisitor.prototype.visitMap = indexOfValue;
181
182 /** @ignore */
183 export const instance = new IndexOfVisitor();