Show / Hide Table of Contents

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.

Inheritance
System.Object
CollectionBase<T>
Set<T>
Implements
System.Collections.ICollection
System.Collections.Generic.ICollection<T>
System.Collections.Generic.IEnumerable<T>
System.Collections.IEnumerable
System.ICloneable
Inherited Members
CollectionBase<T>.ToString()
CollectionBase<T>.CopyTo(T[], Int32)
CollectionBase<T>.ToArray()
CollectionBase<T>.ICollection<T>.IsReadOnly
CollectionBase<T>.AsReadOnly()
CollectionBase<T>.Exists(Predicate<T>)
CollectionBase<T>.TrueForAll(Predicate<T>)
CollectionBase<T>.CountWhere(Predicate<T>)
CollectionBase<T>.FindAll(Predicate<T>)
CollectionBase<T>.RemoveAll(Predicate<T>)
CollectionBase<T>.ForEach(Action<T>)
CollectionBase<T>.ConvertAll<TOutput>(Converter<T, TOutput>)
CollectionBase<T>.ICollection.CopyTo(Array, Int32)
CollectionBase<T>.ICollection.IsSynchronized
CollectionBase<T>.ICollection.SyncRoot
CollectionBase<T>.IEnumerable.GetEnumerator()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
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 Source

Set()

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 Source

Comparer

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.

| Improve this Doc View Source

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
Wintellect.PowerCollections.CollectionBase<T>.Count
Remarks

The size of the set is returned in constant time.

Methods

| Improve this Doc View Source

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
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 item (which was replaced), false otherwise.

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

Clear()

Removes all items from the set.

Declaration
public override sealed void Clear()
Overrides
Wintellect.PowerCollections.CollectionBase<T>.Clear()
Remarks

Clearing the set takes a constant amount of time, regardless of the number of items in it.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 item. False if the set does not contain item.

Overrides
Wintellect.PowerCollections.CollectionBase<T>.Contains(T)
Remarks

Searching the set for an item takes approximately constant time, regardless of the number of items in the set.

| Improve this Doc View Source

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
| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

GetAndRemoveAny()

Declaration
public T GetAndRemoveAny()
Returns
Type Description
T
| Improve this Doc View Source

GetAny()

Returns an arbitrary object or null if there is none

Declaration
public T GetAny()
Returns
Type Description
T
| Improve this Doc View Source

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
Wintellect.PowerCollections.CollectionBase<T>.GetEnumerator()
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.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

IsEmpty()

Declaration
public bool IsEmpty()
Returns
Type Description
System.Boolean
| Improve this Doc View Source

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 otherSet, false otherwise.

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet.

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet.

Remarks

IsProperSubsetOf is computed in time O(M), where M is the size of otherSet.

Exceptions
Type Condition
System.InvalidOperationException

This set and otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet.

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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. IsSupersetOf is computed in time O(M), where M is the size of the 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 otherSet.

Exceptions
Type Condition
System.InvalidOperationException

This set and otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 item was found and removed. False if item was not in the set.

Overrides
Wintellect.PowerCollections.CollectionBase<T>.Remove(T)
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.

| Improve this Doc View Source

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

collection is null.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 item.

Returns
Type Description
System.Boolean

True if the set contains item. False if the set does not contain item.

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 otherSet don't use the same method for comparing items.

| Improve this Doc View Source

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 otherSet don't use the same method for comparing items.

Operators

| Improve this Doc View Source

Addition(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>
| Improve this Doc View Source

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 Source

ICollection<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.

| Improve this Doc View Source

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.

Implements

System.Collections.ICollection
System.Collections.Generic.ICollection<T>
System.Collections.Generic.IEnumerable<T>
System.Collections.IEnumerable
System.ICloneable
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX