Skip to content

Commit 291f889

Browse files
committed
hashset definitions
1 parent 22112f3 commit 291f889

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

hashset/hashset-tests.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// <reference path="hashset.d.ts" />
2+
3+
class Point {
4+
constructor(public x: number, public y: number) {
5+
}
6+
}
7+
8+
function hashPoint(p: Point) {
9+
return "Point:" + p.x + "," + p.y;
10+
}
11+
12+
function pointsEqual(p1: Point, p2: Point) {
13+
return p1.x === p2.x && p1.y === p2.y;
14+
}
15+
16+
var points = new HashSet<Point>({ hashCode: hashPoint, equals: pointsEqual });
17+
18+
points.add(new Point(1, 2));
19+
points.add(new Point(2, 3));
20+
points.add(new Point(1, 2));
21+
points.add(new Point(6, 5));
22+
23+
alert(points.size()); // Alerts 3
24+
25+
var otherPoints = new HashSet<Point>({ hashCode: hashPoint, equals: pointsEqual });
26+
27+
otherPoints.add(new Point(4, 4));
28+
otherPoints.add(new Point(7, 9));
29+
otherPoints.add(new Point(2, 3));
30+
otherPoints.add(new Point(6, 5));
31+
32+
var intersection = points.intersection(otherPoints);
33+
34+
alert(intersection.contains(new Point(2, 3))); // Alerts true
35+
alert(intersection.contains(new Point(7, 9))); // Alerts false
36+
alert(intersection.size()); // Alerts 2

hashset/hashset.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Type definitions for jshashset 3.0
2+
// Project: http://www.timdown.co.uk/jshashtable/jshashset.html
3+
// Definitions by: Sergey Gerasimov <https://github.com/gerichhome/>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/// <reference path="../hashtable/hashtable.d.ts" />
7+
8+
interface IHashSet<TValue>
9+
{
10+
add(value: TValue): void;
11+
addAll(arr: TValue[]): void;
12+
contains(value: TValue): boolean;
13+
14+
clear(): void;
15+
isEmpty(): boolean; values(): TValue[];
16+
17+
remove(value: TValue): void;
18+
size(): number;
19+
20+
clone(): IHashSet<TValue>;
21+
22+
isSubsetOf(set: IHashSet<TValue>): boolean;
23+
intersection(set: IHashSet<TValue>): IHashSet<TValue>;
24+
union(set: IHashSet<TValue>): IHashSet<TValue>;
25+
complement(set: IHashSet<TValue>): IHashSet<TValue>;
26+
}
27+
28+
interface IHashSetStatic {
29+
new <TValue>(): IHashSet<TValue>;
30+
new <TValue>(options: IHashtableOptions<TValue>): IHashSet<TValue>;
31+
new <TValue>(hashCode?: (value: TValue) => any, equals?: (value1: TValue, value2: TValue) => boolean): IHashSet<TValue>;
32+
}
33+
34+
declare var HashSet: IHashSetStatic;
35+
36+
declare module "hashset" {
37+
export = HashSet;
38+
}

0 commit comments

Comments
 (0)