src/vs/editor/common/model/pieceTreeTextBuffer/rbTreeBase.ts
425 LOC · 425 covered · 0 uncovered · 159 ranges · 1708 concepts · 81 introducers · 861 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
textModel.ts ×190
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Piece, PieceTreeBase } from './pieceTreeBase.js';
export class TreeNode {
parent: TreeNode;
left: TreeNode;
right: TreeNode;
color: NodeColor;
// Piece
piece: Piece;
size_left: number; // size of the left subtree (not inorder)
lf_left: number; // line feeds cnt in the left subtree (not in order)
constructor(piece: Piece, color: NodeColor) {
this.piece = piece;
this.color = color;
this.size_left = 0;
this.lf_left = 0;
this.parent = this;
this.left = this;
this.right = this;
}
public next(): TreeNode {
}
let node: TreeNode = this;
while (node.parent !== SENTINEL) {
}
node = node.parent;
}
if (node.parent === SENTINEL) {
}
public prev(): TreeNode {
}
let node: TreeNode = this;
while (node.parent !== SENTINEL) {
}
node = node.parent;
}
if (node.parent === SENTINEL) {
}
public detach(): void {
this.left = null!;
this.right = null!;
}
export const enum NodeColor {
Black = 0,
Red = 1,
}
export const SENTINEL: TreeNode = new TreeNode(null!, NodeColor.Black);
SENTINEL.parent = SENTINEL;
SENTINEL.left = SENTINEL;
SENTINEL.right = SENTINEL;
SENTINEL.color = NodeColor.Black;
export function leftest(node: TreeNode): TreeNode {
}
}
export function righttest(node: TreeNode): TreeNode {
}
}
if (node === SENTINEL) {
return 0;
}
return node.size_left + node.piece.length + calculateSize(node.right);
}
if (node === SENTINEL) {
return 0;
}
return node.lf_left + node.piece.lineFeedCnt + calculateLF(node.right);
}
SENTINEL.parent = SENTINEL;
}
export function leftRotate(tree: PieceTreeBase, x: TreeNode) {
// fix size_left
y.size_left += x.size_left + (x.piece ? x.piece.length : 0);
y.lf_left += x.lf_left + (x.piece ? x.piece.lineFeedCnt : 0);
x.right = y.left;
if (y.left !== SENTINEL) {
}
if (x.parent === SENTINEL) {
}
x.parent = y;
}
export function rightRotate(tree: PieceTreeBase, y: TreeNode) {
y.left = x.right;
if (x.right !== SENTINEL) {
}
// fix size_left
y.size_left -= x.size_left + (x.piece ? x.piece.length : 0);
y.lf_left -= x.lf_left + (x.piece ? x.piece.lineFeedCnt : 0);
if (y.parent === SENTINEL) {
} else {
}
x.right = y;
y.parent = x;
}
export function rbDelete(tree: PieceTreeBase, z: TreeNode) {
let y: TreeNode;
if (z.left === SENTINEL) {
x = y.right;
x = y.left;
x = y.right;
}
if (y === tree.root) {
// if x is null, we are removing the only node
x.color = NodeColor.Black;
z.detach();
resetSentinel();
tree.root.parent = SENTINEL;
return;
}
const yWasRed = (y.color === NodeColor.Red);
if (y === y.parent.left) {
}
if (y === z) {
recomputeTreeMetadata(tree, x);
}
// as we make changes to x's hierarchy, update size_left of subtree first
recomputeTreeMetadata(tree, x);
y.left = z.left;
y.right = z.right;
y.parent = z.parent;
y.color = z.color;
if (z === tree.root) {
}
if (y.left !== SENTINEL) {
y.left.parent = y;
}
if (y.right !== SENTINEL) {
}
// we replace z with y, so in this sub tree, the length change is z.item.length
y.size_left = z.size_left;
y.lf_left = z.lf_left;
recomputeTreeMetadata(tree, y);
}
z.detach();
if (x.parent.left === x) {
const newLFLeft = calculateLF(x);
if (newSizeLeft !== x.parent.size_left || newLFLeft !== x.parent.lf_left) {
const lf_delta = newLFLeft - x.parent.lf_left;
x.parent.size_left = newSizeLeft;
x.parent.lf_left = newLFLeft;
updateTreeMetadata(tree, x.parent, delta, lf_delta);
}
recomputeTreeMetadata(tree, x.parent);
if (yWasRed) {
return;
}
// RB-DELETE-FIXUP
let w: TreeNode;
if (w.color === NodeColor.Red) {
x.parent.color = NodeColor.Red;
leftRotate(tree, x.parent);
w = x.parent.right;
}
if (w.left.color === NodeColor.Black && w.right.color === NodeColor.Black) {
x = x.parent;
w.color = NodeColor.Red;
rightRotate(tree, w);
w = x.parent.right;
}
w.color = x.parent.color;
x.parent.color = NodeColor.Black;
w.right.color = NodeColor.Black;
leftRotate(tree, x.parent);
x = tree.root;
}
if (w.color === NodeColor.Red) {
x.parent.color = NodeColor.Red;
rightRotate(tree, x.parent);
w = x.parent.left;
}
if (w.left.color === NodeColor.Black && w.right.color === NodeColor.Black) {
x = x.parent;
w.color = NodeColor.Red;
leftRotate(tree, w);
w = x.parent.left;
}
w.color = x.parent.color;
x.parent.color = NodeColor.Black;
w.left.color = NodeColor.Black;
rightRotate(tree, x.parent);
x = tree.root;
}
resetSentinel();
}
export function fixInsert(tree: PieceTreeBase, x: TreeNode) {
while (x !== tree.root && x.parent.color === NodeColor.Red) {
if (y.color === NodeColor.Red) {
y.color = NodeColor.Black;
x.parent.parent.color = NodeColor.Red;
x = x.parent.parent;
leftRotate(tree, x);
}
x.parent.color = NodeColor.Black;
x.parent.parent.color = NodeColor.Red;
rightRotate(tree, x.parent.parent);
}
if (y.color === NodeColor.Red) {
y.color = NodeColor.Black;
x.parent.parent.color = NodeColor.Red;
x = x.parent.parent;
rightRotate(tree, x);
}
x.parent.parent.color = NodeColor.Red;
leftRotate(tree, x.parent.parent);
}
tree.root.color = NodeColor.Black;
}
export function updateTreeMetadata(tree: PieceTreeBase, x: TreeNode, delta: number, lineFeedCntDelta: number): void {
while (x !== tree.root && x !== SENTINEL) {
x.parent.lf_left += lineFeedCntDelta;
}
x = x.parent;
}
export function recomputeTreeMetadata(tree: PieceTreeBase, x: TreeNode) {
let lf_delta = 0;
if (x === tree.root) {
return;
}
// go upwards till the node whose left subtree is changed.
}
if (x === tree.root) {
return;
}
// x is the node whose right subtree is changed.
x = x.parent;
delta = calculateSize(x.left) - x.size_left;
lf_delta = calculateLF(x.left) - x.lf_left;
x.size_left += delta;
x.lf_left += lf_delta;
// go upwards till root. O(logN)
x.parent.lf_left += lf_delta;
}
x = x.parent;
}