예제 #1
0
        public void HashTableOpenAddressingCanHashCollisionsCorrectly()
        {
            var hashset = new HashTable_OpenAddressing(10);

            var hash       = hashset.GetHash(1);
            var collisions = new List <long> {
                1
            };

            for (var i = 2; i < 10000 && collisions.Count < 5; i++)
            {
                if (hashset.GetHash(i) == hash)
                {
                    collisions.Add(i);
                }
            }

            for (var i = 0; i < collisions.Count; i++)
            {
                hashset.Add(collisions[i]);

                Assert.IsTrue(hashset.Contains(collisions[i]));

                for (var j = i + 1; j < collisions.Count; j++)
                {
                    Assert.IsFalse(hashset.Contains(collisions[j]), new { i, j }.ToString());
                }
            }

            for (var i = 0; i < 100; i++)
            {
                Assert.AreEqual(collisions.Contains(i), hashset.Contains(i));
            }
        }
예제 #2
0
        private void CalcNumberOfDistinctTwoSums(IEnumerable <long> nums, long min, long max)
        {
            var hashset = new HashTable_OpenAddressing();
            var numbers = new List <long>();

            foreach (var num in nums)
            {
                hashset.Add(num);
                numbers.Add(num);
            }

            hashset.ShowStats();

            var cnt = 0;

            for (var t = min; t <= max; t++)
            {
                if (t % 100 == 0)
                {
                    Console.WriteLine(t);
                }

                foreach (var x in numbers)
                {
                    var y = t - x;
                    if (x != y && hashset.Contains(y))
                    {
                        cnt++;
                        break;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine(cnt);
        }