-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the number of index array computations
- Loading branch information
Showing
18 changed files
with
138 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type {Arrayable, ArrayableNew, Indices} from "../types" | ||
import {AssertionError} from "./assert" | ||
import {BitSet} from "./bitset" | ||
import {range} from "./array" | ||
|
||
export const PackedIndices = BitSet | ||
export type PackedIndices = BitSet | ||
|
||
export class OpaqueIndices implements Indices { | ||
|
||
protected readonly _array: number[] | ||
|
||
constructor(readonly size: number, init: Iterable<number> | 1 | 0 = 0) { | ||
if (init == 0) { | ||
this._array = [] | ||
} else if (init == 1) { | ||
this._array = range(0, size) | ||
} else { | ||
this._array = [...init] | ||
} | ||
this.count = this._array.length | ||
} | ||
|
||
readonly count: number | ||
|
||
get array(): number[] { // TODO readonly | ||
return this._array | ||
} | ||
|
||
static from_packed(indices: PackedIndices): OpaqueIndices { | ||
return new OpaqueIndices(indices.size, indices) | ||
} | ||
|
||
*[Symbol.iterator](): Iterator<number> { | ||
yield* this._array | ||
} | ||
|
||
select<T>(array: Arrayable<T>): Arrayable<T> { | ||
this._check_array_length(array) | ||
const n = this.count | ||
if (n == this.size) { | ||
return array.slice() | ||
} else { | ||
const result = new (array.constructor as ArrayableNew)<T>(n) | ||
let i = 0 | ||
for (const j of this._array) { | ||
result[i++] = array[j] | ||
} | ||
return result | ||
} | ||
} | ||
|
||
protected _check_array_length(other: Arrayable) { | ||
if (!(this.size <= other.length)) { | ||
throw new AssertionError(`Size mismatch (${this.size} != ${other.length})`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.