Beispiel #1
0
 private BidirectionalDictionary(int size, IEqualityComparer <TFirst> firstKeyComparer, IEqualityComparer <TSecond> secondKeyComparer, BidirectionalDictionary <TSecond, TFirst> reverse)
 {
     if (size > 0)
     {
         _buckets = new int[size];
         _entries = new Entry[size];
     }
     else
     {
         _buckets = HashHelpers.SizeOneIntArray;
         _entries = Array.Empty <Entry>();
     }
     if (firstKeyComparer != EqualityComparer <TFirst> .Default)
     {
         _comparer = firstKeyComparer;
     }
     if (reverse == null)
     {
         _shared  = new BidirectionalDictionaryShared();
         _reverse = new BidirectionalDictionary <TSecond, TFirst>(size, secondKeyComparer, firstKeyComparer, this);
     }
     else
     {
         _shared  = reverse._shared;
         _reverse = reverse;
     }
 }
Beispiel #2
0
        /// <summary>
        /// Removes all key pairs from the <see cref="BidirectionalDictionary{TFirst, TSecond}" />.
        /// </summary>
        public void Clear()
        {
            BidirectionalDictionaryShared shared = _shared;
            int count = shared.Count;

            Array.Clear(_buckets, 0, _buckets.Length);
            Array.Clear(_entries, 0, count);
            Array.Clear(_reverse._buckets, 0, _reverse._buckets.Length);
            Array.Clear(_reverse._entries, 0, count);
            shared.Count     = 0;
            shared.FreeList  = -1;
            shared.FreeCount = 0;
            ++shared.Version;
        }
Beispiel #3
0
        /// <summary>
        /// Resizes the internal data structure if necessary to ensure no additional resizing to support the specified capacity.
        /// </summary>
        /// <param name="capacity">The number of elements that the <see cref="BidirectionalDictionary{TFirst, TSecond}" /> must be able to contain.</param>
        /// <returns>The capacity of the <see cref="BidirectionalDictionary{TFirst, TSecond}" />.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity" /> is less than 0.</exception>
        public int EnsureCapacity(int capacity)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity));
            }

            if (_entries.Length >= capacity)
            {
                return(_entries.Length);
            }
            int newSize = HashHelpers.GetPrime(capacity);
            BidirectionalDictionaryShared shared = _shared;
            int count = shared.Count;

            Resize(newSize, count);
            _reverse.Resize(newSize, count);
            ++shared.Version;
            return(newSize);
        }
Beispiel #4
0
                /// <summary>
                /// Advances the enumerator to the next element of the <see cref="BidirectionalDictionary{TFirst, TSecond}.KeyCollection" />.
                /// </summary>
                /// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
                /// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created.</exception>
                public bool MoveNext()
                {
                    BidirectionalDictionaryShared shared = _bidirectionalDictionary._shared;

                    if (_version != shared.Version)
                    {
                        throw new InvalidOperationException(Strings.InvalidOperation_EnumFailedVersion);
                    }

                    Entry[] entries = _bidirectionalDictionary._entries;
                    while ((uint)_index < (uint)shared.Count)
                    {
                        Entry entry = entries[_index++];

                        if (entry.Next >= -1)
                        {
                            _current = entry.Key;
                            return(true);
                        }
                    }
                    _current = default;
                    return(false);
                }