* Cache pseq segment

* Bump version

* Fix caching
This commit is contained in:
Jack Williams 2020-07-28 14:12:08 +01:00 коммит произвёл GitHub
Родитель 98405d991c
Коммит 6b02aed894
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 53 добавлений и 19 удалений

Просмотреть файл

@ -2,7 +2,7 @@
{
"policyName": "public",
"definitionName": "lockStepVersion",
"version": "0.0.0-alpha.3",
"version": "0.0.0-alpha.4",
"nextBump": "prerelease"
}
]

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "@tiny-calc/tool-config",
"version": "0.0.0-alpha.3",
"version": "0.0.0-alpha.4",
"description": "Common config for build tools",
"sideEffects": "false",
"repository": {

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "@tiny-calc/micro",
"version": "0.0.0-alpha.3",
"version": "0.0.0-alpha.4",
"main": "dist/index.js",
"sideEffects": "false",
"repository": {
@ -19,10 +19,10 @@
"adjust-random": "ts-node test/random-adjust-tree.ts"
},
"dependencies": {
"@tiny-calc/nano": "0.0.0-alpha.3"
"@tiny-calc/nano": "0.0.0-alpha.4"
},
"devDependencies": {
"@tiny-calc/tool-config": "0.0.0-alpha.3",
"@tiny-calc/tool-config": "0.0.0-alpha.4",
"@types/mocha": "^5.2.6",
"@types/node": "^10.0.0",
"best-random": "^1.0.3",

Просмотреть файл

@ -75,9 +75,8 @@ export class AdjustTreeBase<T> {
({ index, offset } = find(node, offset));
node = node.segments[index];
}
const segmentStart = position - offset;
({ index, offset } = find(node, offset));
return { segmentStart, index, offset, leaf: node }
return { offset, index, leaf: node }
}
getItem(position: number): { offset: number, segment: T } {

Просмотреть файл

@ -14,9 +14,8 @@ export interface InteriorNode<T> extends RopeBase<AdjustNode<T>> {
}
export interface LeafFocus<T> {
segmentStart: number;
index: number;
offset: number;
index: number;
leaf: LeafNode<T>;
}

Просмотреть файл

@ -1,5 +1,5 @@
import { AdjustTree, createTree, SegmentRange, TreeConfiguration, loadTree, forEachInSegmentRange } from "../adjust-tree/index";
import { LeafFocus } from "../adjust-tree/types";
import { LeafNode, LeafFocus } from "../adjust-tree/types";
const enum PermutationKind {
Empty,
@ -306,6 +306,9 @@ export class PermutationSequence {
next: number;
recycler: Recycler;
focus: LeafFocus<PermutationSegment> | undefined;
cachedSegment: PermutationSegment | undefined;
segmentStart: number | undefined;
segmentEnd: number | undefined;
constructor(snapshot?: PermutationSequenceSnapshot) {
if (snapshot) {
@ -336,14 +339,29 @@ export class PermutationSequence {
if (position < 0 || position >= this.permutationTree.getLength()) {
return undefined;
}
const focus = this.permutationTree.zoom(position);
const { offset, index, leaf } = focus
const segment = leaf.segments[index];
let segment: PermutationSegment | undefined;
let offset: number;
let leaf: LeafNode<PermutationSegment>;
let index: number;
if (this.cachedSegment && position >= this.segmentStart! && position < this.segmentEnd!) {
segment = this.cachedSegment;
offset = position - this.segmentStart!;
({ index, leaf } = this.focus!);
}
else {
({ index, offset, leaf } = this.focus = this.permutationTree.zoom(position));
this.cachedSegment = segment = leaf.segments[index];
this.segmentStart = position - offset;
this.segmentEnd = this.segmentStart + leaf.lengths[index];
}
switch (segment.kind) {
case PermutationKind.Empty:
const fresh = this.freshPermutation();
const len = leaf.lengths[index];
leaf.segments[index] = singleton(offset, len, fresh);
leaf.segments[index] = this.cachedSegment = singleton(offset, len, fresh);
return fresh;
case PermutationKind.Direct:
@ -362,7 +380,22 @@ export class PermutationSequence {
if (position < 0 || position >= this.permutationTree.getLength()) {
return undefined;
}
const { offset, segment } = this.permutationTree.getItem(position);
let segment: PermutationSegment | undefined;
let offset: number;
if (this.cachedSegment && position >= this.segmentStart! && position < this.segmentEnd!) {
segment = this.cachedSegment;
offset = position - this.segmentStart!;
}
else {
let index: number;
let leaf: LeafNode<PermutationSegment>;
({ index, offset, leaf } = this.focus = this.permutationTree.zoom(position));
this.cachedSegment = segment = leaf.segments[index];
this.segmentStart = position - offset;
this.segmentEnd = this.segmentStart + leaf.lengths[index];
}
switch (segment.kind) {
case PermutationKind.Empty:
return UNALLOCATED;
@ -377,6 +410,7 @@ export class PermutationSequence {
insertRange(position: number, length: number) {
this.permutationTree.insertRange(position, length, empty);
this.cachedSegment = undefined;
}
insertFilledRange(position: number, length: number) {
@ -394,6 +428,7 @@ export class PermutationSequence {
this.permutationTree.insertRange(pos, remaining, { kind: PermutationKind.RunLength, content: [remaining, this.next] });
this.next += remaining;
}
this.cachedSegment = undefined;
}
map<T>(cb: (pos: number, perm: number) => T): T[] {
@ -484,6 +519,7 @@ export class PermutationSequence {
}
deleteRange(position: number, length: number) {
this.cachedSegment = undefined;
return this.permutationTree.deleteRange(position, length);
}

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "@tiny-calc/nano",
"version": "0.0.0-alpha.3",
"version": "0.0.0-alpha.4",
"main": "dist/index.js",
"sideEffects": "false",
"repository": {
@ -17,7 +17,7 @@
"test": "mocha -r ts-node/register test/**/*.spec.ts"
},
"devDependencies": {
"@tiny-calc/tool-config": "0.0.0-alpha.3",
"@tiny-calc/tool-config": "0.0.0-alpha.4",
"@types/benchmark": "^1.0.31",
"@types/mocha": "^5.2.6",
"@types/node": "^10.0.0",

Просмотреть файл

@ -15,11 +15,11 @@
"test": "ts-node src/index.ts"
},
"dependencies": {
"@tiny-calc/nano": "0.0.0-alpha.3",
"@tiny-calc/nano": "0.0.0-alpha.4",
"node-fetch": "^2.6.0"
},
"devDependencies": {
"@tiny-calc/tool-config": "0.0.0-alpha.3",
"@tiny-calc/tool-config": "0.0.0-alpha.4",
"@types/node": "^10.0.0",
"rimraf": "^2.6.3",
"ts-node": "^8.1.0",