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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
|
//
// HashSet.cs
//
// Authors:
// Jb Evain <jbevain@novell.com>
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
// HashSet is basically implemented as a reduction of Dictionary<K, V>
namespace System.Collections.Generic {
[Serializable, HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback {
const int INITIAL_SIZE = 10;
const float DEFAULT_LOAD_FACTOR = (90f / 100);
const int NO_SLOT = -1;
const int HASH_FLAG = -2147483648;
struct Link {
public int HashCode;
public int Next;
}
// The hash table contains indices into the "links" array
int [] table;
Link [] links;
T [] slots;
// The number of slots in "links" and "slots" that
// are in use (i.e. filled with data) or have been used and marked as
// "empty" later on.
int touched;
// The index of the first slot in the "empty slots chain".
// "Remove ()" prepends the cleared slots to the empty chain.
// "Add ()" fills the first slot in the empty slots chain with the
// added item (or increases "touched" if the chain itself is empty).
int empty_slot;
// The number of items in this set.
int count;
// The number of items the set can hold without
// resizing the hash table and the slots arrays.
int threshold;
IEqualityComparer<T> comparer;
SerializationInfo si;
// The number of changes made to this set. Used by enumerators
// to detect changes and invalidate themselves.
int generation;
public int Count {
get { return count; }
}
public HashSet ()
{
Init (INITIAL_SIZE, null);
}
public HashSet (IEqualityComparer<T> comparer)
{
Init (INITIAL_SIZE, comparer);
}
public HashSet (IEnumerable<T> collection) : this (collection, null)
{
}
public HashSet (IEnumerable<T> collection, IEqualityComparer<T> comparer)
{
if (collection == null)
throw new ArgumentNullException ("collection");
int capacity = 0;
var col = collection as ICollection<T>;
if (col != null)
capacity = col.Count;
Init (capacity, comparer);
foreach (var item in collection)
Add (item);
}
protected HashSet (SerializationInfo info, StreamingContext context)
{
si = info;
}
void Init (int capacity, IEqualityComparer<T> comparer)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException ("capacity");
this.comparer = comparer ?? EqualityComparer<T>.Default;
if (capacity == 0)
capacity = INITIAL_SIZE;
/* Modify capacity so 'capacity' elements can be added without resizing */
capacity = (int) (capacity / DEFAULT_LOAD_FACTOR) + 1;
InitArrays (capacity);
generation = 0;
}
void InitArrays (int size)
{
table = new int [size];
links = new Link [size];
empty_slot = NO_SLOT;
slots = new T [size];
touched = 0;
threshold = (int) (table.Length * DEFAULT_LOAD_FACTOR);
if (threshold == 0 && table.Length > 0)
threshold = 1;
}
bool SlotsContainsAt (int index, int hash, T item)
{
int current = table [index] - 1;
while (current != NO_SLOT) {
Link link = links [current];
if (link.HashCode == hash && ((hash == HASH_FLAG && (item == null || null == slots [current])) ? (item == null && null == slots [current]) : comparer.Equals (item, slots [current])))
return true;
current = link.Next;
}
return false;
}
public void CopyTo (T [] array)
{
CopyTo (array, 0, count);
}
public void CopyTo (T [] array, int index)
{
CopyTo (array, index, count);
}
public void CopyTo (T [] array, int index, int count)
{
if (array == null)
throw new ArgumentNullException ("array");
if (index < 0)
throw new ArgumentOutOfRangeException ("index");
if (index > array.Length)
throw new ArgumentException ("index larger than largest valid index of array");
if (array.Length - index < count)
throw new ArgumentException ("Destination array cannot hold the requested elements!");
for (int i = 0, items = 0; i < touched && items < count; i++) {
if (GetLinkHashCode (i) != 0)
array [index++] = slots [i];
}
}
void Resize ()
{
int newSize = PrimeHelper.ToPrime ((table.Length << 1) | 1);
// allocate new hash table and link slots array
var newTable = new int [newSize];
var newLinks = new Link [newSize];
for (int i = 0; i < table.Length; i++) {
int current = table [i] - 1;
while (current != NO_SLOT) {
int hashCode = newLinks [current].HashCode = GetItemHashCode (slots [current]);
int index = (hashCode & int.MaxValue) % newSize;
newLinks [current].Next = newTable [index] - 1;
newTable [index] = current + 1;
current = links [current].Next;
}
}
table = newTable;
links = newLinks;
// allocate new data slots, copy data
var newSlots = new T [newSize];
Array.Copy (slots, 0, newSlots, 0, touched);
slots = newSlots;
threshold = (int) (newSize * DEFAULT_LOAD_FACTOR);
}
int GetLinkHashCode (int index)
{
return links [index].HashCode & HASH_FLAG;
}
int GetItemHashCode (T item)
{
if (item == null)
return HASH_FLAG;
return comparer.GetHashCode (item) | HASH_FLAG;
}
public bool Add (T item)
{
int hashCode = GetItemHashCode (item);
int index = (hashCode & int.MaxValue) % table.Length;
if (SlotsContainsAt (index, hashCode, item))
return false;
if (++count > threshold) {
Resize ();
index = (hashCode & int.MaxValue) % table.Length;
}
// find an empty slot
int current = empty_slot;
if (current == NO_SLOT)
current = touched++;
else
empty_slot = links [current].Next;
// store the hash code of the added item,
// prepend the added item to its linked list,
// update the hash table
links [current].HashCode = hashCode;
links [current].Next = table [index] - 1;
table [index] = current + 1;
// store item
slots [current] = item;
generation++;
return true;
}
public IEqualityComparer<T> Comparer {
get { return comparer; }
}
public void Clear ()
{
count = 0;
Array.Clear (table, 0, table.Length);
Array.Clear (slots, 0, slots.Length);
Array.Clear (links, 0, links.Length);
// empty the "empty slots chain"
empty_slot = NO_SLOT;
touched = 0;
generation++;
}
public bool Contains (T item)
{
int hashCode = GetItemHashCode (item);
int index = (hashCode & int.MaxValue) % table.Length;
return SlotsContainsAt (index, hashCode, item);
}
public bool Remove (T item)
{
// get first item of linked list corresponding to given key
int hashCode = GetItemHashCode (item);
int index = (hashCode & int.MaxValue) % table.Length;
int current = table [index] - 1;
// if there is no linked list, return false
if (current == NO_SLOT)
return false;
// walk linked list until right slot (and its predecessor) is
// found or end is reached
int prev = NO_SLOT;
do {
Link link = links [current];
if (link.HashCode == hashCode && ((hashCode == HASH_FLAG && (item == null || null == slots [current])) ? (item == null && null == slots [current]) : comparer.Equals (slots [current], item)))
break;
prev = current;
current = link.Next;
} while (current != NO_SLOT);
// if we reached the end of the chain, return false
if (current == NO_SLOT)
return false;
count--;
// remove slot from linked list
// is slot at beginning of linked list?
if (prev == NO_SLOT)
table [index] = links [current].Next + 1;
else
links [prev].Next = links [current].Next;
// mark slot as empty and prepend it to "empty slots chain"
links [current].Next = empty_slot;
empty_slot = current;
// clear slot
links [current].HashCode = 0;
slots [current] = default (T);
generation++;
return true;
}
public int RemoveWhere (Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException ("predicate");
int counter = 0;
var copy = new T [count];
CopyTo (copy, 0);
foreach (var item in copy) {
if (predicate (item)) {
Remove (item);
counter++;
}
}
return counter;
}
public void TrimExcess ()
{
Resize ();
}
// set operations
public void IntersectWith (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
var copy = new T [count];
CopyTo (copy, 0);
foreach (var item in copy)
if (!other.Contains (item))
Remove (item);
foreach (var item in other)
if (!Contains (item))
Remove (item);
}
public void ExceptWith (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
foreach (var item in other)
Remove (item);
}
public bool Overlaps (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
foreach (var item in other)
if (Contains (item))
return true;
return false;
}
public bool SetEquals (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
if (count != other.Count ())
return false;
foreach (var item in this)
if (!other.Contains (item))
return false;
return true;
}
public void SymmetricExceptWith (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
foreach (var item in other) {
if (Contains (item))
Remove (item);
else
Add (item);
}
}
public void UnionWith (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
foreach (var item in other)
Add (item);
}
bool CheckIsSubsetOf (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
foreach (var item in this)
if (!other.Contains (item))
return false;
return true;
}
public bool IsSubsetOf (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
if (count == 0)
return true;
if (count > other.Count ())
return false;
return CheckIsSubsetOf (other);
}
public bool IsProperSubsetOf (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
if (count == 0)
return true;
if (count >= other.Count ())
return false;
return CheckIsSubsetOf (other);
}
bool CheckIsSupersetOf (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
foreach (var item in other)
if (!Contains (item))
return false;
return true;
}
public bool IsSupersetOf (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
if (count < other.Count ())
return false;
return CheckIsSupersetOf (other);
}
public bool IsProperSupersetOf (IEnumerable<T> other)
{
if (other == null)
throw new ArgumentNullException ("other");
if (count <= other.Count ())
return false;
return CheckIsSupersetOf (other);
}
[MonoTODO]
public static IEqualityComparer<HashSet<T>> CreateSetComparer ()
{
throw new NotImplementedException ();
}
[MonoTODO]
[SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException ();
}
[MonoTODO]
public virtual void OnDeserialization (object sender)
{
if (si == null)
return;
throw new NotImplementedException ();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator ()
{
return new Enumerator (this);
}
bool ICollection<T>.IsReadOnly {
get { return false; }
}
void ICollection<T>.CopyTo (T [] array, int index)
{
CopyTo (array, index);
}
void ICollection<T>.Add (T item)
{
Add (item);
}
IEnumerator IEnumerable.GetEnumerator ()
{
return new Enumerator (this);
}
public Enumerator GetEnumerator ()
{
return new Enumerator (this);
}
[Serializable]
public struct Enumerator : IEnumerator<T>, IDisposable {
HashSet<T> hashset;
int next;
int stamp;
T current;
internal Enumerator (HashSet<T> hashset)
: this ()
{
this.hashset = hashset;
this.stamp = hashset.generation;
}
public bool MoveNext ()
{
CheckState ();
if (next < 0)
return false;
while (next < hashset.touched) {
int cur = next++;
if (hashset.GetLinkHashCode (cur) != 0) {
current = hashset.slots [cur];
return true;
}
}
next = NO_SLOT;
return false;
}
public T Current {
get { return current; }
}
object IEnumerator.Current {
get {
CheckState ();
if (next <= 0)
throw new InvalidOperationException ("Current is not valid");
return current;
}
}
void IEnumerator.Reset ()
{
CheckState ();
next = 0;
}
public void Dispose ()
{
hashset = null;
}
void CheckState ()
{
if (hashset == null)
throw new ObjectDisposedException (null);
if (hashset.generation != stamp)
throw new InvalidOperationException ("HashSet have been modified while it was iterated over");
}
}
// borrowed from System.Collections.HashTable
static class PrimeHelper {
static readonly int [] primes_table = {
11,
19,
37,
73,
109,
163,
251,
367,
557,
823,
1237,
1861,
2777,
4177,
6247,
9371,
14057,
21089,
31627,
47431,
71143,
106721,
160073,
240101,
360163,
540217,
810343,
1215497,
1823231,
2734867,
4102283,
6153409,
9230113,
13845163
};
static bool TestPrime (int x)
{
if ((x & 1) != 0) {
int top = (int) Math.Sqrt (x);
for (int n = 3; n < top; n += 2) {
if ((x % n) == 0)
return false;
}
return true;
}
// There is only one even prime - 2.
return x == 2;
}
static int CalcPrime (int x)
{
for (int i = (x & (~1)) - 1; i < Int32.MaxValue; i += 2)
if (TestPrime (i))
return i;
return x;
}
public static int ToPrime (int x)
{
for (int i = 0; i < primes_table.Length; i++)
if (x <= primes_table [i])
return primes_table [i];
return CalcPrime (x);
}
}
}
}
|