changeset 111:9832caa89140

* Make the SimpleSet a proper collection
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 Jan 2012 16:52:48 +0000
parents a5714f82073d
children 3262c0ad2d3d
files Collections/SimpleSet.cs
diffstat 1 files changed, 31 insertions(+), 16 deletions(-) [+]
line diff
     1.1 --- a/Collections/SimpleSet.cs	Fri Jan 20 20:50:25 2012 +0000
     1.2 +++ b/Collections/SimpleSet.cs	Sat Jan 28 16:52:48 2012 +0000
     1.3 @@ -1,14 +1,13 @@
     1.4  // This file (SimpleSet.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
     1.5  //
     1.6  // 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.
     1.7 -
     1.8  using System;
     1.9  using System.Collections;
    1.10  using System.Collections.Generic;
    1.11  
    1.12  namespace IBBoard.Collections
    1.13  {
    1.14 -	public class SimpleSet<TYPE> : IEnumerable
    1.15 +	public class SimpleSet<TYPE> : ICollection<TYPE>
    1.16  	{		
    1.17  		private Dictionary<int, TYPE> dictionary;
    1.18  		
    1.19 @@ -17,17 +16,9 @@
    1.20  			dictionary = new Dictionary<int,TYPE>();
    1.21  		}
    1.22  		
    1.23 -		public bool Add(TYPE val)
    1.24 +		public void Add(TYPE val)
    1.25  		{
    1.26 -			bool added = false;
    1.27 -			
    1.28 -			if (!dictionary.ContainsKey(val.GetHashCode()))
    1.29 -			{
    1.30 -				dictionary.Add(val.GetHashCode(), val);
    1.31 -				added = true;
    1.32 -			}
    1.33 -			
    1.34 -			return added;
    1.35 +			dictionary.Add(val.GetHashCode(), val);
    1.36  		}
    1.37  		
    1.38  		public bool AddRange(ICollection<TYPE> vals)
    1.39 @@ -48,21 +39,23 @@
    1.40  			return dictionary.Remove(val.GetHashCode());
    1.41  		}
    1.42  		
    1.43 -		public bool Contains (TYPE item)
    1.44 +		public bool Contains(TYPE item)
    1.45  		{
    1.46  			return dictionary.ContainsKey(item.GetHashCode());
    1.47  		}
    1.48  
    1.49 -		public void Clear ()
    1.50 +		public void Clear()
    1.51  		{
    1.52  			dictionary.Clear();
    1.53  		}
    1.54  		
    1.55 -		public int Count {
    1.56 +		public int Count
    1.57 +		{
    1.58  			get { return dictionary.Count; }
    1.59  		}
    1.60  		
    1.61 -		public bool IsReadOnly {
    1.62 +		public bool IsReadOnly
    1.63 +		{
    1.64  			get { return false; }
    1.65  		}
    1.66  		
    1.67 @@ -71,5 +64,27 @@
    1.68  			return dictionary.Values.GetEnumerator();
    1.69  		}
    1.70  
    1.71 +		public void CopyTo(TYPE[] array, int arrayIndex)
    1.72 +		{
    1.73 +			if (arrayIndex + dictionary.Count > array.Length)
    1.74 +			{
    1.75 +				throw new ArgumentOutOfRangeException("arrayIndex", "Insufficient space in array");
    1.76 +			}
    1.77 +			if (arrayIndex < 0)
    1.78 +			{
    1.79 +				throw new ArgumentOutOfRangeException("arrayIndex", "Value must be > 0");
    1.80 +			}
    1.81 +
    1.82 +			int i = arrayIndex;
    1.83 +			foreach (TYPE val in dictionary.Values)
    1.84 +			{
    1.85 +				array[i++] = val;
    1.86 +			}
    1.87 +		}
    1.88 +
    1.89 +		IEnumerator<TYPE> IEnumerable<TYPE>.GetEnumerator()
    1.90 +		{
    1.91 +			return dictionary.Values.GetEnumerator();
    1.92 +		}
    1.93  	}
    1.94  }