blob: e29271a935d58391f62ae4a725627185a8ae21fd [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
Jens Geyerd5436f52014-10-03 19:50:38 +020033 [Serializable]
Roger Meier284a9b52011-12-08 13:39:56 +000034#endif
Jens Geyerd5436f52014-10-03 19:50:38 +020035 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
Jens Geyerd5436f52014-10-03 19:50:38 +020043 HashSet<T> set = new HashSet<T>();
David Reissd831a212009-02-13 03:09:52 +000044#endif
Jens Geyerd5436f52014-10-03 19:50:38 +020045 public int Count
46 {
47 get { return set.Count; }
48 }
David Reissd831a212009-02-13 03:09:52 +000049
Jens Geyerd5436f52014-10-03 19:50:38 +020050 public bool IsReadOnly
51 {
52 get { return false; }
53 }
David Reissd831a212009-02-13 03:09:52 +000054
Jens Geyerd5436f52014-10-03 19:50:38 +020055 public void Add(T item)
56 {
57 set.Add(item);
58 }
David Reissd831a212009-02-13 03:09:52 +000059
Jens Geyerd5436f52014-10-03 19:50:38 +020060 public void Clear()
61 {
62 set.Clear();
63 }
David Reissd831a212009-02-13 03:09:52 +000064
Jens Geyerd5436f52014-10-03 19:50:38 +020065 public bool Contains(T item)
66 {
67 return set.Contains(item);
68 }
David Reissd831a212009-02-13 03:09:52 +000069
Jens Geyerd5436f52014-10-03 19:50:38 +020070 public void CopyTo(T[] array, int arrayIndex)
71 {
72 set.CopyTo(array, arrayIndex);
73 }
David Reissd831a212009-02-13 03:09:52 +000074
Jens Geyerd5436f52014-10-03 19:50:38 +020075 public IEnumerator GetEnumerator()
76 {
77 return set.GetEnumerator();
78 }
David Reissd831a212009-02-13 03:09:52 +000079
Jens Geyerd5436f52014-10-03 19:50:38 +020080 IEnumerator<T> IEnumerable<T>.GetEnumerator()
81 {
82 return ((IEnumerable<T>)set).GetEnumerator();
83 }
David Reissd831a212009-02-13 03:09:52 +000084
Jens Geyerd5436f52014-10-03 19:50:38 +020085 public bool Remove(T item)
86 {
87 return set.Remove(item);
88 }
David Reissd831a212009-02-13 03:09:52 +000089
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>
Jens Geyerd5436f52014-10-03 19:50:38 +020095 {
Roger Meier3d37fba2012-09-19 19:30:36 +000096#if SILVERLIGHT
97 [DataMember]
98#endif
Jens Geyerd5436f52014-10-03 19:50:38 +020099 Dictionary<V, TDictSet<V>> dict = new Dictionary<V, TDictSet<V>>();
David Reissd831a212009-02-13 03:09:52 +0000100
Jens Geyerd5436f52014-10-03 19:50:38 +0200101 public int Count
102 {
103 get { return dict.Count; }
104 }
David Reissd831a212009-02-13 03:09:52 +0000105
Jens Geyerd5436f52014-10-03 19:50:38 +0200106 public bool IsReadOnly
107 {
108 get { return false; }
109 }
David Reissd831a212009-02-13 03:09:52 +0000110
Jens Geyerd5436f52014-10-03 19:50:38 +0200111 public IEnumerator GetEnumerator()
112 {
113 return ((IEnumerable)dict.Keys).GetEnumerator();
114 }
David Reissd831a212009-02-13 03:09:52 +0000115
Jens Geyerd5436f52014-10-03 19:50:38 +0200116 IEnumerator<V> IEnumerable<V>.GetEnumerator()
117 {
118 return dict.Keys.GetEnumerator();
119 }
David Reissd831a212009-02-13 03:09:52 +0000120
Jens Geyerd5436f52014-10-03 19:50:38 +0200121 public bool Add(V item)
122 {
123 if (!dict.ContainsKey(item))
124 {
125 dict[item] = this;
126 return true;
127 }
David Reissd831a212009-02-13 03:09:52 +0000128
Jens Geyerd5436f52014-10-03 19:50:38 +0200129 return false;
130 }
David Reissd831a212009-02-13 03:09:52 +0000131
Jens Geyerd5436f52014-10-03 19:50:38 +0200132 void ICollection<V>.Add(V item)
133 {
134 Add(item);
135 }
David Reissd831a212009-02-13 03:09:52 +0000136
Jens Geyerd5436f52014-10-03 19:50:38 +0200137 public void Clear()
138 {
139 dict.Clear();
140 }
David Reissd831a212009-02-13 03:09:52 +0000141
Jens Geyerd5436f52014-10-03 19:50:38 +0200142 public bool Contains(V item)
143 {
144 return dict.ContainsKey(item);
145 }
David Reissd831a212009-02-13 03:09:52 +0000146
Jens Geyerd5436f52014-10-03 19:50:38 +0200147 public void CopyTo(V[] array, int arrayIndex)
148 {
149 dict.Keys.CopyTo(array, arrayIndex);
150 }
David Reissd831a212009-02-13 03:09:52 +0000151
Jens Geyerd5436f52014-10-03 19:50:38 +0200152 public bool Remove(V item)
153 {
154 return dict.Remove(item);
155 }
156 }
David Reissd831a212009-02-13 03:09:52 +0000157#endif
Jens Geyerd5436f52014-10-03 19:50:38 +0200158 }
David Reissd831a212009-02-13 03:09:52 +0000159
160}