Skip to main content
Code Review

Return to Question

added 247 characters in body
Source Link
user366312
  • 737
  • 4
  • 15

Edit: replace Add() with the following to add the missing "unique" functionality:

public void Add(T item)
{
 if(!m_ListContainer.Contains(item))
 {
 m_ListContainer.Add(item);
 }
}

Edit: replace Add() with the following to add the missing "unique" functionality:

public void Add(T item)
{
 if(!m_ListContainer.Contains(item))
 {
 m_ListContainer.Add(item);
 }
}
Rollback to Revision 4
Source Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 113
public interface ISet<T>: ICloneable, IEnumerable<T>, IList<T>
{
 IEnumerable<T> Union(IEnumerable<T> set2);
 IEnumerable<T> Difference(IEnumerable<T> set2);
 IEnumerable<T> Intersection(IEnumerable<T> set2);
 IEnumerable<T> Complement(IEnumerable<T> universalSet);
 bool Disjoint(IEnumerable<T> set2);
 void AddRange(IEnumerable<T> set);
 IEnumerable<T> ToEnumerable();
}
public class Set<T> : ISet<T>, ICloneable, IEnumerable<T>, IList<T>, IList, ICollection, IEnumerable
{
 private List<T> m_ListContainer = null;
 public Set()
 {
 m_ListContainer = new List<T>();
 }
 public Set(IEnumerable<T> collection)
 {
 m_ListContainer = new List<T>(collection);
 }
 #region IList<T> implementations
 public T this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = value;
 }
 }
 object IList.this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = (T)value;
 }
 }
 public int Count
 {
 get
 {
 return m_ListContainer.Count;
 }
 }
 public bool IsReadOnly
 {
 get
 {
 return false;
 }
 }
 public void Add(T item)
 {
 if(!m_ListContainer.Contains(item))
 {
 m_ListContainer.Add(item);
 }
 }
 public void Clear()
 {
 m_ListContainer.Clear();
 }
 public bool Contains(T item)
 {
 return m_ListContainer.Contains(item);
 }
 public void CopyTo(T[] array, int arrayIndex)
 {
 m_ListContainer.CopyTo(array, arrayIndex);
 }
 public IEnumerator<T> GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 public int IndexOf(T item)
 {
 return m_ListContainer.IndexOf(item);
 }
 public void Insert(int index, T item)
 {
 m_ListContainer.Insert(index, item);
 }
 public bool Remove(T item)
 {
 return m_ListContainer.Remove(item);
 }
 public void RemoveAt(int index)
 {
 m_ListContainer.RemoveAt(index);
 }
 IEnumerator IEnumerable.GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 #endregion
 /// <summary>
 /// complement the current list on the basis of universalset
 /// </summary>
 /// <param name="universalSet"></param>
 public IEnumerable<T> Complement(IEnumerable<T> universalSet)
 {
 // create a copy of the universalSet
 List<T> list = new List<T>(universalSet);
 foreach (T item in m_ListContainer)
 {
 list.Remove(item);
 }
 return list;
 }
 /// <summary>
 /// return [this - set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Difference(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// Two sets A and B are mutually exclusive or disjoint if they 
 /// do not have any shared elements; i.e., their intersection is 
 /// the empty set, A∩B=∅.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public bool Disjoint(IEnumerable<T> set2)
 {
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>)set2).Contains(item))
 {
 return false; 
 }
 }
 return true;
 }
 /// <summary>
 /// The intersection of two sets A and B, denoted by A∩B, consists of all elements 
 /// that are both in A and B. For example, {1,2}∩{2,3}={2}.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Intersection(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if(!((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// return Union [this, set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Union(IEnumerable<T> set2)
 {
 IEnumerable<T> unionList = m_ListContainer.ToArray();//clone the currect data
 List<T> list = new List<T>(unionList);
 list.AddRange(set2);
 return list.ToArray();
 }
 /// <summary>
 /// Implementing IClonable.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
 T [] objects = new T[m_ListContainer.Count];
 int i = 0;
 foreach (T item in m_ListContainer)
 {
 objects[i] = item;
 i++;
 }
 return objects;
 }
 public void AddRange(IEnumerable<T> set)
 {
 m_ListContainer.AddRange(set);
 }
 public IEnumerable<T> ToEnumerable()
 {
 return m_ListContainer.ToArray();
 }
 public void Show()
 {
 foreach (var item in m_ListContainer)
 {
 Console.Write(item + ", ");
 }
 Console.ReadLine();
 }
 public int Add(object value)
 {
 this.Add((T)value);
 return m_ListContainer.Count - 1;
 }
 public bool Contains(object value)
 {
 T item = (T)value;
 return this.Contains(item);
 }
 public int IndexOf(object value)
 {
 T item = (T)value;
 return this.IndexOf(item);
 }
 public void Insert(int index, object value)
 {
 T item = (T)value;
 this.Insert(index, item);
 }
 public void Remove(object value)
 {
 T item = (T)value;
 this.Remove(item);
 }
 public void CopyTo(Array array, int index)
 {
 T[] arr = (T[])array.Clone();
 this.CopyTo(arr, index);
 }
 public bool IsFixedSize
 {
 get
 {
 return false;
 }
 }
 private Object _syncRoot;
 public object SyncRoot
 {
 get
 {
 if (_syncRoot == null)
 {
 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
 }
 return _syncRoot;
 }
 }
 public bool IsSynchronized
 {
 get
 {
 return true;
 }
 }
}

Note: Each element must be unique. I missed that in my implementation.

public interface ISet<T>: ICloneable, IEnumerable<T>, IList<T>
{
 IEnumerable<T> Union(IEnumerable<T> set2);
 IEnumerable<T> Difference(IEnumerable<T> set2);
 IEnumerable<T> Intersection(IEnumerable<T> set2);
 IEnumerable<T> Complement(IEnumerable<T> universalSet);
 bool Disjoint(IEnumerable<T> set2);
 void AddRange(IEnumerable<T> set);
 IEnumerable<T> ToEnumerable();
}
public class Set<T> : ISet<T>, ICloneable, IEnumerable<T>, IList<T>, IList, ICollection, IEnumerable
{
 private List<T> m_ListContainer = null;
 public Set()
 {
 m_ListContainer = new List<T>();
 }
 public Set(IEnumerable<T> collection)
 {
 m_ListContainer = new List<T>(collection);
 }
 #region IList<T> implementations
 public T this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = value;
 }
 }
 object IList.this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = (T)value;
 }
 }
 public int Count
 {
 get
 {
 return m_ListContainer.Count;
 }
 }
 public bool IsReadOnly
 {
 get
 {
 return false;
 }
 }
 public void Add(T item)
 {
 if(!m_ListContainer.Contains(item))
 {
 m_ListContainer.Add(item);
 }
 }
 public void Clear()
 {
 m_ListContainer.Clear();
 }
 public bool Contains(T item)
 {
 return m_ListContainer.Contains(item);
 }
 public void CopyTo(T[] array, int arrayIndex)
 {
 m_ListContainer.CopyTo(array, arrayIndex);
 }
 public IEnumerator<T> GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 public int IndexOf(T item)
 {
 return m_ListContainer.IndexOf(item);
 }
 public void Insert(int index, T item)
 {
 m_ListContainer.Insert(index, item);
 }
 public bool Remove(T item)
 {
 return m_ListContainer.Remove(item);
 }
 public void RemoveAt(int index)
 {
 m_ListContainer.RemoveAt(index);
 }
 IEnumerator IEnumerable.GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 #endregion
 /// <summary>
 /// complement the current list on the basis of universalset
 /// </summary>
 /// <param name="universalSet"></param>
 public IEnumerable<T> Complement(IEnumerable<T> universalSet)
 {
 // create a copy of the universalSet
 List<T> list = new List<T>(universalSet);
 foreach (T item in m_ListContainer)
 {
 list.Remove(item);
 }
 return list;
 }
 /// <summary>
 /// return [this - set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Difference(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// Two sets A and B are mutually exclusive or disjoint if they 
 /// do not have any shared elements; i.e., their intersection is 
 /// the empty set, A∩B=∅.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public bool Disjoint(IEnumerable<T> set2)
 {
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>)set2).Contains(item))
 {
 return false; 
 }
 }
 return true;
 }
 /// <summary>
 /// The intersection of two sets A and B, denoted by A∩B, consists of all elements 
 /// that are both in A and B. For example, {1,2}∩{2,3}={2}.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Intersection(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if(!((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// return Union [this, set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Union(IEnumerable<T> set2)
 {
 IEnumerable<T> unionList = m_ListContainer.ToArray();//clone the currect data
 List<T> list = new List<T>(unionList);
 list.AddRange(set2);
 return list.ToArray();
 }
 /// <summary>
 /// Implementing IClonable.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
 T [] objects = new T[m_ListContainer.Count];
 int i = 0;
 foreach (T item in m_ListContainer)
 {
 objects[i] = item;
 i++;
 }
 return objects;
 }
 public void AddRange(IEnumerable<T> set)
 {
 m_ListContainer.AddRange(set);
 }
 public IEnumerable<T> ToEnumerable()
 {
 return m_ListContainer.ToArray();
 }
 public void Show()
 {
 foreach (var item in m_ListContainer)
 {
 Console.Write(item + ", ");
 }
 Console.ReadLine();
 }
 public int Add(object value)
 {
 this.Add((T)value);
 return m_ListContainer.Count - 1;
 }
 public bool Contains(object value)
 {
 T item = (T)value;
 return this.Contains(item);
 }
 public int IndexOf(object value)
 {
 T item = (T)value;
 return this.IndexOf(item);
 }
 public void Insert(int index, object value)
 {
 T item = (T)value;
 this.Insert(index, item);
 }
 public void Remove(object value)
 {
 T item = (T)value;
 this.Remove(item);
 }
 public void CopyTo(Array array, int index)
 {
 T[] arr = (T[])array.Clone();
 this.CopyTo(arr, index);
 }
 public bool IsFixedSize
 {
 get
 {
 return false;
 }
 }
 private Object _syncRoot;
 public object SyncRoot
 {
 get
 {
 if (_syncRoot == null)
 {
 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
 }
 return _syncRoot;
 }
 }
 public bool IsSynchronized
 {
 get
 {
 return true;
 }
 }
}
public interface ISet<T>: ICloneable, IEnumerable<T>, IList<T>
{
 IEnumerable<T> Union(IEnumerable<T> set2);
 IEnumerable<T> Difference(IEnumerable<T> set2);
 IEnumerable<T> Intersection(IEnumerable<T> set2);
 IEnumerable<T> Complement(IEnumerable<T> universalSet);
 bool Disjoint(IEnumerable<T> set2);
 void AddRange(IEnumerable<T> set);
 IEnumerable<T> ToEnumerable();
}
public class Set<T> : ISet<T>, ICloneable, IEnumerable<T>, IList<T>, IList, ICollection, IEnumerable
{
 private List<T> m_ListContainer = null;
 public Set()
 {
 m_ListContainer = new List<T>();
 }
 public Set(IEnumerable<T> collection)
 {
 m_ListContainer = new List<T>(collection);
 }
 #region IList<T> implementations
 public T this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = value;
 }
 }
 object IList.this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = (T)value;
 }
 }
 public int Count
 {
 get
 {
 return m_ListContainer.Count;
 }
 }
 public bool IsReadOnly
 {
 get
 {
 return false;
 }
 }
 public void Add(T item)
 {
 m_ListContainer.Add(item);
 }
 public void Clear()
 {
 m_ListContainer.Clear();
 }
 public bool Contains(T item)
 {
 return m_ListContainer.Contains(item);
 }
 public void CopyTo(T[] array, int arrayIndex)
 {
 m_ListContainer.CopyTo(array, arrayIndex);
 }
 public IEnumerator<T> GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 public int IndexOf(T item)
 {
 return m_ListContainer.IndexOf(item);
 }
 public void Insert(int index, T item)
 {
 m_ListContainer.Insert(index, item);
 }
 public bool Remove(T item)
 {
 return m_ListContainer.Remove(item);
 }
 public void RemoveAt(int index)
 {
 m_ListContainer.RemoveAt(index);
 }
 IEnumerator IEnumerable.GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 #endregion
 /// <summary>
 /// complement the current list on the basis of universalset
 /// </summary>
 /// <param name="universalSet"></param>
 public IEnumerable<T> Complement(IEnumerable<T> universalSet)
 {
 // create a copy of the universalSet
 List<T> list = new List<T>(universalSet);
 foreach (T item in m_ListContainer)
 {
 list.Remove(item);
 }
 return list;
 }
 /// <summary>
 /// return [this - set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Difference(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// Two sets A and B are mutually exclusive or disjoint if they 
 /// do not have any shared elements; i.e., their intersection is 
 /// the empty set, A∩B=∅.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public bool Disjoint(IEnumerable<T> set2)
 {
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>)set2).Contains(item))
 {
 return false; 
 }
 }
 return true;
 }
 /// <summary>
 /// The intersection of two sets A and B, denoted by A∩B, consists of all elements 
 /// that are both in A and B. For example, {1,2}∩{2,3}={2}.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Intersection(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if(!((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// return Union [this, set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Union(IEnumerable<T> set2)
 {
 IEnumerable<T> unionList = m_ListContainer.ToArray();//clone the currect data
 List<T> list = new List<T>(unionList);
 list.AddRange(set2);
 return list.ToArray();
 }
 /// <summary>
 /// Implementing IClonable.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
 T [] objects = new T[m_ListContainer.Count];
 int i = 0;
 foreach (T item in m_ListContainer)
 {
 objects[i] = item;
 i++;
 }
 return objects;
 }
 public void AddRange(IEnumerable<T> set)
 {
 m_ListContainer.AddRange(set);
 }
 public IEnumerable<T> ToEnumerable()
 {
 return m_ListContainer.ToArray();
 }
 public void Show()
 {
 foreach (var item in m_ListContainer)
 {
 Console.Write(item + ", ");
 }
 Console.ReadLine();
 }
 public int Add(object value)
 {
 this.Add((T)value);
 return m_ListContainer.Count - 1;
 }
 public bool Contains(object value)
 {
 T item = (T)value;
 return this.Contains(item);
 }
 public int IndexOf(object value)
 {
 T item = (T)value;
 return this.IndexOf(item);
 }
 public void Insert(int index, object value)
 {
 T item = (T)value;
 this.Insert(index, item);
 }
 public void Remove(object value)
 {
 T item = (T)value;
 this.Remove(item);
 }
 public void CopyTo(Array array, int index)
 {
 T[] arr = (T[])array.Clone();
 this.CopyTo(arr, index);
 }
 public bool IsFixedSize
 {
 get
 {
 return false;
 }
 }
 private Object _syncRoot;
 public object SyncRoot
 {
 get
 {
 if (_syncRoot == null)
 {
 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
 }
 return _syncRoot;
 }
 }
 public bool IsSynchronized
 {
 get
 {
 return true;
 }
 }
}

Note: Each element must be unique. I missed that in my implementation.

added 83 characters in body
Source Link
user366312
  • 737
  • 4
  • 15
public interface ISet<T>: ICloneable, IEnumerable<T>, IList<T>
{
 IEnumerable<T> Union(IEnumerable<T> set2);
 IEnumerable<T> Difference(IEnumerable<T> set2);
 IEnumerable<T> Intersection(IEnumerable<T> set2);
 IEnumerable<T> Complement(IEnumerable<T> universalSet);
 bool Disjoint(IEnumerable<T> set2);
 void AddRange(IEnumerable<T> set);
 IEnumerable<T> ToEnumerable();
}
public class Set<T> : ISet<T>, ICloneable, IEnumerable<T>, IList<T>, IList, ICollection, IEnumerable
{
 private List<T> m_ListContainer = null;
 public Set()
 {
 m_ListContainer = new List<T>();
 }
 public Set(IEnumerable<T> collection)
 {
 m_ListContainer = new List<T>(collection);
 }
 #region IList<T> implementations
 public T this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = value;
 }
 }
 object IList.this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = (T)value;
 }
 }
 public int Count
 {
 get
 {
 return m_ListContainer.Count;
 }
 }
 public bool IsReadOnly
 {
 get
 {
 return false;
 }
 }
 public void Add(T item)
 {
 if(!m_ListContainer.Contains(item))
 {
 m_ListContainer.Add(item);
 }
 }
 public void Clear()
 {
 m_ListContainer.Clear();
 }
 public bool Contains(T item)
 {
 return m_ListContainer.Contains(item);
 }
 public void CopyTo(T[] array, int arrayIndex)
 {
 m_ListContainer.CopyTo(array, arrayIndex);
 }
 public IEnumerator<T> GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 public int IndexOf(T item)
 {
 return m_ListContainer.IndexOf(item);
 }
 public void Insert(int index, T item)
 {
 m_ListContainer.Insert(index, item);
 }
 public bool Remove(T item)
 {
 return m_ListContainer.Remove(item);
 }
 public void RemoveAt(int index)
 {
 m_ListContainer.RemoveAt(index);
 }
 IEnumerator IEnumerable.GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 #endregion
 /// <summary>
 /// complement the current list on the basis of universalset
 /// </summary>
 /// <param name="universalSet"></param>
 public IEnumerable<T> Complement(IEnumerable<T> universalSet)
 {
 // create a copy of the universalSet
 List<T> list = new List<T>(universalSet);
 foreach (T item in m_ListContainer)
 {
 list.Remove(item);
 }
 return list;
 }
 /// <summary>
 /// return [this - set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Difference(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// Two sets A and B are mutually exclusive or disjoint if they 
 /// do not have any shared elements; i.e., their intersection is 
 /// the empty set, A∩B=∅.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public bool Disjoint(IEnumerable<T> set2)
 {
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>)set2).Contains(item))
 {
 return false; 
 }
 }
 return true;
 }
 /// <summary>
 /// The intersection of two sets A and B, denoted by A∩B, consists of all elements 
 /// that are both in A and B. For example, {1,2}∩{2,3}={2}.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Intersection(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if(!((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// return Union [this, set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Union(IEnumerable<T> set2)
 {
 IEnumerable<T> unionList = m_ListContainer.ToArray();//clone the currect data
 List<T> list = new List<T>(unionList);
 list.AddRange(set2);
 return list.ToArray();
 }
 /// <summary>
 /// Implementing IClonable.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
 T [] objects = new T[m_ListContainer.Count];
 int i = 0;
 foreach (T item in m_ListContainer)
 {
 objects[i] = item;
 i++;
 }
 return objects;
 }
 public void AddRange(IEnumerable<T> set)
 {
 m_ListContainer.AddRange(set);
 }
 public IEnumerable<T> ToEnumerable()
 {
 return m_ListContainer.ToArray();
 }
 public void Show()
 {
 foreach (var item in m_ListContainer)
 {
 Console.Write(item + ", ");
 }
 Console.ReadLine();
 }
 public int Add(object value)
 {
 this.Add((T)value);
 return m_ListContainer.Count - 1;
 }
 public bool Contains(object value)
 {
 T item = (T)value;
 return this.Contains(item);
 }
 public int IndexOf(object value)
 {
 T item = (T)value;
 return this.IndexOf(item);
 }
 public void Insert(int index, object value)
 {
 T item = (T)value;
 this.Insert(index, item);
 }
 public void Remove(object value)
 {
 T item = (T)value;
 this.Remove(item);
 }
 public void CopyTo(Array array, int index)
 {
 T[] arr = (T[])array.Clone();
 this.CopyTo(arr, index);
 }
 public bool IsFixedSize
 {
 get
 {
 return false;
 }
 }
 private Object _syncRoot;
 public object SyncRoot
 {
 get
 {
 if (_syncRoot == null)
 {
 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
 }
 return _syncRoot;
 }
 }
 public bool IsSynchronized
 {
 get
 {
 return true;
 }
 }
}

Note: Each element must be unique. I missed that in my implementation.

public interface ISet<T>: ICloneable, IEnumerable<T>, IList<T>
{
 IEnumerable<T> Union(IEnumerable<T> set2);
 IEnumerable<T> Difference(IEnumerable<T> set2);
 IEnumerable<T> Intersection(IEnumerable<T> set2);
 IEnumerable<T> Complement(IEnumerable<T> universalSet);
 bool Disjoint(IEnumerable<T> set2);
 void AddRange(IEnumerable<T> set);
 IEnumerable<T> ToEnumerable();
}
public class Set<T> : ISet<T>, ICloneable, IEnumerable<T>, IList<T>, IList, ICollection, IEnumerable
{
 private List<T> m_ListContainer = null;
 public Set()
 {
 m_ListContainer = new List<T>();
 }
 public Set(IEnumerable<T> collection)
 {
 m_ListContainer = new List<T>(collection);
 }
 #region IList<T> implementations
 public T this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = value;
 }
 }
 object IList.this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = (T)value;
 }
 }
 public int Count
 {
 get
 {
 return m_ListContainer.Count;
 }
 }
 public bool IsReadOnly
 {
 get
 {
 return false;
 }
 }
 public void Add(T item)
 {
 m_ListContainer.Add(item);
 }
 public void Clear()
 {
 m_ListContainer.Clear();
 }
 public bool Contains(T item)
 {
 return m_ListContainer.Contains(item);
 }
 public void CopyTo(T[] array, int arrayIndex)
 {
 m_ListContainer.CopyTo(array, arrayIndex);
 }
 public IEnumerator<T> GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 public int IndexOf(T item)
 {
 return m_ListContainer.IndexOf(item);
 }
 public void Insert(int index, T item)
 {
 m_ListContainer.Insert(index, item);
 }
 public bool Remove(T item)
 {
 return m_ListContainer.Remove(item);
 }
 public void RemoveAt(int index)
 {
 m_ListContainer.RemoveAt(index);
 }
 IEnumerator IEnumerable.GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 #endregion
 /// <summary>
 /// complement the current list on the basis of universalset
 /// </summary>
 /// <param name="universalSet"></param>
 public IEnumerable<T> Complement(IEnumerable<T> universalSet)
 {
 // create a copy of the universalSet
 List<T> list = new List<T>(universalSet);
 foreach (T item in m_ListContainer)
 {
 list.Remove(item);
 }
 return list;
 }
 /// <summary>
 /// return [this - set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Difference(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// Two sets A and B are mutually exclusive or disjoint if they 
 /// do not have any shared elements; i.e., their intersection is 
 /// the empty set, A∩B=∅.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public bool Disjoint(IEnumerable<T> set2)
 {
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>)set2).Contains(item))
 {
 return false; 
 }
 }
 return true;
 }
 /// <summary>
 /// The intersection of two sets A and B, denoted by A∩B, consists of all elements 
 /// that are both in A and B. For example, {1,2}∩{2,3}={2}.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Intersection(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if(!((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// return Union [this, set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Union(IEnumerable<T> set2)
 {
 IEnumerable<T> unionList = m_ListContainer.ToArray();//clone the currect data
 List<T> list = new List<T>(unionList);
 list.AddRange(set2);
 return list.ToArray();
 }
 /// <summary>
 /// Implementing IClonable.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
 T [] objects = new T[m_ListContainer.Count];
 int i = 0;
 foreach (T item in m_ListContainer)
 {
 objects[i] = item;
 i++;
 }
 return objects;
 }
 public void AddRange(IEnumerable<T> set)
 {
 m_ListContainer.AddRange(set);
 }
 public IEnumerable<T> ToEnumerable()
 {
 return m_ListContainer.ToArray();
 }
 public void Show()
 {
 foreach (var item in m_ListContainer)
 {
 Console.Write(item + ", ");
 }
 Console.ReadLine();
 }
 public int Add(object value)
 {
 this.Add((T)value);
 return m_ListContainer.Count - 1;
 }
 public bool Contains(object value)
 {
 T item = (T)value;
 return this.Contains(item);
 }
 public int IndexOf(object value)
 {
 T item = (T)value;
 return this.IndexOf(item);
 }
 public void Insert(int index, object value)
 {
 T item = (T)value;
 this.Insert(index, item);
 }
 public void Remove(object value)
 {
 T item = (T)value;
 this.Remove(item);
 }
 public void CopyTo(Array array, int index)
 {
 T[] arr = (T[])array.Clone();
 this.CopyTo(arr, index);
 }
 public bool IsFixedSize
 {
 get
 {
 return false;
 }
 }
 private Object _syncRoot;
 public object SyncRoot
 {
 get
 {
 if (_syncRoot == null)
 {
 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
 }
 return _syncRoot;
 }
 }
 public bool IsSynchronized
 {
 get
 {
 return true;
 }
 }
}

Note: Each element must be unique. I missed that in my implementation.

public interface ISet<T>: ICloneable, IEnumerable<T>, IList<T>
{
 IEnumerable<T> Union(IEnumerable<T> set2);
 IEnumerable<T> Difference(IEnumerable<T> set2);
 IEnumerable<T> Intersection(IEnumerable<T> set2);
 IEnumerable<T> Complement(IEnumerable<T> universalSet);
 bool Disjoint(IEnumerable<T> set2);
 void AddRange(IEnumerable<T> set);
 IEnumerable<T> ToEnumerable();
}
public class Set<T> : ISet<T>, ICloneable, IEnumerable<T>, IList<T>, IList, ICollection, IEnumerable
{
 private List<T> m_ListContainer = null;
 public Set()
 {
 m_ListContainer = new List<T>();
 }
 public Set(IEnumerable<T> collection)
 {
 m_ListContainer = new List<T>(collection);
 }
 #region IList<T> implementations
 public T this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = value;
 }
 }
 object IList.this[int index]
 {
 get
 {
 return m_ListContainer[index];
 }
 set
 {
 m_ListContainer[index] = (T)value;
 }
 }
 public int Count
 {
 get
 {
 return m_ListContainer.Count;
 }
 }
 public bool IsReadOnly
 {
 get
 {
 return false;
 }
 }
 public void Add(T item)
 {
 if(!m_ListContainer.Contains(item))
 {
 m_ListContainer.Add(item);
 }
 }
 public void Clear()
 {
 m_ListContainer.Clear();
 }
 public bool Contains(T item)
 {
 return m_ListContainer.Contains(item);
 }
 public void CopyTo(T[] array, int arrayIndex)
 {
 m_ListContainer.CopyTo(array, arrayIndex);
 }
 public IEnumerator<T> GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 public int IndexOf(T item)
 {
 return m_ListContainer.IndexOf(item);
 }
 public void Insert(int index, T item)
 {
 m_ListContainer.Insert(index, item);
 }
 public bool Remove(T item)
 {
 return m_ListContainer.Remove(item);
 }
 public void RemoveAt(int index)
 {
 m_ListContainer.RemoveAt(index);
 }
 IEnumerator IEnumerable.GetEnumerator()
 {
 return m_ListContainer.GetEnumerator();
 }
 #endregion
 /// <summary>
 /// complement the current list on the basis of universalset
 /// </summary>
 /// <param name="universalSet"></param>
 public IEnumerable<T> Complement(IEnumerable<T> universalSet)
 {
 // create a copy of the universalSet
 List<T> list = new List<T>(universalSet);
 foreach (T item in m_ListContainer)
 {
 list.Remove(item);
 }
 return list;
 }
 /// <summary>
 /// return [this - set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Difference(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// Two sets A and B are mutually exclusive or disjoint if they 
 /// do not have any shared elements; i.e., their intersection is 
 /// the empty set, A∩B=∅.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public bool Disjoint(IEnumerable<T> set2)
 {
 foreach (T item in m_ListContainer)
 {
 if (((ISet<T>)set2).Contains(item))
 {
 return false; 
 }
 }
 return true;
 }
 /// <summary>
 /// The intersection of two sets A and B, denoted by A∩B, consists of all elements 
 /// that are both in A and B. For example, {1,2}∩{2,3}={2}.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Intersection(IEnumerable<T> set2)
 {
 List<T> newSet = new List<T>(m_ListContainer.ToArray());
 foreach (T item in m_ListContainer)
 {
 if(!((ISet<T>) set2).Contains(item))
 {
 newSet.Remove(item);
 }
 }
 return newSet;
 }
 /// <summary>
 /// return Union [this, set2]
 /// </summary>
 /// <param name="set2"></param>
 /// <returns></returns>
 public IEnumerable<T> Union(IEnumerable<T> set2)
 {
 IEnumerable<T> unionList = m_ListContainer.ToArray();//clone the currect data
 List<T> list = new List<T>(unionList);
 list.AddRange(set2);
 return list.ToArray();
 }
 /// <summary>
 /// Implementing IClonable.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
 T [] objects = new T[m_ListContainer.Count];
 int i = 0;
 foreach (T item in m_ListContainer)
 {
 objects[i] = item;
 i++;
 }
 return objects;
 }
 public void AddRange(IEnumerable<T> set)
 {
 m_ListContainer.AddRange(set);
 }
 public IEnumerable<T> ToEnumerable()
 {
 return m_ListContainer.ToArray();
 }
 public void Show()
 {
 foreach (var item in m_ListContainer)
 {
 Console.Write(item + ", ");
 }
 Console.ReadLine();
 }
 public int Add(object value)
 {
 this.Add((T)value);
 return m_ListContainer.Count - 1;
 }
 public bool Contains(object value)
 {
 T item = (T)value;
 return this.Contains(item);
 }
 public int IndexOf(object value)
 {
 T item = (T)value;
 return this.IndexOf(item);
 }
 public void Insert(int index, object value)
 {
 T item = (T)value;
 this.Insert(index, item);
 }
 public void Remove(object value)
 {
 T item = (T)value;
 this.Remove(item);
 }
 public void CopyTo(Array array, int index)
 {
 T[] arr = (T[])array.Clone();
 this.CopyTo(arr, index);
 }
 public bool IsFixedSize
 {
 get
 {
 return false;
 }
 }
 private Object _syncRoot;
 public object SyncRoot
 {
 get
 {
 if (_syncRoot == null)
 {
 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
 }
 return _syncRoot;
 }
 }
 public bool IsSynchronized
 {
 get
 {
 return true;
 }
 }
}
added 3 characters in body
Source Link
user366312
  • 737
  • 4
  • 15
Loading
added 97 characters in body
Source Link
user366312
  • 737
  • 4
  • 15
Loading
added 97 characters in body
Source Link
user366312
  • 737
  • 4
  • 15
Loading
Source Link
user366312
  • 737
  • 4
  • 15
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /