blob: b71a8d235c3fe2ee0cfe9120562f76849edbaceb [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
Roger Meier3d37fba2012-09-19 19:30:36 +000024#if SILVERLIGHT
25using System.Runtime.Serialization;
26#endif
27
David Reissd831a212009-02-13 03:09:52 +000028namespace Thrift.Collections
29{
Roger Meier3d37fba2012-09-19 19:30:36 +000030#if SILVERLIGHT
31 [DataContract]
32#else
33 [Serializable]
Roger Meier284a9b52011-12-08 13:39:56 +000034#endif
David Reissd831a212009-02-13 03:09:52 +000035 public class THashSet<T> : ICollection<T>
36 {
Roger Meier284a9b52011-12-08 13:39:56 +000037#if NET_2_0 || SILVERLIGHT
Roger Meier3d37fba2012-09-19 19:30:36 +000038#if SILVERLIGHT
39 [DataMember]
40#endif
Roger Meier284a9b52011-12-08 13:39:56 +000041 TDictSet<T> set = new TDictSet<T>();
David Reissd831a212009-02-13 03:09:52 +000042#else
43 HashSet<T> set = new HashSet<T>();
44#endif
45 public int Count
46 {
47 get { return set.Count; }
48 }
49
50 public bool IsReadOnly
51 {
David Reissd90063f2009-04-27 19:25:23 +000052 get { return false; }
David Reissd831a212009-02-13 03:09:52 +000053 }
54
55 public void Add(T item)
56 {
57 set.Add(item);
58 }
59
60 public void Clear()
61 {
62 set.Clear();
63 }
64
65 public bool Contains(T item)
66 {
67 return set.Contains(item);
68 }
69
70 public void CopyTo(T[] array, int arrayIndex)
71 {
72 set.CopyTo(array, arrayIndex);
73 }
74
75 public IEnumerator GetEnumerator()
76 {
77 return set.GetEnumerator();
78 }
79
80 IEnumerator<T> IEnumerable<T>.GetEnumerator()
81 {
82 return ((IEnumerable<T>)set).GetEnumerator();
83 }
84
85 public bool Remove(T item)
86 {
87 return set.Remove(item);
88 }
89
Roger Meier284a9b52011-12-08 13:39:56 +000090#if NET_2_0 || SILVERLIGHT
Roger Meier3d37fba2012-09-19 19:30:36 +000091#if SILVERLIGHT
92 [DataContract]
93#endif
Roger Meier284a9b52011-12-08 13:39:56 +000094 private class TDictSet<V> : ICollection<V>
David Reissd831a212009-02-13 03:09:52 +000095 {
Roger Meier3d37fba2012-09-19 19:30:36 +000096#if SILVERLIGHT
97 [DataMember]
98#endif
David Reissd831a212009-02-13 03:09:52 +000099 Dictionary<V, TDictSet<V>> dict = new Dictionary<V, TDictSet<V>>();
100
101 public int Count
102 {
103 get { return dict.Count; }
104 }
105
106 public bool IsReadOnly
107 {
108 get { return false; }
109 }
110
111 public IEnumerator GetEnumerator()
112 {
113 return ((IEnumerable)dict.Keys).GetEnumerator();
114 }
115
116 IEnumerator<V> IEnumerable<V>.GetEnumerator()
117 {
118 return dict.Keys.GetEnumerator();
119 }
120
121 public bool Add(V item)
122 {
123 if (!dict.ContainsKey(item))
124 {
125 dict[item] = this;
126 return true;
127 }
128
129 return false;
130 }
131
132 void ICollection<V>.Add(V item)
133 {
134 Add(item);
135 }
136
137 public void Clear()
138 {
139 dict.Clear();
140 }
141
142 public bool Contains(V item)
143 {
144 return dict.ContainsKey(item);
145 }
146
147 public void CopyTo(V[] array, int arrayIndex)
148 {
149 dict.Keys.CopyTo(array, arrayIndex);
150 }
151
152 public bool Remove(V item)
153 {
154 return dict.Remove(item);
155 }
156 }
157#endif
158 }
159
160}