提交 86e46739 编写于 作者: J Johannes Rieken

remove unused sortedList

上级 59fdb4a4
/*---------------------------------------------------------------------------------------------
* 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<TValue> {
current: TValue;
moveNext(): boolean;
hasNext(): boolean;
reset(): void;
dispose(): void;
}
export interface IIterable<TValue> {
getIterator(): IIterator<TValue>;
}
export interface ISortedList<TKey, TValue> extends IIterable<KeyValue<TKey, TValue>> {
/**
* 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<TValue>;
/**
* 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<TKey>;
/**
* 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<TKey, TValue> {
key: TKey;
value: TValue;
}
export class SortedList<TKey, TValue> implements ISortedList<TKey, TValue> {
private static DEFAULT_COMPARATOR = function<TKey>(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<TKey> {
return new ListIterator<TKey>(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<TValue> {
return new ListIterator<TValue>(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<KeyValue<TKey, TValue>> {
return new SortedListIterator<TKey, TValue>(this.keys, this.values);
}
}
class SortedListIterator<TKey, TValue> implements IIterator<KeyValue<TKey, TValue>> {
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<TKey, TValue> {
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<TValue> implements IIterator<TValue> {
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
/*---------------------------------------------------------------------------------------------
* 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<string, string> {
var result = new SortedList.SortedList<string, string>(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();
// <any> cast due to TS bug.
var iterator = <any>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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册