diff Collections/SimpleSet.cs @ 111:9832caa89140

* Make the SimpleSet a proper collection
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 Jan 2012 16:52:48 +0000
parents 0352fa33ee8f
children 3262c0ad2d3d
line wrap: on
line diff
--- a/Collections/SimpleSet.cs	Fri Jan 20 20:50:25 2012 +0000
+++ b/Collections/SimpleSet.cs	Sat Jan 28 16:52:48 2012 +0000
@@ -1,14 +1,13 @@
 // This file (SimpleSet.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
 //
 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
-
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 namespace IBBoard.Collections
 {
-	public class SimpleSet<TYPE> : IEnumerable
+	public class SimpleSet<TYPE> : ICollection<TYPE>
 	{		
 		private Dictionary<int, TYPE> dictionary;
 		
@@ -17,17 +16,9 @@
 			dictionary = new Dictionary<int,TYPE>();
 		}
 		
-		public bool Add(TYPE val)
+		public void Add(TYPE val)
 		{
-			bool added = false;
-			
-			if (!dictionary.ContainsKey(val.GetHashCode()))
-			{
-				dictionary.Add(val.GetHashCode(), val);
-				added = true;
-			}
-			
-			return added;
+			dictionary.Add(val.GetHashCode(), val);
 		}
 		
 		public bool AddRange(ICollection<TYPE> vals)
@@ -48,21 +39,23 @@
 			return dictionary.Remove(val.GetHashCode());
 		}
 		
-		public bool Contains (TYPE item)
+		public bool Contains(TYPE item)
 		{
 			return dictionary.ContainsKey(item.GetHashCode());
 		}
 
-		public void Clear ()
+		public void Clear()
 		{
 			dictionary.Clear();
 		}
 		
-		public int Count {
+		public int Count
+		{
 			get { return dictionary.Count; }
 		}
 		
-		public bool IsReadOnly {
+		public bool IsReadOnly
+		{
 			get { return false; }
 		}
 		
@@ -71,5 +64,27 @@
 			return dictionary.Values.GetEnumerator();
 		}
 
+		public void CopyTo(TYPE[] array, int arrayIndex)
+		{
+			if (arrayIndex + dictionary.Count > array.Length)
+			{
+				throw new ArgumentOutOfRangeException("arrayIndex", "Insufficient space in array");
+			}
+			if (arrayIndex < 0)
+			{
+				throw new ArgumentOutOfRangeException("arrayIndex", "Value must be > 0");
+			}
+
+			int i = arrayIndex;
+			foreach (TYPE val in dictionary.Values)
+			{
+				array[i++] = val;
+			}
+		}
+
+		IEnumerator<TYPE> IEnumerable<TYPE>.GetEnumerator()
+		{
+			return dictionary.Values.GetEnumerator();
+		}
 	}
 }