CollectionHelper
Provides extension methods and helper utilities for working with collections.
メソッド
public static void AddRange(BitArray array, bool[] bits)
CollectionHelper.AddRange(array, bits)
Adds a range of bits to an existing BitArray.
- array
- The BitArray to extend.
- bits
- The bits to add.
public static void AddRange<T>(ICollection<T> source, IEnumerable<T> items)
CollectionHelper.AddRange(source, items)
Adds a range of items to a collection, using optimized methods when available.
- source
- The collection to add items to.
- items
- The items to add.
public static void Add<TKey, TValue>(IDictionary<TKey, TValue> dict, ValueTuple<TKey, TValue> tuple)
CollectionHelper.Add(dict, tuple)
Adds a value tuple as a key-value pair to a dictionary.
- dict
- The dictionary to add to.
- tuple
- The value tuple to add.
public static ICollection<TTarget> AsDuckTypedCollection<TSource, TTarget>(ICollection<TSource> source, Func<TSource, TTarget> sourceToTarget, Func<TTarget, TSource> targetToSource)
result = CollectionHelper.AsDuckTypedCollection(source, sourceToTarget, targetToSource)
Creates a collection adapter that converts between ICollection and ICollection using duck typing.
- source
- The source collection to wrap.
- sourceToTarget
- Function to convert from source type to target type.
- targetToSource
- Function to convert from target type to source type.
戻り値: An ICollection adapter around the source collection.
public static void Bind<T>(INotifyList<T> source, IList<T> destination)
CollectionHelper.Bind(source, destination)
Binds a source notifying list to a destination list, synchronizing additions, removals, insertions, and clear operations.
- source
- The source list that notifies changes.
- destination
- The destination list to synchronize with.
public static T ConcatEx<T, TItem>(T first, T second)
result = CollectionHelper.ConcatEx(first, second)
Concatenates two collections of the same type into a new instance.
- first
- The first collection.
- second
- The second collection.
戻り値: A new collection containing all elements from both input collections.
public static T[] CopyAndClear<T>(ICollection<T> items)
result = CollectionHelper.CopyAndClear(items)
Copies all items from a collection to an array and clears the collection.
- items
- The collection to copy and clear.
戻り値: An array containing the copied items.
public static void CopyTo<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source, IDictionary<TKey, TValue> destination)
CollectionHelper.CopyTo(source, destination)
Copies key-value pairs from a sequence to a dictionary.
- source
- The sequence of key-value pairs.
- destination
- The dictionary to copy to.
public static int Count2(IEnumerable source)
result = CollectionHelper.Count2(source)
Counts the number of elements in a non-generic sequence.
- source
- The sequence to count.
戻り値: The number of elements in the sequence.
public static int DamerauLevenshteinDistance<T>(T[] source, T[] target, int threshold)
result = CollectionHelper.DamerauLevenshteinDistance(source, target, threshold)
Computes the Damerau-Levenshtein distance between two sequences, with a threshold to limit computation.
- source
- The first sequence.
- target
- The second sequence.
- threshold
- The maximum distance to compute; returns int.MaxValue if exceeded.
戻り値: The Damerau-Levenshtein distance, or int.MaxValue if it exceeds the threshold.
public static T ElementAtFromEndOrDefault<T>(IEnumerable<T> source, int index)
result = CollectionHelper.ElementAtFromEndOrDefault(source, index)
Retrieves an element from a sequence counting from the end, or the default value if out of range.
- source
- The sequence to query.
- index
- The zero-based index from the end (0 is the last element).
戻り値: The element at the specified position from the end, or the default value if out of range.
public static T ElementAtFromEndOrDefault<T>(SynchronizedLinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEndOrDefault(list, index)
Retrieves an element from a synchronized linked list counting from the end, or the default value if out of range.
- list
- The synchronized linked list to query.
- index
- The zero-based index from the end (0 is the last element).
戻り値: The element at the specified position from the end, or the default value if out of range.
public static T ElementAtFromEndOrDefault<T>(LinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEndOrDefault(list, index)
Retrieves an element from a linked list counting from the end, or the default value if out of range.
- list
- The linked list to query.
- index
- The zero-based index from the end (0 is the last element).
戻り値: The element at the specified position from the end, or the default value if out of range.
public static T ElementAtFromEnd<T>(SynchronizedLinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEnd(list, index)
Retrieves an element from a synchronized linked list counting from the end.
- list
- The synchronized linked list to query.
- index
- The zero-based index from the end (0 is the last element).
戻り値: The element at the specified position from the end.
public static T ElementAtFromEnd<T>(LinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEnd(list, index)
Retrieves an element from a linked list counting from the end.
- list
- The linked list to query.
- index
- The zero-based index from the end (0 is the last element).
戻り値: The element at the specified position from the end.
public static T ElementAtFromEnd<T>(IEnumerable<T> source, int index)
result = CollectionHelper.ElementAtFromEnd(source, index)
Retrieves an element from a sequence counting from the end.
- source
- The sequence to query.
- index
- The zero-based index from the end (0 is the last element).
戻り値: The element at the specified position from the end.
public static T? ElementAtOr<T>(IEnumerable<T> source, int index)
result = CollectionHelper.ElementAtOr(source, index)
Returns the element at a specified index in a sequence, or null if the index is out of range (for value types).
- source
- The sequence to query.
- index
- The zero-based index of the element to retrieve.
戻り値: The element at the specified index, or null if the index is out of range.
public static T FirstOr<T>(IEnumerable<T> source, T alternate)
result = CollectionHelper.FirstOr(source, alternate)
Returns the first element of a sequence, or a specified alternate value if the sequence is empty.
- source
- The sequence to query.
- alternate
- The value to return if the sequence is empty.
戻り値: The first element, or if the sequence is empty.
public static T? FirstOr<T>(IEnumerable<T> source)
result = CollectionHelper.FirstOr(source)
Returns the first element of a sequence, or null if the sequence is empty (for value types).
- source
- The sequence to query.
戻り値: The first element, or null if the sequence is empty.
public static void ForEach<T>(IEnumerable<T> source, Action<T> action)
CollectionHelper.ForEach(source, action)
Applies an action to each element in a sequence.
- source
- The sequence to iterate over.
- action
- The action to apply to each element.
public static int FromBits(bool[] bits, int startBit)
result = CollectionHelper.FromBits(bits, startBit)
Converts an array of bits to an integer starting from a specified position.
- bits
- The array of bits to convert.
- startBit
- The starting bit position (0-based).
戻り値: The integer value represented by the bits.
public static int FromBits(bool[] bits)
result = CollectionHelper.FromBits(bits)
Converts an array of bits to an integer starting from the beginning.
- bits
- The array of bits to convert.
戻り値: The integer value represented by the bits.
public static long FromBits2(bool[] bits)
result = CollectionHelper.FromBits2(bits)
Converts an array of bits to a long integer starting from the beginning.
- bits
- The array of bits to convert.
戻り値: The long integer value represented by the bits.
public static long FromBits2(bool[] bits, int startBit)
result = CollectionHelper.FromBits2(bits, startBit)
Converts an array of bits to a long integer starting from a specified position.
- bits
- The array of bits to convert.
- startBit
- The starting bit position (0-based).
戻り値: The long integer value represented by the bits.
public static TValue GetAndRemove<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.GetAndRemove(dict, key)
Retrieves a value from a dictionary by key and removes the key-value pair.
- dict
- The dictionary to operate on.
- key
- The key of the value to retrieve and remove.
戻り値: The value associated with the key.
public static int GetHashCodeEx<T>(IEnumerable<T> collection)
result = CollectionHelper.GetHashCodeEx(collection)
Computes a hash code for a sequence of elements.
- collection
- The sequence to compute the hash code for.
戻り値: A hash code for the sequence.
private static int GetIndexFromEnd<T>(IEnumerable<T> source, int index)
result = CollectionHelper.GetIndexFromEnd(source, index)
Calculates the index from the start of a sequence given an index from the end.
- source
- The sequence to query.
- index
- The zero-based index from the end.
戻り値: The zero-based index from the start.
public static IEnumerable<TKey> GetKeys<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TValue value)
result = CollectionHelper.GetKeys(dictionary, value)
Retrieves all keys in a dictionary that map to a specific value.
- dictionary
- The dictionary to query.
- value
- The value to match.
戻り値: An enumerable of keys associated with the specified value.
public static IEnumerable<TKey> GetKeys<TKey, TValue>(SynchronizedDictionary<TKey, TValue> dictionary, TValue value)
result = CollectionHelper.GetKeys(dictionary, value)
Retrieves all keys in a synchronized dictionary that map to a specific value.
- dictionary
- The synchronized dictionary to query.
- value
- The value to match.
戻り値: An enumerable of keys associated with the specified value.
public static bool HasNullItem<T>(IEnumerable<T> items)
result = CollectionHelper.HasNullItem(items)
Determines whether a sequence contains any null items.
- items
- The sequence to check.
戻り値: True if the sequence contains null; otherwise, false.
public static int IndexOf<T>(IEnumerable<T> source, Func<T, bool> predicate)
result = CollectionHelper.IndexOf(source, predicate)
Returns the index of the first element in a sequence that satisfies a specified condition.
- source
- The sequence to search.
- predicate
- The condition to test each element against.
戻り値: The zero-based index of the first matching element, or -1 if no element is found.
public static bool IsEmpty(IEnumerable<char> source)
result = CollectionHelper.IsEmpty(source)
Determines whether a character sequence is null or empty.
- source
- The sequence of characters to check.
戻り値: True if the sequence is null or empty; otherwise, false.
public static bool IsEmpty<T>(T[] source)
result = CollectionHelper.IsEmpty(source)
Determines whether an array is empty.
- source
- The array to check.
戻り値: True if the array is empty; otherwise, false.
public static bool IsEmpty<T>(ICollection<T> source)
result = CollectionHelper.IsEmpty(source)
Determines whether a collection is empty.
- source
- The collection to check.
戻り値: True if the collection is empty; otherwise, false.
public static bool IsEmpty<T>(IEnumerable<T> source)
result = CollectionHelper.IsEmpty(source)
Determines whether a sequence is empty.
- source
- The sequence to check.
戻り値: True if the sequence is empty; otherwise, false.
public static bool IsEmpty<T>(ICircularBuffer<T> buffer)
result = CollectionHelper.IsEmpty(buffer)
Boolean indicating if the ICircularBuffer is empty.
- buffer
- ICircularBuffer
戻り値: True if the buffer is empty; otherwise, false.
public static bool IsEmpty<T>(IEnumerable<T> source, Func<T, bool> predicate)
result = CollectionHelper.IsEmpty(source, predicate)
Determines whether a sequence is empty based on a predicate.
- source
- The sequence to check.
- predicate
- The condition to test each element against.
戻り値: True if no elements match the predicate; otherwise, false.
public static bool IsFull<T>(ICircularBuffer<T> buffer)
result = CollectionHelper.IsFull(buffer)
Boolean indicating if ICircularBuffer is at full capacity. Adding more elements when the buffer is full will cause elements to be removed from the other end of the buffer.
- buffer
- ICircularBuffer
戻り値: True if the buffer is full; otherwise, false.
public static T? LastOr<T>(IEnumerable<T> source)
result = CollectionHelper.LastOr(source)
Returns the last element of a sequence, or null if the sequence is empty (for value types).
- source
- The sequence to query.
戻り値: The last element, or null if the sequence is empty.
public static IEnumerable<T> OrderByDescending<T>(IEnumerable<T> values)
result = CollectionHelper.OrderByDescending(values)
Orders a sequence of values in descending order using the default comparer.
- values
- The sequence to order.
戻り値: An ordered sequence of the input values in descending order.
public static IEnumerable<T> OrderBy<T>(IEnumerable<T> values)
result = CollectionHelper.OrderBy(values)
Orders a sequence of values in ascending order using the default comparer.
- values
- The sequence to order.
戻り値: An ordered sequence of the input values.
public static IOrderedEnumerable<T> OrderBy<T>(IEnumerable<T> collection, Comparison<T> comparison)
result = CollectionHelper.OrderBy(collection, comparison)
Sorts a collection using a Comparison delegate.
- collection
- The collection to sort.
- comparison
- The comparison delegate to determine order.
戻り値: An ordered enumerable of the collection.
public static IEnumerable<TValue[]> Permutations<TKey, TValue>(IEnumerable<TKey> keys, Func<TKey, IEnumerable<TValue>> selector)
result = CollectionHelper.Permutations(keys, selector)
Generates all possible permutations of values selected from keys.
- keys
- The sequence of keys.
- selector
- The function to select possible values for each key.
戻り値: An enumerable of arrays representing all permutations.
public static void RemoveRange<T>(ICollection<T> source, IEnumerable<T> items)
CollectionHelper.RemoveRange(source, items)
Removes a range of items from a collection, using optimized methods when available.
- source
- The collection to remove items from.
- items
- The items to remove.
public static int RemoveWhere2<T>(IList<T> list, Func<T, bool> filter)
result = CollectionHelper.RemoveWhere2(list, filter)
Removes items from a list that match a specified filter and adjusts the list size.
- list
- The list to remove items from.
- filter
- The condition to identify items to remove.
戻り値: The number of items removed.
public static IEnumerable<T> RemoveWhere<T>(ICollection<T> collection, Func<T, bool> filter)
result = CollectionHelper.RemoveWhere(collection, filter)
Removes items from a collection that match a specified filter and returns the removed items.
- collection
- The collection to remove items from.
- filter
- The condition to identify items to remove.
戻り値: An enumerable of the removed items.
public static Task<ValueTuple<TValue, bool>> SafeAddAsyncEx<TKey, TValue>(IDictionary<TKey, TValue> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsyncEx(dictionary, sync, key, handler, cancellationToken)
Asynchronously adds a new key to a dictionary with a value generated by a handler if it doesn't exist, using a reader-writer lock.
- dictionary
- The dictionary to operate on.
- sync
- The reader-writer lock for synchronization.
- key
- The key to add or retrieve.
- handler
- The asynchronous function to generate a value if the key is new.
- cancellationToken
- The cancellation token to cancel the operation.
戻り値: The existing or newly created value.
public static Task<TValue> SafeAddAsync<TKey, TValue>(IDictionary<TKey, TaskCompletionSource<TValue>> dictionary, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsync(dictionary, key, handler, cancellationToken)
Asynchronously adds a new key to a dictionary of task completion sources, ensuring only one task is created per key, using synchronized locking.
- dictionary
- The dictionary mapping keys to task completion sources.
- key
- The key to add or retrieve.
- handler
- The asynchronous function to generate a value if the key is new.
- cancellationToken
- The cancellation token to cancel the operation.
戻り値: The task representing the value for the key.
public static Task<TValue> SafeAddAsync<TKey, TValue>(IDictionary<TKey, TaskCompletionSource<TValue>> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsync(dictionary, sync, key, handler, cancellationToken)
Asynchronously adds a new key to a dictionary of task completion sources, ensuring only one task is created per key, using a reader-writer lock.
- dictionary
- The dictionary mapping keys to task completion sources.
- sync
- The reader-writer lock for synchronization.
- key
- The key to add or retrieve.
- handler
- The asynchronous function to generate a value if the key is new.
- cancellationToken
- The cancellation token to cancel the operation.
戻り値: The task representing the value for the key.
public static Task<TValue> SafeAddAsync<TKey, TValue>(IDictionary<TKey, TValue> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsync(dictionary, sync, key, handler, cancellationToken)
Asynchronously adds a new key to a dictionary with a value generated by a handler if it doesn't exist, using a reader-writer lock.
- dictionary
- The dictionary to operate on.
- sync
- The reader-writer lock for synchronization.
- key
- The key to add or retrieve.
- handler
- The asynchronous function to generate a value if the key is new.
- cancellationToken
- The cancellation token to cancel the operation.
戻り値: The existing or newly created value.
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key)
result = CollectionHelper.SafeAdd(dictionary, key)
Adds a new key to a dictionary with a default value if it doesn't exist, and returns the value.
- dictionary
- The dictionary to operate on.
- key
- The key to add or retrieve.
戻り値: The existing or newly created value.
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> handler, bool isNew)
result = CollectionHelper.SafeAdd(dictionary, key, handler, isNew)
Adds a new key to a dictionary with a value generated by a handler if it doesn't exist, indicating whether it was new.
- dictionary
- The dictionary to operate on.
- key
- The key to add or retrieve.
- handler
- The function to generate a value if the key is new.
- isNew
- When this method returns, indicates whether the key was newly added.
戻り値: The existing or newly created value.
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> handler)
result = CollectionHelper.SafeAdd(dictionary, key, handler)
Adds a new key to a dictionary with a value generated by a handler if it doesn't exist.
- dictionary
- The dictionary to operate on.
- key
- The key to add or retrieve.
- handler
- The function to generate a value if the key is new.
戻り値: The existing or newly created value.
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, bool isNew)
result = CollectionHelper.SafeAdd(dictionary, key, isNew)
Adds a new key to a dictionary with a default value if it doesn't exist, indicating whether it was new.
- dictionary
- The dictionary to operate on.
- key
- The key to add or retrieve.
- isNew
- When this method returns, indicates whether the key was newly added.
戻り値: The existing or newly created value.
public static Task<IEnumerable<T1>> SelectManyAsync<T, T1>(IEnumerable<T> enumeration, Func<T, Task<IEnumerable<T1>>> func)
result = CollectionHelper.SelectManyAsync(enumeration, func)
Asynchronously flattens a sequence of tasks that each return a sequence into a single sequence.
- enumeration
- The source sequence of elements.
- func
- The asynchronous function to transform each element into a sequence.
戻り値: A task that resolves to a flattened sequence of results.
public static IEnumerable<T> SelectMany<T>(IEnumerable<IEnumerable<T>> values)
result = CollectionHelper.SelectMany(values)
Flattens a sequence of sequences into a single sequence.
- values
- The sequence of sequences to flatten.
戻り値: A single sequence containing all elements from the input sequences.
public static bool SequenceEqual<T>(IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer)
result = CollectionHelper.SequenceEqual(first, second, comparer)
Determines whether two sequences are equal using a custom equality comparer function.
- first
- The first sequence to compare.
- second
- The second sequence to compare.
- comparer
- The function to determine equality between elements.
戻り値: True if the sequences are equal; otherwise, false.
public static T SingleWhenOnly<T>(IEnumerable<T> source)
result = CollectionHelper.SingleWhenOnly(source)
Returns the single element of a sequence if it contains exactly one element, otherwise returns the default value.
- source
- The sequence to query.
戻り値: The single element if the sequence has exactly one; otherwise, the default value of .
public static void SyncDo<TCollection>(TCollection collection, Action<TCollection> action)
CollectionHelper.SyncDo(collection, action)
Executes an action on a synchronized collection with thread-safe access.
- collection
- The synchronized collection to operate on.
- action
- The action to execute.
public static TResult SyncGet<TCollection, TResult>(TCollection collection, Func<TCollection, TResult> func)
result = CollectionHelper.SyncGet(collection, func)
Executes a function on a synchronized collection with thread-safe access.
- collection
- The synchronized collection to operate on.
- func
- The function to execute.
戻り値: The result of the function.
public static SynchronizedList<T> Sync<T>(IList<T> list)
result = CollectionHelper.Sync(list)
Converts a list to a synchronized list, or returns it if already synchronized.
- list
- The list to synchronize.
戻り値: A SynchronizedList containing the list's elements, or null if is null.
public static SynchronizedSet<T> Sync<T>(HashSet<T> list)
result = CollectionHelper.Sync(list)
Converts a hash set to a synchronized set.
- list
- The hash set to synchronize.
戻り値: A SynchronizedSet containing the set's elements, or null if is null.
public static SynchronizedDictionary<TKey, TValue> Sync<TKey, TValue>(IDictionary<TKey, TValue> dict)
result = CollectionHelper.Sync(dict)
Converts a dictionary to a synchronized dictionary, or returns it if already synchronized.
- dict
- The dictionary to synchronize.
戻り値: A SynchronizedDictionary containing the dictionary's elements, or null if is null.
public static bool[] ToBits(double value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)
Converts a double value to a specified range of bits.
- value
- The double value to convert.
- startBit
- The starting bit position (0-based).
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the specified range.
public static bool[] ToBits(double value, int count)
result = CollectionHelper.ToBits(value, count)
Converts a double value to a specified number of bits.
- value
- The double value to convert.
- count
- The number of bits to return.
戻り値: An array of bits representing the value.
public static bool[] ToBits(double value)
result = CollectionHelper.ToBits(value)
Converts a double value to its binary representation as an array of bits.
- value
- The double value to convert.
戻り値: An array of 64 bits representing the value.
public static bool[] ToBits(float value, int bitCount)
result = CollectionHelper.ToBits(value, bitCount)
Converts a float value to a specified number of bits.
- value
- The float value to convert.
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the value.
public static bool[] ToBits(float value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)
Converts a float value to a specified range of bits.
- value
- The float value to convert.
- startBit
- The starting bit position (0-based).
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the specified range.
public static bool[] ToBits(long value, int bitCount)
result = CollectionHelper.ToBits(value, bitCount)
Converts a long value to a specified number of bits.
- value
- The long value to convert.
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the value.
public static bool[] ToBits(long value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)
Converts a long value to a specified range of bits.
- value
- The long value to convert.
- startBit
- The starting bit position (0-based).
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the specified range.
public static bool[] ToBits(int value)
result = CollectionHelper.ToBits(value)
Converts an int value to its binary representation as an array of bits.
- value
- The int value to convert.
戻り値: An array of 32 bits representing the value.
public static bool[] ToBits(int value, int bitCount)
result = CollectionHelper.ToBits(value, bitCount)
Converts an int value to a specified number of bits.
- value
- The int value to convert.
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the value.
public static bool[] ToBits(int value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)
Converts an int value to a specified range of bits.
- value
- The int value to convert.
- startBit
- The starting bit position (0-based).
- bitCount
- The number of bits to return.
戻り値: An array of bits representing the specified range.
public static bool[] ToBits(float value)
result = CollectionHelper.ToBits(value)
Converts a float value to its binary representation as an array of bits.
- value
- The float value to convert.
戻り値: An array of 32 bits representing the value.
public static bool[] ToBits(long value)
result = CollectionHelper.ToBits(value)
Converts a long value to its binary representation as an array of bits.
- value
- The long value to convert.
戻り値: An array of 64 bits representing the value.
public static IEqualityComparer<T> ToComparer<T>(Func<T, T, bool> comparer)
result = CollectionHelper.ToComparer(comparer)
Converts a function to an IEqualityComparer instance.
- comparer
- The function to determine equality between two objects.
戻り値: An IEqualityComparer instance.
public static IComparer<T> ToComparer<T>(Func<T, T, int> comparer)
result = CollectionHelper.ToComparer(comparer)
Converts a function to an IComparer instance.
- comparer
- The function to compare two objects.
戻り値: An IComparer instance.
public static IComparer<T> ToComparer<T>(Comparison<T> comparer)
result = CollectionHelper.ToComparer(comparer)
Converts a Comparison delegate to an IComparer instance.
- comparer
- The comparison delegate.
戻り値: An IComparer instance.
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
result = CollectionHelper.ToDictionary(source, comparer)
Converts a sequence of key-value pairs to a dictionary with a specified equality comparer.
- source
- The sequence of key-value pairs.
- comparer
- The equality comparer for keys.
戻り値: A dictionary containing the key-value pairs.
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<Tuple<TKey, TValue>> source)
result = CollectionHelper.ToDictionary(source)
Converts a sequence of tuples to a dictionary using the default equality comparer.
- source
- The sequence of tuples.
戻り値: A dictionary containing the key-value pairs from the tuples.
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<Tuple<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
result = CollectionHelper.ToDictionary(source, comparer)
Converts a sequence of tuples to a dictionary with a specified equality comparer.
- source
- The sequence of tuples.
- comparer
- The equality comparer for keys.
戻り値: A dictionary containing the key-value pairs from the tuples.
public static IDictionary<TKey, IEnumerable<TValue>> ToDictionary<TKey, TValue>(IEnumerable<IGrouping<TKey, TValue>> grouping)
result = CollectionHelper.ToDictionary(grouping)
Converts a grouping into a dictionary where each key maps to its group of values.
- grouping
- The grouping to convert.
戻り値: A dictionary mapping keys to their groups.
public static IDictionary<TKey, TValue> ToDictionary<TSource, TKey, TValue>(IEnumerable<TSource> source, Func<TSource, int, TKey> keySelector, Func<TSource, int, TValue> valueSelector)
result = CollectionHelper.ToDictionary(source, keySelector, valueSelector)
Converts a sequence into a dictionary using selector functions for keys and values.
- source
- The source sequence.
- keySelector
- The function to extract keys from elements.
- valueSelector
- The function to extract values from elements.
戻り値: A dictionary containing the transformed key-value pairs.
public static Func<T, T, int> ToFunc<T>(Comparison<T> comparer)
result = CollectionHelper.ToFunc(comparer)
Converts a Comparison delegate to a function.
- comparer
- The comparison delegate.
戻り値: A function that compares two objects.
public static ISet<string> ToIgnoreCaseSet(IEnumerable<string> values)
result = CollectionHelper.ToIgnoreCaseSet(values)
Converts a sequence of strings to a case-insensitive set.
- values
- The sequence of strings to convert.
戻り値: A set containing the unique strings, ignoring case.
public static PairSet<TKey, TValue> ToPairSet<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source)
result = CollectionHelper.ToPairSet(source)
Converts a sequence of key-value pairs into a PairSet with the default equality comparer.
- source
- The sequence of key-value pairs.
戻り値: A PairSet containing the key-value pairs.
public static PairSet<TKey, TValue> ToPairSet<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
result = CollectionHelper.ToPairSet(source, comparer)
Converts a sequence of key-value pairs into a PairSet with a specified equality comparer.
- source
- The sequence of key-value pairs.
- comparer
- The equality comparer for keys.
戻り値: A PairSet containing the key-value pairs.
public static PairSet<TKey, TValue> ToPairSet<TSource, TKey, TValue>(IEnumerable<TSource> source, Func<TSource, int, TKey> keySelector, Func<TSource, int, TValue> valueSelector)
result = CollectionHelper.ToPairSet(source, keySelector, valueSelector)
Converts a sequence into a PairSet using selector functions for keys and values.
- source
- The source sequence.
- keySelector
- The function to extract keys from elements.
- valueSelector
- The function to extract values from elements.
戻り値: A PairSet containing the transformed key-value pairs.
public static KeyValuePair<TKey, TValue> ToPair<TKey, TValue>(ValueTuple<TKey, TValue> _)
result = CollectionHelper.ToPair(_)
Converts a value tuple to a key-value pair.
- _
- The value tuple to convert.
戻り値: A KeyValuePair containing the tuple's key and value.
public static KeyValuePair<TKey, TValue> ToPair<TKey, TValue>(Tuple<TKey, TValue> pair)
result = CollectionHelper.ToPair(pair)
Converts a tuple to a key-value pair.
- pair
- The tuple to convert.
戻り値: A key-value pair containing the tuple's items.
public static Tuple<TKey, TValue> ToTuple<TKey, TValue>(KeyValuePair<TKey, TValue> pair)
result = CollectionHelper.ToTuple(pair)
Converts a key-value pair to a tuple.
- pair
- The key-value pair to convert.
戻り値: A tuple containing the key and value.
public static bool TryAdd2<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, TValue value)
result = CollectionHelper.TryAdd2(dict, key, value)
Adds a key-value pair to a dictionary if the key does not already exist.
- dict
- The dictionary to add the pair to.
- key
- The key to add.
- value
- The value to add.
戻り値: True if the pair was added; false if the key already existed.
public static void TryAdd<T>(ICollection<T> collection, IEnumerable<T> values)
CollectionHelper.TryAdd(collection, values)
Adds a range of values to a collection, ensuring no duplicates if the collection already contains them.
- collection
- The collection to add values to.
- values
- The values to add.
public static bool TryAdd<T>(ICollection<T> collection, T value)
result = CollectionHelper.TryAdd(collection, value)
Adds a value to a collection if it does not already exist.
- collection
- The collection to add the value to.
- value
- The value to add.
戻り値: True if the value was added; false if it already existed.
public static T? TryDequeue2<T>(Queue<T> queue)
result = CollectionHelper.TryDequeue2(queue)
Attempts to dequeue an item from a queue, returning null if the queue is empty (for value types).
- queue
- The queue to dequeue from.
戻り値: The dequeued item if available; otherwise, null.
public static T? TryDequeue2<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryDequeue2(queue)
Attempts to dequeue an item from a synchronized queue, returning null if the queue is empty (for value types).
- queue
- The synchronized queue to dequeue from.
戻り値: The dequeued item if available; otherwise, null.
public static T TryDequeue<T>(Queue<T> queue)
result = CollectionHelper.TryDequeue(queue)
Attempts to dequeue an item from a queue, returning null if the queue is empty (for reference types).
- queue
- The queue to dequeue from.
戻り値: The dequeued item if available; otherwise, null.
public static T TryDequeue<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryDequeue(queue)
Attempts to dequeue an item from a synchronized queue, returning null if the queue is empty (for reference types).
- queue
- The synchronized queue to dequeue from.
戻り値: The dequeued item if available; otherwise, null.
public static TValue? TryGetAndRemove2<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetAndRemove2(dict, key)
Attempts to retrieve a value from a dictionary by key and remove it, returning null if not found (for value types).
- dict
- The dictionary to operate on.
- key
- The key of the value to retrieve and remove.
戻り値: The value if found and removed; otherwise, null.
public static TValue TryGetAndRemove<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetAndRemove(dict, key)
Attempts to retrieve a value from a dictionary by key and remove it, returning the default value if not found.
- dict
- The dictionary to operate on.
- key
- The key of the value to retrieve and remove.
戻り値: The value if found and removed; otherwise, the default value of .
public static bool TryGetAndRemove<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, TValue value)
result = CollectionHelper.TryGetAndRemove(dict, key, value)
Attempts to retrieve a value from a dictionary by key and remove it, indicating success.
- dict
- The dictionary to operate on.
- key
- The key of the value to retrieve and remove.
- value
- When this method returns, contains the value if found; otherwise, the default value.
戻り値: True if the value was found and removed; otherwise, false.
public static TKey? TryGetKey2<TKey, TValue>(PairSet<TKey, TValue> pairSet, TValue value)
result = CollectionHelper.TryGetKey2(pairSet, value)
Attempts to retrieve a key from a pair set by value, returning null if not found (for value-type keys).
- pairSet
- The pair set to query.
- value
- The value to look up.
戻り値: The key if found; otherwise, null.
public static TKey TryGetKey<TKey, TValue>(PairSet<TKey, TValue> pairSet, TValue value)
result = CollectionHelper.TryGetKey(pairSet, value)
Attempts to retrieve a key from a pair set by value, returning the default value if not found.
- pairSet
- The pair set to query.
- value
- The value to look up.
戻り値: The key if found; otherwise, the default value of .
public static TValue? TryGetValue2<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetValue2(dict, key)
Attempts to retrieve a value from a dictionary by key, returning null if not found (for value types).
- dict
- The dictionary to query.
- key
- The key to look up.
戻り値: The value if found; otherwise, null.
public static TValue TryGetValue<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetValue(dict, key)
Attempts to retrieve a value from a dictionary by key, returning the default value if not found.
- dict
- The dictionary to query.
- key
- The key to look up.
戻り値: The value if found; otherwise, the default value of .
public static T? TryPeek2<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryPeek2(queue)
Attempts to peek at the next item in a synchronized queue, returning null if the queue is empty (for value types).
- queue
- The synchronized queue to peek into.
戻り値: The next item if available; otherwise, null.
public static T? TryPeek2<T>(Queue<T> queue)
result = CollectionHelper.TryPeek2(queue)
Attempts to peek at the next item in a queue, returning null if the queue is empty (for value types).
- queue
- The queue to peek into.
戻り値: The next item if available; otherwise, null.
public static T TryPeek<T>(Queue<T> queue)
result = CollectionHelper.TryPeek(queue)
Attempts to peek at the next item in a queue, returning null if the queue is empty (for reference types).
- queue
- The queue to peek into.
戻り値: The next item if available; otherwise, null.
public static T TryPeek<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryPeek(queue)
Attempts to peek at the next item in a synchronized queue, returning null if the queue is empty (for reference types).
- queue
- The synchronized queue to peek into.
戻り値: The next item if available; otherwise, null.
public static IDictionary<TKey, TValue> TypedAs<TKey, TValue>(IDictionary dictionary)
result = CollectionHelper.TypedAs(dictionary)
Converts a non-generic dictionary to a strongly-typed dictionary.
- dictionary
- The non-generic dictionary.
戻り値: A strongly-typed dictionary with the same key-value pairs.
public static IEnumerable<T> WhereNotNull<T>(IEnumerable<T> enu)
result = CollectionHelper.WhereNotNull(enu)
Filters a sequence of nullable elements to exclude null values.
- enu
- Source sequence.
戻り値: The sequence of non-null elements.
public static IEnumerable<T> WhereNotNull<T>(IEnumerable<T?> enu)
result = CollectionHelper.WhereNotNull(enu)
Filters a sequence of nullable elements to exclude null values.
- enu
- Source sequence.
戻り値: The sequence of non-null elements.
public static IEnumerable<TSource> WhereWithPrevious<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, bool> predicate)
result = CollectionHelper.WhereWithPrevious(source, predicate)
Filters a sequence based on a predicate that compares each element with its previous element.
- source
- The sequence to filter.
- predicate
- The function to test each element against its previous element.
戻り値: An enumerable of elements where the predicate returns true when compared to the previous element.