view Collections/SimpleSet.cs @ 112:3262c0ad2d3d

* Fix hasty SimpleSet change
author IBBoard <dev@ibboard.co.uk>
date Tue, 31 Jan 2012 20:46:10 +0000
parents 9832caa89140
children
line source
1 // This file (SimpleSet.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
2 //
3 // 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.
4 using System;
5 using System.Collections;
6 using System.Collections.Generic;
8 namespace IBBoard.Collections
9 {
10 public class SimpleSet<TYPE> : ICollection<TYPE>
11 {
12 private Dictionary<int, TYPE> dictionary;
14 public SimpleSet()
15 {
16 dictionary = new Dictionary<int,TYPE>();
17 }
19 public void Add(TYPE val)
20 {
21 dictionary[val.GetHashCode()] = val;
22 }
24 public bool AddRange(ICollection<TYPE> vals)
25 {
26 bool added = false;
28 foreach (TYPE val in vals)
29 {
30 Add(val);
31 added = true;
32 }
34 return added;
35 }
37 public bool Remove(TYPE val)
38 {
39 return dictionary.Remove(val.GetHashCode());
40 }
42 public bool Contains(TYPE item)
43 {
44 return dictionary.ContainsKey(item.GetHashCode());
45 }
47 public void Clear()
48 {
49 dictionary.Clear();
50 }
52 public int Count
53 {
54 get { return dictionary.Count; }
55 }
57 public bool IsReadOnly
58 {
59 get { return false; }
60 }
62 public IEnumerator GetEnumerator()
63 {
64 return dictionary.Values.GetEnumerator();
65 }
67 public void CopyTo(TYPE[] array, int arrayIndex)
68 {
69 if (arrayIndex + dictionary.Count > array.Length)
70 {
71 throw new ArgumentOutOfRangeException("arrayIndex", "Insufficient space in array");
72 }
73 if (arrayIndex < 0)
74 {
75 throw new ArgumentOutOfRangeException("arrayIndex", "Value must be > 0");
76 }
78 int i = arrayIndex;
79 foreach (TYPE val in dictionary.Values)
80 {
81 array[i++] = val;
82 }
83 }
85 IEnumerator<TYPE> IEnumerable<TYPE>.GetEnumerator()
86 {
87 return dictionary.Values.GetEnumerator();
88 }
89 }
90 }