typescript-collections
Version:
A complete, fully tested data structure library written in TypeScript.
92 lines (91 loc) • 3.43 kB
TypeScript
import * as util from './util';
import Set from './Set';
export default class Bag<T> {
private toStrF;
private dictionary;
private nElements;
/**
* Creates an empty bag.
* @class <p>A bag is a special kind of set in which members are
* allowed to appear more than once.</p>
* <p>If the inserted elements are custom objects a function
* which converts elements to unique strings must be provided. Example:</p>
*
* <pre>
* function petToString(pet) {
* return pet.name;
* }
* </pre>
*
* @constructor
* @param {function(Object):string=} toStrFunction optional function used
* to convert elements to strings. If the elements aren't strings or if toString()
* is not appropriate, a custom function which receives an object and returns a
* unique string must be provided.
*/
constructor(toStrFunction?: (item: T) => string);
/**
* Adds nCopies of the specified object to this bag.
* @param {Object} element element to add.
* @param {number=} nCopies the number of copies to add, if this argument is
* undefined 1 copy is added.
* @return {boolean} true unless element is undefined.
*/
add(element: T, nCopies?: number): boolean;
/**
* Counts the number of copies of the specified object in this bag.
* @param {Object} element the object to search for..
* @return {number} the number of copies of the object, 0 if not found
*/
count(element: T): number;
/**
* Returns true if this bag contains the specified element.
* @param {Object} element element to search for.
* @return {boolean} true if this bag contains the specified element,
* false otherwise.
*/
contains(element: T): boolean;
/**
* Removes nCopies of the specified object to this bag.
* If the number of copies to remove is greater than the actual number
* of copies in the Bag, all copies are removed.
* @param {Object} element element to remove.
* @param {number=} nCopies the number of copies to remove, if this argument is
* undefined 1 copy is removed.
* @return {boolean} true if at least 1 element was removed.
*/
remove(element: T, nCopies?: number): boolean;
/**
* Returns an array containing all of the elements in this big in arbitrary order,
* including multiple copies.
* @return {Array} an array containing all of the elements in this bag.
*/
toArray(): T[];
/**
* Returns a set of unique elements in this bag.
* @return {collections.Set<T>} a set of unique elements in this bag.
*/
toSet(): Set<T>;
/**
* Executes the provided function once for each element
* present in this bag, including multiple copies.
* @param {function(Object):*} callback function to execute, it is
* invoked with one argument: the element. To break the iteration you can
* optionally return false.
*/
forEach(callback: util.ILoopFunction<T>): void;
/**
* Returns the number of elements in this bag.
* @return {number} the number of elements in this bag.
*/
size(): number;
/**
* Returns true if this bag contains no elements.
* @return {boolean} true if this bag contains no elements.
*/
isEmpty(): boolean;
/**
* Removes all of the elements from this bag.
*/
clear(): void;
}