HashSet and Dictionary in TypeScript: export of Interface -
i think need feedback on collection classes - still learning typescript , javascript , these implementations can surely improved. looking forward suggestion. think use generic types in useful way, advice here appreciated.
the answer looking removing duplicate ihashtable definition end of both snippets , moving own file, cannot done seems. unsure if interface in first place. compiles , works way, far can see.
the collection types incomplete , define basic function @ moment. once sure use language , features correct other functions should not difficult.
here hashset:
import { ihashable } "./ihashable" export class hashset<t extends ihashable> { private _items: hashtable<t>; public constructor() { this._items = {}; } public add(key: t): void { let str: string = key.gethash(); if (this._items[str] == null) { this._items[str] = key; } else { throw new rangeerror("key '" + str + "' exists."); } } public contains(key: t): boolean { let str: string = key.gethash(); return this._items[str] != null; } } interface hashtable<t> { [key: string]: t; }
i wonder if can avoid checking-before-adding in way. javascript-dictionary relies on allow duplicates, avoid them there no other way check myself?
this dictionary:
import { ihashable } "./ihashable" export class dictionary<t1 extends ihashable, t2> { private _items: hashtable<keyvaluepair<t1, t2>>; public constructor() { this._items = {}; } public add(key: t1, value: t2) { let str: string = key.gethash(); if (this._items[str] == null) { let kvp: keyvaluepair<t1, t2> = new keyvaluepair(key, value); this._items[str] = kvp; } else { throw new rangeerror("key '" + str + "' exists."); } } public containskey(key: t1): boolean { let str: string = key.gethash(); return this._items[str] != null; } public get(key: t1): t2 { let str: string = key.gethash(); let kvp: keyvaluepair<t1, t2> = this._items[str]; if (kvp == null) throw new rangeerror("key '" + str + "' not found") return kvp.value; } } export class keyvaluepair<t1 extends ihashable, t2> { private _key: t1; private _value: t2; public key(): t1 { return this._key; } public value(): t2 { return this._value; } public constructor(key: t1, value: t2) { this._key = key; this._value = value; } } interface hashtable<t> { [key: string]: t; }
both rely on definition of ihashable (hashable , hashtable: should find other names.)
export interface ihashable { gethash(): string; }
the dictionary looks bit strange, "wraps" dictionary new type keyvaluepair , uses in javascript dictionary. hope gain doing own type key, in , out, long offers string can indexed. - no idea if makes sense or completly wrong.
what missing count of items in collection, way remove items, , way iterate on keys , values. regarding iterating on post question implementation of list , foreach on it, hoping iterating keys or values might possible in same way.
probably important question forgot here: how gethash-method build own class? going have static number on classes, , count 1 in constructor before assign number each instance. guarantee uniqueness... there better?
thanks tip! ralf
Comments
Post a Comment