]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/js/src/vector/dictionary.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / vector / dictionary.ts
CommitLineData
1d09f67e
TL
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
18import { Data } from '../data';
19import { Vector } from '../vector';
20import { BaseVector } from './base';
21import { VectorType as V } from '../interfaces';
22import { VectorBuilderOptions } from './index';
23import { vectorFromValuesWithType } from './index';
24import { VectorBuilderOptionsAsync } from './index';
25import { DataType, Dictionary, TKeys } from '../type';
26
27/** @ignore */
28type FromArgs<T extends DataType = any, TKey extends TKeys = TKeys> = [Vector<T>, TKey, ArrayLike<number> | TKey['TArray']];
29
30/** @ignore */
31export class DictionaryVector<T extends DataType = any, TKey extends TKeys = TKeys> extends BaseVector<Dictionary<T, TKey>> {
32 public static from<T extends DataType = any, TKey extends TKeys = TKeys>(...args: FromArgs<T, TKey>): V<Dictionary<T, TKey>>;
33 public static from<T extends DataType = any, TKey extends TKeys = TKeys>(input: VectorBuilderOptions<Dictionary<T, TKey>>): Vector<Dictionary<T, TKey>>;
34 public static from<T extends DataType = any, TKey extends TKeys = TKeys>(input: VectorBuilderOptionsAsync<Dictionary<T, TKey>>): Promise<Vector<Dictionary<T, TKey>>>;
35 /** @nocollapse */
36 public static from<T extends DataType = any, TKey extends TKeys = TKeys>(...args: any[]) {
37 if (args.length === 3) {
38 const [values, indices, keys] = args as FromArgs<T, TKey>;
39 const type = new Dictionary(values.type, indices, null, null);
40 return Vector.new(Data.Dictionary(type, 0, keys.length, 0, null, keys, values));
41 }
42 return vectorFromValuesWithType(() => args[0].type, args[0]);
43 }
44
45 constructor(data: Data<Dictionary<T, TKey>>) {
46 super(data);
47 this.indices = Vector.new(data.clone(this.type.indices));
48 }
49
50 public readonly indices: V<TKey>;
51
52 public get dictionary() { return <Vector<T>> this.data.dictionary; }
53 public reverseLookup(value: T) { return this.dictionary.indexOf(value); }
54 public getKey(idx: number): TKey['TValue'] | null { return this.indices.get(idx); }
55 public getValue(key: number): T['TValue'] | null { return this.dictionary.get(key); }
56 public setKey(idx: number, key: TKey['TValue'] | null) { return this.indices.set(idx, key); }
57 public setValue(key: number, value: T['TValue'] | null) { return this.dictionary.set(key, value); }
58}
59
60(DictionaryVector.prototype as any).indices = null;