diff --git a/hashset/hashset-tests.ts b/hashset/hashset-tests.ts new file mode 100644 index 000000000..fda2e4d1a --- /dev/null +++ b/hashset/hashset-tests.ts @@ -0,0 +1,36 @@ +/// + +class Point { + constructor(public x: number, public y: number) { + } +} + +function hashPoint(p: Point) { + return "Point:" + p.x + "," + p.y; +} + +function pointsEqual(p1: Point, p2: Point) { + return p1.x === p2.x && p1.y === p2.y; +} + +var points = new HashSet({ hashCode: hashPoint, equals: pointsEqual }); + +points.add(new Point(1, 2)); +points.add(new Point(2, 3)); +points.add(new Point(1, 2)); +points.add(new Point(6, 5)); + +alert(points.size()); // Alerts 3 + +var otherPoints = new HashSet({ hashCode: hashPoint, equals: pointsEqual }); + +otherPoints.add(new Point(4, 4)); +otherPoints.add(new Point(7, 9)); +otherPoints.add(new Point(2, 3)); +otherPoints.add(new Point(6, 5)); + +var intersection = points.intersection(otherPoints); + +alert(intersection.contains(new Point(2, 3))); // Alerts true +alert(intersection.contains(new Point(7, 9))); // Alerts false +alert(intersection.size()); // Alerts 2 diff --git a/hashset/hashset.d.ts b/hashset/hashset.d.ts new file mode 100644 index 000000000..cca5af535 --- /dev/null +++ b/hashset/hashset.d.ts @@ -0,0 +1,38 @@ +// Type definitions for jshashset 3.0 +// Project: http://www.timdown.co.uk/jshashtable/jshashset.html +// Definitions by: Sergey Gerasimov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +interface IHashSet +{ + add(value: TValue): void; + addAll(arr: TValue[]): void; + contains(value: TValue): boolean; + + clear(): void; + isEmpty(): boolean; values(): TValue[]; + + remove(value: TValue): void; + size(): number; + + clone(): IHashSet; + + isSubsetOf(set: IHashSet): boolean; + intersection(set: IHashSet): IHashSet; + union(set: IHashSet): IHashSet; + complement(set: IHashSet): IHashSet; +} + +interface IHashSetStatic { + new (): IHashSet; + new (options: IHashtableOptions): IHashSet; + new (hashCode?: (value: TValue) => any, equals?: (value1: TValue, value2: TValue) => boolean): IHashSet; +} + +declare var HashSet: IHashSetStatic; + +declare module "hashset" { + export = HashSet; +} \ No newline at end of file