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

History - ensure that history items a unique #178694

Merged
merged 5 commits into from Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions src/vs/base/common/history.ts
Expand Up @@ -118,6 +118,7 @@ interface HistoryNode<T> {

export class HistoryNavigator2<T> {

private valueSet: Set<T>;
private head: HistoryNode<T>;
private tail: HistoryNode<T>;
private cursor: HistoryNode<T>;
Expand All @@ -135,6 +136,7 @@ export class HistoryNavigator2<T> {
next: undefined
};

this.valueSet = new Set<T>([history[0]]);
for (let i = 1; i < history.length; i++) {
this.add(history[i]);
}
Expand All @@ -152,7 +154,15 @@ export class HistoryNavigator2<T> {
this.cursor = this.tail;
this.size++;

if (this.valueSet.has(value)) {
this._deleteFromList(value);
} else {
this.valueSet.add(value);
}

while (this.size > this.capacity) {
this.valueSet.delete(this.head.value);

this.head = this.head.next!;
this.head.previous = undefined;
this.size--;
Expand All @@ -163,8 +173,20 @@ export class HistoryNavigator2<T> {
* @returns old last value
*/
replaceLast(value: T): T {
if (this.tail.value === value) {
return value;
}

const oldValue = this.tail.value;
this.valueSet.delete(oldValue);
this.tail.value = value;

if (this.valueSet.has(value)) {
this._deleteFromList(value);
} else {
this.valueSet.add(value);
}

return oldValue;
}

Expand Down Expand Up @@ -193,14 +215,7 @@ export class HistoryNavigator2<T> {
}

has(t: T): boolean {
let temp: HistoryNode<T> | undefined = this.head;
while (temp) {
if (temp.value === t) {
return true;
}
temp = temp.next;
}
return false;
return this.valueSet.has(t);
}

resetCursor(): T {
Expand All @@ -216,4 +231,24 @@ export class HistoryNavigator2<T> {
node = node.next;
}
}

private _deleteFromList(value: T): void {
let temp = this.head;

while (temp !== this.tail) {
if (temp.value === value) {
if (temp === this.head) {
this.head = this.head.next!;
this.head.previous = undefined;
} else {
temp.previous!.next = temp.next;
temp.next!.previous = temp.previous;
}

this.size--;
}

temp = temp.next!;
}
}
}
94 changes: 93 additions & 1 deletion src/vs/base/test/common/history.test.ts
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { HistoryNavigator } from 'vs/base/common/history';
import { HistoryNavigator, HistoryNavigator2 } from 'vs/base/common/history';

suite('History Navigator', () => {

Expand Down Expand Up @@ -176,3 +176,95 @@ suite('History Navigator', () => {
return result;
}
});

suite('History Navigator 2', () => {

test('constructor', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4']);

assert.strictEqual(testObject.current(), '4');
assert.strictEqual(testObject.isAtEnd(), true);
});

test('constructor - initial history is not empty', () => {
assert.throws(() => new HistoryNavigator2([]));
});

test('constructor - capacity limit', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4'], 3);

assert.strictEqual(testObject.current(), '4');
assert.strictEqual(testObject.isAtEnd(), true);
assert.strictEqual(testObject.has('1'), false);
});

test('constructor - duplicate values', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4', '3', '2', '1']);

assert.strictEqual(testObject.current(), '1');
assert.strictEqual(testObject.isAtEnd(), true);
});

test('navigation', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4']);

assert.strictEqual(testObject.current(), '4');
assert.strictEqual(testObject.isAtEnd(), true);

assert.strictEqual(testObject.next(), '4');
assert.strictEqual(testObject.previous(), '3');
assert.strictEqual(testObject.previous(), '2');
assert.strictEqual(testObject.previous(), '1');
assert.strictEqual(testObject.previous(), '1');

assert.strictEqual(testObject.current(), '1');
assert.strictEqual(testObject.next(), '2');
assert.strictEqual(testObject.resetCursor(), '4');
});

test('add', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4']);
testObject.add('5');

assert.strictEqual(testObject.current(), '5');
assert.strictEqual(testObject.isAtEnd(), true);
});

test('add - existing value', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4']);
testObject.add('2');

assert.strictEqual(testObject.current(), '2');
assert.strictEqual(testObject.isAtEnd(), true);

assert.strictEqual(testObject.previous(), '4');
assert.strictEqual(testObject.previous(), '3');
assert.strictEqual(testObject.previous(), '1');
});

test('replaceLast', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4']);
testObject.replaceLast('5');

assert.strictEqual(testObject.current(), '5');
assert.strictEqual(testObject.isAtEnd(), true);
assert.strictEqual(testObject.has('4'), false);

assert.strictEqual(testObject.previous(), '3');
assert.strictEqual(testObject.previous(), '2');
assert.strictEqual(testObject.previous(), '1');
});

test('replaceLast - existing value', () => {
const testObject = new HistoryNavigator2(['1', '2', '3', '4']);
testObject.replaceLast('2');

assert.strictEqual(testObject.current(), '2');
assert.strictEqual(testObject.isAtEnd(), true);
assert.strictEqual(testObject.has('4'), false);

assert.strictEqual(testObject.previous(), '3');
assert.strictEqual(testObject.previous(), '1');
});

});