Exemplo n.º 1
0
        public void InsertionAfterClear()
        {
            var dict = new FastDictionary <int, int>(200);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            dict.Clear();

            Assert.Equal(0, dict.Count);
            Assert.Equal(256, dict.Capacity);

            for (int i = 0; i < 100; i += 10)
            {
                dict[i] = i;
            }


            for (int i = 0; i < 100; i++)
            {
                if (i % 10 == 0)
                {
                    Assert.True(dict.Contains(i));
                }
                else
                {
                    Assert.False(dict.Contains(i));
                }
            }
        }
Exemplo n.º 2
0
        private static long BenchmarkFastDictionaryStringOut(string[] tuples, int tries)
        {
            int y        = 0;
            var fast     = Stopwatch.StartNew();
            var fastDict = new FastDictionary <int, string>(tuples.Length * 2);

            for (int i = 0; i < tries; i++)
            {
                for (int j = 0; j < tuples.Length; j++)
                {
                    fastDict[j] = tuples[j];
                }

                string k;
                for (int j = 0; j < tuples.Length; j++)
                {
                    fastDict.TryGetValue(j, out k);
                    //k = fastDict[j];
                    if (k != null)
                    {
                        y++;
                    }
                }

                //fastDict.Clear();
            }
            fast.Stop();
            return(fast.ElapsedMilliseconds);
        }
Exemplo n.º 3
0
        public void EnumeratorsWithJumps()
        {
            var dict = new FastDictionary <int, int>(16);

            dict[1]  = 1;
            dict[2]  = 2;
            dict[15] = 15;

            int count = 0;

            foreach (var item in dict.Keys)
            {
                count++;
            }
            Assert.Equal(3, count);

            count = 0;
            foreach (var item in dict.Values)
            {
                count++;
            }
            Assert.Equal(3, count);

            count = 0;
            foreach (var item in dict)
            {
                count++;
            }
            Assert.Equal(3, count);
        }
Exemplo n.º 4
0
        public void ConstructionWithFastDictionaryAndDifferentComparer()
        {
            var equalityComparer = new CustomIntEqualityComparer();

            var dict = new FastDictionary <int, int>(200, equalityComparer);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            var fromFastDictionary = new FastDictionary <int, int>(dict, EqualityComparer <int> .Default);

            Assert.Equal(dict.Count, fromFastDictionary.Count);
            Assert.Equal(dict.Capacity, fromFastDictionary.Capacity);
            Assert.NotSame(dict.Comparer, fromFastDictionary.Comparer);

            int count = 0;

            foreach (var item in fromFastDictionary)
            {
                Assert.Equal(item.Key, item.Value);
                count++;
            }
            Assert.Equal(100, count);
        }
Exemplo n.º 5
0
        private static long BenchmarkFastDictionaryString(string[] tuples, int tries)
        {
            var fast     = Stopwatch.StartNew();
            var fastDict = new FastDictionary <string, int>(tuples.Length * 2);

            for (int i = 0; i < tries; i++)
            {
                for (int j = 0; j < tuples.Length; j++)
                {
                    fastDict[tuples[j]] = j;
                }

                int k;
                for (int j = 0; j < tuples.Length; j++)
                {
                    fastDict.TryGetValue(tuples[j], out k);
                    // k = fastDict[tuples[j]];
                    k++;
                }

                //fastDict.Clear();
            }
            fast.Stop();
            return(fast.ElapsedMilliseconds);
        }
Exemplo n.º 6
0
        public void ConsecutiveInsertionsWithoutGrow()
        {
            var dict = new FastDictionary <int, int>(200);

            for (int i = 0; i < 100; i++)
            {
                dict.Add(i, i);
            }

            for (int i = 0; i < 100; i++)
            {
                Assert.True(dict.Contains(i));
                Assert.Equal(i, dict[i]);
            }

            int count = 0;

            foreach (var item in dict)
            {
                Assert.Equal(item.Key, item.Value);
                count++;
            }
            Assert.Equal(100, count);

            Assert.Equal(100, dict.Count);
            Assert.Equal(256, dict.Capacity);
        }
Exemplo n.º 7
0
        public void ConsecutiveInsertsWithShrink()
        {
            var dict = new FastDictionary <int, int>();

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            dict.Clear();

            for (int i = 0; i < 33; i++)
            {
                dict[i] = i;
            }

            dict.Remove(32);

            int value;

            Assert.True(dict.TryGetValue(0, out value));

            Assert.Equal(32, dict.Count);
            Assert.True(dict.Capacity > 32);
        }
Exemplo n.º 8
0
        public void ConsecutiveRemovesWithGrow()
        {
            var dict = new FastDictionary <int, int>(4);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            for (int i = 0; i < 100; i += 2)
            {
                Assert.True(dict.Remove(i));
            }

            for (int i = 0; i < 100; i++)
            {
                if (i % 2 == 0)
                {
                    Assert.False(dict.Contains(i));
                }
                else
                {
                    Assert.True(dict.Contains(i));
                }
            }

            Assert.Equal(50, dict.Count);
            Assert.Equal(256, dict.Capacity);
        }
Exemplo n.º 9
0
        public void ConstructionWithNonPowerOf2()
        {
            var dict = new FastDictionary <int, int>(5);

            Assert.Equal(0, dict.Count);
            Assert.Equal(8, dict.Capacity);
            Assert.NotNull(dict.Comparer);
        }
Exemplo n.º 10
0
        public void Duplicates()
        {
            var dict = new FastDictionary <int, int>(16);

            dict[1] = 1;
            dict[1] = 2;

            Assert.Equal(2, dict[1]);
            Assert.Throws <ArgumentException>(() => dict.Add(1, 3));
        }
Exemplo n.º 11
0
        public void AddWithHashCollision()
        {
            var dict = new FastDictionary <string, int>(new CollidingStringEqualityComparer());

            dict.Add("a", 1);
            dict.Add("b", 1);
            dict.Remove("a");

            Assert.Throws <ArgumentException>(() => dict.Add("b", 1));
        }
Exemplo n.º 12
0
        public void UpdateWithHashCollision()
        {
            var dict = new FastDictionary <string, int>(new CollidingStringEqualityComparer());

            dict["a"] = 1;
            dict["b"] = 1;
            dict.Remove("a");
            dict["b"] = 1;

            Assert.Equal(1, dict.Count);
        }
Exemplo n.º 13
0
        public void ConstructionWithExplicitZeroAndNegative()
        {
            var dict = new FastDictionary <int, int>(0);

            Assert.Equal(0, dict.Count);
            Assert.Equal(4, dict.Capacity);
            Assert.NotNull(dict.Comparer);

            dict = new FastDictionary <int, int>(-1);
            Assert.Equal(0, dict.Count);
            Assert.Equal(4, dict.Capacity);
            Assert.NotNull(dict.Comparer);
        }
Exemplo n.º 14
0
        public void AddAndRemoveWithoutGrowth()
        {
            var dict = new FastDictionary <long, int>(8);

            for (int i = 0; i < 100; i++)
            {
                dict.Add(i, i);
                dict.Remove(i);
                int dummy;
                Assert.False(dict.TryGetValue(i, out dummy));
            }

            Assert.Equal(0, dict.Count);
            Assert.Equal(8, dict.Capacity);
        }
Exemplo n.º 15
0
        public void AddDeleted()
        {
            var dict = new FastDictionary <int, int>(16);

            dict.Add(1, 1);
            dict.Add(2, 2);
            dict.Remove(1);
            dict.Add(17, 17);

            Assert.False(dict.Contains(1));
            Assert.True(dict.Contains(2));
            Assert.True(dict.Contains(17));

            Assert.Equal(2, dict.Count);
            Assert.Equal(16, dict.Capacity);
        }
Exemplo n.º 16
0
        public void ConsecutiveInsertionsAndGrow()
        {
            var dict = new FastDictionary <int, int>(4);

            for (int i = 0; i < 100; i++)
            {
                dict.Add(i, i);
            }

            for (int i = 0; i < 100; i++)
            {
                Assert.True(dict.Contains(i));
                Assert.Equal(i, dict[i]);
            }

            Assert.Equal(100, dict.Count);
            Assert.Equal(256, dict.Capacity);
        }
Exemplo n.º 17
0
        public void InsertDeleted()
        {
            var dict = new FastDictionary <int, int>(16);

            dict[1] = 1;
            dict[2] = 2;

            dict.Remove(1);

            dict[17] = 17;

            Assert.False(dict.Contains(1));
            Assert.True(dict.Contains(2));
            Assert.True(dict.Contains(17));

            Assert.Equal(2, dict.Count);
            Assert.Equal(16, dict.Capacity);
        }
Exemplo n.º 18
0
        public void UseOfOfBoundsHashes()
        {
            var dict = new FastDictionary <int, int>(16, new ForceOutOfRangeHashesEqualityComparer());

            dict[1] = 1;
            dict[2] = 2;

            Assert.Equal(1, dict[1]);
            Assert.Equal(2, dict[2]);

            dict.Remove(1);
            Assert.False(dict.Contains(1));
            Assert.True(dict.Contains(2));

            dict.Remove(2);
            Assert.False(dict.Contains(1));
            Assert.False(dict.Contains(2));
        }
Exemplo n.º 19
0
        public void ValuesArePresent()
        {
            var dict = new FastDictionary <int, int>(4);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            int count = 0;

            foreach (var value in dict.Values.ToList())
            {
                Assert.True(dict.ContainsValue(value));
                count++;
            }
            Assert.Equal(100, count);
        }
Exemplo n.º 20
0
        public void Construction()
        {
            var dict = new FastDictionary <int, int>();

            Assert.Equal(0, dict.Count);
            Assert.Equal(32, dict.Capacity);
            Assert.NotNull(dict.Comparer);

            dict = new FastDictionary <int, int>(null as IEqualityComparer <int>);
            Assert.Equal(0, dict.Count);
            Assert.Equal(32, dict.Capacity);
            Assert.NotNull(dict.Comparer);

            dict = new FastDictionary <int, int>(16);
            Assert.Equal(0, dict.Count);
            Assert.Equal(16, dict.Capacity);
            Assert.NotNull(dict.Comparer);
        }
Exemplo n.º 21
0
        public void Clear()
        {
            var dict = new FastDictionary <int, int>(200);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            dict.Clear();

            Assert.Equal(0, dict.Count);
            Assert.Equal(256, dict.Capacity);

            for (int i = 0; i < 100; i++)
            {
                Assert.False(dict.Contains(i));
            }
        }
Exemplo n.º 22
0
        public void KeysArePresent()
        {
            var dict = new FastDictionary <int, int>(4);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            int count = 0;

            foreach (var key in dict.Keys.ToList())
            {
                Assert.True(dict.ContainsKey(key));
                Assert.Equal(key, dict[key]);
                count++;
            }
            Assert.Equal(100, count);
        }
Exemplo n.º 23
0
        public void ConstructionWithNativeDictionary()
        {
            var dict = new Dictionary <int, int>(200, EqualityComparer <int> .Default);

            for (int i = 0; i < 100; i++)
            {
                dict[i] = i;
            }

            var fromFastDictionary = new FastDictionary <int, int>(dict.Count, dict, EqualityComparer <int> .Default);

            Assert.Equal(dict.Count, fromFastDictionary.Count);
            Assert.Equal(dict.Comparer, fromFastDictionary.Comparer);

            int count = 0;

            foreach (var item in fromFastDictionary)
            {
                Assert.Equal(item.Key, item.Value);
                count++;
            }
            Assert.Equal(100, count);
        }
Exemplo n.º 24
0
        public FastDictionary(int initialBucketCount, FastDictionary <TKey, TValue> src, IEqualityComparer <TKey> comparer)
        {
            Contract.Requires(src != null);
            Contract.Ensures(_capacity >= initialBucketCount);
            Contract.Ensures(_capacity >= src._capacity);

            this.comparer = comparer ?? EqualityComparer <TKey> .Default;

            this._initialCapacity     = DictionaryHelper.NextPowerOf2(initialBucketCount);
            this._capacity            = Math.Max(src._capacity, initialBucketCount);
            this._capacityM1          = this._capacity - 1;
            this._size                = src._size;
            this._numberOfUsed        = src._numberOfUsed;
            this._numberOfDeleted     = src._numberOfDeleted;
            this._nextGrowthThreshold = src._nextGrowthThreshold;

            int newCapacity = _capacity;

            if (comparer == src.comparer)
            {
                // Initialization through copy (very efficient) because the comparer is the same.
                this._entries = new Entry[newCapacity];
                Array.Copy(src._entries, _entries, newCapacity);
            }
            else
            {
                // Initialization through rehashing because the comparer is not the same.
                var entries = new Entry[newCapacity];
                BlockCopyMemoryHelper.Memset(entries, new Entry(kUnusedHash, default(TKey), default(TValue)));

                // Creating a temporary alias to use for rehashing.
                this._entries = src._entries;

                // This call will rewrite the aliases
                Rehash(entries);
            }
        }
Exemplo n.º 25
0
 public FastDictionary(FastDictionary <TKey, TValue> src, IEqualityComparer <TKey> comparer)
     : this(src._capacity, src, comparer)
 {
 }
Exemplo n.º 26
0
 internal Enumerator(FastDictionary <TKey, TValue> dictionary)
 {
     this.dictionary = dictionary;
     index           = 0;
     currentValue    = default(TValue);
 }
Exemplo n.º 27
0
            public ValueCollection(FastDictionary <TKey, TValue> dictionary)
            {
                Contract.Requires(dictionary != null);

                this.dictionary = dictionary;
            }
Exemplo n.º 28
0
 internal Enumerator(FastDictionary <TKey, TValue> dictionary)
 {
     this.dictionary = dictionary;
     this.index      = 0;
     this.current    = new KeyValuePair <TKey, TValue>();
 }
Exemplo n.º 29
0
 public FastDictionary(FastDictionary <TKey, TValue> src)
     : this(src._capacity, src, src.comparer)
 {
 }