blob: e2fc8b5265324feb430e730e70e309f5b846b208 [file] [log] [blame]
David Reissd831a212009-02-13 03:09:52 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
Kevin Clarkab4460d2009-03-20 02:28:41 +000010 * http://www.apache.org/licenses/LICENSE-2.0
David Reissd831a212009-02-13 03:09:52 +000011 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20using System;
21using System.Collections;
22using System.Collections.Generic;
23
24namespace Thrift.Collections
25{
Roger Meier284a9b52011-12-08 13:39:56 +000026#if !SILVERLIGHT
Roger Meier1e20e9f2011-07-17 14:47:48 +000027 [Serializable]
Roger Meier284a9b52011-12-08 13:39:56 +000028#endif
David Reissd831a212009-02-13 03:09:52 +000029 public class THashSet<T> : ICollection<T>
30 {
Roger Meier284a9b52011-12-08 13:39:56 +000031#if NET_2_0 || SILVERLIGHT
32 TDictSet<T> set = new TDictSet<T>();
David Reissd831a212009-02-13 03:09:52 +000033#else
34 HashSet<T> set = new HashSet<T>();
35#endif
36 public int Count
37 {
38 get { return set.Count; }
39 }
40
41 public bool IsReadOnly
42 {
David Reissd90063f2009-04-27 19:25:23 +000043 get { return false; }
David Reissd831a212009-02-13 03:09:52 +000044 }
45
46 public void Add(T item)
47 {
48 set.Add(item);
49 }
50
51 public void Clear()
52 {
53 set.Clear();
54 }
55
56 public bool Contains(T item)
57 {
58 return set.Contains(item);
59 }
60
61 public void CopyTo(T[] array, int arrayIndex)
62 {
63 set.CopyTo(array, arrayIndex);
64 }
65
66 public IEnumerator GetEnumerator()
67 {
68 return set.GetEnumerator();
69 }
70
71 IEnumerator<T> IEnumerable<T>.GetEnumerator()
72 {
73 return ((IEnumerable<T>)set).GetEnumerator();
74 }
75
76 public bool Remove(T item)
77 {
78 return set.Remove(item);
79 }
80
Roger Meier284a9b52011-12-08 13:39:56 +000081#if NET_2_0 || SILVERLIGHT
82 private class TDictSet<V> : ICollection<V>
David Reissd831a212009-02-13 03:09:52 +000083 {
84 Dictionary<V, TDictSet<V>> dict = new Dictionary<V, TDictSet<V>>();
85
86 public int Count
87 {
88 get { return dict.Count; }
89 }
90
91 public bool IsReadOnly
92 {
93 get { return false; }
94 }
95
96 public IEnumerator GetEnumerator()
97 {
98 return ((IEnumerable)dict.Keys).GetEnumerator();
99 }
100
101 IEnumerator<V> IEnumerable<V>.GetEnumerator()
102 {
103 return dict.Keys.GetEnumerator();
104 }
105
106 public bool Add(V item)
107 {
108 if (!dict.ContainsKey(item))
109 {
110 dict[item] = this;
111 return true;
112 }
113
114 return false;
115 }
116
117 void ICollection<V>.Add(V item)
118 {
119 Add(item);
120 }
121
122 public void Clear()
123 {
124 dict.Clear();
125 }
126
127 public bool Contains(V item)
128 {
129 return dict.ContainsKey(item);
130 }
131
132 public void CopyTo(V[] array, int arrayIndex)
133 {
134 dict.Keys.CopyTo(array, arrayIndex);
135 }
136
137 public bool Remove(V item)
138 {
139 return dict.Remove(item);
140 }
141 }
142#endif
143 }
144
145}