diff --git a/src/vs/base/common/collections.ts b/src/vs/base/common/collections.ts index 0fd34ece7f59aae1efedddeaa5f9e58f9ae65996..fa21886b07fa66ff010b657c64a3797964bc55ab 100644 --- a/src/vs/base/common/collections.ts +++ b/src/vs/base/common/collections.ts @@ -31,10 +31,10 @@ export function createNumberDictionary():INumberDictionary { /** * Looks up and returns a property that is owned - * by the provided map object. + * by the provided map object. * @param what The key. * @param from A native JavaScript object that stores items. - * @param alternate A default value this is return in case an item with + * @param alternate A default value this is return in case an item with * the key isn't found. */ export function lookup(from:IStringDictionary, what:string, alternate?:T):T; @@ -74,14 +74,14 @@ export function lookupOrInsert(from:any, stringOrNumber:any, alternate:any):T */ export function insert(into: IStringDictionary, data: T, hashFn: (data: T) => string): void; export function insert(into: INumberDictionary, data: T, hashFn: (data: T) => string): void; -export function insert(into: any, data: T, hashFn: (data: T) => string): void { +export function insert(into: any, data: T, hashFn: (data: T) => string): void { into[hashFn(data)] = data; } var hasOwnProperty = Object.prototype.hasOwnProperty; /** - * Returns {{true}} iff the provided object contains a property + * Returns {{true}} iff the provided object contains a property * with the given name. */ export function contains(from:IStringDictionary, what:string):boolean; @@ -90,25 +90,6 @@ export function contains(from:any, what:any):boolean { return hasOwnProperty.call(from, what); } - -export function keys(from:IStringDictionary):IIterable; -export function keys(from:INumberDictionary):IIterable; -export function keys(from:any):IIterable { - - return { - every: function(callback:(element:any)=>boolean):boolean { - for (var key in from) { - if (hasOwnProperty.call(from, key)) { - if(!callback(key)) { - return false; - } - } - } - return true; - } - }; -} - /** * Returns an array which contains all values that reside * in the given set. @@ -167,145 +148,3 @@ export function groupBy(data: T[], groupFn: (element: T) => string): IStringD data.forEach(element => lookupOrInsert(result, groupFn(element), []).push(element)); return result; } - - -/** - * An iterable of a given type. This iterable is - * compatible with the JavaScript array. - */ -export interface IIterable { - - /** - * Iterates over every element in the array - * as long as the callback does not return some - * 'falsy' value. - * @param callback A function that is called for each element - * @return {{true}} if every element has been visited, - * {{false}} if it returned early - */ - every(callback:(element:E)=>boolean):boolean; -} - -export var EmptyIterable:IIterable = { - every: function(callback) { - return true; - } -}; - -export function combine(iterables:IIterable[]):IIterable { - var len = iterables.length; - if(len === 0) { - return EmptyIterable; - } else if(len === 1) { - return iterables[0]; - } - return { - every: function(callback:(element:E)=>any) { - for(var i = 0; i < len; i++) { - if(!iterables[i].every(callback)) { - return false; - } - } - return true; - } - }; -} - -export function singleton(element:E):IIterable { - return { - every: function(callback) { - return callback(element); - } - }; -} - -export function toArray(iterable:IIterable):E[] { - if(Array.isArray(iterable)) { - return iterable; - } else { - var result:E[] = []; - iterable.every((e) => { - result.push(e); - return true; - }); - return result; - } -} - -///** -// * ECMAScript 6 iterator -// */ -//export interface IIterator { -// next(): { done: boolean; value?: T; }; -//} -// -//export function empty():IIterator { -// return { -// next: function() { return { done: true }; } -// }; -//} -// -//export function iterator(array: T[]): IIterator { -// var i = 0; -// return { -// next: () => { -// if(i < array.length) { -// return { -// done: false, -// value: array[i++] -// }; -// } else { -// return { -// done: true -// }; -// } -// } -// }; -//} - -interface ICacheRow { - element: T; - onRemove: ()=>void; -} - -/** - * Limited size cache. Provided a certain cache size limit, it - * removes the older elements as new ones are inserted. - */ -export class LimitedSizeCache { - - private cache: { [id: string]: ICacheRow }; - private order: string[]; - - constructor(private size: number) { - this.cache = Object.create(null); - this.order = []; - } - - public get(id: string): T { - var result = this.cache[id]; - return result && result.element; - } - - public put(id: string, element: T, onRemove: ()=>void): void { - var existing = this.cache[id]; - var row: ICacheRow = { element: element, onRemove: onRemove }; - - this.cache[id] = row; - - if (!existing) { - this.order.push(id); - } - - this.swipe(); - } - - private swipe(): void { - while (this.order.length > this.size) { - var id = this.order.shift(); - var row = this.cache[id]; - row.onRemove(); - delete this.cache[id]; - } - } -} \ No newline at end of file