Example #1
0
        public static List <int> patience_longest_increasing_sequence(List <int> v)
        {
            List <int> result = new List <int>();

            if (!v.Any())
            {
                return(result);
            }

            List <Patience <T> > top_cards = new List <Patience <T> >();

            for (int i = 0; i < v.Count; ++i)
            {
                int  val     = v[i];
                bool handled = false;
                for (int j = 0; j < top_cards.Count; ++j)
                {
                    if (top_cards[j].value > val)
                    {
                        var left = j > 0 ? top_cards[j - 1] : null;
                        top_cards[j] = new Patience <T>(val, left, top_cards[j]);
                        handled      = true;
                        break;
                    }
                }
                if (!handled)
                {
                    var left = (!top_cards.Any()) ? null : top_cards[top_cards.Count - 1];
                    top_cards.Add(new Patience <T>(val, left, null));
                }
            }

            if (!top_cards.Any())
            {
                throw new Exception();
            }
            int n = top_cards.Count;

            // result.resize(n);
            int[] interm = result.ToArray();
            Array.Resize(ref interm, n);
            result = interm.ToList();
            Patience <T> p = top_cards[n - 1];

            for (int i = 0; i < n; ++i)
            {
                if (p == null)
                {
                    throw new Exception();
                }
                result[n - i - 1] = p.value;
                p = p.left;
            }
            if (p != null)
            {
                throw new Exception();
            }
            return(result);
        }
Example #2
0
 public Patience(int v, Patience <T> l, Patience <T> d)
 {
     value = v;
     left  = l;
     down  = d;
 }
Example #3
0
        public void add_vec_to_diff(ref Diff <T> a, int fileid, List <T> b)
        {
            int      bmask  = 1 << fileid;
            Diff <T> result = new Diff <T>(a.dimension, a.mask | bmask);
            Diff <T> suffix = new Diff <T>(a.dimension, a.mask | bmask);
            int      i      = 0;

            while (i < a.lines.Count && i < b.Count && comparer.Equals(a.lines[i].text, b[i]))
            {
                T line = b[i];
                result.lines.Add(new Line <T>(line, a.lines[i].mask | bmask));
                ++i;
            }
            int      ja = a.lines.Count;
            int      jb = b.Count;
            List <T> ua = new List <T>();
            List <T> ub = new List <T>();

            for (int k = i; k < ja; ++k)
            {
                T          line   = a.lines[k].text;
                List <int> d      = unique_lines.lookup(line);
                bool       failed = d[fileid] == 0;
                if (failed)
                {
                    continue;
                }
                for (int k2 = i; !failed && k2 < ja; ++k2)
                {
                    if (k2 == k)
                    {
                        continue;
                    }
                    if (comparer.Equals(a.lines[k2].text, line))
                    {
                        failed = true;
                    }
                }
                if (failed)
                {
                    continue;
                }
                bool found = false;
                for (int k2 = i; !failed && k2 < jb; ++k2)
                {
                    if (comparer.Equals(b[k2], line))
                    {
                        if (found)
                        {
                            failed = true;
                        }
                        found = true;
                    }
                }
                if (failed || !found)
                {
                    continue;
                }
                ua.Add(line);
            }
            for (int k = i; k < jb; ++k)
            {
                T line = b[k];
                if (Class1 <T> .MyIndexOf(ua, line, comparer) >= 0)
                {
                    ub.Add(line);
                }
            }
            List <T> lcs = Patience <T> .patience_unique_lcs(ua, ub, comparer);

            if (lcs.Count == 0)
            {
                Diff <T> ta = new Diff <T>(a.dimension, a.mask | bmask);
                List <T> tb = new List <T>();
                for (int j = i; j < jb; ++j)
                {
                    tb.Add(b[j]);
                }
                for (int k = i; k < ja; ++k)
                {
                    ta.lines.Add(new Line <T>(a.lines[k].text, a.lines[k].mask));
                }
                this.add_vec_to_diff_classical(ref ta, fileid, tb);
                result.append(ta);
            }
            else
            {
                int      ak = i;
                int      bk = i;
                Diff <T> ta = new Diff <T>(a.dimension, a.mask);
                List <T> tb = new List <T>();
                for (int lcx = 0; lcx < lcs.Count; ++lcx)
                {
                    while (!comparer.Equals(a.lines[ak].text, lcs[lcx]))
                    {
                        ta.lines.Add(a.lines[ak]);
                        ++ak;
                    }
                    while (!comparer.Equals(b[bk], lcs[lcx]))
                    {
                        tb.Add(b[bk]);
                        ++bk;
                    }
                    ta.mask = a.mask;
                    this.add_vec_to_diff(ref ta, fileid, tb);
                    result.append(ta);
                    ta.lines.Clear();
                    tb.Clear();
                    result.lines.Add(new Line <T>(lcs[lcx], a.lines[ak].mask | bmask));
                    ++ak;
                    ++bk;
                }
                while (ak < ja)
                {
                    ta.lines.Add(a.lines[ak]);
                    ++ak;
                }
                while (bk < jb)
                {
                    tb.Add(b[bk]);
                    ++bk;
                }
                ta.mask = a.mask;
                this.add_vec_to_diff(ref ta, fileid, tb);
                result.append(ta);
            }
            a = result; // COPY!
        }