diff --git a/heap/heap-tests.ts b/heap/heap-tests.ts new file mode 100644 index 0000000000..605425893c --- /dev/null +++ b/heap/heap-tests.ts @@ -0,0 +1,96 @@ +/// + +var numberComparator = (a: number, b: number) => { return a.toString().length - b.toString().length; }; +var stringComparator = (a: string, b: string) => { return a.length - b.length; }; + +// Test constructor +var numberHeap: Heap = new Heap(); +numberHeap = new Heap(numberComparator); + +var stringHeap: Heap = new Heap(); +stringHeap = new Heap(stringComparator); + +// Test instance methods +numberHeap.push(0); +stringHeap.push("foo"); +numberHeap.insert(0); +stringHeap.insert("foo"); + +var numberIdentifier: number = numberHeap.pop(); +var stringIdentifier: string = stringHeap.pop(); + +numberIdentifier = numberHeap.replace(1); +stringIdentifier = stringHeap.replace("bar"); + +numberIdentifier = numberHeap.pushpop(2); +stringIdentifier = stringHeap.pushpop("bar"); + +numberHeap.heapify(); +stringHeap.heapify(); + +numberHeap.updateItem(2); +stringHeap.updateItem("bar"); + +var booleanIdentifier: boolean = numberHeap.empty(); +booleanIdentifier = stringHeap.empty(); + +numberIdentifier = numberHeap.size(); +numberIdentifier = stringHeap.size(); + +var numberArray: number[] = numberHeap.toArray(); +var stringArray: string[] = stringHeap.toArray(); + +numberHeap = numberHeap.clone(); +numberHeap = numberHeap.copy(); + +stringHeap = stringHeap.clone(); +stringHeap = stringHeap.copy(); + +// Test static methods +Heap.push(numberArray, 3); +Heap.push(numberArray, 3, numberComparator); + +Heap.push(stringArray, "foo"); +Heap.push(stringArray, "foo", stringComparator); + +numberIdentifier = Heap.pop(numberArray); +numberIdentifier = Heap.pop(numberArray, numberComparator); + +stringIdentifier = Heap.pop(stringArray); +stringIdentifier = Heap.pop(stringArray, stringComparator); + +numberIdentifier = Heap.replace(numberArray, 1); +numberIdentifier = Heap.replace(numberArray, 1, numberComparator); + +stringIdentifier = Heap.replace(stringArray, "foo"); +stringIdentifier = Heap.replace(stringArray, "foo", stringComparator); + +numberIdentifier = Heap.pushpop(numberArray, 1); +numberIdentifier = Heap.pushpop(numberArray, 1, numberComparator); + +stringIdentifier = Heap.pushpop(stringArray, "foo"); +stringIdentifier = Heap.pushpop(stringArray, "foo", stringComparator); + +Heap.heapify(numberArray); +Heap.heapify(numberArray, numberComparator); + +Heap.heapify(stringArray); +Heap.heapify(stringArray, stringComparator); + +Heap.updateItem(numberArray, 1); +Heap.updateItem(numberArray, 1, numberComparator); + +Heap.updateItem(stringArray, "foo"); +Heap.updateItem(stringArray, "foo", stringComparator); + +Heap.nlargest(numberArray, 1); +Heap.nlargest(numberArray, 1, numberComparator); + +Heap.nlargest(stringArray, 1); +Heap.nlargest(stringArray, 1, stringComparator); + +Heap.nsmallest(numberArray, 1); +Heap.nsmallest(numberArray, 1, numberComparator); + +Heap.nsmallest(stringArray, 1); +Heap.nsmallest(stringArray, 1, stringComparator); diff --git a/heap/heap.d.ts b/heap/heap.d.ts new file mode 100644 index 0000000000..3ec573d1cc --- /dev/null +++ b/heap/heap.d.ts @@ -0,0 +1,84 @@ +// Type definitions for heap 0.2.6 +// Project: https://github.com/qiao/heap.js +// Definitions by: Ryan McNamara +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare class Heap { + + // Constructor + + constructor(cmp?: (a: T, b: T) => number); + + // Instance Methods + + // Push item onto heap. + push(item: T): void; + insert(item: T): void; + + // Pop the smallest item off the heap and return it. + pop(): T; + + // Return the smallest item of the heap. + peek(): T; + top(): T; + front(): T; + + // Pop and return the current smallest value, and add the new item. + // This is more efficient than pop() followed by push(), and can be more appropriate when using a fixed size heap. + // Note that the value returned may be larger than item! + replace(item: T): T; + + // Fast version of a push followed by a pop. + pushpop(item: T): T; + + // Rebuild the heap. This method may come handy when the priority of the internal data is being modified. + heapify(): void; + + // Update the position of the given item in the heap. This function should be called every time the item is being modified. + updateItem(item: T): void; + + // Determine whether the heap is empty. + empty(): boolean; + + // Get the number of elements stored in the heap. + size(): number; + + // Return the array representation of the heap. (note: the array is a shallow copy of the heap's internal nodes) + toArray(): T[]; + + // Return a clone of the heap. (note: the internal data is a shallow copy of the original one) + clone(): Heap + copy(): Heap + + // Static Methods + + // Push item onto array, maintaining the heap invariant. + static push(array: T[], item: T, cmp?: (a: T, b: T) => number): void; + + // Pop the smallest item off the array, maintaining the heap invariant. + static pop(array: T[], cmp?: (a: T, b: T) => number): T; + + // Pop and return the current smallest value, and add the new item. + // This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed size heap. Note that the value returned may be larger than item! + static replace(array: T[], item: T, cmp?: (a: T, b: T) => number): T; + + // Fast version of a heappush followed by a heappop. + static pushpop(array: T[], item: T, cmp?: (a: T, b: T) => number): T; + + // Build the heap. + static heapify(array: T[], cmp?: (a: T, b: T) => number): Heap; + + // Update the position of the given item in the heap. This function should be called every time the item is being modified. + static updateItem(array: T[], item: T, cmp?: (a: T, b: T) => number): void; + + // Find the n largest elements in a dataset. + static nlargest(array: T[], n: number, cmp?: (a: T, b: T) => number): T[]; + + // Find the n smallest elements in a dataset. + static nsmallest(array: T[], n: number, cmp?: (a: T, b: T) => number): T[]; + +} + +declare module 'heap' { + export = Heap; +}