public void Test_Set_ValidIndex()
        {
            var cba = new CompactBoundedArray <Int32>(5, 15);

            cba[5] = 1;

            Assert.Equal(1, cba[5]);
        }
        private static void Sort(String[] array,
                                 String[] aux,
                                 Int32 low,
                                 Int32 high,
                                 Int32 index,
                                 Alphabet alphabet)
        {
            if (high <= low + CUT_OFF)
            {
                InsertionSort(array, low, high, index);

                return;
            }

            CompactBoundedArray <Int32> count = new CompactBoundedArray <Int32>(alphabet.MinChar, alphabet.MaxChar + 2);

            // 1 - Compute frequency counts.

            for (int i = low; i <= high; i++)
            {
                count[CharAt(array[i], index) + 2]++;
            }

            // 2 - Transform counts to indices.

            for (int i = 0; i < alphabet.Radix + 1; i++)
            {
                count[i + 1] += count[i];
            }

            // 4 - Distribute.

            for (int i = low; i <= high; i++)
            {
                aux[count[CharAt(array[i], index) + 1]++] = array[i];
            }

            // 5 - Copy back.

            for (int i = low; i <= high; i++)
            {
                array[i] = aux[i - low];
            }

            // 6 - Recursively sort for each character value.

            for (int i = 0; i < alphabet.Radix; i++)
            {
                Sort(array,
                     aux,
                     low + count[i],
                     low + count[i + 1] - 1,
                     index + 1,
                     alphabet);
            }
        }
Exemplo n.º 3
0
        public static void Sort(String[] array, Int32 stringLength, Alphabet alphabet)
        {
            String[] aux = new String[array.Length];
            CompactBoundedArray <Int32> count = new CompactBoundedArray <Int32>(alphabet.MinChar, alphabet.MaxChar);

            // Sort by key-indexed counting on dth character.
            // Same logic of KeyIndexedCountingSort.
            // Reading right to left.

            for (int c = stringLength - 1; c >= 0; c--)
            {
                // 1 - Compute frequency counts.

                for (int i = 0; i < array.Length; i++)
                {
                    count[array[i][c] + 1]++;
                }

                // 2 - Compute cumulates.

                for (int i = count.LowerBound; i < count.UpperBound; i++)
                {
                    count[i + 1] += count[i];
                }

                // 3 - Move data.

                for (int i = 0; i < array.Length; i++)
                {
                    aux[count[array[i][c]]++] = array[i];
                }

                // 4 - Copy data back.

                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = aux[i];
                }

                // Cleaning for the next round.

                for (int i = count.LowerBound; i < count.UpperBound; i++)
                {
                    count[i] = 0;
                }
            }
        }
        public void Test_LowerBound()
        {
            var cba = new CompactBoundedArray <Int32>(10, 20);

            Assert.Equal(10, cba.LowerBound);
        }
        public void Test_Set_InvalidIndex()
        {
            var cba = new CompactBoundedArray <Int32>(5, 15);

            Assert.Throws <IndexOutOfRangeException>(() => cba[2] = 1);
        }
        public void Test_NotInRange()
        {
            var cba = new CompactBoundedArray <Int32>(5, 15);

            Assert.False(cba.InRange(2));
        }
        public void Test_InRange()
        {
            var cba = new CompactBoundedArray <Int32>(5, 15);

            Assert.True(cba.InRange(5));
        }
        public void Test_Lenght()
        {
            var cba = new CompactBoundedArray <Int32>(5, 15);

            Assert.Equal(10, cba.Length);
        }