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 | |
| 24 | namespace Thrift.Collections |
| 25 | { |
Roger Meier | 1e20e9f | 2011-07-17 14:47:48 +0000 | [diff] [blame^] | 26 | [Serializable] |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 27 | public class THashSet<T> : ICollection<T> |
| 28 | { |
| 29 | #if NET_2_0 |
| 30 | TDictSet<T> set = new TDictSet<T>(); |
| 31 | #else |
| 32 | HashSet<T> set = new HashSet<T>(); |
| 33 | #endif |
| 34 | public int Count |
| 35 | { |
| 36 | get { return set.Count; } |
| 37 | } |
| 38 | |
| 39 | public bool IsReadOnly |
| 40 | { |
David Reiss | d90063f | 2009-04-27 19:25:23 +0000 | [diff] [blame] | 41 | get { return false; } |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public void Add(T item) |
| 45 | { |
| 46 | set.Add(item); |
| 47 | } |
| 48 | |
| 49 | public void Clear() |
| 50 | { |
| 51 | set.Clear(); |
| 52 | } |
| 53 | |
| 54 | public bool Contains(T item) |
| 55 | { |
| 56 | return set.Contains(item); |
| 57 | } |
| 58 | |
| 59 | public void CopyTo(T[] array, int arrayIndex) |
| 60 | { |
| 61 | set.CopyTo(array, arrayIndex); |
| 62 | } |
| 63 | |
| 64 | public IEnumerator GetEnumerator() |
| 65 | { |
| 66 | return set.GetEnumerator(); |
| 67 | } |
| 68 | |
| 69 | IEnumerator<T> IEnumerable<T>.GetEnumerator() |
| 70 | { |
| 71 | return ((IEnumerable<T>)set).GetEnumerator(); |
| 72 | } |
| 73 | |
| 74 | public bool Remove(T item) |
| 75 | { |
| 76 | return set.Remove(item); |
| 77 | } |
| 78 | |
| 79 | #if NET_2_0 |
| 80 | private class TDictSet<V> : ICollection<V> |
| 81 | { |
| 82 | Dictionary<V, TDictSet<V>> dict = new Dictionary<V, TDictSet<V>>(); |
| 83 | |
| 84 | public int Count |
| 85 | { |
| 86 | get { return dict.Count; } |
| 87 | } |
| 88 | |
| 89 | public bool IsReadOnly |
| 90 | { |
| 91 | get { return false; } |
| 92 | } |
| 93 | |
| 94 | public IEnumerator GetEnumerator() |
| 95 | { |
| 96 | return ((IEnumerable)dict.Keys).GetEnumerator(); |
| 97 | } |
| 98 | |
| 99 | IEnumerator<V> IEnumerable<V>.GetEnumerator() |
| 100 | { |
| 101 | return dict.Keys.GetEnumerator(); |
| 102 | } |
| 103 | |
| 104 | public bool Add(V item) |
| 105 | { |
| 106 | if (!dict.ContainsKey(item)) |
| 107 | { |
| 108 | dict[item] = this; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | void ICollection<V>.Add(V item) |
| 116 | { |
| 117 | Add(item); |
| 118 | } |
| 119 | |
| 120 | public void Clear() |
| 121 | { |
| 122 | dict.Clear(); |
| 123 | } |
| 124 | |
| 125 | public bool Contains(V item) |
| 126 | { |
| 127 | return dict.ContainsKey(item); |
| 128 | } |
| 129 | |
| 130 | public void CopyTo(V[] array, int arrayIndex) |
| 131 | { |
| 132 | dict.Keys.CopyTo(array, arrayIndex); |
| 133 | } |
| 134 | |
| 135 | public bool Remove(V item) |
| 136 | { |
| 137 | return dict.Remove(item); |
| 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | } |