/// <summary> /// Creates an immutable hash set based on the contents of this instance. /// </summary> /// <returns>An immutable set.</returns> /// <remarks> /// This method is an O(n) operation, and approaches O(1) time as the number of /// actual mutations to the set since the last call to this method approaches 0. /// </remarks> public ImmutableHashSet <T> ToImmutable() { // Creating an instance of ImmutableSortedMap<T> with our root node automatically freezes our tree, // ensuring that the returned instance is immutable. Any further mutations made to this builder // will clone (and unfreeze) the spine of modified nodes until the next time this method is invoked. return(_immutable ??= ImmutableHashSet <T> .Wrap(_root, _equalityComparer, _count)); }
/// <summary> /// Returns an immutable hash set that captures the result of this mutation. /// </summary> /// <param name="priorSet">The prior version of the set. Used to capture the equality comparer and previous count, when applicable.</param> /// <returns>The new collection.</returns> internal ImmutableHashSet <T> Finalize(ImmutableHashSet <T> priorSet) { Requires.NotNull(priorSet, "priorSet"); int count = this.Count; if (this.CountType == ImmutableHashSet <T> .CountType.Adjustment) { count += priorSet._count; } return(priorSet.Wrap(this.Root, count)); }