Class: LruSet

bu. LruSet

Creates a new LRU set given an equality predicate, hash function and maximum size. An LRU set holds up to a maximum number of items, ordered by their age. When the addition of an item would cause the maximum size to be exceeded, the new item replaces the oldest item in the set. As a special case, an LRU set with maximum size 0 always rejects the insertion of an item.


new LruSet(equals, hash, maxsize)

Parameters:
Name Type Description
equals function

the equality predicate.

hash function

the hash function.

maxsize number

the maximum size.

Source:
bu/collections/lruset.js

Methods


add(item)

Adds an item, replacing either an existing equal item, or the oldest item when the maximum size would be exceeded; the added item becomes the newest. Returns the replaced item if it does not equal the inserted item, otherwise null.

If the maximum size is 0, do nothing and return the item.

Parameters:
Name Type Description
item Object

the item to add.

Source:
bu/collections/lruset.js
Returns:

the replaced item.

Type
Object

clear()

Removes all items from the set.

Source:
bu/collections/lruset.js

each(fn)

Calls fn(item) for each item in the set, in an undefined order. Returns the number of times fn was called. The result is undefined if the set is mutated during iteration.

Parameters:
Name Type Description
fn function

the function to call.

Source:
bu/collections/lruset.js
Returns:

Number of times fn was called.

Type
number

has(item)

Returns whether an item is in the set.

Parameters:
Name Type Description
item Object

the key to search.

Source:
bu/collections/lruset.js
Returns:

True if item exists.

Type
boolean

modulus()

Gets the modulus.

Source:
bu/collections/lruset.js
Returns:

The modulus.

Type
number

remove(item)

Removes an item. Returns the removed item, or null if the item was not found.

Parameters:
Name Type Description
item Object

the item to remove.

Source:
bu/collections/lruset.js
Returns:

the removed item.

Type
Object

size()

Returns the number of items in the set.

Source:
bu/collections/lruset.js
Returns:

Number of items.

Type
number