hashset definitions

This commit is contained in:
Sergey Gerasimov
2014-01-09 08:51:27 +04:00
parent 22112f362d
commit 291f88988c
2 changed files with 74 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/// <reference path="hashset.d.ts" />
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<Point>({ 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<Point>({ 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
+38
View File
@@ -0,0 +1,38 @@
// Type definitions for jshashset 3.0
// Project: http://www.timdown.co.uk/jshashtable/jshashset.html
// Definitions by: Sergey Gerasimov <https://github.com/gerichhome/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../hashtable/hashtable.d.ts" />
interface IHashSet<TValue>
{
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<TValue>;
isSubsetOf(set: IHashSet<TValue>): boolean;
intersection(set: IHashSet<TValue>): IHashSet<TValue>;
union(set: IHashSet<TValue>): IHashSet<TValue>;
complement(set: IHashSet<TValue>): IHashSet<TValue>;
}
interface IHashSetStatic {
new <TValue>(): IHashSet<TValue>;
new <TValue>(options: IHashtableOptions<TValue>): IHashSet<TValue>;
new <TValue>(hashCode?: (value: TValue) => any, equals?: (value1: TValue, value2: TValue) => boolean): IHashSet<TValue>;
}
declare var HashSet: IHashSetStatic;
declare module "hashset" {
export = HashSet;
}