Exemple #1
0
        public void LastIndexOf_int_OrderIsCorrectWithManyDuplicates(int count)
        {
            SegmentedList <T> list = GenericListFactory(count);
            SegmentedList <T> withoutDuplicates = list.ToSegmentedList();

            list.AddRange(list);
            list.AddRange(list);
            list.AddRange(list);

            Assert.All(
                Enumerable.Range(0, count),
                i =>
            {
                Assert.All(
                    Enumerable.Range(0, 4),
                    j =>
                {
                    int expectedIndex = (j * count) + i;
                    Assert.Equal(
                        expectedIndex,
                        list.LastIndexOf(withoutDuplicates[i], (count * (j + 1)) - 1)
                        );
                    Assert.Equal(
                        expectedIndex,
                        list.LastIndexOf(withoutDuplicates[i], (count * (j + 1)) - 1, count)
                        );
                }
                    );
            }
                );
        }
        public void AddRange_AddSelfAsEnumerable_ThrowsExceptionWhenNotEmpty()
        {
            SegmentedList <T?> list = GenericListFactory(0) !;

            // Succeeds when list is empty.
            list.AddRange(list);
            list.AddRange(list.Where(_ => true));

            // Succeeds when list has elements and is added as collection.
            list.Add(default);
Exemple #3
0
        public TokenStream(TreeData treeData, SyntaxFormattingOptions options, TextSpan spanToFormat, AbstractTriviaDataFactory factory)
        {
            using (Logger.LogBlock(FunctionId.Formatting_TokenStreamConstruction, CancellationToken.None))
            {
                // initialize basic info
                _factory  = factory;
                _treeData = treeData;
                _options  = options;

                // use some heuristics to get initial size of list rather than blindly start from default size == 4
                var sizeOfList = spanToFormat.Length / MagicTextLengthToTokensRatio;
                _tokens = new SegmentedList <SyntaxToken>(sizeOfList);
                _tokens.AddRange(_treeData.GetApplicableTokens(spanToFormat));

                Debug.Assert(this.TokenCount > 0);

                // initialize trivia related info
                _cachedOriginalTriviaInfo = new SegmentedArray <TriviaData>(this.TokenCount - 1);

                // Func Cache
                _getTriviaData         = this.GetTriviaData;
                _getOriginalTriviaData = this.GetOriginalTriviaData;
            }

            DebugCheckTokenOrder();
        }
Exemple #4
0
        public void IndexOf_OrderIsCorrect(
            IndexOfMethod indexOfMethod,
            int count,
            bool frontToBackOrder
            )
        {
            SegmentedList <T> list = GenericListFactory(count);
            SegmentedList <T> withoutDuplicates = list.ToSegmentedList();

            list.AddRange(list);
            IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod);

            Assert.All(
                Enumerable.Range(0, count),
                i =>
            {
                if (frontToBackOrder)
                {
                    Assert.Equal(i, IndexOf(list, withoutDuplicates[i]));
                }
                else
                {
                    Assert.Equal(count + i, IndexOf(list, withoutDuplicates[i]));
                }
            }
                );
        }
        public void AddRange(EnumerableType enumerableType, int listLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
        {
            SegmentedList <T> list          = GenericListFactory(listLength);
            SegmentedList <T> listBeforeAdd = list.ToSegmentedList();
            IEnumerable <T>   enumerable    = CreateEnumerable(enumerableType, list, enumerableLength, numberOfMatchingElements, numberOfDuplicateElements);

            list.AddRange(enumerable);

            // Check that the first section of the List is unchanged
            Assert.All(Enumerable.Range(0, listLength), index =>
            {
                Assert.Equal(listBeforeAdd[index], list[index]);
            });

            // Check that the added elements are correct
            Assert.All(Enumerable.Range(0, enumerableLength), index =>
            {
                Assert.Equal(enumerable.ElementAt(index), list[index + listLength]);
            });
        }
Exemple #6
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            historyWorkspace.VerifyAccess();
            BitmapLayer       layer               = (BitmapLayer)historyWorkspace.Document.Layers[this.layerIndex];
            Surface           layerSurface        = layer.Surface;
            TileMathHelper    tileMathHelper      = new TileMathHelper(layerSurface.Width, layerSurface.Height, this.rendererTileEdgeLog2);
            TileMathHelper    tileGroupMathHelper = new TileMathHelper(layerSurface.Width, layerSurface.Height, this.rendererTileEdgeLog2 + 3);
            HistoryMemento    actionHM            = null;
            ManualResetEvent  taskFinishedEvent   = new ManualResetEvent(false);
            ThreadTask <Unit> commitTask          = historyWorkspace.TaskManager.StartNewThreadTask(delegate(Task task) {
                SegmentedList <RectInt32> list = tileGroupMathHelper.EnumerateTilesClippedToSourceRect(this.rendererClipRect).ToSegmentedList <RectInt32>();
                ListUtil.FisherYatesShuffle <RectInt32>(list);
                ConcurrentQueue <ISurface <ColorBgra> > queue = new ConcurrentQueue <ISurface <ColorBgra> >();
                ConcurrentQueue <RectInt32> changedTiles      = new ConcurrentQueue <RectInt32>();
                ConcurrentQueue <TupleStruct <RectInt32, ISurface <ColorBgra> > > changedTileGroups = new ConcurrentQueue <TupleStruct <RectInt32, ISurface <ColorBgra> > >();
                double progressIncrement = 1.0 / ((double)Math.Max(1, list.Count));
                task.Progress            = 0.0;
                Work.ParallelForEach <RectInt32>(WaitType.Blocking, list, delegate(RectInt32 tileGroupRect) {
                    ISurface <ColorBgra> dst = RetryManager.RunMemorySensitiveOperation <ISurfaceRef <ColorBgra> >(() => SurfaceAllocator.Bgra.Allocate <ColorBgra>(tileGroupRect.Size, AllocationOptions.ZeroFillNotRequired));
                    this.renderer.Render(dst, tileGroupRect.Location);
                    bool flag = false;
                    foreach (RectInt32 num in tileMathHelper.EnumerateTilesClippedToSourceRect(tileGroupRect))
                    {
                        RectInt32 bounds = RectInt32.Offset(num, -tileGroupRect.Location);
                        using (ISurface <ColorBgra> surface2 = layerSurface.CreateWindow(num))
                        {
                            using (ISurface <ColorBgra> surface3 = dst.CreateWindow <ColorBgra>(bounds))
                            {
                                if (!SurfaceBgraUtil.ArePixelsEqual(surface2, surface3))
                                {
                                    flag = true;
                                    changedTiles.Enqueue(num);
                                }
                            }
                        }
                    }
                    if (flag)
                    {
                        changedTileGroups.Enqueue(TupleStruct.Create <RectInt32, ISurface <ColorBgra> >(tileGroupRect, dst));
                    }
                    else
                    {
                        DisposableUtil.Free <ISurface <ColorBgra> >(ref dst);
                    }
                    task.IncrementProgressBy(progressIncrement);
                }, WorkItemQueuePriority.Normal, null);
                task.Progress = null;
                if (changedTiles.Count == 0)
                {
                    actionHM = null;
                }
                else
                {
                    SegmentedList <RectInt32> scans = new SegmentedList <RectInt32>(changedTiles.Count, 7);
                    scans.AddRange(changedTiles);
                    ScansHelpers.SortScansByTopLeft(scans);
                    ScansHelpers.ConsolidateSortedScansInPlace(scans);
                    actionHM = new BitmapHistoryMemento(this.historyMementoName, this.historyMementoImage, historyWorkspace, this.layerIndex, scans);
                    this.EnterCriticalRegion();
                    Work.ParallelForEach <TupleStruct <RectInt32, ISurface <ColorBgra> > >(WaitType.Blocking, changedTileGroups, delegate(TupleStruct <RectInt32, ISurface <ColorBgra> > tileInfo) {
                        using (ISurface <ColorBgra> surface = layerSurface.CreateWindow(tileInfo.Item1))
                        {
                            tileInfo.Item2.Render(surface, PointInt32.Zero);
                        }
                        tileInfo.Item2.Dispose();
                    }, WorkItemQueuePriority.Normal, null);
                    foreach (RectInt32 num in scans)
                    {
                        layer.Invalidate(num);
                    }
                }
            }, ApartmentState.MTA);

            commitTask.ResultAsync <Unit>().Receive(delegate(Result <Unit> r) {
                taskFinishedEvent.Set();
            }).Observe();
            if (!taskFinishedEvent.WaitOne(this.delayUntilProgressDialogMs))
            {
                string headerText       = PdnResources.GetString("ApplyRendererToBitmapLayerHistoryFunction.ProgressDialog.HeaderText");
                string headerTextFormat = PdnResources.GetString("ApplyRendererToBitmapLayerHistoryFunction.ProgressDialog.HeaderText.Format");
                using (TaskProgressDialog progressDialog = new TaskProgressDialog())
                {
                    Action updateHeaderText = delegate {
                        progressDialog.VerifyAccess();
                        if (!progressDialog.IsDisposed)
                        {
                            string text1;
                            double?progress = commitTask.Progress;
                            if (!progress.HasValue)
                            {
                                text1 = headerText;
                            }
                            else
                            {
                                text1 = string.Format(headerTextFormat, (progress.Value * 100.0).ToString("N0"));
                            }
                            progressDialog.HeaderText = text1;
                        }
                    };
                    progressDialog.Text             = this.historyMementoName;
                    progressDialog.Icon             = this.historyMementoImage.Reference.ToIcon();
                    progressDialog.CloseOnFinished  = true;
                    progressDialog.ShowCancelButton = false;
                    progressDialog.Task             = commitTask;
                    updateHeaderText();
                    commitTask.ProgressChanged += delegate(object s, ValueEventArgs <double?> e) {
                        PdnSynchronizationContext.Instance.EnsurePosted(updateHeaderText);
                    };
                    progressDialog.ShowDialog(historyWorkspace.Window);
                }
            }
            if (!commitTask.TaskResult.IsError)
            {
                return(actionHM);
            }
            if (commitTask.TaskResult.Error is OutOfMemoryException)
            {
                throw new OutOfMemoryException(null, commitTask.TaskResult.Error);
            }
            throw new AggregateException(null, commitTask.TaskResult.Error);
        }