Example #1
0
        void testHeap()
        {
            int[] arr = new int[10];
            var   rnd = new Random(101);

            for (int i = 0; i < 10; i++)
            {
                arr[i] = rnd.Next(1, 100);
            }
            Func <int, int, int> cmp = (x, y) => x - y;
            var h = new MyHeap <int>(arr, cmp);

            for (int i = 0; i < 10; i++)
            {
                var min = h.PopMin();
            }
        }
Example #2
0
        SrchRec[] calcDiff(MyWord[][] arr, int nRow)
        {
            int max_res = 1000;
            int max_d   = 1000;

            var begin = Environment.TickCount;
            var res   = new SrchRec[arr[0].Length];

            for (int i = 0; i < arr[0].Length; i++)
            {
                res[i] = new SrchRec()
                {
                    path = new int[] { i }, d = 0
                };
            }
            for (var row = 1; row < nRow; row++)
            {
                var lst     = arr[row];
                var prevLst = arr[row - 1];
                var tmplRes = new List <int[]>();
                for (var j = 0; j < res.Length; j++)
                {
                    var prevD = res[j].d;
                    var prevI = res[j].path[row - 1];
                    var prevW = prevLst[prevI];
                    for (var k = 0; k < lst.Length; k++)
                    {
                        var curW = lst[k];
                        if (prevW.titleId == curW.titleId)
                        {
                            //var d = wordDiff(res[j].w, lst[k]);
                            var d = Convert.ToInt32(prevW.parId) - Convert.ToInt32(curW.parId);
                            d *= d < 0 ? -100 : 100;
                            d += Math.Abs(prevW.pos - curW.pos) * 10;
                            d += prevD;
                            tmplRes.Add(new int[] { j, k, d });
                        }
                    }
                }

                var h   = new MyHeap <int[]>(tmplRes.ToArray(), (x, y) => x[2] - y[2]);
                var n   = Math.Min(max_res, tmplRes.Count);
                var top = new SrchRec[n];
                for (int i = 0; i < n; i++)
                {
                    var t = h.PopMin();

                    //chk distance
                    if (t[2] > max_d)
                    {
                        Array.Resize(ref top, i);
                        break;
                    }

                    var newRec = new SrchRec()
                    {
                        d = t[2], path = new int[row + 1]
                    };
                    res[t[0]].path.CopyTo(newRec.path, 0);
                    newRec.path[row] = t[1];
                    top[i]           = newRec;
                }
                res = top;
            }
            var elapsed = Environment.TickCount - begin;

            Debug.WriteLine("calc diff {0}", elapsed);
            foreach (var item in res)
            {
                var row = 0;
                item.detail = item.path.Select(v => arr[row++][v]).ToList();
            }
            return(res);
        }