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