Beispiel #1
0
        public void LazySumSegmentTree_ForRandomOperations()
        {
            var rand = new Random();

            for (int a = 0; a < _sourceArrays.Length; ++a)
            {
                var sourceArray           = _sourceArrays[a];
                var lazySumSegmentTree    = new LazySumSegmentTree(sourceArray);
                var arrayBasedSegmentTree = new ArrayBasedSegmentTree <SumQueryObject, int>(sourceArray);

                for (int r = 0; r < 1000; ++r)
                {
                    int firstIndex  = rand.Next(0, sourceArray.Length);
                    int secondIndex = rand.Next(0, sourceArray.Length);
                    int startIndex  = Math.Min(firstIndex, secondIndex);
                    int endIndex    = Math.Max(firstIndex, secondIndex);
                    int mode        = rand.Next(2);

                    if (mode == 0)
                    {
                        NaiveSegmentTreeAlternatives.Update(sourceArray, startIndex, endIndex, x => x + r);
                        lazySumSegmentTree.Update(startIndex, endIndex, rangeAddition: r);
                        arrayBasedSegmentTree.Update(startIndex, endIndex, x => x + r);
                    }
                    else
                    {
                        int expected = NaiveSegmentTreeAlternatives.SumQuery(sourceArray, startIndex, endIndex);
                        Assert.AreEqual(expected, lazySumSegmentTree.SumQuery(startIndex, endIndex));
                        Assert.AreEqual(expected, arrayBasedSegmentTree.Query(startIndex, endIndex));
                    }
                }
            }
        }
Beispiel #2
0
        private void VerifiesUpdates <TQueryObject>(Func <IReadOnlyList <int>, int, int, int> naiveVerifier)
            where TQueryObject : SegmentTreeQueryObject <TQueryObject, int>, new()
        {
            Func <int, int> updater = x => x + 1;

            for (int a = 0; a < _sourceArrays.Length; ++a)
            {
                var sourceArray             = _sourceArrays[a];
                var nodeBasedSegmentTree    = new NodeBasedSegmentTree <TQueryObject, int>(sourceArray);
                var arrayBasedSegmentTree   = new ArrayBasedSegmentTree <TQueryObject, int>(sourceArray);
                var nonRecursiveSegmentTree = new NonRecursiveSegmentTree <TQueryObject, int>(sourceArray);

                for (int i = 0; i < sourceArray.Length; ++i)
                {
                    for (int j = i; j < sourceArray.Length; ++j)
                    {
                        NaiveSegmentTreeAlternatives.Update(sourceArray, i, j, updater);
                        nodeBasedSegmentTree.Update(i, j, updater);
                        arrayBasedSegmentTree.Update(i, j, updater);
                        nonRecursiveSegmentTree.Update(i, j, updater);

                        int expected = naiveVerifier(sourceArray, i, j);
                        Assert.AreEqual(expected, nodeBasedSegmentTree.Query(i, j));
                        Assert.AreEqual(expected, arrayBasedSegmentTree.Query(i, j));
                        Assert.AreEqual(expected, nonRecursiveSegmentTree.Query(i, j));
                    }
                }
            }
        }