using System;
using System.Collections;
namespace SmartCoalApplication
{
///
/// Represents an enumerable collection of items. Each item can only be present
/// in the collection once. An item's identity is determined by a combination
/// of the return values from its GetHashCode and Equals methods.
/// This class is analagous to C++'s std::set template class.
///
[Serializable]
public class Set
: IEnumerable,
ICloneable,
ICollection
{
private Hashtable hashtable;
///
/// Adds an element to the set.
///
/// The object reference to be included in the set.
/// item is a null reference
/// item is already in the Set
public void Add(object item)
{
try
{
hashtable.Add(item, null);
}
catch (ArgumentNullException e1)
{
throw e1;
}
catch (ArgumentException e2)
{
throw e2;
}
}
///
/// Removes an element from the set.
///
/// The object reference to be excluded from the set.
/// item is a null reference
public void Remove(object item)
{
try
{
hashtable.Remove(item);
}
catch (ArgumentNullException e1)
{
throw e1;
}
}
///
/// Determines whether the Set includes a specific element.
///
/// The object reference to check for.
/// true if the Set includes item, false if it doesn't.
/// item is a null reference.
public bool Contains(object item)
{
try
{
return hashtable.ContainsKey(item);
}
catch (ArgumentNullException e1)
{
throw e1;
}
}
///
/// Constructs an empty Set.
///
public Set()
{
this.hashtable = new Hashtable();
}
///
/// Constructs a Set with data copied from the given list.
///
///
public Set(IEnumerable cloneMe)
{
this.hashtable = new Hashtable();
foreach (object theObject in cloneMe)
{
Add(theObject);
}
}
public static Set Create(params T[] items)
{
return new Set(items);
}
///
/// Constructs a copy of a Set.
///
/// The Set to copy from.
private Set(Set copyMe)
{
hashtable = (Hashtable)copyMe.Clone();
}
#region IEnumerable Members
///
/// Returns an IEnumerator that can be used to enumerate through the items in the Set.
///
/// An IEnumerator for the Set.
public IEnumerator GetEnumerator()
{
return hashtable.Keys.GetEnumerator();
}
#endregion
#region ICloneable Members
///
/// Returns a copy of the Set. The elements in the Set are copied by-reference only.
///
///
public object Clone()
{
return new Set(this);
}
#endregion
#region ICollection Members
///
/// Gets a value indicating whether or not the Set is synchronized (thread-safe).
///
public bool IsSynchronized
{
get
{
return false;
}
}
///
/// Gets a value indicating how many elements are contained within the Set.
///
public int Count
{
get
{
return hashtable.Count;
}
}
///
/// Copies the Set elements to a one-dimensional Array instance at a specified index.
///
/// The one-dimensional Array that is the destination of the objects copied from the Set. The Array must have zero-based indexing.
/// The zero-based index in array at which copying begins.
/// array is a null reference.
/// index is less than zero.
/// The array is not one-dimensional, or the array could not contain the objects copied to it.
/// The Array does not have enough space, starting from the given offset, to contain all the Set's objects.
public void CopyTo(Array array, int index)
{
int i = index;
if (array == null)
{
throw new ArgumentNullException("array");
}
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
foreach (object o in this)
{
try
{
array.SetValue(o, i);
}
catch (ArgumentException e1)
{
throw e1;
}
catch (IndexOutOfRangeException e2)
{
throw e2;
}
++i;
}
}
///
/// Gets an object that can be used to synchronize access to the Set.
///
public object SyncRoot
{
get
{
return this;
}
}
#endregion
///
/// Copies the elements of the Set to a new generic array.
///
/// An array of object references.
public object[] ToArray()
{
object[] array = new object[Count];
int index = 0;
foreach (object o in this)
{
array[index] = o;
++index;
}
return array;
}
///
/// Copies the elements of the Set to a new array of the requested type.
///
/// The Type of array to create and copy elements to.
/// An array of objects of the requested type.
public Array ToArray(Type type)
{
Array array = Array.CreateInstance(type, Count);
int index = 0;
foreach (object o in this)
{
array.SetValue(o, index);
++index;
}
return array;
}
}
}