1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
//---------------------------------------------------------------------
// <copyright file="Set.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Linq;
namespace System.Data.Common.Utils {
// An interface for a set abstraction
internal class Set<TElement> : InternalBase, IEnumerable<TElement>
{
#region Fields
/// <summary>
/// Instance of set value comparer.
/// </summary>
internal static readonly IEqualityComparer<Set<TElement>> ValueComparer =
new SetValueComparer();
/// <summary>
/// Instance of empty set with default comparer.
/// </summary>
internal static readonly Set<TElement> Empty = new Set<TElement>().MakeReadOnly();
private readonly HashSet<TElement> _values;
private bool _isReadOnly;
#endregion
#region Constructors
/// <summary>
/// Initialize set with the same values and comparer as other set.
/// </summary>
internal Set(Set<TElement> other)
: this(other._values, other.Comparer)
{
}
/// <summary>
/// Initialize empty set with default comparer.
/// </summary>
internal Set()
: this(null, null)
{
}
/// <summary>
/// Initialize a set with the given elements and using default comparer.
/// </summary>
internal Set(IEnumerable<TElement> elements)
: this(elements, null)
{
}
/// <summary>
/// Initializes an empty set with the given comparer.
/// </summary>
internal Set(IEqualityComparer<TElement> comparer)
: this(null, comparer)
{
}
/// <summary>
/// Initialize a set with the given elements and comparer.
/// </summary>
internal Set(IEnumerable<TElement> elements, IEqualityComparer<TElement> comparer)
{
_values = new HashSet<TElement>(
elements ?? Enumerable.Empty<TElement>(),
comparer ?? EqualityComparer<TElement>.Default);
}
#endregion
#region Properties
/// <summary>
/// Gets the number of elements in this set.
/// </summary>
internal int Count
{
get
{
return _values.Count;
}
}
/// <summary>
/// Gets the comparer used to determine equality and hash codes for elements of the set.
/// </summary>
internal IEqualityComparer<TElement> Comparer
{
get
{
return _values.Comparer;
}
}
#endregion
#region Methods
/// <summary>
/// Determines whether the given element exists in the set.
/// </summary>
internal bool Contains(TElement element)
{
return _values.Contains(element);
}
/// <summary>
/// Requires: !IsReadOnly
/// Adds given element to the set. If the set already contains
/// the element, does nothing.
/// </summary>
internal void Add(TElement element)
{
AssertReadWrite();
_values.Add(element);
}
/// <summary>
/// Requires: !IsReadOnly
/// Adds given elements to the set. If the set already contains
/// one of the elements, does nothing.
/// </summary>
internal void AddRange(IEnumerable<TElement> elements)
{
AssertReadWrite();
foreach (TElement element in elements)
{
Add(element);
}
}
/// <summary>
/// Requires: !IsReadOnly
/// Removes given element from the set. If the set does not contain
/// the element, does nothing.
/// </summary>
internal void Remove(TElement element)
{
AssertReadWrite();
_values.Remove(element);
}
/// <summary>
/// Requires: !IsReadOnly
/// Removes all elements from the set.
/// </summary>
internal void Clear()
{
AssertReadWrite();
_values.Clear();
}
/// <summary>
/// Returns an array containing all elements of the set. Order is arbitrary.
/// </summary>
internal TElement[] ToArray()
{
return _values.ToArray();
}
/// <summary>
/// Requires: other set must not be null and must have the same comparer.
/// Returns true if this set contains the same elements as the other set.
/// </summary>
internal bool SetEquals(Set<TElement> other)
{
AssertSetCompatible(other);
return _values.Count == other._values.Count
&& _values.IsSubsetOf(other._values);
}
/// <summary>
/// Requires: other set must not be null and must have the same comparer.
/// Returns true if all elements in this set are contained in the other set.
/// </summary>
internal bool IsSubsetOf(Set<TElement> other)
{
AssertSetCompatible(other);
return _values.IsSubsetOf(other._values);
}
/// <summary>
/// Requires: other set must not be null and must have the same comparer.
/// Returns true if this set and other set have some elements in common.
/// </summary>
internal bool Overlaps(Set<TElement> other)
{
AssertSetCompatible(other);
return _values.Overlaps(other._values);
}
/// <summary>
/// Requires: !IsReadOnly
/// Requires: other collection must not be null.
/// Subtracts other set from this set, leaving the result in this.
/// </summary>
internal void Subtract(IEnumerable<TElement> other)
{
AssertReadWrite();
_values.ExceptWith(other);
}
/// <summary>
/// Requires: other collection must not be null.
/// Subtracts other set from this set, returning result.
/// </summary>
internal Set<TElement> Difference(IEnumerable<TElement> other)
{
Set<TElement> copy = new Set<TElement>(this);
copy.Subtract(other);
return copy;
}
/// <summary>
/// Requires: !IsReadOnly
/// Requires: other collection must not be null.
/// Unions other set with this set, leaving the result in this set.
/// </summary>
internal void Unite(IEnumerable<TElement> other)
{
AssertReadWrite();
_values.UnionWith(other);
}
/// <summary>
/// Requires: other collection must not be null.
/// Unions other set with this set, returning the result.
/// </summary>
internal Set<TElement> Union(IEnumerable<TElement> other)
{
Set<TElement> copy = new Set<TElement>(this);
copy.Unite(other);
return copy;
}
/// <summary>
/// Requires: !IsReadOnly
/// Requires: other set must not be null and must have the same comparer.
/// Intersects this set and other set, leaving the result in this set.
/// </summary>
internal void Intersect(Set<TElement> other)
{
AssertReadWrite();
AssertSetCompatible(other);
_values.IntersectWith(other._values);
}
/// <summary>
/// Returns a readonly version of this set.
/// </summary>
internal Set<TElement> AsReadOnly()
{
if (_isReadOnly)
{
// once it's readonly, it's always readonly
return this;
}
Set<TElement> copy = new Set<TElement>(this);
copy._isReadOnly = true;
return copy;
}
/// <summary>
/// Makes this set readonly and returns this set.
/// </summary>
internal Set<TElement> MakeReadOnly()
{
_isReadOnly = true;
return this;
}
/// <summary>
/// Returns aggregate hash code of all elements in this set.
/// </summary>
internal int GetElementsHashCode()
{
int hashCode = 0;
foreach (TElement element in this)
{
hashCode ^= Comparer.GetHashCode(element);
}
return hashCode;
}
/// <summary>
/// Returns typed enumerator over elements of the set.
/// Uses HashSet<TElement>.Enumerator to avoid boxing struct.
/// </summary>
public HashSet<TElement>.Enumerator GetEnumerator()
{
return _values.GetEnumerator();
}
[Conditional("DEBUG")]
private void AssertReadWrite()
{
Debug.Assert(!_isReadOnly, "attempting to modify readonly collection");
}
[Conditional("DEBUG")]
private void AssertSetCompatible(Set<TElement> other)
{
Debug.Assert(other != null, "other set null");
Debug.Assert(other.Comparer.GetType().Equals(this.Comparer.GetType()));
}
#endregion
#region IEnumerable<TElement> Members
public class Enumerator : IEnumerator<TElement>
{
private Dictionary<TElement, bool>.KeyCollection.Enumerator keys;
internal Enumerator(Dictionary<TElement, bool>.KeyCollection.Enumerator keys)
{
this.keys = keys;
}
public TElement Current { get { return keys.Current; } }
public void Dispose() { keys.Dispose(); }
object IEnumerator.Current { get { return ((IEnumerator)keys).Current; } }
public bool MoveNext() { return keys.MoveNext(); }
void System.Collections.IEnumerator.Reset() { ((System.Collections.IEnumerator)keys).Reset(); }
}
IEnumerator<TElement> IEnumerable<TElement>.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an untyped enumeration of elements in the set.
/// </summary>
/// <returns>Enumeration of set members.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region InternalBase
internal override void ToCompactString(StringBuilder builder)
{
StringUtil.ToCommaSeparatedStringSorted(builder, this);
}
#endregion
#region Nested types
private class SetValueComparer : IEqualityComparer<Set<TElement>>
{
bool IEqualityComparer<Set<TElement>>.Equals(Set<TElement> x, Set<TElement> y)
{
Debug.Assert(null != x && null != y, "comparer must be used only in context of Dictionary/HashSet");
return x.SetEquals(y);
}
int IEqualityComparer<Set<TElement>>.GetHashCode(Set<TElement> obj)
{
Debug.Assert(null != obj, "comparer must be used only in context of Dictionary/HashSet");
return obj.GetElementsHashCode();
}
}
#endregion
}
}
|