Example #1
0
        private void Initialize(int capacity)
        {
            _size    = HashHelpers.GetPrime(capacity);
            _buckets = new NoLohData <int>();
            _entries = new NoLohData <Entry>();

            _buckets.EnsureSize(_size);
            _buckets.SetAllValues(-1);

            _entries.EnsureSize(_size);

            freeList = -1;
        }
Example #2
0
        public virtual void OnDeserialization(Object sender)
        {
            SerializationInfo siInfo;

            HashHelpers.SerializationInfoTable.TryGetValue(this, out siInfo);

            if (siInfo == null)
            {
                // It might be necessary to call OnDeserialization from a container if the container object also implements
                // OnDeserialization. However, remoting will call OnDeserialization again.
                // We can return immediately if this function is called twice.
                // Note we set remove the serialization info from the table at the end of this method.
                return;
            }

            int realVersion = siInfo.GetInt32(VersionName);
            int hashsize    = siInfo.GetInt32(HashSizeName);

            comparer = (IEqualityComparer <TKey>)siInfo.GetValue(ComparerName, typeof(IEqualityComparer <TKey>));

            if (hashsize != 0)
            {
                Resize(hashsize, false);
                freeList = -1;

                KeyValuePair <TKey, TValue>[] array = (KeyValuePair <TKey, TValue>[])
                                                      siInfo.GetValue(KeyValuePairsName, typeof(KeyValuePair <TKey, TValue>[]));

                if (array == null)
                {
                    ThrowHelper.ThrowSerializationException(ExceptionResource.Serialization_MissingKeys);
                }

                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i].Key == null)
                    {
                        ThrowHelper.ThrowSerializationException(ExceptionResource.Serialization_NullKey);
                    }
                    Insert(array[i].Key, array[i].Value, true);
                }
            }
            else
            {
                _buckets = null;
            }

            version = realVersion;
            HashHelpers.SerializationInfoTable.Remove(this);
        }
Example #3
0
        private void Resize(int newSize, bool forceNewHashCodes)
        {
            Contract.Assert(newSize >= _size);
            _buckets = _buckets ?? new NoLohData <int>();
            _buckets.EnsureSize(newSize);
            _buckets.SetAllValues(-1);
            _entries = _entries ?? new NoLohData <Entry>();
            _entries.EnsureSize(newSize);
            int temp = 0;

            if (forceNewHashCodes)
            {
                for (int i = 0; temp < count && i < _entries.VirtualArrayCount; i++)
                {
                    var chunkData = _entries.Values[i];
                    for (int j = 0; temp < count && j < chunkData.Length; j++)
                    {
                        if (chunkData[j].hashCode != -1)
                        {
                            chunkData[j].hashCode = (comparer.GetHashCode(chunkData[j].key) & 0x7FFFFFFF);
                            temp++;
                        }
                    }
                }
            }
            temp = 0;
            for (int i = 0; temp < count && i < _entries.VirtualArrayCount; i++)
            {
                var chunkData = _entries.Values[i];
                for (int j = 0; temp < count && j < chunkData.Length; j++)
                {
                    if (chunkData[j].hashCode >= 0)
                    {
                        int bucketChunk = -1, bucketIndexInChunk = -1;
                        int bucket = ExtractChunkAndIndexInChunk(chunkData[j].hashCode % newSize, _maxBucketChunkElementCount, ref bucketChunk, ref bucketIndexInChunk);
                        chunkData[j].next = _buckets.Values[bucketChunk][bucketIndexInChunk];
                        _buckets.Values[bucketChunk][bucketIndexInChunk] = MergeChunkAndIndexInChunk(_maxEntryChunkElementCount, i, j);
                        temp++;
                    }
                }
            }
            _size = newSize;
        }