mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Created type definitions and tests for heap: https://www.npmjs.com/package/heap
This commit is contained in:
parent
3a45891ed4
commit
4e5a848013
96
heap/heap-tests.ts
Normal file
96
heap/heap-tests.ts
Normal file
@ -0,0 +1,96 @@
|
||||
/// <reference path="heap.d.ts" />
|
||||
|
||||
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<number> = new Heap<number>();
|
||||
numberHeap = new Heap<number>(numberComparator);
|
||||
|
||||
var stringHeap: Heap<string> = new Heap<string>();
|
||||
stringHeap = new Heap<string>(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);
|
||||
84
heap/heap.d.ts
vendored
Normal file
84
heap/heap.d.ts
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
// Type definitions for heap 0.2.6
|
||||
// Project: https://github.com/qiao/heap.js
|
||||
// Definitions by: Ryan McNamara <https://github.com/ryan10132>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare class Heap<T> {
|
||||
|
||||
// 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<T>
|
||||
copy(): Heap<T>
|
||||
|
||||
// Static Methods
|
||||
|
||||
// Push item onto array, maintaining the heap invariant.
|
||||
static push<T>(array: T[], item: T, cmp?: (a: T, b: T) => number): void;
|
||||
|
||||
// Pop the smallest item off the array, maintaining the heap invariant.
|
||||
static pop<T>(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<T>(array: T[], item: T, cmp?: (a: T, b: T) => number): T;
|
||||
|
||||
// Fast version of a heappush followed by a heappop.
|
||||
static pushpop<T>(array: T[], item: T, cmp?: (a: T, b: T) => number): T;
|
||||
|
||||
// Build the heap.
|
||||
static heapify<T>(array: T[], cmp?: (a: T, b: T) => number): Heap<T>;
|
||||
|
||||
// Update the position of the given item in the heap. This function should be called every time the item is being modified.
|
||||
static updateItem<T>(array: T[], item: T, cmp?: (a: T, b: T) => number): void;
|
||||
|
||||
// Find the n largest elements in a dataset.
|
||||
static nlargest<T>(array: T[], n: number, cmp?: (a: T, b: T) => number): T[];
|
||||
|
||||
// Find the n smallest elements in a dataset.
|
||||
static nsmallest<T>(array: T[], n: number, cmp?: (a: T, b: T) => number): T[];
|
||||
|
||||
}
|
||||
|
||||
declare module 'heap' {
|
||||
export = Heap;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user