comparison 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
comparison
equal deleted inserted replaced
110:a5714f82073d 111:9832caa89140
1 // This file (SimpleSet.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. 1 // This file (SimpleSet.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
2 // 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. 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
5 using System; 4 using System;
6 using System.Collections; 5 using System.Collections;
7 using System.Collections.Generic; 6 using System.Collections.Generic;
8 7
9 namespace IBBoard.Collections 8 namespace IBBoard.Collections
10 { 9 {
11 public class SimpleSet<TYPE> : IEnumerable 10 public class SimpleSet<TYPE> : ICollection<TYPE>
12 { 11 {
13 private Dictionary<int, TYPE> dictionary; 12 private Dictionary<int, TYPE> dictionary;
14 13
15 public SimpleSet() 14 public SimpleSet()
16 { 15 {
17 dictionary = new Dictionary<int,TYPE>(); 16 dictionary = new Dictionary<int,TYPE>();
18 } 17 }
19 18
20 public bool Add(TYPE val) 19 public void Add(TYPE val)
21 { 20 {
22 bool added = false; 21 dictionary.Add(val.GetHashCode(), val);
23
24 if (!dictionary.ContainsKey(val.GetHashCode()))
25 {
26 dictionary.Add(val.GetHashCode(), val);
27 added = true;
28 }
29
30 return added;
31 } 22 }
32 23
33 public bool AddRange(ICollection<TYPE> vals) 24 public bool AddRange(ICollection<TYPE> vals)
34 { 25 {
35 bool added = false; 26 bool added = false;
46 public bool Remove(TYPE val) 37 public bool Remove(TYPE val)
47 { 38 {
48 return dictionary.Remove(val.GetHashCode()); 39 return dictionary.Remove(val.GetHashCode());
49 } 40 }
50 41
51 public bool Contains (TYPE item) 42 public bool Contains(TYPE item)
52 { 43 {
53 return dictionary.ContainsKey(item.GetHashCode()); 44 return dictionary.ContainsKey(item.GetHashCode());
54 } 45 }
55 46
56 public void Clear () 47 public void Clear()
57 { 48 {
58 dictionary.Clear(); 49 dictionary.Clear();
59 } 50 }
60 51
61 public int Count { 52 public int Count
53 {
62 get { return dictionary.Count; } 54 get { return dictionary.Count; }
63 } 55 }
64 56
65 public bool IsReadOnly { 57 public bool IsReadOnly
58 {
66 get { return false; } 59 get { return false; }
67 } 60 }
68 61
69 public IEnumerator GetEnumerator() 62 public IEnumerator GetEnumerator()
70 { 63 {
71 return dictionary.Values.GetEnumerator(); 64 return dictionary.Values.GetEnumerator();
72 } 65 }
73 66
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 }
77
78 int i = arrayIndex;
79 foreach (TYPE val in dictionary.Values)
80 {
81 array[i++] = val;
82 }
83 }
84
85 IEnumerator<TYPE> IEnumerable<TYPE>.GetEnumerator()
86 {
87 return dictionary.Values.GetEnumerator();
88 }
74 } 89 }
75 } 90 }