Class Algorithms
Algorithms contains a number of static methods that implement algorithms that work on collections. Most of the methods deal with the standard generic collection interfaces such as IEnumerable<T>, ICollection<T> and IList<T>.
Inheritance
Inherited Members
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
public static class Algorithms
Methods
| Improve this Doc View SourceBinarySearch<T>(IList<T>, T, IComparer<T>, out Int32)
Searches a sorted list for an item via binary search. The list must be sorted by the ordering in the passed instance of IComparer<T>.
Declaration
public static int BinarySearch<T>(IList<T> list, T item, IComparer<T> comparer, out int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The sorted list to search. |
| T | item | The item to search for. |
| System.Collections.Generic.IComparer<T> | comparer | The comparer instance used to sort the list. Only the Compare method is used. |
| System.Int32 | index | Returns the first index at which the item can be found. If the return
value is zero, indicating that |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
BinarySearch<T>(IList<T>, T, Comparison<T>, out Int32)
Searches a sorted list for an item via binary search. The list must be sorted by the ordering in the passed Comparison<T> delegate.
Declaration
public static int BinarySearch<T>(IList<T> list, T item, Comparison<T> comparison, out int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The sorted list to search. |
| T | item | The item to search for. |
| System.Comparison<T> | comparison | The comparison delegate used to sort the list. |
| System.Int32 | index | Returns the first index at which the item can be found. If the return
value is zero, indicating that |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
BinarySearch<T>(IList<T>, T, out Int32)
Searches a sorted list for an item via binary search. The list must be sorted by the natural ordering of the type (it's implementation of IComparable<T>).
Declaration
public static int BinarySearch<T>(IList<T> list, T item, out int index)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The sorted list to search. |
| T | item | The item to search for. |
| System.Int32 | index | Returns the first index at which the item can be found. If the return
value is zero, indicating that |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
CartesianProduct<TFirst, TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)
Computes the cartestian product of two collections: all possible pairs of items, with the first item taken from the first collection and the second item taken from the second collection. If the first collection has N items, and the second collection has M items, the cartesian product will have N * M pairs.
Declaration
public static IEnumerable<Pair<TFirst, TSecond>> CartesianProduct<TFirst, TSecond>(IEnumerable<TFirst> first, IEnumerable<TSecond> second)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<TFirst> | first | The first collection. |
| System.Collections.Generic.IEnumerable<TSecond> | second | The second collection. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<Pair<TFirst, TSecond>> | An IEnumerable<Pair<TFirst, TSecond>> that enumerates the cartesian product of the two collections. |
Type Parameters
| Name | Description |
|---|---|
| TFirst | The type of items in the first collection. |
| TSecond | The type of items in the second collection. |
Concatenate<T>(IEnumerable<T>[])
Concatenates all the items from several collections. The collections need not be of the same type, but must have the same item type.
Declaration
public static IEnumerable<T> Concatenate<T>(params IEnumerable<T>[] collections)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T>[] | collections | The set of collections to concatenate. In many languages, this parameter can be specified as several individual parameters. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable that enumerates all the items in each of the collections, in order. |
Type Parameters
| Name | Description |
|---|---|
| T |
Convert<TSource, TDest>(IEnumerable<TSource>, Converter<TSource, TDest>)
Convert a collection of items by applying a delegate to each item in the collection. The resulting collection
contains the result of applying converter
to each item in sourceCollection
, in
order.
Declaration
public static IEnumerable<TDest> Convert<TSource, TDest>(IEnumerable<TSource> sourceCollection, Converter<TSource, TDest> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<TSource> | sourceCollection | The collection of item being converted. |
| System.Converter<TSource, TDest> | converter | A delegate to the method to call, passing each item in |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<TDest> | The resulting collection from applying |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of items in the collection to convert. |
| TDest | The type each item is being converted to. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
Copy<T>(IEnumerable<T>, T[], Int32)
Copies all of the items from the collection source
to the array dest
, starting
at the index destIndex
.
Declaration
public static void Copy<T>(IEnumerable<T> source, T[] dest, int destIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | source | The collection that provide the source items. |
| T[] | dest | The array to store the items into. |
| System.Int32 | destIndex | The index to begin copying items to. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
| System.ArgumentException | The collection has more items than will fit into the array. In this case, the array has been filled with as many items as fit before the exception is thrown. |
Copy<T>(IEnumerable<T>, T[], Int32, Int32)
Copies at most count
items from the collection source
to the array dest
, starting
at the index destIndex
. The source collection must not be
the destination array or part thereof.
Declaration
public static void Copy<T>(IEnumerable<T> source, T[] dest, int destIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | source | The collection that provide the source items. |
| T[] | dest | The array to store the items into. |
| System.Int32 | destIndex | The index to begin copying items to. |
| System.Int32 | count | The maximum number of items to copy. The array must be large enought to fit this number of items. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
Copy<T>(IEnumerable<T>, IList<T>, Int32)
Copies all of the items from the collection source
to the list dest
, starting
at the index destIndex
. If necessary, the size of the destination list is expanded.
Declaration
public static void Copy<T>(IEnumerable<T> source, IList<T> dest, int destIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | source | The collection that provide the source items. |
| System.Collections.Generic.IList<T> | dest | The list to store the items into. |
| System.Int32 | destIndex | The index to begin copying items to. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
Copy<T>(IEnumerable<T>, IList<T>, Int32, Int32)
Copies at most count
items from the collection source
to the list dest
, starting
at the index destIndex
. If necessary, the size of the destination list is expanded. The source collection must not be
the destination list or part thereof.
Declaration
public static void Copy<T>(IEnumerable<T> source, IList<T> dest, int destIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | source | The collection that provide the source items. |
| System.Collections.Generic.IList<T> | dest | The list to store the items into. |
| System.Int32 | destIndex | The index to begin copying items to. |
| System.Int32 | count | The maximum number of items to copy. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
Copy<T>(IList<T>, Int32, T[], Int32, Int32)
Copies count
items from the list or array source
, starting at the index sourceIndex
,
to the array dest
, starting at the index destIndex
.
The source may be the same as the destination array.
Declaration
public static void Copy<T>(IList<T> source, int sourceIndex, T[] dest, int destIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | source | The list or array that provide the source items. |
| System.Int32 | sourceIndex | The index within |
| T[] | dest | The array to store the items into. |
| System.Int32 | destIndex | The index within |
| System.Int32 | count | The maximum number of items to copy. The destination array must be large enough to hold this many items. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
Copy<T>(IList<T>, Int32, IList<T>, Int32, Int32)
Copies count
items from the list source
, starting at the index sourceIndex
,
to the list dest
, starting at the index destIndex
. If necessary, the size of the destination list is expanded.
The source and destination lists may be the same.
Declaration
public static void Copy<T>(IList<T> source, int sourceIndex, IList<T> dest, int destIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | source | The collection that provide the source items. |
| System.Int32 | sourceIndex | The index within |
| System.Collections.Generic.IList<T> | dest | The list to store the items into. |
| System.Int32 | destIndex | The index within |
| System.Int32 | count | The maximum number of items to copy. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
Count<T>(IEnumerable<T>)
Count the number of items in an IEnumerable<T> collection. If a more specific collection type is being used, it is more efficient to use the Count property, if one is provided.
Declaration
public static int Count<T>(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to count items in. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If the collection implements ICollection<T>, this method simply returns ICollection<T>.Count. Otherwise, it enumerates all items and counts them.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
CountEqual<T>(IEnumerable<T>, T)
Counts the number of items in the collection that are equal to find
.
Declaration
public static int CountEqual<T>(IEnumerable<T> collection, T find)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to count items in. |
| T | find | The item to compare to. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items in the collection that are equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
CountEqual<T>(IEnumerable<T>, T, IEqualityComparer<T>)
Counts the number of items in the collection that are equal to find
.
Declaration
public static int CountEqual<T>(IEnumerable<T> collection, T find, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to count items in. |
| T | find | The item to compare to. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The comparer to use to determine if two items are equal. Only the Equals member function will be called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items in the collection that are equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
|
CountWhere<T>(IEnumerable<T>, Predicate<T>)
Counts the number of items in the collection that satisfy the condition
defined by predicate
.
Declaration
public static int CountWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to count items in. |
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items in the collection that satisfy |
Type Parameters
| Name | Description |
|---|---|
| T |
DisjointSets<T>(IEnumerable<T>, IEnumerable<T>)
Determines if two collections are disjoint, considered as sets. Two sets are disjoint if they have no common items.
Declaration
public static bool DisjointSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsDisjoint method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
DisjointSets<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines if two collections are disjoint, considered as sets. Two sets are disjoint if they have no common items.
Declaration
public static bool DisjointSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparerComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsDisjoint method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
EqualCollections<T>(IEnumerable<T>, IEnumerable<T>)
Determines if the two collections contain equal items in the same order. The two collections do not need to be of the same type; it is permissible to compare an array and an OrderedBag, for instance.
Declaration
public static bool EqualCollections<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to compare. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to compare. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the collections have equal items in the same order. If both collections are empty, true is returned. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collections. |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
EqualCollections<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines if the two collections contain equal items in the same order. The passed instance of IEqualityComparer<T> is used for determining if two items are equal.
Declaration
public static bool EqualCollections<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to compare. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to compare. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals member function of this interface is called. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the collections have equal items in the same order. If both collections are empty, true is returned. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collections. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
EqualCollections<T>(IEnumerable<T>, IEnumerable<T>, BinaryPredicate<T>)
Determines if the two collections contain "equal" items in the same order. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static bool EqualCollections<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to compare. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to compare. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". This predicate can compute any relation between two items; it need not represent equality or an equivalence relation. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collections. |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being tested for need not be equality. For example, the following code determines if each integer in list1 is less than or equal to the corresponding integer in list2.
List<int> list1, list2;
if (EqualCollections(list1, list2, delegate(int x, int y) { return x <= y; }) {
// the check is true...
}
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
EqualSets<T>(IEnumerable<T>, IEnumerable<T>)
Determines if two collections are equal, considered as sets. Two sets are equal if they have have the same items, with order not being significant.
Declaration
public static bool EqualSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the EqualTo method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
EqualSets<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines if two collections are equal, considered as sets. Two sets are equal if they have have the same items, with order not being significant.
Declaration
public static bool EqualSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the EqualTo method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
Exists<T>(IEnumerable<T>, Predicate<T>)
Determines if a collection contains any item that satisfies the condition
defined by predicate
.
Declaration
public static bool Exists<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to check all the items in. |
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the collection contains one or more items that satisfy the condition
defined by |
Type Parameters
| Name | Description |
|---|---|
| T |
Fill<T>(T[], T)
Replaces each item in a array with a given value.
Declaration
public static void Fill<T>(T[] array, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to modify. |
| T | value | The value to fill with. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
Fill<T>(IList<T>, T)
Replaces each item in a list with a given value. The list does not change in size.
Declaration
public static void Fill<T>(IList<T> list, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to modify. |
| T | value | The value to fill with. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
|
| System.ArgumentNullException |
|
FillRange<T>(T[], Int32, Int32, T)
Replaces each item in a part of a array with a given value.
Declaration
public static void FillRange<T>(T[] array, int start, int count, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to modify. |
| System.Int32 | start | The index at which to start filling. The first index in the array has index 0. |
| System.Int32 | count | The number of items to fill. |
| T | value | The value to fill with. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
FillRange<T>(IList<T>, Int32, Int32, T)
Replaces each item in a part of a list with a given value.
Declaration
public static void FillRange<T>(IList<T> list, int start, int count, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to modify. |
| System.Int32 | start | The index at which to start filling. The first index in the list has index 0. |
| System.Int32 | count | The number of items to fill. |
| T | value | The value to fill with. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
FindFirstIndexWhere<T>(IList<T>, Predicate<T>)
Finds the index of the first item in a list that satisfies the condition
defined by predicate
.
Declaration
public static int FindFirstIndexWhere<T>(IList<T> list, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item satisfying the condition. -1 if no such item exists in the list. |
Type Parameters
| Name | Description |
|---|---|
| T |
FindFirstWhere<T>(IEnumerable<T>, Predicate<T>)
Finds the first item in a collection that satisfies the condition
defined by predicate
.
Declaration
public static T FindFirstWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| T | The first item in the collection that matches the condition, or the default value for T (0 or null) if no item that matches the condition is found. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If the default value for T could be present in the collection, and would be matched by the predicate, then this method is inappropriate, because you cannot disguish whether the default value for T was actually present in the collection, or no items matched the predicate. In this case, use TryFindFirstWhere.
See Also
| Improve this Doc View SourceFindIndicesWhere<T>(IList<T>, Predicate<T>)
Enumerates the indices of all the items in list
that satisfy the condition defined
by predicate
.
Declaration
public static IEnumerable<int> FindIndicesWhere<T>(IList<T> list, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to check all the items in. |
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | An IEnumerable<T> that enumerates the indices of items that satisfy the condition. |
Type Parameters
| Name | Description |
|---|---|
| T |
FindLastIndexWhere<T>(IList<T>, Predicate<T>)
Finds the index of the last item in a list that satisfies the condition
defined by predicate
.
Declaration
public static int FindLastIndexWhere<T>(IList<T> list, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item satisfying the condition. -1 if no such item exists in the list. |
Type Parameters
| Name | Description |
|---|---|
| T |
FindLastWhere<T>(IEnumerable<T>, Predicate<T>)
Finds the last item in a collection that satisfies the condition
defined by predicate
.
Declaration
public static T FindLastWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| T | The last item in the collection that matches the condition, or the default value for T (0 or null) if no item that matches the condition is found. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If the collection implements IList<T>, then the list is scanned in reverse until a matching item is found. Otherwise, the entire collection is iterated in the forward direction.
If the default value for T could be present in the collection, and would be matched by the predicate, then this method is inappropriate, because you cannot disguish whether the default value for T was actually present in the collection, or no items matched the predicate. In this case, use TryFindFirstWhere.
See Also
| Improve this Doc View SourceFindWhere<T>(IEnumerable<T>, Predicate<T>)
Enumerates all the items in collection
that satisfy the condition defined
by predicate
.
Declaration
public static IEnumerable<T> FindWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to check all the items in. |
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable<T> that enumerates the items that satisfy the condition. |
Type Parameters
| Name | Description |
|---|---|
| T |
FirstConsecutiveEqual<T>(IList<T>, Int32)
Finds the first occurence of count
consecutive equal items in the
list.
Declaration
public static int FirstConsecutiveEqual<T>(IList<T> list, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to examine. |
| System.Int32 | count | The number of consecutive equal items to look for. The count must be at least 1. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the first run of |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
FirstConsecutiveEqual<T>(IList<T>, Int32, IEqualityComparer<T>)
Finds the first occurence of count
consecutive equal items in the
list. A passed IEqualityComparer is used to determine equality.
Declaration
public static int FirstConsecutiveEqual<T>(IList<T> list, int count, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to examine. |
| System.Int32 | count | The number of consecutive equal items to look for. The count must be at least 1. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the first run of |
Type Parameters
| Name | Description |
|---|---|
| T |
FirstConsecutiveEqual<T>(IList<T>, Int32, BinaryPredicate<T>)
Finds the first occurence of count
consecutive "equal" items in the
list. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static int FirstConsecutiveEqual<T>(IList<T> list, int count, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to examine. |
| System.Int32 | count | The number of consecutive equal items to look for. The count must be at least 1. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the first run of |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being tested for need not be true equality.
FirstConsecutiveWhere<T>(IList<T>, Int32, Predicate<T>)
Finds the first occurence of count
consecutive items in the
list for which a given predicate returns true.
Declaration
public static int FirstConsecutiveWhere<T>(IList<T> list, int count, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to examine. |
| System.Int32 | count | The number of consecutive items to look for. The count must be at least 1. |
| System.Predicate<T> | predicate | The predicate used to test each item. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the first run of |
Type Parameters
| Name | Description |
|---|---|
| T |
FirstIndexOf<T>(IList<T>, T)
Finds the index of the first item in a list equal to a given item.
Declaration
public static int FirstIndexOf<T>(IList<T> list, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
FirstIndexOf<T>(IList<T>, T, IEqualityComparer<T>)
Finds the index of the first item in a list equal to a given item. A passed IEqualityComparer is used to determine equality.
Declaration
public static int FirstIndexOf<T>(IList<T> list, T item, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| T | item | The item to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
FirstIndexOfMany<T>(IList<T>, IEnumerable<T>)
Finds the index of the first item in a list equal to one of several given items.
Declaration
public static int FirstIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | The items to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item equal to any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
FirstIndexOfMany<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)
Finds the index of the first item in a list equal to one of several given items. A passed IEqualityComparer is used to determine equality.
Declaration
public static int FirstIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | The items to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode methods will be called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item equal to any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
FirstIndexOfMany<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)
Finds the index of the first item in a list "equal" to one of several given items. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static int FirstIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | The items to search for. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item "equal" to any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality. This methods finds
first item X which satisfies BinaryPredicate(X,Y), where Y is one of the items in itemsToLookFor
ForEach<T>(IEnumerable<T>, Action<T>)
Performs the specified action on each item in a collection.
Declaration
public static void ForEach<T>(IEnumerable<T> collection, Action<T> action)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
| System.Action<T> | action | An Action delegate which is invoked for each item in |
Type Parameters
| Name | Description |
|---|---|
| T |
GeneratePermutations<T>(IEnumerable<T>)
Generates all the possible permutations of the items in collection
. If collection
has N items, then N factorial permutations will be generated. This method does not compare the items to determine if
any of them are equal. If some items are equal, the same permutation may be generated more than once. For example,
if the collections contains the three items A, A, and B, then this method will generate the six permutations, AAB, AAB,
ABA, ABA, BAA, BAA (not necessarily in that order). To take equal items into account, use the GenerateSortedPermutations
method.
Declaration
public static IEnumerable<T[]> GeneratePermutations<T>(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection of items to permute. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T[]> | An IEnumerable<T[]> that enumerations all the possible permutations of the
items in |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items to permute. |
GenerateSortedPermutations<T>(IEnumerable<T>)
Generates all the possible permutations of the items in collection
, in lexicographical order.
Even if some items are equal, the same permutation will not be generated more than once. For example,
if the collections contains the three items A, A, and B, then this method will generate only the three permutations, AAB, ABA,
BAA.
Declaration
public static IEnumerable<T[]> GenerateSortedPermutations<T>(IEnumerable<T> collection)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection of items to permute. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T[]> | An IEnumerable<T[]> that enumerations all the possible permutations of the
items in |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items to permute. |
GenerateSortedPermutations<T>(IEnumerable<T>, IComparer<T>)
Generates all the possible permutations of the items in collection
, in lexicographical order. A
supplied IComparer<T> instance is used to compare the items.
Even if some items are equal, the same permutation will not be generated more than once. For example,
if the collections contains the three items A, A, and B, then this method will generate only the three permutations, AAB, ABA,
BAA.
Declaration
public static IEnumerable<T[]> GenerateSortedPermutations<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection of items to permute. |
| System.Collections.Generic.IComparer<T> | comparer | The IComparer<T> used to compare the items. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T[]> | An IEnumerable<T[]> that enumerations all the possible permutations of the
items in |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items to permute. |
GenerateSortedPermutations<T>(IEnumerable<T>, Comparison<T>)
Generates all the possible permutations of the items in collection
, in lexicographical order. A
supplied Comparison<T> delegate is used to compare the items.
Even if some items are equal, the same permutation will not be generated more than once. For example,
if the collections contains the three items A, A, and B, then this method will generate only the three permutations, AAB, ABA,
BAA.
Declaration
public static IEnumerable<T[]> GenerateSortedPermutations<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection of items to permute. |
| System.Comparison<T> | comparison | The Comparison<T> delegate used to compare the items. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T[]> | An IEnumerable<T[]> that enumerations all the possible permutations of the
items in |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items to permute. |
GetCollectionEqualityComparer<T>()
Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, considered in order. This is the same notion of equality as in Algorithms.EqualCollections, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.
Declaration
public static IEqualityComparer<IEnumerable<T>> GetCollectionEqualityComparer<T>()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>> | IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality. |
Type Parameters
| Name | Description |
|---|---|
| T |
Examples
The following code creates a Dictionary where the keys are a collection of strings.
Dictionary<IEnumerable<string>, int> =
new Dictionary<IEnumerable<string>, int>(Algorithms.GetCollectionEqualityComparer<string>());
See Also
| Improve this Doc View SourceGetCollectionEqualityComparer<T>(IEqualityComparer<T>)
Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, considered in order. This is the same notion of equality as in Algorithms.EqualCollections, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.
An IEqualityComparer<T> is used to determine if individual T's are equal
Declaration
public static IEqualityComparer<IEnumerable<T>> GetCollectionEqualityComparer<T>(IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | An IEqualityComparer<T> implementation used to compare individual T's. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>> | IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality. |
Type Parameters
| Name | Description |
|---|---|
| T |
Examples
The following code creates a Dictionary where the keys are a collection of strings, compared in a case-insensitive way
Dictionary<IEnumerable<string>, int> =
new Dictionary<IEnumerable<string>, int>(Algorithms.GetCollectionEqualityComparer<string>(StringComparer.CurrentCultureIgnoreCase));
See Also
| Improve this Doc View SourceGetComparerFromComparison<T>(Comparison<T>)
Given a comparison delegate that compares two items of type T, gets an IComparer<T> instance that performs the same comparison.
Declaration
public static IComparer<T> GetComparerFromComparison<T>(Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Comparison<T> | comparison | The comparison delegate to use. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IComparer<T> | An IComparer<T> that performs the same comparing operation
as |
Type Parameters
| Name | Description |
|---|---|
| T |
GetComparisonFromComparer<T>(IComparer<T>)
Given in IComparer<T> instenace that comparers two items from type T, gets a Comparison delegate that performs the same comparison.
Declaration
public static Comparison<T> GetComparisonFromComparer<T>(IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IComparer<T> | comparer | The IComparer<T> instance to use. |
Returns
| Type | Description |
|---|---|
| System.Comparison<T> | A Comparison<T> delegate that performans the same comparing
operation as |
Type Parameters
| Name | Description |
|---|---|
| T |
GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue>)
Creates a delegate that converts keys to values by used a dictionary to map values. Keys that a not present in the dictionary are converted to the default value (zero or null).
Declaration
public static Converter<TKey, TValue> GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary used to perform the conversion. |
Returns
| Type | Description |
|---|---|
| System.Converter<TKey, TValue> | A delegate to a method that converts keys to values. |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
This delegate can be used as a parameter in Convert or ConvertAll methods to convert entire collections.
GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue>, TValue)
Creates a delegate that converts keys to values by used a dictionary to map values. Keys that a not present in the dictionary are converted to a supplied default value.
Declaration
public static Converter<TKey, TValue> GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TValue defaultValue)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary used to perform the conversion. |
| TValue | defaultValue | The result of the conversion for keys that are not present in the dictionary. |
Returns
| Type | Description |
|---|---|
| System.Converter<TKey, TValue> | A delegate to a method that converts keys to values. |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
This delegate can be used as a parameter in Convert or ConvertAll methods to convert entire collections.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
GetIdentityComparer<T>()
Gets an IEqualityComparer<T> instance that can be used to compare objects of type T for object identity only. Two objects compare equal only if they are references to the same object.
Declaration
public static IEqualityComparer<T> GetIdentityComparer<T>()
where T : class
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<T> | An IEqualityComparer<T> instance for identity comparison. |
Type Parameters
| Name | Description |
|---|---|
| T |
GetLexicographicalComparer<T>()
Creates an IComparer instance that can be used for comparing ordered sequences of type T; that is IEnumerable<Tgt;. This comparer can be used for collections or algorithms that use sequences of T as an item type. The Lexicographical ordered of sequences is for comparison.
Declaration
public static IComparer<IEnumerable<T>> GetLexicographicalComparer<T>()
where T : IComparable<T>
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IComparer<System.Collections.Generic.IEnumerable<T>> | At IComparer<IEnumerable<T>> that compares sequences of T. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
T must implement either IComparable<T> and this implementation is used to compare the items.
GetLexicographicalComparer<T>(IComparer<T>)
Creates an IComparer instance that can be used for comparing ordered sequences of type T; that is IEnumerable<Tgt;. This comparer can be uses for collections or algorithms that use sequences of T as an item type. The Lexicographics ordered of sequences is for comparison.
Declaration
public static IComparer<IEnumerable<T>> GetLexicographicalComparer<T>(IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IComparer<T> | comparer | A comparer instance used to compare individual items of type T. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IComparer<System.Collections.Generic.IEnumerable<T>> | At IComparer<IEnumerable<T>> that compares sequences of T. |
Type Parameters
| Name | Description |
|---|---|
| T |
GetLexicographicalComparer<T>(Comparison<T>)
Creates an IComparer instance that can be used for comparing ordered sequences of type T; that is IEnumerable<Tgt;. This comparer can be uses for collections or algorithms that use sequences of T as an item type. The Lexicographics ordered of sequences is for comparison.
Declaration
public static IComparer<IEnumerable<T>> GetLexicographicalComparer<T>(Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Comparison<T> | comparison | A comparison delegate used to compare individual items of type T. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IComparer<System.Collections.Generic.IEnumerable<T>> | At IComparer<IEnumerable<T>> that compares sequences of T. |
Type Parameters
| Name | Description |
|---|---|
| T |
GetReverseComparer<T>(IComparer<T>)
Reverses the order of comparison of an IComparer<T>. The resulting comparer can be used, for example, to sort a collection in descending order. Equality and hash codes are unchanged.
Declaration
public static IComparer<T> GetReverseComparer<T>(IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IComparer<T> | comparer | The comparer to reverse. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IComparer<T> | An IComparer<T> that compares items in the reverse order of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items thta are being compared. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
GetReverseComparison<T>(Comparison<T>)
Reverses the order of comparison of an Comparison<T>. The resulting comparison can be used, for example, to sort a collection in descending order.
Declaration
public static Comparison<T> GetReverseComparison<T>(Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Comparison<T> | comparison | The comparison to reverse. |
Returns
| Type | Description |
|---|---|
| System.Comparison<T> | A Comparison<T> that compares items in the reverse order of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items that are being compared. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
GetSetEqualityComparer<T>()
Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, without regard to order. This is the same notion of equality as in Algorithms.EqualSets, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.
An IEqualityComparer<T> is used to determine if individual T's are equal
Declaration
public static IEqualityComparer<IEnumerable<T>> GetSetEqualityComparer<T>()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>> | IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality, without regard to order. |
Type Parameters
| Name | Description |
|---|---|
| T |
Examples
The following code creates a Dictionary where the keys are a set of strings, without regard to order
Dictionary<IEnumerable<string>, int> =
new Dictionary<IEnumerable<string>, int>(Algorithms.GetSetEqualityComparer<string>(StringComparer.CurrentCultureIgnoreCase));
See Also
| Improve this Doc View SourceGetSetEqualityComparer<T>(IEqualityComparer<T>)
Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, without regard to order. This is the same notion of equality as in Algorithms.EqualSets, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.
Declaration
public static IEqualityComparer<IEnumerable<T>> GetSetEqualityComparer<T>(IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | An IEqualityComparer<T> implementation used to compare individual T's. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>> | IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality, without regard to order. |
Type Parameters
| Name | Description |
|---|---|
| T |
Examples
The following code creates a Dictionary where the keys are a set of strings, without regard to order
Dictionary<IEnumerable<string>, int> =
new Dictionary<IEnumerable<string>, int>(Algorithms.GetSetEqualityComparer<string>());
See Also
| Improve this Doc View SourceIndexOfMaximum<T>(IList<T>)
Finds the index of the maximum value in a list.
Declaration
public static int IndexOfMaximum<T>(IList<T> list)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the largest item in the list. If the maximum value appears multiple times, the index of the first appearance is used. If the list is empty, -1 is returned. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Remarks
Values in the list are compared by using the IComparable<T> interfaces implementation on the type T.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
IndexOfMaximum<T>(IList<T>, IComparer<T>)
Finds the index of the maximum value in a list. A supplied IComparer<T> is used to compare the items in the collection.
Declaration
public static int IndexOfMaximum<T>(IList<T> list, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IComparer<T> | comparer | The comparer instance used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the largest item in the list. If the maximum value appears multiple times, the index of the first appearance is used. If the list is empty, -1 is returned. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
IndexOfMaximum<T>(IList<T>, Comparison<T>)
Finds the index of the maximum value in a list. A supplied Comparison<T> delegate is used to compare the items in the collection.
Declaration
public static int IndexOfMaximum<T>(IList<T> list, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Comparison<T> | comparison | The comparison used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the largest item in the list. If the maximum value appears multiple times, the index of the first appearance is used. If the list is empty, -1 is returned. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
IndexOfMinimum<T>(IList<T>)
Finds the index of the minimum value in a list.
Declaration
public static int IndexOfMinimum<T>(IList<T> list)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the smallest item in the list. If the minimum value appears multiple times, the index of the first appearance is used. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Remarks
Values in the list are compared by using the IComparable<T> interfaces implementation on the type T.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
IndexOfMinimum<T>(IList<T>, IComparer<T>)
Finds the index of the minimum value in a list. A supplied IComparer<T> is used to compare the items in the collection.
Declaration
public static int IndexOfMinimum<T>(IList<T> list, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IComparer<T> | comparer | The comparer instance used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the smallest item in the list. If the minimum value appears multiple times, the index of the first appearance is used. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
IndexOfMinimum<T>(IList<T>, Comparison<T>)
Finds the index of the minimum value in a list. A supplied Comparison<T> delegate is used to compare the items in the collection.
Declaration
public static int IndexOfMinimum<T>(IList<T> list, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Comparison<T> | comparison | The comparison delegate used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the smallest item in the list. If the minimum value appears multiple times, the index of the first appearance is used. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
IndicesOf<T>(IList<T>, T)
Enumerates the indices of all the items in a list equal to a given item.
Declaration
public static IEnumerable<int> IndicesOf<T>(IList<T> list, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | An IEnumerable<T> that enumerates the indices of items equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
IndicesOf<T>(IList<T>, T, IEqualityComparer<T>)
Enumerates the indices of all the items in a list equal to a given item. A passed IEqualityComparer is used to determine equality.
Declaration
public static IEnumerable<int> IndicesOf<T>(IList<T> list, T item, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| T | item | The item to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | An IEnumerable<T> that enumerates the indices of items equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
IndicesOfMany<T>(IList<T>, IEnumerable<T>)
Enumerates the indices of all the items in a list equal to one of several given items.
Declaration
public static IEnumerable<int> IndicesOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | A collection of items to search for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | An IEnumerable<T> that enumerates the indices of items equal to
any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
IndicesOfMany<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)
Enumerates the indices of all the items in a list equal to one of several given items. A passed IEqualityComparer is used to determine equality.
Declaration
public static IEnumerable<int> IndicesOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | A collection of items to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | An IEnumerable<T> that enumerates the indices of items equal to
any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
IndicesOfMany<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)
Enumerates the indices of all the items in a list equal to one of several given items. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static IEnumerable<int> IndicesOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | A collection of items to search for. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | An IEnumerable<T> that enumerates the indices of items "equal" to any of the items
in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality. This methods finds
last item X which satisfies BinaryPredicate(X,Y), where Y is one of the items in itemsToLookFor
IsProperSubsetOf<T>(IEnumerable<T>, IEnumerable<T>)
Determines if one collection is a proper subset of another, considered as sets. The first set is a proper subset of the second set if every item in the first set also occurs in the second set, and the first set is strictly smaller than the second set. If an item appears X times in the first set, it must appear at least X times in the second set.
Declaration
public static bool IsProperSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
IsProperSubsetOf<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines if one collection is a proper subset of another, considered as sets. The first set is a proper subset of the second set if every item in the first set also occurs in the second set, and the first set is strictly smaller than the second set. If an item appears X times in the first set, it must appear at least X times in the second set.
Declaration
public static bool IsProperSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
IsSubsetOf<T>(IEnumerable<T>, IEnumerable<T>)
Determines if one collection is a subset of another, considered as sets. The first set is a subset of the second set if every item in the first set also occurs in the second set. If an item appears X times in the first set, it must appear at least X times in the second set.
Declaration
public static bool IsSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
IsSubsetOf<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines if one collection is a subset of another, considered as sets. The first set is a subset of the second set if every item in the first set also occurs in the second set. If an item appears X times in the first set, it must appear at least X times in the second set.
Declaration
public static bool IsSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
LastIndexOf<T>(IList<T>, T)
Finds the index of the last item in a list equal to a given item.
Declaration
public static int LastIndexOf<T>(IList<T> list, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
LastIndexOf<T>(IList<T>, T, IEqualityComparer<T>)
Finds the index of the last item in a list equal to a given item. A passed IEqualityComparer is used to determine equality.
Declaration
public static int LastIndexOf<T>(IList<T> list, T item, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| T | item | The item to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item equal to |
Type Parameters
| Name | Description |
|---|---|
| T |
LastIndexOfMany<T>(IList<T>, IEnumerable<T>)
Finds the index of the last item in a list equal to one of several given items.
Declaration
public static int LastIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | The items to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item equal to any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
LastIndexOfMany<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)
Finds the index of the last item in a list equal to one of several given items. A passed IEqualityComparer is used to determine equality.
Declaration
public static int LastIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | The items to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item equal to any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
LastIndexOfMany<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)
Finds the index of the last item in a list "equal" to one of several given items. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static int LastIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | itemsToLookFor | The items to search for. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item "equal" to any of the items in the collection |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality. This methods finds
last item X which satisfies BinaryPredicate(X,Y), where Y is one of the items in itemsToLookFor
LexicographicalCompare<T>(IEnumerable<T>, IEnumerable<T>)
Performs a lexicographical comparison of two sequences of values. A lexicographical comparison compares corresponding pairs of elements from two sequences in order. If the first element of sequence1 is less than the first element of sequence2, then the comparison ends and the first sequence is lexicographically less than the second. If the first elements of each sequence are equal, then the comparison proceeds to the second element of each sequence. If one sequence is shorter than the other, but corresponding elements are all equal, then the shorter sequence is considered less than the longer one.
Declaration
public static int LexicographicalCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | sequence1 | The first sequence to compare. |
| System.Collections.Generic.IEnumerable<T> | sequence2 | The second sequence to compare. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Less than zero if |
Type Parameters
| Name | Description |
|---|---|
| T | Types of items to compare. This type must implement IComparable<T> to allow items to be compared. |
Remarks
T must implement either IComparable<T> and this implementation is used to compare the items.
Exceptions
| Type | Condition |
|---|---|
| System.NotSupportedException | T does not implement IComparable<T> or IComparable. |
LexicographicalCompare<T>(IEnumerable<T>, IEnumerable<T>, IComparer<T>)
Performs a lexicographical comparison of two sequences of values, using a supplied comparer interface. A lexicographical comparison compares corresponding pairs of elements from two sequences in order. If the first element of sequence1 is less than the first element of sequence2, then the comparison ends and the first sequence is lexicographically less than the second. If the first elements of each sequence are equal, then the comparison proceeds to the second element of each sequence. If one sequence is shorter than the other, but corresponding elements are all equal, then the shorter sequence is considered less than the longer one.
Declaration
public static int LexicographicalCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | sequence1 | The first sequence to compare. |
| System.Collections.Generic.IEnumerable<T> | sequence2 | The second sequence to compare. |
| System.Collections.Generic.IComparer<T> | comparer | The IComparer<T> used to compare items. Only the Compare member function of this interface is called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Less than zero if |
Type Parameters
| Name | Description |
|---|---|
| T | Types of items to compare. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
LexicographicalCompare<T>(IEnumerable<T>, IEnumerable<T>, Comparison<T>)
Performs a lexicographical comparison of two sequences of values, using a supplied comparison delegate. A lexicographical comparison compares corresponding pairs of elements from two sequences in order. If the first element of sequence1 is less than the first element of sequence2, then the comparison ends and the first sequence is lexicographically less than the second. If the first elements of each sequence are equal, then the comparison proceeds to the second element of each sequence. If one sequence is shorter than the other, but corresponding elements are all equal, then the shorter sequence is considered less than the longer one.
Declaration
public static int LexicographicalCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | sequence1 | The first sequence to compare. |
| System.Collections.Generic.IEnumerable<T> | sequence2 | The second sequence to compare. |
| System.Comparison<T> | comparison | The IComparison<T> delegate to compare items. Only the Compare member function of this interface is called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Less than zero if |
Type Parameters
| Name | Description |
|---|---|
| T | Types of items to compare. |
Maximum<T>(IEnumerable<T>)
Finds the maximum value in a collection.
Declaration
public static T Maximum<T>(IEnumerable<T> collection)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
Returns
| Type | Description |
|---|---|
| T | The largest item in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Remarks
Values in the collection are compared by using the IComparable<T> interfaces implementation on the type T.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
Maximum<T>(IEnumerable<T>, IComparer<T>)
Finds the maximum value in a collection. A supplied IComparer<T> is used to compare the items in the collection.
Declaration
public static T Maximum<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
| System.Collections.Generic.IComparer<T> | comparer | The comparer instance used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| T | The largest item in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
Maximum<T>(IEnumerable<T>, Comparison<T>)
Finds the maximum value in a collection. A supplied Comparison<T> delegate is used to compare the items in the collection.
Declaration
public static T Maximum<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
| System.Comparison<T> | comparison | The comparison used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| T | The largest item in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
MergeSorted<T>(IComparer<T>, IEnumerable<T>[])
Merge several sorted collections into a single sorted collection. Each input collection must be sorted by the ordering in the passed instance of IComparer<T>. The merging is stable; equal items maintain their ordering, and equal items in different collections are placed in the order of the collections.
Declaration
public static IEnumerable<T> MergeSorted<T>(IComparer<T> comparer, params IEnumerable<T>[] collections)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IComparer<T> | comparer | The comparer instance used to sort the list. Only the Compare method is used. |
| System.Collections.Generic.IEnumerable<T>[] | collections | The set of collections to merge. In many languages, this parameter can be specified as several individual parameters. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable<T> that enumerates all the items in all the collections in sorted order. |
Type Parameters
| Name | Description |
|---|---|
| T |
MergeSorted<T>(IEnumerable<T>[])
Merge several sorted collections into a single sorted collection. Each input collection must be sorted by the natural ordering of the type (it's implementation of IComparable<T>). The merging is stable; equal items maintain their ordering, and equal items in different collections are placed in the order of the collections.
Declaration
public static IEnumerable<T> MergeSorted<T>(params IEnumerable<T>[] collections)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T>[] | collections | The set of collections to merge. In many languages, this parameter can be specified as several individual parameters. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable<T> that enumerates all the items in all the collections in sorted order. |
Type Parameters
| Name | Description |
|---|---|
| T |
MergeSorted<T>(Comparison<T>, IEnumerable<T>[])
Merge several sorted collections into a single sorted collection. Each input collection must be sorted by the ordering in the passed Comparison<T> delegate. The merging is stable; equal items maintain their ordering, and equal items in different collections are placed in the order of the collections.
Declaration
public static IEnumerable<T> MergeSorted<T>(Comparison<T> comparison, params IEnumerable<T>[] collections)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Comparison<T> | comparison | The comparison delegate used to sort the collections. |
| System.Collections.Generic.IEnumerable<T>[] | collections | The set of collections to merge. In many languages, this parameter can be specified as several individual parameters. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable<T> that enumerates all the items in all the collections in sorted order. |
Type Parameters
| Name | Description |
|---|---|
| T |
Minimum<T>(IEnumerable<T>)
Finds the minimum value in a collection.
Declaration
public static T Minimum<T>(IEnumerable<T> collection)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
Returns
| Type | Description |
|---|---|
| T | The smallest item in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Remarks
Values in the collection are compared by using the IComparable<T> interfaces implementation on the type T.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
Minimum<T>(IEnumerable<T>, IComparer<T>)
Finds the minimum value in a collection. A supplied IComparer<T> is used to compare the items in the collection.
Declaration
public static T Minimum<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
| System.Collections.Generic.IComparer<T> | comparer | The comparer instance used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| T | The smallest item in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
Minimum<T>(IEnumerable<T>, Comparison<T>)
Finds the minimum value in a collection. A supplied Comparison<T> delegate is used to compare the items in the collection.
Declaration
public static T Minimum<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to search. |
| System.Comparison<T> | comparison | The comparison used to compare items in the collection. |
Returns
| Type | Description |
|---|---|
| T | The smallest item in the collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The collection is empty. |
| System.ArgumentNullException |
|
NCopiesOf<T>(Int32, T)
Creates an IEnumerator that enumerates a given item n
times.
Declaration
public static IEnumerable<T> NCopiesOf<T>(int n, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | n | The number of times to enumerate the item. |
| T | item | The item that should occur in the enumeration. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable<T> that yields |
Type Parameters
| Name | Description |
|---|---|
| T |
Examples
The following creates a list consisting of 1000 copies of the double 1.0.
List<double> list = new List<double>(Algorithms.NCopiesOf(1000, 1.0));
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | The argument |
Partition<T>(IList<T>, Predicate<T>)
Partition a list or array based on a predicate. After partitioning, all items for which the predicate returned true precede all items for which the predicate returned false.
Declaration
public static int Partition<T>(IList<T> list, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to partition. |
| System.Predicate<T> | predicate | A delegate that defines the partitioning condition. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the second half of the partition; i.e., the first item for
which |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
RandomShuffle<T>(IEnumerable<T>)
Randomly shuffles the items in a collection, yielding a new collection.
Declaration
public static T[] RandomShuffle<T>(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to shuffle. |
Returns
| Type | Description |
|---|---|
| T[] | An array with the same size and items as |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the items in the collection. |
RandomShuffle<T>(IEnumerable<T>, Random)
Randomly shuffles the items in a collection, yielding a new collection.
Declaration
public static T[] RandomShuffle<T>(IEnumerable<T> collection, Random randomGenerator)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to shuffle. |
| System.Random | randomGenerator | The random number generator to use to select the random order. |
Returns
| Type | Description |
|---|---|
| T[] | An array with the same size and items as |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the items in the collection. |
RandomShuffleInPlace<T>(IList<T>)
Randomly shuffles the items in a list or array, in place.
Declaration
public static void RandomShuffleInPlace<T>(IList<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to shuffle. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
RandomShuffleInPlace<T>(IList<T>, Random)
Randomly shuffles the items in a list or array, in place.
Declaration
public static void RandomShuffleInPlace<T>(IList<T> list, Random randomGenerator)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to shuffle. |
| System.Random | randomGenerator | The random number generator to use to select the random order. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
RandomSubset<T>(IEnumerable<T>, Int32)
Picks a random subset of count
items from collection
, and places
those items into a random order. No item is selected more than once.
Declaration
public static T[] RandomSubset<T>(IEnumerable<T> collection, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection of items to select from. This collection is not changed. |
| System.Int32 | count | The number of items in the subset to choose. |
Returns
| Type | Description |
|---|---|
| T[] | An array of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Remarks
If the collection implements IList<T>, then this method takes time O(count
).
Otherwise, this method takes time O(N), where N is the number of items in the collection.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
RandomSubset<T>(IEnumerable<T>, Int32, Random)
Picks a random subset of count
items from collection
, and places
those items into a random order. No item is selected more than once.
Declaration
public static T[] RandomSubset<T>(IEnumerable<T> collection, int count, Random randomGenerator)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection of items to select from. This collection is not changed. |
| System.Int32 | count | The number of items in the subset to choose. |
| System.Random | randomGenerator | The random number generates used to make the selection. |
Returns
| Type | Description |
|---|---|
| T[] | An array of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Remarks
If the collection implements IList<T>, then this method takes time O(count
).
Otherwise, this method takes time O(N), where N is the number of items in the collection.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentNullException |
|
Range<T>(T[], Int32, Int32)
Returns a view onto a sub-range of an array. Items from array
are not copied; the
returned IList<T> is simply a different view onto the same underlying items. Changes to array
are reflected in the view, and vice versa. Insertions and deletions in the view change the size of the
view. After an insertion, the last item in array
"falls off the end". After a deletion, the
last item in array becomes the default value (0 or null).
Declaration
public static IList<T> Range<T>(T[] array, int start, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to view. |
| System.Int32 | start | The starting index of the view. |
| System.Int32 | count | The number of items in the view. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<T> | A list that is a view onto the given sub-array. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
This method can be used to apply an algorithm to a portion of a array. For example:
Algorithms.ReverseInPlace(Algorithms.Range(array, 3, 6))
will reverse the 6 items beginning at index 3.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
Range<T>(IList<T>, Int32, Int32)
Returns a view onto a sub-range of a list. Items from list
are not copied; the
returned IList<T> is simply a different view onto the same underlying items. Changes to list
are reflected in the view, and vice versa. Insertions and deletions in the view change the size of the
view, but insertions and deletions in the underlying list do not.
Declaration
public static IList<T> Range<T>(IList<T> list, int start, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to view. |
| System.Int32 | start | The starting index of the view. |
| System.Int32 | count | The number of items in the view. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<T> | A list that is a view onto the given sub-list. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the items in the list. |
Remarks
This method can be used to apply an algorithm to a portion of a list. For example:
Algorithms.ReverseInPlace(Algorithms.Range(list, 3, 6))
will reverse the 6 items beginning at index 3.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
ReadOnly<T>(ICollection<T>)
Returns a read-only view onto a collection. The returned ICollection<T> interface only allows operations that do not change the collection: GetEnumerator, Contains, CopyTo, Count. The ReadOnly property returns false, indicating that the collection is read-only. All other methods on the interface throw a NotSupportedException.
Declaration
public static ICollection<T> ReadOnly<T>(ICollection<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.ICollection<T> | collection | The collection to wrap. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<T> | A read-only view onto |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
Remarks
The data in the underlying collection is not copied. If the underlying collection is changed, then the read-only view also changes accordingly.
ReadOnly<T>(IList<T>)
Returns a read-only view onto a list. The returned IList<T> interface only allows operations that do not change the list: GetEnumerator, Contains, CopyTo, Count, IndexOf, and the get accessor of the indexer. The IsReadOnly property returns true, indicating that the list is read-only. All other methods on the interface throw a NotSupportedException.
Declaration
public static IList<T> ReadOnly<T>(IList<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to wrap. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<T> | A read-only view onto |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Remarks
The data in the underlying list is not copied. If the underlying list is changed, then the read-only view also changes accordingly.
ReadOnly<TKey, TValue>(IDictionary<TKey, TValue>)
Returns a read-only view onto a dictionary. The returned IDictionary<TKey,TValue> interface only allows operations that do not change the dictionary. The IsReadOnly property returns true, indicating that the dictionary is read-only. All other methods on the interface throw a NotSupportedException.
Declaration
public static IDictionary<TKey, TValue> ReadOnly<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary to wrap. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IDictionary<TKey, TValue> | A read-only view onto |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
The data in the underlying dictionary is not copied. If the underlying dictionary is changed, then the read-only view also changes accordingly.
ReadWriteList<T>(T[])
Creates a read-write IList<T> wrapper around an array. When an array is implicitely converted to an IList<T>, changes to the items in the array cannot be made through the interface. This method creates a read-write IList<T> wrapper on an array that can be used to make changes to the array.
Use this method when you need to pass an array to an algorithms that takes an IList<T> and that tries to modify items in the list. Algorithms in this class generally do not need this method, since they have been design to operate on arrays even when they are passed as an IList<T>.
Declaration
public static IList<T> ReadWriteList<T>(T[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to wrap. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<T> | An IList<T> wrapper onto |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since arrays cannot be resized, inserting an item causes the last item in the array to be automatically removed. Removing an item causes the last item in the array to be replaced with a default value (0 or null). Clearing the list causes all the items to be replaced with a default value.
RemoveDuplicates<T>(IEnumerable<T>)
Remove consecutive equal items from a collection, yielding another collection. In each run of consecutive equal items in the collection, all items after the first item in the run are removed.
Declaration
public static IEnumerable<T> RemoveDuplicates<T>(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An new collection with the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
RemoveDuplicates<T>(IEnumerable<T>, IEqualityComparer<T>)
Remove consecutive equal items from a collection, yielding another collection. In each run of consecutive equal items in the collection, all items after the first item in the run are removed. A passed IEqualityComparer is used to determine equality.
Declaration
public static IEnumerable<T> RemoveDuplicates<T>(IEnumerable<T> collection, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An new collection with the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
RemoveDuplicates<T>(IEnumerable<T>, BinaryPredicate<T>)
Remove consecutive "equal" items from a collection, yielding another collection. In each run of consecutive equal items in the collection, all items after the first item in the run are removed. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static IEnumerable<T> RemoveDuplicates<T>(IEnumerable<T> collection, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". An item |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An new collection with the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality.
RemoveDuplicatesInPlace<T>(IList<T>)
Remove consecutive equal items from a list or array. In each run of consecutive equal items in the list, all items after the first item in the run are removed. The removal is done in-place, changing the list.
Declaration
public static void RemoveDuplicatesInPlace<T>(IList<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to process. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
RemoveDuplicatesInPlace<T>(IList<T>, IEqualityComparer<T>)
Remove subsequent consecutive equal items from a list or array. In each run of consecutive equal items in the list, all items after the first item in the run are removed. The replacement is done in-place, changing the list. A passed IEqualityComparer is used to determine equality.
Declaration
public static void RemoveDuplicatesInPlace<T>(IList<T> list, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to process. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
RemoveDuplicatesInPlace<T>(IList<T>, BinaryPredicate<T>)
Remove consecutive "equal" items from a list or array. In each run of consecutive equal items in the list, all items after the first item in the run are removed. The replacement is done in-place, changing the list. The passed BinaryPredicate is used to determine if two items are "equal".
Declaration
public static void RemoveDuplicatesInPlace<T>(IList<T> list, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to process. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being tested for need not be true equality.
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
RemoveWhere<T>(ICollection<T>, Predicate<T>)
Removes all the items in the collection that satisfy the condition
defined by predicate
.
Declaration
public static ICollection<T> RemoveWhere<T>(ICollection<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.ICollection<T> | collection | The collection to check all the items in. |
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<T> | Returns a collection of the items that were removed. This collection contains the
items in the same order that they orginally appeared in |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
If the collection if an array or implements IList<T>, an efficient algorithm that compacts items is used. If not, then ICollection<T>.Remove is used to remove items from the collection. If the collection is an array or fixed-size list, the non-removed elements are placed, in order, at the beginning of the list, and the remaining list items are filled with a default value (0 or null).
Replace<T>(IEnumerable<T>, T, T)
Replace all items in a collection equal to a particular value with another values, yielding another collection.
Declaration
public static IEnumerable<T> Replace<T>(IEnumerable<T> collection, T itemFind, T replaceWith)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
| T | itemFind | The value to find and replace within |
| T | replaceWith | The new value to replace with. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An new collection with the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
Replace<T>(IEnumerable<T>, T, T, IEqualityComparer<T>)
Replace all items in a collection equal to a particular value with another values, yielding another collection. A passed IEqualityComparer is used to determine equality.
Declaration
public static IEnumerable<T> Replace<T>(IEnumerable<T> collection, T itemFind, T replaceWith, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
| T | itemFind | The value to find and replace within |
| T | replaceWith | The new value to replace with. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An new collection with the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Replace<T>(IEnumerable<T>, Predicate<T>, T)
Replace all items in a collection that a predicate evalues at true with a value, yielding another collection. .
Declaration
public static IEnumerable<T> Replace<T>(IEnumerable<T> collection, Predicate<T> predicate, T replaceWith)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | The collection to process. |
| System.Predicate<T> | predicate | The predicate used to evaluate items with the collection. If the predicate returns true for a particular
item, the item is replaces with |
| T | replaceWith | The new value to replace with. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An new collection with the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
ReplaceInPlace<T>(IList<T>, T, T)
Replace all items in a list or array equal to a particular value with another value. The replacement is done in-place, changing the list.
Declaration
public static void ReplaceInPlace<T>(IList<T> list, T itemFind, T replaceWith)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to process. |
| T | itemFind | The value to find and replace within |
| T | replaceWith | The new value to replace with. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
ReplaceInPlace<T>(IList<T>, T, T, IEqualityComparer<T>)
Replace all items in a list or array equal to a particular value with another values. The replacement is done in-place, changing the list. A passed IEqualityComparer is used to determine equality.
Declaration
public static void ReplaceInPlace<T>(IList<T> list, T itemFind, T replaceWith, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to process. |
| T | itemFind | The value to find and replace within |
| T | replaceWith | The new value to replace with. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
ReplaceInPlace<T>(IList<T>, Predicate<T>, T)
Replace all items in a list or array that a predicate evaluates at true with a value. The replacement is done in-place, changing the list.
Declaration
public static void ReplaceInPlace<T>(IList<T> list, Predicate<T> predicate, T replaceWith)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to process. |
| System.Predicate<T> | predicate | The predicate used to evaluate items with the collection. If the predicate returns true for a particular
item, the item is replaces with |
| T | replaceWith | The new value to replace with. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
Reverse<T>(IList<T>)
Reverses a list and returns the reversed list, without changing the source list.
Declaration
public static IEnumerable<T> Reverse<T>(IList<T> source)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | source | The list to reverse. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | A collection that contains the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
ReverseInPlace<T>(IList<T>)
Reverses a list or array in place.
Declaration
public static void ReverseInPlace<T>(IList<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to reverse. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
| System.ArgumentException |
|
Rotate<T>(IList<T>, Int32)
Rotates a list and returns the rotated list, without changing the source list.
Declaration
public static IEnumerable<T> Rotate<T>(IList<T> source, int amountToRotate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | source | The list to rotate. |
| System.Int32 | amountToRotate | The number of elements to rotate. This value can be positive or negative. For example, rotating by positive 3 means that source[3] is the first item in the returned collection. Rotating by negative 3 means that source[source.Count - 3] is the first item in the returned collection. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | A collection that contains the items from |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
RotateInPlace<T>(IList<T>, Int32)
Rotates a list or array in place.
Declaration
public static void RotateInPlace<T>(IList<T> list, int amountToRotate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list or array to rotate. |
| System.Int32 | amountToRotate | The number of elements to rotate. This value can be positive or negative. For example, rotating by positive 3 means that list[3] is the first item in the resulting list. Rotating by negative 3 means that list[list.Count - 3] is the first item in the resulting list. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Although arrays cast to IList<T> are normally read-only, this method
will work correctly and modify an array passed as list
.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SearchForSubsequence<T>(IList<T>, IEnumerable<T>)
Searchs a list for a sub-sequence of items that match a particular pattern. A subsequence
of list
matches pattern at index i if list[i] is equal to the first item
in pattern
, list[i+1] is equal to the second item in pattern
,
and so forth for all the items in pattern
.
Declaration
public static int SearchForSubsequence<T>(IList<T> list, IEnumerable<T> pattern)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | pattern | The sequence of items to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The first index with |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Remarks
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
SearchForSubsequence<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)
Searchs a list for a sub-sequence of items that match a particular pattern. A subsequence
of list
matches pattern at index i if list[i] is equal to the first item
in pattern
, list[i+1] is equal to the second item in pattern
,
and so forth for all the items in pattern
. The passed
instance of IEqualityComparer<T> is used for determining if two items are equal.
Declaration
public static int SearchForSubsequence<T>(IList<T> list, IEnumerable<T> pattern, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | pattern | The sequence of items to search for. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The first index with |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
SearchForSubsequence<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)
Searchs a list for a sub-sequence of items that match a particular pattern. A subsequence
of list
matches pattern at index i if list[i] is "equal" to the first item
in pattern
, list[i+1] is "equal" to the second item in pattern
,
and so forth for all the items in pattern
. The passed
BinaryPredicate is used to determine if two items are "equal".
Declaration
public static int SearchForSubsequence<T>(IList<T> list, IEnumerable<T> pattern, BinaryPredicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IList<T> | list | The list to search. |
| System.Collections.Generic.IEnumerable<T> | pattern | The sequence of items to search for. |
| BinaryPredicate<T> | predicate | The BinaryPredicate used to compare items for "equality". |
Returns
| Type | Description |
|---|---|
| System.Int32 | The first index with |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Remarks
Since an arbitrary BinaryPredicate is passed to this function, what is being tested for in the pattern need not be equality.
SetDifference<T>(IEnumerable<T>, IEnumerable<T>)
Computes the set-theoretic difference of two collections. The difference of two sets is all items that appear in the first set, but not in the second. If an item appears X times in the first set, and Y times in the second set, the difference contains the item X - Y times (0 times if X < Y). The source collections are not changed. A new collection is created with the difference of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to difference. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to difference. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The difference of |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Difference or DifferenceWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetDifference<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Computes the set-theoretic difference of two collections. The difference of two sets is all items that appear in the first set, but not in the second. If an item appears X times in the first set, and Y times in the second set, the difference contains the item X - Y times (0 times if X < Y). The source collections are not changed. A new collection is created with the difference of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to difference. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to difference. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The difference of |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the difference or differenceWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetIntersection<T>(IEnumerable<T>, IEnumerable<T>)
Computes the set-theoretic intersection of two collections. The intersection of two sets is all items that appear in both of the sets. If an item appears X times in one set, and Y times in the other set, the intersection contains the item Minimum(X,Y) times. The source collections are not changed. A new collection is created with the intersection of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetIntersection<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to intersect. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to intersect. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The intersection of the two collections, considered as sets. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Intersection or IntersectionWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetIntersection<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Computes the set-theoretic intersection of two collections. The intersection of two sets is all items that appear in both of the sets. If an item appears X times in one set, and Y times in the other set, the intersection contains the item Minimum(X,Y) times. The source collections are not changed. A new collection is created with the intersection of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetIntersection<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to intersect. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to intersect. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The intersection of the two collections, considered as sets. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Intersection or IntersectionWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetSymmetricDifference<T>(IEnumerable<T>, IEnumerable<T>)
Computes the set-theoretic symmetric difference of two collections. The symmetric difference of two sets is all items that appear in the one of the sets, but not in the other. If an item appears X times in the one set, and Y times in the other set, the symmetric difference contains the item AbsoluteValue(X - Y) times. The source collections are not changed. A new collection is created with the symmetric difference of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetSymmetricDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to symmetric difference. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to symmetric difference. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The symmetric difference of |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the SymmetricDifference or SymmetricDifferenceWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetSymmetricDifference<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Computes the set-theoretic symmetric difference of two collections. The symmetric difference of two sets is all items that appear in the one of the sets, but not in the other. If an item appears X times in the one set, and Y times in the other set, the symmetric difference contains the item AbsoluteValue(X - Y) times. The source collections are not changed. A new collection is created with the symmetric difference of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetSymmetricDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to symmetric difference. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to symmetric difference. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The symmetric difference of |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the symmetric difference or symmetric differenceWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetUnion<T>(IEnumerable<T>, IEnumerable<T>)
Computes the set-theoretic union of two collections. The union of two sets is all items that appear in either of the sets. If an item appears X times in one set, and Y times in the other set, the union contains the item Maximum(X,Y) times. The source collections are not changed. A new collection is created with the union of the collections; the order of the items in this collection is undefined.
Declaration
public static IEnumerable<T> SetUnion<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection1 | The first collection to union. |
| System.Collections.Generic.IEnumerable<T> | collection2 | The second collection to union. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | The union of the two collections, considered as sets. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.
The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.
If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Union or UnionWith methods on that class.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SetUnion<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)