David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 1 | /** |
| 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 Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 11 | * |
| 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 | |
| 20 | using System; |
| 21 | using System.Collections; |
| 22 | using System.Collections.Generic; |
| 23 | |
Roger Meier | 3d37fba | 2012-09-19 19:30:36 +0000 | [diff] [blame] | 24 | #if SILVERLIGHT |
| 25 | using System.Runtime.Serialization; |
| 26 | #endif |
| 27 | |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 28 | namespace Thrift.Collections |
| 29 | { |
Roger Meier | 3d37fba | 2012-09-19 19:30:36 +0000 | [diff] [blame] | 30 | #if SILVERLIGHT |
| 31 | [DataContract] |
| 32 | #else |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 33 | [Serializable] |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 34 | #endif |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 35 | public class THashSet<T> : ICollection<T> |
| 36 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 37 | #if NET_2_0 || SILVERLIGHT |
Roger Meier | 3d37fba | 2012-09-19 19:30:36 +0000 | [diff] [blame] | 38 | #if SILVERLIGHT |
| 39 | [DataMember] |
| 40 | #endif |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 41 | TDictSet<T> set = new TDictSet<T>(); |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 42 | #else |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 43 | HashSet<T> set = new HashSet<T>(); |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 44 | #endif |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 45 | public int Count |
| 46 | { |
| 47 | get { return set.Count; } |
| 48 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 49 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 50 | public bool IsReadOnly |
| 51 | { |
| 52 | get { return false; } |
| 53 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 54 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 55 | public void Add(T item) |
| 56 | { |
| 57 | set.Add(item); |
| 58 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 59 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 60 | public void Clear() |
| 61 | { |
| 62 | set.Clear(); |
| 63 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 64 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 65 | public bool Contains(T item) |
| 66 | { |
| 67 | return set.Contains(item); |
| 68 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 69 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 70 | public void CopyTo(T[] array, int arrayIndex) |
| 71 | { |
| 72 | set.CopyTo(array, arrayIndex); |
| 73 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 74 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 75 | public IEnumerator GetEnumerator() |
| 76 | { |
| 77 | return set.GetEnumerator(); |
| 78 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 79 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 80 | IEnumerator<T> IEnumerable<T>.GetEnumerator() |
| 81 | { |
| 82 | return ((IEnumerable<T>)set).GetEnumerator(); |
| 83 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 84 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 85 | public bool Remove(T item) |
| 86 | { |
| 87 | return set.Remove(item); |
| 88 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 89 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 90 | #if NET_2_0 || SILVERLIGHT |
Roger Meier | 3d37fba | 2012-09-19 19:30:36 +0000 | [diff] [blame] | 91 | #if SILVERLIGHT |
| 92 | [DataContract] |
| 93 | #endif |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 94 | private class TDictSet<V> : ICollection<V> |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 95 | { |
Roger Meier | 3d37fba | 2012-09-19 19:30:36 +0000 | [diff] [blame] | 96 | #if SILVERLIGHT |
| 97 | [DataMember] |
| 98 | #endif |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 99 | Dictionary<V, TDictSet<V>> dict = new Dictionary<V, TDictSet<V>>(); |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 100 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 101 | public int Count |
| 102 | { |
| 103 | get { return dict.Count; } |
| 104 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 105 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 106 | public bool IsReadOnly |
| 107 | { |
| 108 | get { return false; } |
| 109 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 110 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 111 | public IEnumerator GetEnumerator() |
| 112 | { |
| 113 | return ((IEnumerable)dict.Keys).GetEnumerator(); |
| 114 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 115 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 116 | IEnumerator<V> IEnumerable<V>.GetEnumerator() |
| 117 | { |
| 118 | return dict.Keys.GetEnumerator(); |
| 119 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 120 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 121 | public bool Add(V item) |
| 122 | { |
| 123 | if (!dict.ContainsKey(item)) |
| 124 | { |
| 125 | dict[item] = this; |
| 126 | return true; |
| 127 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 128 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 129 | return false; |
| 130 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 131 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 132 | void ICollection<V>.Add(V item) |
| 133 | { |
| 134 | Add(item); |
| 135 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 136 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 137 | public void Clear() |
| 138 | { |
| 139 | dict.Clear(); |
| 140 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 141 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 142 | public bool Contains(V item) |
| 143 | { |
| 144 | return dict.ContainsKey(item); |
| 145 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 146 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 147 | public void CopyTo(V[] array, int arrayIndex) |
| 148 | { |
| 149 | dict.Keys.CopyTo(array, arrayIndex); |
| 150 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 151 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 152 | public bool Remove(V item) |
| 153 | { |
| 154 | return dict.Remove(item); |
| 155 | } |
| 156 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 157 | #endif |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 158 | } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 159 | |
| 160 | } |