From 86e4673905d425f322c773e593c6581bfc5217b0 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 17 Jun 2016 13:56:44 +0200 Subject: [PATCH] remove unused sortedList --- src/vs/base/common/sortedList.ts | 256 --------------------- src/vs/base/test/common/sortedList.test.ts | 111 --------- 2 files changed, 367 deletions(-) delete mode 100644 src/vs/base/common/sortedList.ts delete mode 100644 src/vs/base/test/common/sortedList.test.ts diff --git a/src/vs/base/common/sortedList.ts b/src/vs/base/common/sortedList.ts deleted file mode 100644 index c15d6ecbba5..00000000000 --- a/src/vs/base/common/sortedList.ts +++ /dev/null @@ -1,256 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -'use strict'; - -import Arrays = require('vs/base/common/arrays'); - -export interface IIterator { - current: TValue; - moveNext(): boolean; - hasNext(): boolean; - reset(): void; - dispose(): void; -} - -export interface IIterable { - getIterator(): IIterator; -} - -export interface ISortedList extends IIterable> { - /** - * Number of elements in a sorted list. - * O(1) - */ - count: number; - - /** - * Gets the value associated with the specified key. - * Returns null if there is no value associated with the key. - * O(log n) - */ - getValue(key: TKey): TValue; - - /** - * Gets an iterator over values. - * O(1) - */ - getValues(): IIterator; - - /** - * Gets the value at the specified index. - * Returns null if index is out of bounds. - * O(1) - */ - getValueByIndex(index: number): TValue; - - /** - * Gets the key at the specified index. - * Returns null if index is out of bounds. - * O(1) - */ - getKey(index: number): TKey; - - /** - * Gets an iterator over keys. - * O(1) - */ - getKeys(): IIterator; - - /** - * Returns the zero-based index of the specified key in a SortedList object. - * Returns -1 if the key is not found. - * O(log n) - */ - indexOfKey(key: TKey): number; - - /** - * Adds the specified key and value to the sorted list. - * O(n) - */ - add(key: TKey, value: TValue): void; - - /** - * Removes a value from the sorted list. - * Returns true if the value got removed, false otherwise. - * O(n) - */ - remove(key: TKey): boolean; -} - -export interface KeyValue { - key: TKey; - value: TValue; -} - -export class SortedList implements ISortedList { - - private static DEFAULT_COMPARATOR = function(first: TKey, second: TKey) { - return first < second ? -1 : first > second ? 1 : 0; - }; - - private keys: TKey[]; - private values: TValue[]; - private comparator: (first: TKey, second: TKey) => number; - - constructor(comparator?: (first: TKey, second: TKey) => number) { - this.keys = []; - this.values = []; - this.comparator = comparator || SortedList.DEFAULT_COMPARATOR; - } - - public get count(): number { - return this.keys.length; - } - - public getValueByIndex(index: number): TValue { - if (0 <= index && index < this.values.length) { - return this.values[index]; - } - - return null; - } - - public getKey(index: number): TKey { - if (0 <= index && index < this.keys.length) { - return this.keys[index]; - } - - return null; - } - - public getKeys(): IIterator { - return new ListIterator(this.keys); - } - - public getValue(key: TKey): TValue { - if (!key) { - throw new Error('Key must be defined.'); - } - var indexOfKey = this.indexOfKey(key); - if (indexOfKey >= 0) { - return this.values[indexOfKey]; - } - - return null; - } - - public getValues(): IIterator { - return new ListIterator(this.values); - } - - public indexOfKey(key: TKey): number { - if (!key) { - throw new Error('Key must be defined.'); - } - return Math.max(-1, Arrays.binarySearch(this.keys, key, this.comparator)); - } - - public add(key: TKey, value: TValue): void { - if (!key || !value) { - throw new Error('Key and value must be defined.'); - } - - var position = 0; - while (position < this.keys.length && this.comparator(key, this.keys[position]) > 0) { - position++; - } - - this.keys.splice(position, 0, key); - this.values.splice(position, 0, value); - } - - public remove(key: TKey): boolean { - if (!key) { - throw new Error('Key must be defined.'); - } - var indexOfKey = this.indexOfKey(key); - if (indexOfKey >= 0) { - this.values.splice(indexOfKey, 1); - this.keys.splice(indexOfKey, 1); - } - - return indexOfKey >= 0; - } - - public getIterator(): IIterator> { - return new SortedListIterator(this.keys, this.values); - } -} - -class SortedListIterator implements IIterator> { - private keys: TKey[]; - private values: TValue[]; - private index: number; - - constructor(keys: TKey[], values: TValue[]) { - this.keys = keys; - this.values = values; - this.index = -1; - } - - public get current(): KeyValue { - if (this.index < 0 || this.keys.length < this.index) { - return null; - } - - return { - key: this.keys[this.index], - value: this.values[this.index] - }; - } - - public moveNext(): boolean { - this.index++; - return this.index < this.keys.length; - } - - public hasNext(): boolean { - return this.index + 1 < this.keys.length; - } - - public reset(): void { - this.index = -1; - } - - public dispose(): void { - this.keys = null; - this.values = null; - } -} - -class ListIterator implements IIterator { - private values: TValue[]; - private index: number; - - constructor(values: TValue[]) { - this.values = values; - this.index = -1; - } - - public get current(): TValue { - if (this.index < 0 || this.values.length < this.index) { - return null; - } - - return this.values[this.index]; - } - - public moveNext(): boolean { - this.index++; - return this.index < this.values.length; - } - - public hasNext(): boolean { - return this.index + 1 < this.values.length; - } - - public reset(): void { - this.index = -1; - } - - public dispose(): void { - this.values = null; - } -} \ No newline at end of file diff --git a/src/vs/base/test/common/sortedList.test.ts b/src/vs/base/test/common/sortedList.test.ts deleted file mode 100644 index d4e9a65234a..00000000000 --- a/src/vs/base/test/common/sortedList.test.ts +++ /dev/null @@ -1,111 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -'use strict'; - -import * as assert from 'assert'; -import SortedList = require('vs/base/common/sortedList'); - -function getCuisineKeys(): string[] { - return ['german', 'swiss', 'french', 'italian', 'english','scotish', 'turksih', 'hungarian', 'serbian', 'swedish', 'russian', - 'portugesse', 'american', 'japenesse', 'chinesse', 'bosnian', 'makedonian', 'libanese', 'mexican', 'thailand']; -} - -function getCuisineValues(): string[] { - return ['beer', 'cheese', 'wine', 'pizza', 'chips', 'whiskey', 'kebab', 'gulash', 'pljeska', 'salmon', 'vodka', - 'shrimp', 'burger', 'susshi', 'sechuan', 'cevapcici', 'burek', 'falafel', 'burito', 'curryduck']; -} - -function shouldThrow(func: (key: any) => any, key: any): void { - try { - func(key); - assert(false); - } catch (e) { - assert(true); - } -} - -var keys = getCuisineKeys(); -var values = getCuisineValues(); - -function getCuisineList(withoutIterator:boolean = false): SortedList.ISortedList { - var result = new SortedList.SortedList(withoutIterator ? null : (first: string, second: string) => { - return first.localeCompare(second); - }); - for (var i = 0; i < keys.length; i++) { - result.add(keys[i], values[i]); - } - - return result; -} - -suite('SortedList', () => { - test('sorted list add', function() { - var sortedList = getCuisineList(); - assert.equal(sortedList.count, keys.length); - - for (var i = 0; i < keys.length; i++) { - assert.equal(sortedList.getValue(keys[i]), values[i]); - assert(sortedList.indexOfKey(keys[i]) >= 0); - assert.notEqual(sortedList.getValueByIndex(i), null); - } - }); - - test('sorted list add with default comparator', function() { - var sortedList = getCuisineList(true /* without comparator */); - assert.equal(sortedList.count, keys.length); - - for (var i = 0; i < keys.length; i++) { - assert.equal(sortedList.getValue(keys[i]), values[i]); - assert(sortedList.indexOfKey(keys[i]) >= 0); - assert.notEqual(sortedList.getValueByIndex(i), null); - } - }); - - test('sorted list remove', function() { - var sortedList = getCuisineList(); - for (var i = 0; i < keys.length; i++) { - assert(sortedList.remove(keys[i])); - } - assert.equal(sortedList.count, 0); - }); - - test('sorted list iterator', function() { - var sortedList = getCuisineList(); - // cast due to TS bug. - var iterator = sortedList.getIterator(); - var elementCount = 0; - while (iterator.moveNext()) { - elementCount++; - assert(keys.indexOf(iterator.current.key) >= 0); - assert(values.indexOf(iterator.current.value) >= 0); - } - assert.equal(elementCount, sortedList.count); - - iterator.reset(); - elementCount = 0; - assert(iterator.hasNext()); - while (iterator.moveNext()) { - elementCount++; - assert(keys.indexOf(iterator.current.key) >= 0); - assert(values.indexOf(iterator.current.value) >= 0); - } - assert(!iterator.hasNext()); - assert.equal(elementCount, sortedList.count); - }); - - test('sorted list bad op', function() { - var sortedList = getCuisineList(); - assert.equal(sortedList.getKey(127), null); - assert.equal(sortedList.getKey(-1), null); - assert.equal(sortedList.getValue('banana'), null); - assert.equal(sortedList.remove('unexistingKey'), false); - assert.equal(sortedList.getValueByIndex(-4), null); - assert.equal(sortedList.getValueByIndex(1114), null); - assert.equal(sortedList.indexOfKey('fakeKey'), -1); - shouldThrow(sortedList.indexOfKey, null); - shouldThrow(sortedList.getValue, null); - shouldThrow(sortedList.remove, null); - }); -}); \ No newline at end of file -- GitLab