Spaces:
Sleeping
Sleeping
File size: 8,666 Bytes
be5030f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import { clampRange } from '../util/vector';
import { DataType } from '../type';
import { selectChunkArgs } from '../util/args';
import { AbstractVector, Vector } from '../vector';
/** @ignore */
export class Chunked extends AbstractVector {
constructor(type, chunks = [], offsets = calculateOffsets(chunks)) {
super();
this._nullCount = -1;
this._type = type;
this._chunks = chunks;
this._chunkOffsets = offsets;
this._length = offsets[offsets.length - 1];
this._numChildren = (this._type.children || []).length;
}
/** @nocollapse */
static flatten(...vectors) {
return selectChunkArgs(Vector, vectors);
}
/** @nocollapse */
static concat(...vectors) {
const chunks = Chunked.flatten(...vectors);
return new Chunked(chunks[0].type, chunks);
}
get type() { return this._type; }
get length() { return this._length; }
get chunks() { return this._chunks; }
get typeId() { return this._type.typeId; }
get VectorName() { return `Chunked<${this._type}>`; }
get data() {
return this._chunks[0] ? this._chunks[0].data : null;
}
get ArrayType() { return this._type.ArrayType; }
get numChildren() { return this._numChildren; }
get stride() { return this._chunks[0] ? this._chunks[0].stride : 1; }
get byteLength() {
return this._chunks.reduce((byteLength, chunk) => byteLength + chunk.byteLength, 0);
}
get nullCount() {
let nullCount = this._nullCount;
if (nullCount < 0) {
this._nullCount = nullCount = this._chunks.reduce((x, { nullCount }) => x + nullCount, 0);
}
return nullCount;
}
get indices() {
if (DataType.isDictionary(this._type)) {
if (!this._indices) {
const chunks = this._chunks;
this._indices = (chunks.length === 1
? chunks[0].indices
: Chunked.concat(...chunks.map((x) => x.indices)));
}
return this._indices;
}
return null;
}
get dictionary() {
if (DataType.isDictionary(this._type)) {
return this._chunks[this._chunks.length - 1].data.dictionary;
}
return null;
}
*[Symbol.iterator]() {
for (const chunk of this._chunks) {
yield* chunk;
}
}
clone(chunks = this._chunks) {
return new Chunked(this._type, chunks);
}
concat(...others) {
return this.clone(Chunked.flatten(this, ...others));
}
slice(begin, end) {
return clampRange(this, begin, end, this._sliceInternal);
}
getChildAt(index) {
if (index < 0 || index >= this._numChildren) {
return null;
}
let columns = this._children || (this._children = []);
let child, field, chunks;
if (child = columns[index]) {
return child;
}
if (field = (this._type.children || [])[index]) {
chunks = this._chunks
.map((vector) => vector.getChildAt(index))
.filter((vec) => vec != null);
if (chunks.length > 0) {
return (columns[index] = new Chunked(field.type, chunks));
}
}
return null;
}
search(index, then) {
let idx = index;
// binary search to find the child vector and value indices
let offsets = this._chunkOffsets, rhs = offsets.length - 1;
// return early if out of bounds, or if there's just one child
if (idx < 0) {
return null;
}
if (idx >= offsets[rhs]) {
return null;
}
if (rhs <= 1) {
return then ? then(this, 0, idx) : [0, idx];
}
let lhs = 0, pos = 0, mid = 0;
do {
if (lhs + 1 === rhs) {
return then ? then(this, lhs, idx - pos) : [lhs, idx - pos];
}
mid = lhs + ((rhs - lhs) / 2) | 0;
idx >= offsets[mid] ? (lhs = mid) : (rhs = mid);
} while (idx < offsets[rhs] && idx >= (pos = offsets[lhs]));
return null;
}
isValid(index) {
return !!this.search(index, this.isValidInternal);
}
get(index) {
return this.search(index, this.getInternal);
}
set(index, value) {
this.search(index, ({ chunks }, i, j) => chunks[i].set(j, value));
}
indexOf(element, offset) {
if (offset && typeof offset === 'number') {
return this.search(offset, (self, i, j) => this.indexOfInternal(self, i, j, element));
}
return this.indexOfInternal(this, 0, Math.max(0, offset || 0), element);
}
toArray() {
const { chunks } = this;
const n = chunks.length;
let ArrayType = this._type.ArrayType;
if (n <= 0) {
return new ArrayType(0);
}
if (n <= 1) {
return chunks[0].toArray();
}
let len = 0, src = new Array(n);
for (let i = -1; ++i < n;) {
len += (src[i] = chunks[i].toArray()).length;
}
if (ArrayType !== src[0].constructor) {
ArrayType = src[0].constructor;
}
let dst = new ArrayType(len);
let set = ArrayType === Array ? arraySet : typedSet;
for (let i = -1, idx = 0; ++i < n;) {
idx = set(src[i], dst, idx);
}
return dst;
}
getInternal({ _chunks }, i, j) { return _chunks[i].get(j); }
isValidInternal({ _chunks }, i, j) { return _chunks[i].isValid(j); }
indexOfInternal({ _chunks }, chunkIndex, fromIndex, element) {
let i = chunkIndex - 1, n = _chunks.length;
let start = fromIndex, offset = 0, found = -1;
while (++i < n) {
if (~(found = _chunks[i].indexOf(element, start))) {
return offset + found;
}
start = 0;
offset += _chunks[i].length;
}
return -1;
}
_sliceInternal(self, begin, end) {
const slices = [];
const { chunks, _chunkOffsets: chunkOffsets } = self;
for (let i = -1, n = chunks.length; ++i < n;) {
const chunk = chunks[i];
const chunkLength = chunk.length;
const chunkOffset = chunkOffsets[i];
// If the child is to the right of the slice boundary, we can stop
if (chunkOffset >= end) {
break;
}
// If the child is to the left of of the slice boundary, exclude
if (begin >= chunkOffset + chunkLength) {
continue;
}
// If the child is between both left and right boundaries, include w/o slicing
if (chunkOffset >= begin && (chunkOffset + chunkLength) <= end) {
slices.push(chunk);
continue;
}
// If the child overlaps one of the slice boundaries, include that slice
const from = Math.max(0, begin - chunkOffset);
const to = Math.min(end - chunkOffset, chunkLength);
slices.push(chunk.slice(from, to));
}
return self.clone(slices);
}
}
/** @ignore */
function calculateOffsets(vectors) {
let offsets = new Uint32Array((vectors || []).length + 1);
let offset = offsets[0] = 0, length = offsets.length;
for (let index = 0; ++index < length;) {
offsets[index] = (offset += vectors[index - 1].length);
}
return offsets;
}
/** @ignore */
const typedSet = (src, dst, offset) => {
dst.set(src, offset);
return (offset + src.length);
};
/** @ignore */
const arraySet = (src, dst, offset) => {
let idx = offset;
for (let i = -1, n = src.length; ++i < n;) {
dst[idx++] = src[i];
}
return idx;
};
//# sourceMappingURL=chunked.mjs.map
|