Beispiel #1
0
        /// <summary>
        /// Find duplicates in an array with only 4kb of memory available
        /// </summary>
        /// <param name="arr"></param>
        public static void FindDuplicates(int[] arr)
        {
            BitVector bv = new BitVector(32000);

            for (int i = 0; i < arr.Length; i++)
            {
                int num  = arr[i];
                int num0 = num - 1;

                if (bv.Get(num0))
                {
                    Console.WriteLine("Duplicate: " + num);
                }
                else
                {
                    bv.Add(num0);
                }
            }
        }
Beispiel #2
0
        public void BitVectorTests_Basics()
        {
            BitVector     vector   = new BitVector(false, 260);
            HashSet <int> expected = new HashSet <int>();

            // Empty vector
            vector.Trim();
            VerifySame(expected, vector);
            Assert.Null(vector.Array);
            Assert.Equal(260, vector.Capacity);

            // Add every third item
            for (int i = 0; i < vector.Capacity; i += 3)
            {
                // True the first time
                Assert.Equal(expected.Add(i), vector.Add(i));

                // False when already present
                Assert.Equal(expected.Add(i), vector.Add(i));
            }

            VerifySame(expected, vector);

            // Remove every sixth item
            for (int i = 0; i < vector.Capacity; i += 6)
            {
                // True the first time
                Assert.Equal(expected.Remove(i), vector.Remove(i));

                // False when already present
                Assert.Equal(expected.Remove(i), vector.Remove(i));
            }

            VerifySame(expected, vector);

            // Contains
            for (int i = 0; i < vector.Capacity; ++i)
            {
                Assert.Equal(expected.Contains(i), vector.Contains(i));
            }

            // Verify untyped enumerator, Reset()
            List <int>  expectedList = new List <int>(expected);
            IEnumerator untyped      = ((IEnumerable)vector).GetEnumerator();
            int         index        = 0;

            while (untyped.MoveNext())
            {
                Assert.Equal(expectedList[index], untyped.Current);
                index++;
            }

            untyped.Reset();
            index = 0;
            while (untyped.MoveNext())
            {
                Assert.Equal(expectedList[index], untyped.Current);
                index++;
            }

            // Automatic growth w/default (need 126 ints = 4,001 / 32 rounded up)
            vector.Add(4000);
            expected.Add(4000);
            Assert.Equal(4001, vector.Capacity);
            Assert.Equal(((4001 + 31) / 32), vector.Array?.Length ?? 0);
            VerifySame(expected, vector);

            // Clear
            vector.Clear();
            Assert.Empty(vector);
            Assert.Equal(0, vector.Capacity);

            // UnionWith
            vector.UnionWith(expected);
            VerifySame(expected, vector);

            // ExceptWith
            vector.ExceptWith(expected);
            Assert.Empty(vector);

            // SetAll
            vector.Clear();
            vector[100] = true;

            vector.SetAll(true);
            Assert.Equal(vector.Capacity, vector.Count);
            Assert.False(vector[vector.Capacity]);
            Assert.True(vector[vector.Capacity - 1]);

            vector.SetAll(false);
            Assert.Empty(vector);
            Assert.False(vector[vector.Capacity]);

            // SetAll (exact multiple of 32)
            vector.Clear();
            vector[3999] = true;

            vector.SetAll(true);
            Assert.Equal(vector.Capacity, vector.Count);
            Assert.False(vector[vector.Capacity]);
            Assert.True(vector[vector.Capacity - 1]);

            vector.SetAll(false);
            Assert.Empty(vector);
            Assert.False(vector[vector.Capacity]);
            Assert.False(vector[vector.Capacity - 1]);
        }