]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | import { Data } from '../data'; | |
19 | import { Vector } from '../vector'; | |
20 | import { BaseVector } from './base'; | |
21 | import { VectorType as V } from '../interfaces'; | |
22 | import { VectorBuilderOptions } from './index'; | |
23 | import { vectorFromValuesWithType } from './index'; | |
24 | import { VectorBuilderOptionsAsync } from './index'; | |
25 | import { DataType, Dictionary, TKeys } from '../type'; | |
26 | ||
27 | /** @ignore */ | |
28 | type FromArgs<T extends DataType = any, TKey extends TKeys = TKeys> = [Vector<T>, TKey, ArrayLike<number> | TKey['TArray']]; | |
29 | ||
30 | /** @ignore */ | |
31 | export 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; |