API/BidirectionalDictionary

BidirectionalDictionary.New(dict, key_comparison_selector, value_comparison_selector)

Construct and return a new BidirectionalDictionary

Parameters

dict
optional: a BidirectionalDictionary or lua table to populate the new dictionary with
key_comparison_selector
optional: a function to generate a unique key per element
value_comparison_selector
optional: a function to generate a unique value per element

Usage

BidirectionalDictionary.New()
BidirectionalDictionary.New({ a = 1, b = 2 })
BidirectionalDictionary.New({ a = 1, b = 2 }, string.upper)
BidirectionalDictionary.New(nil, string.upper)


BidirectionalDictionary.prototype:Add(key, value)

Add the specific key and value to the BidirectionalDictionary.
This will error if an element with the same key already exists in the BidirectionalDictionary. To avoid erroring in this case, use :Set(key, value) instead. This will error if an element with the same value already exists in the BidirectionalDictionary.

Parameters

key
the key of the element to add
value
the value of the element to add

Usage

dict:Add("key", "value")


BidirectionalDictionary.prototype:Clear()

Clear all elements from the BidirectionalDictionary

Usage

dict:Clear()


BidirectionalDictionary.prototype:Clone()

Make a shallow clone of the Set.

Usage

local other = dict:Clone()


BidirectionalDictionary.prototype:ContainsKey(key)

Return whether a given key is present in the BidirectionalDictionary

Parameters

key
the key to check for

Return value

a boolean

Usage

local found = dict:ContainsKey("key")


BidirectionalDictionary.prototype:ContainsValue(value)

Return whether a given value is present in the BidirectionalDictionary

Parameters

value
the value to check for

Return value

a boolean

Usage

local found = dict:ContainsValue("value")


BidirectionalDictionary.prototype:ConvertToReadOnly()

Make the Set read-only, preventing any further modifications.
There is no way to convert a Set back to being modifiable.

Return value

the same Set that was made read-only

Usage

set:ConvertToReadOnly()


BidirectionalDictionary.prototype:ForEachByPair(action)

Immediately performs an action on each element in the dictionary.
If the action returns false, that will act as a break and prevent any more execution on the sequence.

Parameters

action
a function that takes the key, value, and the 1-based index of the element.

Usage

dict:ForEachByPair(function(key, value, index) end)


BidirectionalDictionary.prototype:Get(key)

Get the specific value in the BidirectionalDictionary given the requested key.
This will error if the key is not in the BidirectionalDictionary.

Parameters

key
the key of the element to get

Return value

the value requested

Usage

local value = dict:Get("key")


BidirectionalDictionary.prototype:GetEnumerator()

Return an enumerator for the BidirectionalDictionary that returns the keys of the dictionary


BidirectionalDictionary.prototype:GetOrDefault(key, default)

Get the specific value or a default value in the BidirectionalDictionary given the requested key.
This will return the default value if not found.

Parameters

key
the key of the element to get
default
the default value to return if the key is not found.

Return value

the value requested or the default value

Usage

local value = dict:GetOrDefault("key", 0)


BidirectionalDictionary.prototype:Inverse()

Return the inverse of this BidirectionalDictionary, with the keys and values swapped.

Usage

local inverse = dict:Inverse()


BidirectionalDictionary.prototype:IsReadOnly()

Return whether the Set is read-only, and thus cannot have modifications made to it.

Return value

a boolean

Usage

local read_only = set:IsReadOnly()


BidirectionalDictionary.prototype:Iterate()

Return a lua iterator that returns the index, key, and value of each element and can be used in for loops.

Usage

for index, key, value in BidirectionalDictionary.New({ a = 1, b = 2 }):Iterate() do end


BidirectionalDictionary.prototype:Keys()

Return an Enumerable of all the keys in the BidirectionalDictionary.
This may not return in a known order.

Return value

an Enumerable

Usage

local keys = dict:Keys()


BidirectionalDictionary.prototype:Merge(other)

Merge another dictionary onto the current BidirectionalDictionary.

Parameters

other
a BidirectionalDictionary to Merge onto the current

Usage

BidirectionalDictionary.From({ alpha = 1, bravo = 2 }):Merge({ bravo = 3, charlie = 4 }) -- BidirectionalDictionary["alpha": 1, "bravo": 3, "charlie": 4]


BidirectionalDictionary.prototype:PickAndRemoveRandom()

Randomly pick a random element in the BidirectionalDictionary, remove it, and return the key and value.
This will error if the Set is empty.

Return values

  1. a random key in the BidirectionalDictionary
  2. a random value in the BidirectionalDictionary

Usage

local key, value = dict:PickAndRemoveRandom()


BidirectionalDictionary.prototype:Remove(key)

Removes the element with the provided key from the BidirectionalDictionary

Parameters

key
the key to remove

Return value

whether the element was removed

Usage

local removed = dict:Remove("key")


BidirectionalDictionary.prototype:RemoveWhere(predicate)

Remove all elements which match a given predicate

Parameters

predicate
a function which is passed each key and value and should return true to remove, false to keep

Return value

the number of elements removed

Usage

local num = dict:RemoveWhere(function(k, v) return v % 2 == 0 end)


BidirectionalDictionary.prototype:SelectByPair(selector)

Project each element of the sequence to a new element.

Parameters

selector
the transform function which takes the key, value, and the 1-based index

Return value

an Enumerable

Usage

dict:SelectByPair(function(key, value, index) return key.."="..value end)


BidirectionalDictionary.prototype:Set(key, value)

Set the specific key and value to the BidirectionalDictionary.
If overriding an existing value, the key will not change or be overridden.

Parameters

key
the key of the element to set
value
the value of the element to set

Usage

dict:Set("key", "value")


BidirectionalDictionary.prototype:SetContract(contract)

Set a contract that will be verified against any existing elements and any added elements or changed values.
This is handy for if you want to verify that all keys are strings and all values are positive integers or something like that. This will call :VerifyContract()

Parameters

contract
a function that is passed the key and value and should return whether the pair is valid.

Usage

dict:SetContract(function(k, v) return type(k) == "string" and type(v) == "number" end)


BidirectionalDictionary.prototype:ToKeyValuePairs()

Return an Enumerable of KeyValuePair representing all the elements of the current BidirectionalDictionary.

Return value

an Enumerable

Usage

local kvps = dict:ToKeyValuePairs()


BidirectionalDictionary.prototype:ToTable(kind)

Convert the BidirectionalDictionary to a simple lua table.
This will be missing values if nil is used.

Parameters

kind
"set" to return a table where all values are true, "list" to return a list-like table, or nil to return a standard dictionary-like table

Usage

BidirectionalDictionary.New({ hey = "there" }):ToTable()["hey"] == "there"
BidirectionalDictionary.New({ hey = "there" }):ToTable("list")[1] == "hey"
BidirectionalDictionary.New({ hey = "there" }):ToTable("set")["hey"] == true


BidirectionalDictionary.prototype:TryGetValue(key)

Try to get the specific value in the BidirectionalDictionary given the requested key.

Parameters

key
the key of the element to get

Return values

  1. whether the get was successful
  2. the value requested

Usage

local success, value = dict:TryGetValue("key")


BidirectionalDictionary.prototype:Values()

Return an Enumerable of all the values in the BidirectionalDictionary.
This may not return in a known order.

Return value

an Enumerable

Usage

local values = dict:Values()


BidirectionalDictionary.prototype:VerifyContract()

Verify that the contract for this BidirectionalDictionary is valid for all elements in the dictionary.
If there is no contract, this does nothing.

Usage

dict:VerifyContract()


Enumerable.prototype:ToBidirectionalDictionary(key_selector, value_selector, key_comparison_selector, value_comparison_selector)

Create and return a new BidirectionalDictionary from the current Enumerable based on the provided selectors.

Parameters

key_selector
a function to get the key of the dictionary from an element
value_selector
optional: a function to get the value of the dictionary from an element
key_comparison_selector
optional: a function to generate a unique key per element
value_comparison_selector
optional: a function to generate a unique value per element

Return value

a BidirectionalDictionary

Usage

Enumerable.From({ "Alpha", "Bravo", "Charlie" }):ToBidirectionalDictionary(function(x) return x:sub(1, 1) end)
Enumerable.From({ "Alpha", "Bravo", "Charlie" }):ToBidirectionalDictionary(function(x) return x:sub(1, 1) end, string.lower)
Enumerable.From({ "Alpha", "Bravo", "Charlie" }):ToBidirectionalDictionary(function(x) return x:sub(1, 1) end, nil, string.upper)
Enumerable.From({ "Alpha", "Bravo", "Charlie" }):ToBidirectionalDictionary(function(x) return x:sub(1, 1) end, nil, nil, string.upper)


KeyValuePair.prototype:Key()

Return the key of the current KeyValuePair

Return value

a value

Usage

local key = kvp:Key()


KeyValuePair.prototype:KeyAndValue()

Return the key and value of the current KeyValuePair

Return values

  1. the key
  2. the value

Usage

local key, value = kvp:KeyAndValue()


KeyValuePair.prototype:Value()

Return the value of the current KeyValuePair

Return value

a value

Usage

local value = kvp:Value()



Comments

Posts Quoted:
Reply
Clear All Quotes