Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree): refactor RangeMap #23353

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
* Licensed under the MIT License.
*/

/* eslint-disable @typescript-eslint/no-non-null-assertion */

import type { ChangesetLocalId, RevisionTag } from "../core/index.js";
import {
type Mutable,
type RangeMap,
brand,
getFirstEntryFromRangeMap,
getOrAddEmptyToMap,
setInRangeMap,
} from "../util/index.js";
import { type Mutable, RangeMap, brand } from "../util/index.js";

/**
* A unique ID allocator that returns the output ID for the same input ID.
Expand Down Expand Up @@ -56,38 +51,42 @@ export const MemoizedIdRangeAllocator = {
allocate(key: string | number | undefined, startId: number, length?: number): IdRange[] {
let count = length ?? 1;
const out: IdRange[] = [];
const ranges = getOrAddEmptyToMap(rangeMap, key);
let ranges = rangeMap.get(key as RevisionTag);
if (ranges === undefined) {
ranges = new RangeMap<number>();
rangeMap.set(key as RevisionTag, ranges);
}
let currId = startId;
while (count > 0) {
const firstRange = getFirstEntryFromRangeMap(ranges, currId, count);
const firstRange = ranges.getFirstEntryFromRange(currId, count);
if (firstRange === undefined) {
const newId = _nextId;
_nextId += count;
setInRangeMap(ranges, currId, count, newId);
ranges.setInRange(currId, count, newId);
out.push({ first: brand(newId), count });
count = 0;
} else {
const idRange: Mutable<IdRange> = {
first: brand(firstRange.value),
first: brand(firstRange.value!),
count: firstRange.length,
};
if (currId < firstRange.start) {
const countToAdd = firstRange.start - currId;
setInRangeMap(ranges, currId, countToAdd, _nextId);
if (currId < firstRange.start!) {
const countToAdd = firstRange.start! - currId;
ranges.setInRange(currId, countToAdd, _nextId);
out.push({ first: brand(_nextId), count: countToAdd });
_nextId += countToAdd;
currId += countToAdd;
count -= countToAdd;
} else if (firstRange.start < currId) {
const countToTrim = currId - firstRange.start;
} else if (firstRange.start! < currId) {
const countToTrim = currId - firstRange.start!;
idRange.first = brand((idRange.first as number) + countToTrim);
idRange.count -= countToTrim;
}
if (idRange.count > count) {
idRange.count = count;
} else if (
idRange.count < count &&
firstRange.value + firstRange.length === _nextId
firstRange.value! + firstRange.length === _nextId
) {
// The existing range can be extended
_nextId += count - idRange.count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
*/

import type { ChangesetLocalId, RevisionTag } from "../../core/index.js";
import {
type RangeMap,
type RangeQueryResult,
getFromRangeMap,
getOrAddInMap,
setInRangeMap,
} from "../../util/index.js";
import { RangeMap, type RangeQueryResult } from "../../util/index.js";
import type { NodeId } from "./modularChangeTypes.js";

export type CrossFieldMap<T> = Map<RevisionTag | undefined, RangeMap<T>>;
Expand All @@ -32,7 +26,12 @@ export function setInCrossFieldMap<T>(
count: number,
value: T,
): void {
setInRangeMap(getOrAddInMap(map, revision, []), id, count, value);
let rangeMap = map.get(revision);
if (rangeMap === undefined) {
rangeMap = new RangeMap();
map.set(revision, rangeMap);
}
rangeMap.setInRange(id, count, value);
}

export function getFirstFromCrossFieldMap<T>(
Expand All @@ -41,7 +40,8 @@ export function getFirstFromCrossFieldMap<T>(
id: ChangesetLocalId,
count: number,
): RangeQueryResult<T> {
return getFromRangeMap(map.get(revision) ?? [], id, count);
const rangeMap = map.has(revision) ? (map.get(revision) as RangeMap<T>) : new RangeMap<T>();
return rangeMap.getFromRange(id, count);
}

/**
Expand Down
Loading
Loading