view Collections/SimpleSet.cs @ 43:2baf3f949cfb

* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe) * Automated cleanups no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Oct 2009 09:47:16 +0000
parents 0352fa33ee8f
children 9832caa89140
line wrap: on
line source

// 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
	{		
		private Dictionary<int, TYPE> dictionary;
		
		public SimpleSet()
		{
			dictionary = new Dictionary<int,TYPE>();
		}
		
		public bool Add(TYPE val)
		{
			bool added = false;
			
			if (!dictionary.ContainsKey(val.GetHashCode()))
			{
				dictionary.Add(val.GetHashCode(), val);
				added = true;
			}
			
			return added;
		}
		
		public bool AddRange(ICollection<TYPE> vals)
		{
			bool added = false;
			
			foreach (TYPE val in vals)
			{
				Add(val);
				added = true;
			}
			
			return added;
		}
		
		public bool Remove(TYPE val)
		{
			return dictionary.Remove(val.GetHashCode());
		}
		
		public bool Contains (TYPE item)
		{
			return dictionary.ContainsKey(item.GetHashCode());
		}

		public void Clear ()
		{
			dictionary.Clear();
		}
		
		public int Count {
			get { return dictionary.Count; }
		}
		
		public bool IsReadOnly {
			get { return false; }
		}
		
		public IEnumerator GetEnumerator()
		{
			return dictionary.Values.GetEnumerator();
		}

	}
}