summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Methodical/VT/port/lcs.cs
blob: fd73be213d30dd5f6e36a1c50b02c36237ddcc2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace JitTest
{
    internal struct LCSV
    {
        private int _v;
        private const int RANK = 4;

        private static String buildLCS(LCSV[,,,] b, char[] X, LCSV[] ind)
        {
            for (int i = 0; i < RANK; i++)
                if (ind[i]._v == 0) return "";

            LCSV L = b[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v];
            LCSV Z;
            Z._v = 0;
            if (L._v == RANK)
            {
                for (LCSV i = Z; i._v < RANK; i._v++)
                    ind[i._v]._v--;
                LCSV idx = ind[0];
                return buildLCS(b, X, ind) + X[idx._v];
            }
            if (L._v >= 0 && L._v < RANK)
            {
                ind[L._v]._v--;
                return buildLCS(b, X, ind);
            }
            throw new Exception();
        }

        private static void findLCS(LCSV[,,,] c, LCSV[,,,] b, char[][] seq, LCSV[] len)
        {
            LCSV[] ind = new LCSV[RANK];
            for (ind[0]._v = 1; ind[0]._v < len[0]._v; ind[0]._v++)
            {
                for (ind[1]._v = 1; ind[1]._v < len[1]._v; ind[1]._v++)
                {
                    for (ind[2]._v = 1; ind[2]._v < len[2]._v; ind[2]._v++)
                    {
                        for (ind[3]._v = 1; ind[3]._v < len[3]._v; ind[3]._v++)
                        {
                            bool eqFlag = true;
                            for (int i = 1; i < RANK; i++)
                            {
                                if (seq[i][ind[i]._v - 1] != seq[i - 1][ind[i - 1]._v - 1])
                                {
                                    eqFlag = false;
                                    break;
                                }
                            }

                            if (eqFlag)
                            {
                                c[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v]._v =
                                    c[ind[0]._v - 1, ind[1]._v - 1, ind[2]._v - 1, ind[3]._v - 1]._v + 1;
                                b[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v]._v = RANK;
                                continue;
                            }

                            LCSV Z;
                            Z._v = 0;

                            LCSV R, M;
                            R._v = M._v = -1;
                            for (LCSV i = Z; i._v < RANK; i._v++)
                            {
                                ind[i._v]._v--;
                                if (c[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v]._v > M._v)
                                {
                                    R = i;
                                    M = c[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v];
                                }
                                ind[i._v]._v++;
                            }
                            if (R._v < 0 || M._v < 0)
                                throw new Exception();

                            c[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v] = M;
                            b[ind[0]._v, ind[1]._v, ind[2]._v, ind[3]._v] = R;
                        }
                    }
                }
            }
        }

        private static int Main()
        {
            Console.WriteLine("Test searches for longest common subsequence of 4 strings\n\n");
            String[] str = new String[RANK] {
                "The Sun has left his blackness",
                "and has found a fresher morning",
                "and the fair Moon rejoices",
                "in the clear and cloudless night"
            };

            LCSV[] len = new LCSV[RANK];
            char[][] seq = new char[RANK][];
            for (int i = 0; i < RANK; i++)
            {
                len[i]._v = str[i].Length + 1;
                seq[i] = str[i].ToCharArray();
            }

            LCSV[,,,] c = new LCSV[len[0]._v, len[1]._v, len[2]._v, len[3]._v];
            LCSV[,,,] b = new LCSV[len[0]._v, len[1]._v, len[2]._v, len[3]._v];

            findLCS(c, b, seq, len);

            for (int i = 0; i < RANK; i++)
                len[i]._v--;

            if ("n ha  es" == buildLCS(b, seq[0], len))
            {
                Console.WriteLine("Test passed");
                return 100;
            }
            else
            {
                Console.WriteLine("Test failed.");
                return 1;
            }
        }
    }
}