# HG changeset patch # User IBBoard # Date 1327769568 0 # Node ID 9832caa8914095438c7c84172d3ebacb6a7b83a5 # Parent a5714f82073d01a699f1ae0a580bd1fe9bf92fc3 * Make the SimpleSet a proper collection diff -r a5714f82073d -r 9832caa89140 Collections/SimpleSet.cs --- 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 : IEnumerable + public class SimpleSet : ICollection { private Dictionary dictionary; @@ -17,17 +16,9 @@ dictionary = new Dictionary(); } - 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 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 IEnumerable.GetEnumerator() + { + return dictionary.Values.GetEnumerator(); + } } }