Collections

Basilisk has good implementations of the most important data structures for application work. All collections are immutable - methods which would mutate them (in normal code) return new versions of the collections.

Where time complexity is stated, recall that log[32] of 1 billion is just less than 6.

Vector

class Vector

The Vector class is a random access data structure which can be accessed by numeric key. Push, pop and set are all O(log[32] n), which makes the time complexity very low for practical datasets.

Note that - as for all Basilisk collections - the constructor is private and the from static method should be used instead.

static Vector.from([source : mixed]) → Vector

Creates a new Vector from the specified source object.

Parameters:source – An array, Vector, or object with a .forEach method. This will be iterated to fill the vector. Passing null will result in an empty Vector.
Vector.length : number

The number of elements in the Vector. This is a pre-computed property, so access is O(1)

Vector.get(index : number) → any

Retrieve the value at a particular position in the Vector. Note that (unlike Javascript arrays) retrieving a position which is outside the range of the collection is an Error. Time complexity: O(log[32] n)

Parameters:index – The position in the vector to return. Must be in the range (-length ; length). Negative indexes are interpreted as being from the end of the vector (ie. .length + index).
Vector.push(value : any) → Vector

Creates a new Vector which has the specified value in its last position. The instance on which it is called is not modified. Time complexity: O(log[32] n)

Vector.set(index : number, value : any) → Vector

Creates a new Vector which has the specified position replaced with the specified value. If the value `===` the current value in that position, will return this. Time complexity: O(log[32] n)

Parameters:index – a number in the range (-length ; length).
Vector.pop() → Vector

Returns a new Vector which has the item in the final position removed. Time complexity: O(log[32] n)

Vector.peek() → any

Returns the last element in the Vector.

Vector.forEach(callback: function (item : any, index : number), context:any)

Iterates over the Vector in order, calling the callback for each element in turn. It is perfectly valid to pass a function which takes fewer arguments (ie. function (item) instead of function (item, key) - this is handled natively by Javascript).

Vector.equals(other : any) → boolean

Checks whether the two Vectors are equal. Each element is checked in turn. If all elements are equal (see equality-protocol)

Parameters:other – Another object to check for equality. If this is not a Vector, this will never return true.

StringMap

class StringMap

A HashMap of strings to any other object. In Typescript, this class is generic on type T of the stored objects.

Note that - as for all Basilisk collections - the constructor is private and the from static method should be used instead.

static StringMap.from([source : mixed]) → StringMap

Create a new StringMap from the specified source object.

If the object is a StringMap, then that object is returned directly.

Finally, the object is iterated using for in and own properties are added to the map.

StringMap.get(key : string[, default: any = undefined]) → any

Retrieve the value stored against the key. If it is not present, then the default will be returned (if none is provided, undefined is returned.)

StringMap.set(key : string, value: any) → StringMap

Returns a new StringMap with the added relation. The original map is not changed.

StringMap.remove(key : string) → StringMap

Returns a new StringMap with the relation removed, if it was ever present. The original map is not changed.

StringMap.has(key : string) → boolean

Returns whether the specified key is set in the map. Note that undefined is a perfectly legitimate value, so “set” is not the same as “not undefined”.

StringMap.forEach(function (value : any, key : string)[, context: any = undefined]) → any

Iterate over the elements of the map in an undefined order. The function will be called with the value and key for each item in turn. Optionally, you can specify a context which will appear as this to the function.

HashMap

class HashMap

A configurable HashMap of values. In Typescript, this class is generic on type T of the stored objects, type K of keys.

Note that - as for all Basilisk collections - the constructor is private and the from static method should be used instead.

static HashMap.from(hashFn: function (key : any) -> Number[, source : mixed]) → Vector

Create a new HashMap from the specified source object. The hashFn will be called every time the hash of a key needs to be evaluated, and should handle any object you might use as a key. basilisk.hashCode is a standard implementation which should handle most important cases.

If the object is a HashMap and its hashFn === the provided function, then it will be returned directly. Otherwise it will be iterated and each key passed through the provided hashFunction.

Finally, the object is iterated using for in and own properties are added to the map.

HashMap.get(key : any[, default: any = undefined]) → any

Retrieve the value stored against the key. If it is not present, then the default will be returned (if none is provided, undefined is returned.)

HashMap.set(key : any, value: any) → HashMap

Returns a new HashMap with the added relation. The original map is not changed.

HashMap.remove(key : any) → HashMap

Returns a new HashMap with the relation removed, if it was ever present. The original map is not changed.

HashMap.has(key : any) → boolean

Returns whether the specified key is set in the map. Note that undefined is a perfectly legitimate value, so “set” is not the same as “not undefined”.

HashMap.forEach(function (value : any, key : any)[, context: any = undefined])

Iterate over the elements of the map in an undefined order. The function will be called with the value and key for each item in turn. Optionally, you can specify a context which will appear as this to the function.

hashCode(key:any) → uint

Generate a hashCode for the provided object. If the object has a hashCode method, that will be called and the return returned. For strings, numbers, booleans, null and undefined, a default hash implementation is used.

If none of the above apply a TypeError is thrown.

Hash functions should be fast, deterministic, and well distributed over the integers.