Code:
                         / 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Linq / Parallel / Utils / Set.cs / 1305376 / Set.cs
                        
                        
                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// 
// Util.cs 
//
// [....]  
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System.Collections.Generic; 
namespace System.Linq.Parallel 
{ 
    /// 
    /// A set for various operations. Shamelessly stolen from LINQ's source code. 
    /// @
    internal class Set 
    {
 
        int[] buckets; 
        Slot[] slots;
        int count; 
        int freeList;
        IEqualityComparer comparer;
        internal Set() : this(null) 
        {
        } 
 
        internal Set(IEqualityComparer comparer)
        { 
            if (comparer == null) comparer = EqualityComparer.Default;
            this.comparer = comparer;
            buckets = new int[7];
            slots = new Slot[7]; 
            freeList = -1;
        } 
 
        // If value is not in set, add it and return true; otherwise return false
        internal bool Add(TElement value) 
        {
            return !Find(value, true);
        }
 
        // Check whether value is in set
        internal bool Contains(TElement value) 
        { 
            return Find(value, false);
        } 
        // If value is in set, remove it and return true; otherwise return false
        internal bool Remove(TElement value)
        { 
            int hashCode = comparer.GetHashCode(value) & 0x7FFFFFFF;
            int bucket = hashCode % buckets.Length; 
            int last = -1; 
            for (int i = buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
            { 
                if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value))
                {
                    if (last < 0)
                    { 
                        buckets[bucket] = slots[i].next + 1;
                    } 
                    else 
                    {
                        slots[last].next = slots[i].next; 
                    }
                    slots[i].hashCode = -1;
                    slots[i].value = default(TElement);
                    slots[i].next = freeList; 
                    freeList = i;
                    return true; 
                } 
            }
            return false; 
        }
        internal bool Find(TElement value, bool add)
        { 
            int hashCode = comparer.GetHashCode(value) & 0x7FFFFFFF;
            for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next) 
            { 
                if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value)) return true;
            } 
            if (add)
            {
                int index;
                if (freeList >= 0) 
                {
                    index = freeList; 
                    freeList = slots[index].next; 
                }
                else 
                {
                    if (count == slots.Length) Resize();
                    index = count;
                    count++; 
                }
                int bucket = hashCode % buckets.Length; 
                slots[index].hashCode = hashCode; 
                slots[index].value = value;
                slots[index].next = buckets[bucket] - 1; 
                buckets[bucket] = index + 1;
            }
            return false;
        } 
        void Resize() 
        { 
            int newSize = checked(count * 2 + 1);
            int[] newBuckets = new int[newSize]; 
            Slot[] newSlots = new Slot[newSize];
            Array.Copy(slots, 0, newSlots, 0, count);
            for (int i = 0; i < count; i++)
            { 
                int bucket = newSlots[i].hashCode % newSize;
                newSlots[i].next = newBuckets[bucket] - 1; 
                newBuckets[bucket] = i + 1; 
            }
            buckets = newBuckets; 
            slots = newSlots;
        }
        internal struct Slot 
        {
            internal int hashCode; 
            internal TElement value; 
            internal int next;
        } 
    }
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// 
// Util.cs 
//
// [....]  
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System.Collections.Generic; 
namespace System.Linq.Parallel 
{ 
    /// 
    /// A set for various operations. Shamelessly stolen from LINQ's source code. 
    /// @
    internal class Set 
    {
 
        int[] buckets; 
        Slot[] slots;
        int count; 
        int freeList;
        IEqualityComparer comparer;
        internal Set() : this(null) 
        {
        } 
 
        internal Set(IEqualityComparer comparer)
        { 
            if (comparer == null) comparer = EqualityComparer.Default;
            this.comparer = comparer;
            buckets = new int[7];
            slots = new Slot[7]; 
            freeList = -1;
        } 
 
        // If value is not in set, add it and return true; otherwise return false
        internal bool Add(TElement value) 
        {
            return !Find(value, true);
        }
 
        // Check whether value is in set
        internal bool Contains(TElement value) 
        { 
            return Find(value, false);
        } 
        // If value is in set, remove it and return true; otherwise return false
        internal bool Remove(TElement value)
        { 
            int hashCode = comparer.GetHashCode(value) & 0x7FFFFFFF;
            int bucket = hashCode % buckets.Length; 
            int last = -1; 
            for (int i = buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
            { 
                if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value))
                {
                    if (last < 0)
                    { 
                        buckets[bucket] = slots[i].next + 1;
                    } 
                    else 
                    {
                        slots[last].next = slots[i].next; 
                    }
                    slots[i].hashCode = -1;
                    slots[i].value = default(TElement);
                    slots[i].next = freeList; 
                    freeList = i;
                    return true; 
                } 
            }
            return false; 
        }
        internal bool Find(TElement value, bool add)
        { 
            int hashCode = comparer.GetHashCode(value) & 0x7FFFFFFF;
            for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next) 
            { 
                if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value)) return true;
            } 
            if (add)
            {
                int index;
                if (freeList >= 0) 
                {
                    index = freeList; 
                    freeList = slots[index].next; 
                }
                else 
                {
                    if (count == slots.Length) Resize();
                    index = count;
                    count++; 
                }
                int bucket = hashCode % buckets.Length; 
                slots[index].hashCode = hashCode; 
                slots[index].value = value;
                slots[index].next = buckets[bucket] - 1; 
                buckets[bucket] = index + 1;
            }
            return false;
        } 
        void Resize() 
        { 
            int newSize = checked(count * 2 + 1);
            int[] newBuckets = new int[newSize]; 
            Slot[] newSlots = new Slot[newSize];
            Array.Copy(slots, 0, newSlots, 0, count);
            for (int i = 0; i < count; i++)
            { 
                int bucket = newSlots[i].hashCode % newSize;
                newSlots[i].next = newBuckets[bucket] - 1; 
                newBuckets[bucket] = i + 1; 
            }
            buckets = newBuckets; 
            slots = newSlots;
        }
        internal struct Slot 
        {
            internal int hashCode; 
            internal TElement value; 
            internal int next;
        } 
    }
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                                  
                        
                        
                        
                    Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ConfigurationConverterBase.cs
- TraceAsyncResult.cs
- ToolStripGrip.cs
- XmlSignatureProperties.cs
- MenuCommand.cs
- WindowVisualStateTracker.cs
- AppSettingsReader.cs
- XmlQualifiedNameTest.cs
- DataGridViewComboBoxColumn.cs
- RegexFCD.cs
- SoapAttributeAttribute.cs
- LockedBorderGlyph.cs
- TextSpan.cs
- HtmlHead.cs
- DiagnosticsConfigurationHandler.cs
- ValueOfAction.cs
- NameSpaceEvent.cs
- MediaEntryAttribute.cs
- ChildDocumentBlock.cs
- DataTemplateSelector.cs
- UrlMappingCollection.cs
- ViewGenResults.cs
- BaseTemplatedMobileComponentEditor.cs
- SafePointer.cs
- BinaryObjectInfo.cs
- FixedStringLookup.cs
- StyleTypedPropertyAttribute.cs
- ToolstripProfessionalRenderer.cs
- EllipseGeometry.cs
- GeneratedView.cs
- GestureRecognitionResult.cs
- EmptyReadOnlyDictionaryInternal.cs
- TreeNode.cs
- AddInSegmentDirectoryNotFoundException.cs
- Positioning.cs
- PipelineModuleStepContainer.cs
- HttpCapabilitiesEvaluator.cs
- OdbcConnectionPoolProviderInfo.cs
- DataGridViewCellLinkedList.cs
- GridViewDeletedEventArgs.cs
- RefType.cs
- PropertyInformationCollection.cs
- CompilerError.cs
- DrawingContextWalker.cs
- TextParagraphView.cs
- WebResourceUtil.cs
- StreamUpdate.cs
- DictionarySectionHandler.cs
- SqlLiftIndependentRowExpressions.cs
- SqlNotificationRequest.cs
- ListBoxItem.cs
- ObjectSet.cs
- LexicalChunk.cs
- SvcFileManager.cs
- ValidatingPropertiesEventArgs.cs
- PageSettings.cs
- UndoManager.cs
- KnownTypeDataContractResolver.cs
- DBCommandBuilder.cs
- DataGridCellInfo.cs
- TextEffectResolver.cs
- COM2FontConverter.cs
- BitmapEffectRenderDataResource.cs
- DataGridViewColumnConverter.cs
- ObjectTokenCategory.cs
- EventLogWatcher.cs
- DeviceContexts.cs
- TemplatedControlDesigner.cs
- Span.cs
- XmlWriter.cs
- TreeNodeCollection.cs
- TriggerActionCollection.cs
- RequestBringIntoViewEventArgs.cs
- TemplateField.cs
- ExternalCalls.cs
- PropertyFilter.cs
- StretchValidation.cs
- ReadOnlyCollectionBase.cs
- InvalidAsynchronousStateException.cs
- EventLogPermissionAttribute.cs
- String.cs
- Closure.cs
- Popup.cs
- IconConverter.cs
- DataViewManagerListItemTypeDescriptor.cs
- RoutedUICommand.cs
- WizardPanel.cs
- ResourcePermissionBase.cs
- SystemColors.cs
- RegexStringValidatorAttribute.cs
- AppSettingsReader.cs
- HtmlHead.cs
- UriTemplateMatch.cs
- DiscreteKeyFrames.cs
- TreeNodeBindingDepthConverter.cs
- NumericUpDown.cs
- _DynamicWinsockMethods.cs
- RadioButtonList.cs
- SpellCheck.cs
- WizardPanelChangingEventArgs.cs