Class Utility
Static class containing extension helper methods for various built-in C# classes, as well as a static helper method for "swapping" references.
Inheritance
Inherited Members
Namespace: GoRogue
Assembly: GoRogue.dll
Syntax
public static class Utility
Methods
| Improve this Doc View SourceAsReadOnly<K, V>(IDictionary<K, V>)
Adds an AsReadOnly method to System.Collections.Generic.IDictionary<TKey, TValue>, similar to the AsReadOnly method of System.Collections.Generic.IList<T>, that returns a read-only reference to the dictionary.
Declaration
public static ReadOnlyDictionary<K, V> AsReadOnly<K, V>(this IDictionary<K, V> dictionary)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<K, V> | dictionary |
Returns
Type | Description |
---|---|
System.Collections.ObjectModel.ReadOnlyDictionary<K, V> | A ReadOnlyDictionary instance for the specified dictionary. |
Type Parameters
Name | Description |
---|---|
K | Type of keys of the dictionary. |
V | Type of values of the dictionary. |
ExtendToString<T>(T[,], String, String, Func<T, String>, String, String, String, String)
Extension method for 2D arrays that allows retrieving a string representing the contents.
Declaration
public static string ExtendToString<T>(this T[, ] array, string begin = "[\n", string beginRow = "\t[", Func<T, string> elementStringifier = null, string rowSeparator = ",\n", string elementSeparator = ", ", string endRow = "]", string end = "\n]")
Parameters
Type | Name | Description |
---|---|---|
T[,] | array | |
System.String | begin | Character(s) that should precede the string representation of the 2D array. |
System.String | beginRow | Character(s) that should precede the string representation of each row. |
System.Func<T, System.String> | elementStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type T. |
System.String | rowSeparator | Character(s) used to separate each row from the next. |
System.String | elementSeparator | Character(s) used to separate each element from the next. |
System.String | endRow | Character(s) that should follow the string representation of each row. |
System.String | end | Character(s) that should follow the string representation of the 2D array. |
Returns
Type | Description |
---|---|
System.String | A string representation of the 2D array. |
Type Parameters
Name | Description |
---|---|
T |
ExtendToString<T>(IEnumerable<T>, String, Func<T, String>, String, String)
Extension method for System.Collections.Generic.IEnumerable<T> that allows retrieving a string representing the contents.
Declaration
public static string ExtendToString<T>(this IEnumerable<T> enumerable, string begin = "[", Func<T, string> elementStringifier = null, string separator = ", ", string end = "]")
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable | |
System.String | begin | Character(s) that should precede the string representation of the IEnumerable's elements. |
System.Func<T, System.String> | elementStringifier | Function to use to get the string representation of each element. Specifying null uses the ToString function of type T. |
System.String | separator | Characters to separate the IEnumerable's elements by. |
System.String | end | Character(s) that should follow the string representation of the IEnumerable's elements. |
Returns
Type | Description |
---|---|
System.String | A string representation of the IEnumerable. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
Built-in C# data structures like System.Collections.Generic.List`1 implement System.Collections.Generic.IEnumerable<T>, and as such this method can be used to stringify the contents of C# built-in data structures.
When no customization paramters are specified, it defaults to a representation looking something like [elem1, elem2, elem3].
ExtendToString<T>(ISet<T>, String, Func<T, String>, String, String)
Extension method for System.Collections.Generic.ISet<T> that allows retrieving a string representing the contents.
Declaration
public static string ExtendToString<T>(this ISet<T> set, string begin = "set(", Func<T, string> elementStringifier = null, string separator = ", ", string end = ")")
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ISet<T> | set | |
System.String | begin | Character(s) that should precede the string representation of the set's elements. |
System.Func<T, System.String> | elementStringifier | Function to use to get the string representation of each element. Specifying null uses the ToString function of type T. |
System.String | separator | Characters to separate the set's items by. |
System.String | end | Character(s) that should follow the string representation of the set's elements. |
Returns
Type | Description |
---|---|
System.String | A string representation of the ISet. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
Built-in C# data structures like System.Collections.Generic.HashSet<T> implement System.Collections.Generic.ISet<T>, and as such this method can be used to stringify the contents of C# built-in set structures.
When no customization paramters are specified, it defaults to a representation looking something like set(elem1, elem2, elem3).
ExtendToString<K, V>(IDictionary<K, V>, String, Func<K, String>, Func<V, String>, String, String, String)
Extension method for dictionaries that allows retrieving a string representing the dictionary's contents.
Declaration
public static string ExtendToString<K, V>(this IDictionary<K, V> dictionary, string begin = "{", Func<K, string> keyStringifier = null, Func<V, string> valueStringifier = null, string kvSeparator = " : ", string pairSeparator = ", ", string end = "}")
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<K, V> | dictionary | |
System.String | begin | Character(s) that should precede the string representation of the dictionary's elements. |
System.Func<K, System.String> | keyStringifier | Function to use to get the string representation of each key. Specifying null uses the ToString function of type K. |
System.Func<V, System.String> | valueStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type V. |
System.String | kvSeparator | Characters used to separate each value from its key. |
System.String | pairSeparator | Characters used to separate each key-value pair from the next. |
System.String | end | Character(s) that should follow the string representation of the dictionary's elements. |
Returns
Type | Description |
---|---|
System.String | A string representation of the IDictionary. |
Type Parameters
Name | Description |
---|---|
K | |
V |
Remarks
Built-in C# data structures like System.Collections.Generic.Dictionary<TKey, TValue> implement System.Collections.Generic.IDictionary<TKey, TValue>, and as such this method can be used to stringify the contents of C# built-in dictionary structures.
When no customization paramters are specified, it defaults to a representation looking something like {key1 : value, key2 : value}.
ExtendToStringGrid<T>(T[,], Int32, String, String, Func<T, String>, String, String, String, String)
Extension method for 2D arrays that allows retrieving a string representing the contents, formatted as if the 2D array represents a coordinate plane/grid.
Declaration
public static string ExtendToStringGrid<T>(this T[, ] array, int fieldSize, string begin = "", string beginRow = "", Func<T, string> elementStringifier = null, string rowSeparator = "\n", string elementSeparator = " ", string endRow = "", string end = "")
Parameters
Type | Name | Description |
---|---|---|
T[,] | array | |
System.Int32 | fieldSize | The amount of space each element should take up in characters. A positive number aligns the text to the right of the space, while a negative number aligns the text to the left. |
System.String | begin | Character(s) that should precede the string representation of the 2D array. |
System.String | beginRow | Character(s) that should precede the string representation of each row. |
System.Func<T, System.String> | elementStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type T. |
System.String | rowSeparator | Character(s) used to separate each row from the next. |
System.String | elementSeparator | Character(s) used to separate each element from the next. |
System.String | endRow | Character(s) that should follow the string representation of each row. |
System.String | end | Character(s) that should follow the string representation of the 2D array. |
Returns
Type | Description |
---|---|
System.String | A string representation of the 2D array, formatted as if the array represents a 2D coordinate plane/grid map. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
This differs from ExtendToString<T>(T[,], String, String, Func<T, String>, String, String, String, String) in that this method prints the array such that array[x+1, y] is printed to the RIGHT of array[x, y], rather than BELOW it. Effectively it assumes the indexes being used are grid/coordinate plane coordinates.
ExtendToStringGrid<T>(T[,], String, String, Func<T, String>, String, String, String, String)
Extension method for 2D arrays that allows retrieving a string representing the contents, formatted as if the 2D array represents a coordinate plane/grid.
Declaration
public static string ExtendToStringGrid<T>(this T[, ] array, string begin = "", string beginRow = "", Func<T, string> elementStringifier = null, string rowSeparator = "\n", string elementSeparator = " ", string endRow = "", string end = "")
Parameters
Type | Name | Description |
---|---|---|
T[,] | array | |
System.String | begin | Character(s) that should precede the string representation of the 2D array. |
System.String | beginRow | Character(s) that should precede the string representation of each row. |
System.Func<T, System.String> | elementStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type T. |
System.String | rowSeparator | Character(s) used to separate each row from the next. |
System.String | elementSeparator | Character(s) used to separate each element from the next. |
System.String | endRow | Character(s) that should follow the string representation of each row. |
System.String | end | Character(s) that should follow the string representation of the 2D array. |
Returns
Type | Description |
---|---|
System.String | A string representation of the 2D array, formatted as if the array represents a 2D coordinate plane/grid map. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
This differs from ExtendToString<T>(T[,], String, String, Func<T, String>, String, String, String, String) in that this method prints the array such that array[x+1, y] is printed to the RIGHT of array[x, y], rather than BELOW it. Effectively it assumes the indexes being used are grid/coordinate plane coordinates.
FisherYatesShuffle<T>(IList<T>, IGenerator)
Extension method for System.Collections.Generic.IList<T> that implements a fisher-yates shuffle. Modifies the list it is called on to randomly rearrange the elements therein.
Declaration
public static void FisherYatesShuffle<T>(this IList<T> list, IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IList<T> | list | |
Troschuetz.Random.IGenerator | rng | RNG to use. Specifying null causes DefaultRNG to be used |
Type Parameters
Name | Description |
---|---|
T |
Multiply(String, Int32)
"Multiplies", aka repeats, a string the given number of times.
Declaration
public static string Multiply(this string str, int numTimes)
Parameters
Type | Name | Description |
---|---|---|
System.String | str | |
System.Int32 | numTimes | The number of times to repeat the string. |
Returns
Type | Description |
---|---|
System.String | The current string repeated |
RandomIndex<T>(IReadOnlyList<T>, Func<Int32, Boolean>, IGenerator)
Extension method that selects and returns a random valid index from the list for which the selector function given returns true, using the rng specified. Indices are repeatedly selected until a qualifying index is found. -1 is returned if the list is empty.
Declaration
public static int RandomIndex<T>(this IReadOnlyList<T> list, Func<int, bool> selector, IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<T> | list | |
System.Func<System.Int32, System.Boolean> | selector | Function that returns true if the given index is valid selection, false otherwise. |
Troschuetz.Random.IGenerator | rng | RNG to use. Specifying null causes DefaultRNG to be used. |
Returns
Type | Description |
---|---|
System.Int32 | Index selected. |
Type Parameters
Name | Description |
---|---|
T |
RandomIndex<T>(IReadOnlyList<T>, IGenerator)
Extension method that selects and returns a random valid index from the list, using the rng specified. -1 is returned if the list is empty.
Declaration
public static int RandomIndex<T>(this IReadOnlyList<T> list, IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<T> | list | |
Troschuetz.Random.IGenerator | rng | RNG to use. Specifying null causes DefaultRNG to be used. |
Returns
Type | Description |
---|---|
System.Int32 | The index selected. |
Type Parameters
Name | Description |
---|---|
T |
RandomItem<T>(IReadOnlyList<T>, Func<T, Boolean>, IGenerator)
Extension method that selects and returns a random item from the list for which the given selector returns true, using the rng specified. Items are repeatedly selected until a qualifying item is found. default(T) is returned if the list is empty.
Declaration
public static T RandomItem<T>(this IReadOnlyList<T> list, Func<T, bool> selector, IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<T> | list | |
System.Func<T, System.Boolean> | selector | Function that returns true if the given item is valid selection, false otherwise. |
Troschuetz.Random.IGenerator | rng | RNG to use. Specifying null causes DefaultRNG to be used. |
Returns
Type | Description |
---|---|
T | Item selected. |
Type Parameters
Name | Description |
---|---|
T |
RandomItem<T>(IReadOnlyList<T>, IGenerator)
Extension method that selects and returns a random item from the list, using the rng specified. default(T) is returned if the list is empty.
Declaration
public static T RandomItem<T>(this IReadOnlyList<T> list, IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<T> | list | |
Troschuetz.Random.IGenerator | rng | RNG to use. Specifying null causes DefaultRNG to be used. |
Returns
Type | Description |
---|---|
T | Item selected. |
Type Parameters
Name | Description |
---|---|
T |
Swap<T>(ref T, ref T)
Swaps the values pointed to by lhs
and rhs
.
Declaration
public static void Swap<T>(ref T lhs, ref T rhs)
Parameters
Type | Name | Description |
---|---|---|
T | lhs | |
T | rhs |
Type Parameters
Name | Description |
---|---|
T |
Yield<T>(T)
Convenience function that yields the given item as a single-item IEnumerable.
Declaration
public static IEnumerable<T> Yield<T>(this T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | An IEnumerable containing only the item the function is called on. |
Type Parameters
Name | Description |
---|---|
T |
Yield<T>(T[])
Takes multiple parameters and converts them to an IEnumerable.
Declaration
public static IEnumerable<T> Yield<T>(params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T[] | values | Parameters (specified as multiple parameters to the function). |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | An IEnumerable of all of the given items, in the order they were given to the function. |
Type Parameters
Name | Description |
---|---|
T |