Class Set<T>
Set<T> is a collection that contains items of type T. The item are maintained in a haphazard, unpredictable order, and duplicate items are not allowed.
Implements
Inherited Members
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
[Serializable]
public class Set<T> : CollectionBase<T>, ICollection, ICollection<T>, IEnumerable<T>, IEnumerable, ICloneable
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The items are compared in one of two ways. If T implements IComparable<T> then the Equals method of that interface will be used to compare items, otherwise the Equals method from Object will be used. Alternatively, an instance of IComparer<T> can be passed to the constructor to use to compare items.
Set is implemented as a hash table. Inserting, deleting, and looking up an an element all are done in approximately constant time, regardless of the number of items in the Set.
Constructors
| Improve this Doc View SourceSet()
Creates a new Set. The Equals method and GetHashCode method on T will be used to compare items for equality.
Declaration
public Set()
Remarks
Items that are null are permitted, and will be sorted before all other items.
Set(IEnumerable<T>)
Creates a new Set. The Equals method and GetHashCode method on T will be used to compare items for equality.
Declaration
public Set(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | A collection with items to be placed into the Set. |
Remarks
Items that are null are permitted.
Set(IEnumerable<T>, IEqualityComparer<T>)
Creates a new Set. The Equals and GetHashCode method of the passed comparer object will be used to compare items in this set. The set is initialized with all the items in the given collection.
Declaration
public Set(IEnumerable<T> collection, IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | A collection with items to be placed into the Set. |
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | An instance of IEqualityComparer<T> that will be used to compare items. |
Set(IEqualityComparer<T>)
Creates a new Set. The Equals and GetHashCode method of the passed comparer object will be used to compare items in this set.
Declaration
public Set(IEqualityComparer<T> equalityComparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEqualityComparer<T> | equalityComparer | An instance of IEqualityComparer<T> that will be used to compare items. |
Properties
| Improve this Doc View SourceComparer
Returns the IEqualityComparer<T> used to compare items in this set.
Declaration
public IEqualityComparer<T> Comparer { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<T> | If the set was created using a comparer, that comparer is returned. Otherwise the default comparer for T (EqualityComparer<T>.Default) is returned. |
Count
Returns the number of items in the set.
Declaration
public override sealed int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The number of items in the set. |
Overrides
Remarks
The size of the set is returned in constant time.
Methods
| Improve this Doc View SourceAdd(T)
Adds a new item to the set. If the set already contains an item equal to
item, that item is replaced with item.
Declaration
public bool Add(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to add to the set. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the set already contained an item equal to |
Remarks
Equality between items is determined by the comparison instance or delegate used to create the set.
Adding an item takes approximately constant time, regardless of the number of items in the set.
AddMany(IEnumerable<T>)
Adds all the items in collection to the set. If the set already contains an item equal to
one of the items in collection, that item will be replaced.
Declaration
public void AddMany(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | A collection of items to add to the set. |
Remarks
Equality between items is determined by the comparison instance or delegate used to create the set.
Adding the collection takes time O(M), where M is the
number of items in collection.
Clear()
Removes all items from the set.
Declaration
public override sealed void Clear()
Overrides
Remarks
Clearing the set takes a constant amount of time, regardless of the number of items in it.
Clone()
Makes a shallow clone of this set; i.e., if items of the set are reference types, then they are not cloned. If T is a value type, then each element is copied as if by simple assignment.
Declaration
public Set<T> Clone()
Returns
| Type | Description |
|---|---|
| Set<T> | The cloned set. |
Remarks
Cloning the set takes time O(N), where N is the number of items in the set.
CloneContents()
Makes a deep clone of this set. A new set is created with a clone of each element of this set, by calling ICloneable.Clone on each element. If T is a value type, then each element is copied as if by simple assignment.
Declaration
public Set<T> CloneContents()
Returns
| Type | Description |
|---|---|
| Set<T> | The cloned set. |
Remarks
If T is a reference type, it must implement ICloneable. Otherwise, an InvalidOperationException is thrown.
Cloning the set takes time O(N), where N is the number of items in the set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | T is a reference type that does not implement ICloneable. |
Contains(T)
Determines if this set contains an item equal to item. The set
is not changed.
Declaration
public override sealed bool Contains(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the set contains |
Overrides
Remarks
Searching the set for an item takes approximately constant time, regardless of the number of items in the set.
ContainsAll(IEnumerable<T>)
Declaration
public bool ContainsAll(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection |
Returns
| Type | Description |
|---|---|
| System.Boolean |
Difference(Set<T>)
Computes the difference of this set with another set. The difference of these two sets
is all items that appear in this set, but not in otherSet. A new set is
created with the difference of the sets and is returned. This set and the other set
are unchanged.
Declaration
public Set<T> Difference(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to difference with. |
Returns
| Type | Description |
|---|---|
| Set<T> | The difference of the two sets. |
Remarks
The difference of two sets is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
DifferenceWith(Set<T>)
Computes the difference of this set with another set. The difference of these two sets
is all items that appear in this set, but not in otherSet. This set receives
the difference of the two sets; the other set is unchanged.
Declaration
public void DifferenceWith(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to difference with. |
Remarks
The difference of two sets is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
GetAndRemoveAny()
Declaration
public T GetAndRemoveAny()
Returns
| Type | Description |
|---|---|
| T |
GetAny()
Returns an arbitrary object or null if there is none
Declaration
public T GetAny()
Returns
| Type | Description |
|---|---|
| T |
GetEnumerator()
Returns an enumerator that enumerates all the items in the set. The items are enumerated in sorted order.
Declaration
public override sealed IEnumerator<T> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<T> | An enumerator for enumerating all the items in the Set. |
Overrides
Remarks
Typically, this method is not called directly. Instead the "foreach" statement is used to enumerate the items, which uses this method implicitly.
If an item is added to or deleted from the set while it is being enumerated, then the enumeration will end with an InvalidOperationException.
Enumerating all the items in the set takes time O(N), where N is the number of items in the set.
Intersection(Set<T>)
Computes the intersection of this set with another set. The intersection of two sets is all items that appear in both of the sets. A new set is created with the intersection of the sets and is returned. This set and the other set are unchanged.
Declaration
public Set<T> Intersection(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to intersection with. |
Returns
| Type | Description |
|---|---|
| Set<T> | The intersection of the two sets. |
Remarks
When equal items appear in both sets, the intersection will include an arbitrary choice of one of the two equal items.
The intersection of two sets is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IntersectionWith(Set<T>)
Computes the intersection of this set with another set. The intersection of two sets is all items that appear in both of the sets. This set receives the intersection of the two sets, the other set is unchanged.
Declaration
public void IntersectionWith(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to intersection with. |
Remarks
When equal items appear in both sets, the intersection will include an arbitrary choice of one of the two equal items.
The intersection of two sets is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IsDisjointFrom(Set<T>)
Determines if this set is disjoint from another set. Two sets are disjoint if no item from one set is equal to any item in the other set.
Declaration
public bool IsDisjointFrom(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to check disjointness with. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the two sets are disjoint, false otherwise. |
Remarks
The answer is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IsEmpty()
Declaration
public bool IsEmpty()
Returns
| Type | Description |
|---|---|
| System.Boolean |
IsEqualTo(Set<T>)
Determines if this set is equal to another set. This set is equal to
otherSet if they contain the same items.
Declaration
public bool IsEqualTo(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to compare to |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this set is equal to |
Remarks
IsEqualTo is computed in time O(N), where N is the number of items in this set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IsProperSubsetOf(Set<T>)
Determines if this set is a proper subset of another set. Neither set is modified.
This set is a subset of otherSet if every element in this set
is also in otherSet. Additionally, this set must have strictly
fewer items than otherSet.
Declaration
public bool IsProperSubsetOf(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to compare to. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this is a proper subset of |
Remarks
IsProperSubsetOf is computed in time O(N), where N is the size of the this set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IsProperSupersetOf(Set<T>)
Determines if this set is a proper superset of another set. Neither set is modified.
This set is a proper superset of otherSet if every element in
otherSet is also in this set.
Additionally, this set must have strictly more items than otherSet.
Declaration
public bool IsProperSupersetOf(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to compare to. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this is a proper superset of |
Remarks
IsProperSubsetOf is computed in time O(M), where M is the size of
otherSet.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IsSubsetOf(Set<T>)
Determines if this set is a subset of another set. Neither set is modified.
This set is a subset of otherSet if every element in this set
is also in otherSet.
Declaration
public bool IsSubsetOf(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to compare to. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this is a subset of |
Remarks
IsSubsetOf is computed in time O(N), where N is the size of the this set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
IsSupersetOf(Set<T>)
Determines if this set is a superset of another set. Neither set is modified.
This set is a superset of otherSet if every element in
otherSet is also in this set.
otherSet.
Declaration
public bool IsSupersetOf(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to compare to. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this is a superset of |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
Remove(T)
Searches the set for an item equal to item, and if found,
removes it from the set. If not found, the set is unchanged.
Declaration
public override sealed bool Remove(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to remove. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
Overrides
Remarks
Equality between items is determined by the comparison instance or delegate used to create the set.
Removing an item from the set takes approximately constant time, regardless of the size of the set.
RemoveMany(IEnumerable<T>)
Removes all the items in collection from the set.
Declaration
public int RemoveMany(IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<T> | collection | A collection of items to remove from the set. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items removed from the set. |
Remarks
Equality between items is determined by the comparison instance or delegate used to create the set.
Removing the collection takes time O(M), where M is the
number of items in collection.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
SymmetricDifference(Set<T>)
Computes the symmetric difference of this set with another set. The symmetric difference of two sets is all items that appear in either of the sets, but not both. A new set is created with the symmetric difference of the sets and is returned. This set and the other set are unchanged.
Declaration
public Set<T> SymmetricDifference(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to symmetric difference with. |
Returns
| Type | Description |
|---|---|
| Set<T> | The symmetric difference of the two sets. |
Remarks
The symmetric difference of two sets is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
SymmetricDifferenceWith(Set<T>)
Computes the symmetric difference of this set with another set. The symmetric difference of two sets is all items that appear in either of the sets, but not both. This set receives the symmetric difference of the two sets; the other set is unchanged.
Declaration
public void SymmetricDifferenceWith(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to symmetric difference with. |
Remarks
The symmetric difference of two sets is computed in time O(N), where N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
TryGetItem(T, out T)
Determines if this set contains an item equal to item, according to the
comparison mechanism that was used when the set was created. The set
is not changed.
If the set does contain an item equal to item, then the item from the set is returned.
Declaration
public bool TryGetItem(T item, out T foundItem)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search for. |
| T | foundItem | Returns the item from the set that was equal to |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the set contains |
Remarks
Searching the set for an item takes approximately constant time, regardless of the number of items in the set.
Examples
In the following example, the set contains strings which are compared in a case-insensitive manner.
Set<string> set = new Set<string>(StringComparer.CurrentCultureIgnoreCase);
set.Add("HELLO");
string s;
bool b = set.TryGetItem("Hello", out s); // b receives true, s receives "HELLO".
|
Improve this Doc
View Source
Union(Set<T>)
Computes the union of this set with another set. The union of two sets is all items that appear in either or both of the sets. A new set is created with the union of the sets and is returned. This set and the other set are unchanged.
Declaration
public Set<T> Union(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to union with. |
Returns
| Type | Description |
|---|---|
| Set<T> | The union of the two sets. |
Remarks
If equal items appear in both sets, the union will include an arbitrary choice of one of the two equal items.
The union of two sets is computed in time O(M + N), where M is the size of the one set, and N is the size of the other set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
UnionWith(Set<T>)
Computes the union of this set with another set. The union of two sets is all items that appear in either or both of the sets. This set receives the union of the two sets, the other set is unchanged.
Declaration
public void UnionWith(Set<T> otherSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | otherSet | Set to union with. |
Remarks
If equal items appear in both sets, the union will include an arbitrary choice of one of the two equal items.
The union of two sets is computed in time O(M + N), where M is the size of the larger set, and N is the size of the smaller set.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | This set and |
Operators
| Improve this Doc View SourceAddition(Set<T>, T)
Declaration
public static Set<T> operator +(Set<T> t1, T t2)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | t1 | |
| T | t2 |
Returns
| Type | Description |
|---|---|
| Set<T> |
Addition(Set<T>, Set<T>)
Declaration
public static Set<T> operator +(Set<T> t1, Set<T> t2)
Parameters
| Type | Name | Description |
|---|---|---|
| Set<T> | t1 | |
| Set<T> | t2 |
Returns
| Type | Description |
|---|---|
| Set<T> |
Explicit Interface Implementations
| Improve this Doc View SourceICollection<T>.Add(T)
Adds a new item to the set. If the set already contains an item equal to
item, that item is replaced with item.
Declaration
void ICollection<T>.Add(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to add to the set. |
Remarks
Equality between items is determined by the comparison instance or delegate used to create the set.
Adding an item takes approximately constant time, regardless of the number of items in the set.
ICloneable.Clone()
Makes a shallow clone of this set; i.e., if items of the set are reference types, then they are not cloned. If T is a value type, then each element is copied as if by simple assignment.
Declaration
object ICloneable.Clone()
Returns
| Type | Description |
|---|---|
| System.Object | The cloned set. |
Remarks
Cloning the set takes time O(N), where N is the number of items in the set.