Construct a new tree-based dictionary.
const d = new TreeDict<number, string>()
Similar to JavaScript's built-in map type, the constructor accepts a list of key-value pairs that will immediately be added to the resulting dictionary.
const d = new TreeDict<number, string>([
[1, 'one'],
[2, 'two']
])
The dictionary can be tweaked by providing a TreeDictOptions-object, which allows to configure things like the default compare function and value equality.
const d = new TreeDict<number, string>({
compareKeys: (a, b) => a < b,
valuesEqual: (a, b) => a === b,
elements: [[1, 'one'], [2, 'two']]
})
Count the amount of elements in the collection.
⚠️ In most cases, this should be an O(1)
operation. However, there are
cases where this can be an O(n)
operation. Therefore, it is recommended
to always cache the result in a local variable.
Add an element to the collection. If the element already exists, update its value.
The location where the element is placed depends on the collection type, and in the generic case there is no guarantee on the location where it is inserted.
This method returns a pair with the first element indicating whether the element was added, while the second element refers to the actual location of the element.
Optional
hint: anyA transparent object obtained by getAddHint.
A tree-based dictionary that only allows one item with the same key.
The following table summarises the worst-case time complexity of the most commonly used properies of this class. For more information, see the documentation of the respective property.
O(log(n))
O(1)
O(log(n))
O(log(n))
O(log(n))
O(log(n))
O(1)
When a new entry is added with a key that is already taken, the dictionary will replace the corresponding entry with the new one.
If you need to throw an error when a key is already taken, simply use has().
The items in a tree-based dictionary are guaranteed to be sorted on their keys.
See