annotate Collections/SimpleSet.cs @ 121:9131bc46903e default tip

* Add NamedStream wrapper class to support warfoundry:#419
author IBBoard <dev@ibboard.co.uk>
date Wed, 28 Nov 2012 20:21:40 +0000
parents 3262c0ad2d3d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 3
diff changeset
1 // This file (SimpleSet.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 3
diff changeset
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.
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 using System;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System.Collections;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.Collections
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
10 public class SimpleSet<TYPE> : ICollection<TYPE>
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 private Dictionary<int, TYPE> dictionary;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 public SimpleSet()
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 dictionary = new Dictionary<int,TYPE>();
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
19 public void Add(TYPE val)
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
112
3262c0ad2d3d * Fix hasty SimpleSet change
IBBoard <dev@ibboard.co.uk>
parents: 111
diff changeset
21 dictionary[val.GetHashCode()] = val;
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 public bool AddRange(ICollection<TYPE> vals)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 bool added = false;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 foreach (TYPE val in vals)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 Add(val);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 added = true;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 return added;
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 public bool Remove(TYPE val)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 return dictionary.Remove(val.GetHashCode());
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
42 public bool Contains(TYPE item)
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 return dictionary.ContainsKey(item.GetHashCode());
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
47 public void Clear()
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 dictionary.Clear();
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
52 public int Count
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
53 {
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 get { return dictionary.Count; }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
57 public bool IsReadOnly
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
58 {
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 get { return false; }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 public IEnumerator GetEnumerator()
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 {
3
3dab3db95e4a Fix SimpleSet so that the enumerator works over the values in the set
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
64 return dictionary.Values.GetEnumerator();
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66
111
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
67 public void CopyTo(TYPE[] array, int arrayIndex)
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
68 {
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
69 if (arrayIndex + dictionary.Count > array.Length)
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
70 {
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
71 throw new ArgumentOutOfRangeException("arrayIndex", "Insufficient space in array");
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
72 }
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
73 if (arrayIndex < 0)
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
74 {
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
75 throw new ArgumentOutOfRangeException("arrayIndex", "Value must be > 0");
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
76 }
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
77
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
78 int i = arrayIndex;
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
79 foreach (TYPE val in dictionary.Values)
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
80 {
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
81 array[i++] = val;
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
82 }
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
83 }
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
84
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
85 IEnumerator<TYPE> IEnumerable<TYPE>.GetEnumerator()
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
86 {
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
87 return dictionary.Values.GetEnumerator();
9832caa89140 * Make the SimpleSet a proper collection
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
88 }
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 }