Skip to content

Commit

Permalink
[Refactor] new folder src/base/common/structures to hold all kinds …
Browse files Browse the repository at this point in the history
…of data structures
  • Loading branch information
Bistard committed Sep 2, 2023
1 parent 50a14ec commit a9d44a3
Show file tree
Hide file tree
Showing 35 changed files with 727 additions and 714 deletions.
2 changes: 1 addition & 1 deletion src/base/browser/basic/contextMenu/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addDisposableListener, DomUtility, EventType } from "src/base/browser/b
import { FastElement } from "src/base/browser/basic/fastElement";
import { AnchorAbstractPosition, AnchorMode, calcViewPositionAlongAxis, IAnchorBox } from "src/base/browser/basic/view";
import { Disposable, DisposableManager, IDisposable } from "src/base/common/dispose";
import { Range } from "src/base/common/range";
import { Range } from "src/base/common/structures/range";
import { IDomBox, IPosition } from "src/base/common/util/size";

export interface IAnchor {
Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/basic/sash/sash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AbstractSashController, HorizontalSashController, IAbstractSashControll
import { Disposable, DisposableManager } from "src/base/common/dispose";
import { addDisposableListener, EventType, Orientation } from "src/base/browser/basic/dom";
import { Emitter, Register } from "src/base/common/event";
import { IRange } from "src/base/common/range";
import { IRange } from "src/base/common/structures/range";
import { Mutable } from "src/base/common/util/type";
import { cancellableTimeout } from "src/base/common/util/async";
import { VisibilityController } from "src/base/browser/basic/visibilityController";
Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/listView/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IListView, IViewItemChangeEvent } from "src/base/browser/secondary/listView/listView";
import { IListWidget } from "src/base/browser/secondary/listWidget/listWidget";
import { Register } from "src/base/common/event";
import { IRange } from "src/base/common/range";
import { IRange } from "src/base/common/structures/range";

/**
* Common interfaces shares between {@link IListView} and {@link IListWidget}.
Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/listView/listView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ScrollbarType } from "src/base/browser/secondary/scrollableWidget/scrol
import { Disposable, IDisposable } from "src/base/common/dispose";
import { DomEmitter, DomUtility, EventType } from "src/base/browser/basic/dom";
import { Emitter, Register } from "src/base/common/event";
import { IRange, ISpliceable, Range, RangeTable } from "src/base/common/range";
import { IRange, ISpliceable, Range, RangeTable } from "src/base/common/structures/range";
import { IScrollEvent, Scrollable } from "src/base/common/scrollable";
import { IListItemProvider } from "src/base/browser/secondary/listView/listItemProvider";
import { memoize } from "src/base/common/memoization";
Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/listWidget/listWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Disposable, IDisposable } from "src/base/common/dispose";
import { Event, Register } from "src/base/common/event";
import { createStandardKeyboardEvent, IStandardKeyboardEvent, KeyCode } from "src/base/common/keyboard";
import { memoize } from "src/base/common/memoization";
import { IRange } from "src/base/common/range";
import { IRange } from "src/base/common/structures/range";
import { IScrollEvent } from "src/base/common/scrollable";
import { isNumber, nullToUndefined } from "src/base/common/util/type";

Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/tree/abstractTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IListContextmenuEvent, IListMouseEvent, IListTouchEvent, IListWidget, I
import { IListDragAndDropProvider } from "src/base/browser/secondary/listWidget/listWidgetDragAndDrop";
import { Disposable, IDisposable } from "src/base/common/dispose";
import { Event, Register, RelayEmitter } from "src/base/common/event";
import { ISpliceable } from "src/base/common/range";
import { ISpliceable } from "src/base/common/structures/range";
import { IScrollEvent } from "src/base/common/scrollable";
import { IListViewRenderer } from "src/base/browser/secondary/listView/listRenderer";
import { IStandardKeyboardEvent } from "src/base/common/keyboard";
Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/tree/asyncTreeModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IAsyncNode, IChildrenProvider, IIdentiityProivder } from "src/base/browser/secondary/tree/asyncTree";
import { IMultiTreeModelOptions, FlexMultiTreeModel } from "src/base/browser/secondary/tree/multiTreeModel";
import { ITreeNode } from "src/base/browser/secondary/tree/tree";
import { ISpliceable } from "src/base/common/range";
import { ISpliceable } from "src/base/common/structures/range";
import { Blocker } from "src/base/common/util/async";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/tree/indexTreeModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITreeModel, ITreeSpliceEvent, ITreeNode, ITreeNodeItem, ITreeCollapseStateChangeEvent, IFlexNode } from "src/base/browser/secondary/tree/tree";
import { ITreeFilterProvider } from "src/base/browser/secondary/tree/treeFilter";
import { DelayableEmitter, Emitter, Register } from "src/base/common/event";
import { ISpliceable } from "src/base/common/range";
import { ISpliceable } from "src/base/common/structures/range";

const INVALID_INDEX = -1;

Expand Down
2 changes: 1 addition & 1 deletion src/base/browser/secondary/tree/multiTreeModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Register } from "src/base/common/event";
import { ISpliceable } from "src/base/common/range";
import { ISpliceable } from "src/base/common/structures/range";
import { IIndexTreeModelOptions, IIndexTreeModel, IndexTreeModel, ITreeModelSpliceOptions, IIndexTreeModelBase, IFlexIndexTreeModel, FlexIndexTreeModel } from "src/base/browser/secondary/tree/indexTreeModel";
import { ITreeModel, ITreeSpliceEvent, ITreeNode, ITreeNodeItem, ITreeCollapseStateChangeEvent, IFlexNode } from "src/base/browser/secondary/tree/tree";

Expand Down
2 changes: 1 addition & 1 deletion src/base/common/event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LinkedList } from "src/base/common/util/linkedList";
import { LinkedList } from "src/base/common/structures/linkedList";
import { Disposable, DisposableManager, disposeAll, IDisposable, toDisposable } from "src/base/common/dispose";
import { ErrorHandler } from "src/base/common/error";
import { ITask } from "src/base/common/util/async";
Expand Down
150 changes: 150 additions & 0 deletions src/base/common/structures/deque.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { IDisposable } from "src/base/common/dispose";
import { IIterable } from "src/base/common/util/iterable";

/**
* Interface for {@link IDeque}.
*/
export interface IDeque<T> extends IIterable<T>, IDisposable {
size(): number;
empty(): boolean;

at(index: number): T;
front(): T;
back(): T;

pushBack(element: T): void;
pushFront(element: T): void;
popBack(): T;
popFront(): T;

insert(index: number, element: T): void;
remove(index: number): T;
replace(index: number, element: T): T;
swap(first: number, second: number): void;
reverse(): void;

extendFront(other: Deque<T>): void;
extendBack(other: Deque<T>): void;
clear(): number;
}

export class Deque<T> implements IDeque<T> {

// [field]
private _arr: T[];

// [constructor]
constructor(elements: T[] = []) {
this._arr = [];
for (const element of elements) {
this._arr.push(element);
}
}

// [public methods]
public size(): number {
return this._arr.length;
}

public empty(): boolean {
return !this._arr.length;
}

public at(index: number): T {
if (index < 0 || index >= this.size()) {
throw new Error(`Invalid index when getting elements in deque at ${index}.`);
}
return this._arr[index]!;
}

public front(): T {
return this.at(0);
}

public back(): T {
return this.at(this.size() - 1);
}

public pushBack(element: T): void {
this._arr.splice(this.size(), 0, element);
}

public pushFront(element: T): void {
this._arr.splice(0, 0, element);
}

public popBack(): T {
if (this.empty()) {
throw new Error('Cannot pop back from deque because it is empty.');
}
return this._arr.pop()!;
}

public popFront(): T {
if (this.empty()) {
throw new Error('Cannot pop front from deque because it is empty.');
}
return this._arr.splice(0, 1)[0]!;
}

public insert(index: number, element: T): void {
if (index < 0 || index > this.size()) {
throw new Error(`Invalid index when inserting elements in deque at ${index}.`);
}
this._arr.splice(index, 0, element);
}

public remove(index: number): T {
const removed = this._arr.splice(index, 1);
if (!removed.length) {
throw new Error(`Cannot remove elements from deque at invalid index ${index}.`);
}
return removed[0]!;
}

public replace(index: number, element: T): T {
const old = this.at(index);
this._arr[index] = element;
return old;
}

public swap(first: number, second: number): void {
const firstElement = this.at(first);
this._arr[first] = this.at(second);
this._arr[second] = firstElement;
}

public reverse(): void {
this._arr.reverse();
}

public extendFront(other: Deque<T>): void {
for (const element of other) {
this.pushFront(element);
}
}

public extendBack(other: Deque<T>): void {
for (const element of other) {
this.pushBack(element);
}
}

public clear(): number {
const removed = this.size();
this._arr = [];
return removed;
}

public dispose(): void {
this.clear();
}

*[Symbol.iterator](): Iterator<T> {
let idx = 0;
while (idx < this.size()) {
yield this._arr[idx]!;
idx += 1;
}
}
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a9d44a3

Please sign in to comment.