Inheritance: IComparer, IComparer
        //****************************************

        /// <summary>
        /// Adds an element to the collection
        /// </summary>
        /// <param name="item">The element to add</param>
        /// <returns>True if the element was added, False if it was already in the set</returns>
        /// <exception cref="ArgumentNullException">Item was null</exception>
        public bool Add(T item)
        {         //****************************************
            int Key, Index;
            T?  Current;

            //****************************************

            if (item is null)
            {
                throw new ArgumentNullException(nameof(item), "Cannot add null to a Weak Hash Set");
            }

            Key   = Comparer.GetHashCode(item);
            Index = Array.BinarySearch <SetItem>(_Values, 0, _Size, new SetItem(Key));

            // Is there a matching hash code?
            if (Index >= 0)
            {
                // BinarySearch is not guaranteed to return the first matching value, so we may need to move back
                while (Index > 0 && _Values[Index - 1].HashCode == Key)
                {
                    Index--;
                }

                for (;;)
                {
                    try
                    {
                        Current = (T?)_Values[Index].Item.Target;

                        // Do we match this item?
                        if (Current != null && Comparer.Equals(Current, item))
                        {
                            return(false);                            // Yes, so return False
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        // The GCHandle was disposed
                    }

                    // Are we at the end of the list?
                    if (Index == _Size - 1)
                    {
                        break;                         // Yes, so we need to insert the new item at the end
                    }
                    // Is there another item with the same key?
                    if (_Values[Index + 1].HashCode != Key)
                    {
                        break;                         // Nope, so we can insert the new item at the current Index
                    }
                    // Yes, so loop back and check that
                    Index++;
                }

                Insert(Index, Key, item);

                return(true);
            }

            // No matching item, insert at the nearest spot above
            Insert(~Index, Key, item);

            return(true);
        }
Esempio n. 2
0
File: 6.cs Progetto: qifanyyy/CLCDSA
 public PriorityQueue()
 {
     list     = new T[16];
     Count    = 0;
     Comparer = System.Collections.Generic.Comparer <T> .Default;
 }
Esempio n. 3
0
 bool InRange(T item)
 {
     return(Comparer.Compare(item, lower) >= 0 &&
            Comparer.Compare(item, upper) <= 0);
 }