コード例 #1
0
ファイル: Program.cs プロジェクト: Siderite/FragmentList
        private static void testBinarySearch(Random rnd, FragmentList <int> lst, Stopwatch sw)
        {
            if (lst.Count == 0)
            {
                return;
            }
            var index = rnd.Next(0, lst.Count);
            var val   = lst[index];
            var val2  = rnd.Next();

            sw.Start();
            var idx = lst.BinarySearch(val);

            if (idx >= 0 && lst[idx] != val)
            {
                throw new Exception("Binary search");
            }
            if (idx < 0)
            {
                idx = ~idx;
                if (idx > 0 && lst[idx - 1] >= val)
                {
                    throw new Exception("Binary search");
                }
                if (idx < lst.Count && lst[idx] <= val)
                {
                    throw new Exception("Binary search");
                }
            }
            idx = lst.BinarySearch(val2);
            if (idx >= 0 && lst[idx] != val2)
            {
                throw new Exception("Binary search");
            }
            if (idx < 0)
            {
                idx = ~idx;
                if (idx > 0 && lst[idx - 1] >= val2)
                {
                    throw new Exception("Binary search");
                }
                if (idx < lst.Count && lst[idx] <= val2)
                {
                    throw new Exception("Binary search");
                }
            }
            sw.Stop();
        }