コード例 #1
0
        public void RemoveAtTest()
        {
            int count = _array.Count;

            _array.RemoveAt(3);
            Assert.Equal(count - 1, _array.Count);
            Assert.Equal(0, _array[3]);
            Assert.Throws <ArgumentOutOfRangeException>(() => _array.RemoveAt(count + 5));
        }
コード例 #2
0
        public void RemoveAtTest()
        {
            var a = new SparseArray <bool> {
                [5] = true, [90] = false
            };

            a.RemoveAt(5);
            Assert.That(a.Count, Is.EqualTo(1));
            a.RemoveAt(10);
            Assert.That(a.Count, Is.EqualTo(1));
        }
コード例 #3
0
        public void RemoveAt_OK()
        {
            var sa = new SparseArray <decimal>(2, new Dictionary <int, decimal>()
            {
                { 0, 1M }, { 1, 2M }
            });

            Assert.AreEqual(2, sa.Capacity, "Incorrect capacity.");
            Assert.AreEqual(2, sa.Count, "Incorrect count.");
            Assert.AreEqual(2, sa.Sparsity, "Incorrect sparsity.");
            Assert.AreEqual(1.0M, sa.SparsityPercent, "Incorrect sparsity percent.");
            Assert.IsTrue(sa.Keys.SequenceEqual(new List <int> {
                0, 1
            }), "Incorrect keys.");
            Assert.IsTrue(sa.ToArray().SequenceEqual(new[] { 1M, 2M }), "Unequal elements in sparse array");

            sa.RemoveAt(1);

            Assert.AreEqual(2, sa.Capacity, "Incorrect capacity after RemoveAt(...).");
            Assert.AreEqual(2, sa.Count, "Incorrect count after RemoveAt(...).");
            Assert.AreEqual(1, sa.Sparsity, "Incorrect sparsity after RemoveAt(...).");
            Assert.AreEqual(0.5M, sa.SparsityPercent, "Incorrect sparsity percent after RemoveAt(...).");
            Assert.IsTrue(sa.Keys.SequenceEqual(new List <int> {
                0
            }), "Incorrect keys after RemoveAt(...).");
            Assert.IsTrue(sa.ToArray().SequenceEqual(new[] { 1M, decimal.Zero }), "Unequal elements in sparse array after RemoveAt(...)");
        }
コード例 #4
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                // Prepare the data
                Queue <(long, long)> queue = new Queue <(long, long)>();
                for (int i = 0; i < _count; ++i)
                {
                    queue.Enqueue((i + 1, 1));
                }

                // Process the queue
                while (queue.TryDequeue(out var elf))
                {
                    if (!queue.TryDequeue(out var next))
                    {
                        // The elf is the last one, he won
                        return(elf.Item1.ToString());
                    }

                    // Steal the presents !
                    queue.Enqueue((elf.Item1, elf.Item2 + next.Item2));
                }

                return("not found");
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                // The same with another structure
                SparseArray <long> elves = new SparseArray <long>(_count);
                for (long i = 0; i < elves.Length; ++i)
                {
                    elves[i] = 1;
                }

                // Process the array
                long current  = 0;
                long opposite = elves.RealLength / 2;
                while (elves.RealLength > 1)
                {
                    elves[current] += elves[opposite];
                    elves.RemoveAt(opposite);
                    if (elves.RealLength % 2 == 0)
                    {
                        opposite = elves.Next(opposite, 2);
                    }
                    else
                    {
                        opposite = elves.Next(opposite, 1);
                    }
                    current = elves.Next(current);
                }

                return((current + 1).ToString());
            }

            return("");
        }
コード例 #5
0
ファイル: SparseArrayTest.cs プロジェクト: gisdevelope/aegis
        public void SparseArrayRemoveAtTest()
        {
            SparseArray <Int32> array = new SparseArray <Int32>(this.values);

            array.RemoveAt(1);
            array.RemoveAt(1);
            array.RemoveAt(3);

            array.Length.ShouldBe(this.values.Length - 3);

            array[0].ShouldBe(0);
            array[1].ShouldBe(15);
            array[2].ShouldBe(20);
            array[3].ShouldBe(30);

            // exceptions
            Should.Throw <ArgumentOutOfRangeException>(() => array.RemoveAt(-1));
            Should.Throw <ArgumentOutOfRangeException>(() => array.RemoveAt(array.Length));
        }
コード例 #6
0
        public void SparseArrayRemoveAtTest()
        {
            SparseArray <Int32> array = new SparseArray <Int32>(_values);

            array.RemoveAt(1);
            array.RemoveAt(1);
            array.RemoveAt(3);

            Assert.AreEqual(_values.Length - 3, array.Length);

            Assert.AreEqual(0, array[0]);
            Assert.AreEqual(2, array[1]);
            Assert.AreEqual(3, array[2]);
            Assert.AreEqual(0, array[3]);


            // exceptions

            Assert.Throws <ArgumentOutOfRangeException>(() => array.RemoveAt(-1));
            Assert.Throws <ArgumentOutOfRangeException>(() => array.RemoveAt(array.Length));
        }
コード例 #7
0
ファイル: Scene.cs プロジェクト: slagusev/AndEngine.net
        public virtual bool OnSceneTouchEvent(/* final */ TouchEvent pSceneTouchEvent)
        {
            //* final */ int action = pSceneTouchEvent.GetAction();
            MotionEvent action = pSceneTouchEvent.GetMotionEvent();
            // final bool isDownAction = action == MotionEvent.ACTION_DOWN;
            bool isDownAction = (action == (MotionEvent)MotionEvent.ActionPointer1Down);

            // final float sceneTouchEventX = pSceneTouchEvent.getX();
            float sceneTouchEventX = pSceneTouchEvent.X;
            // final float sceneTouchEventY = pSceneTouchEvent.getY();
            float sceneTouchEventY = pSceneTouchEvent.Y;

            if (this.mTouchAreaBindingEnabled && !isDownAction)
            {
                /* final */
                SparseArray <ITouchArea> touchAreaBindings = this.mTouchAreaBindings;
                /* final */
                ITouchArea boundTouchArea = touchAreaBindings[pSceneTouchEvent.GetPointerID()];

                /* In the case a ITouchArea has been bound to this PointerID,
                 * we'll pass this this TouchEvent to the same ITouchArea. */
                if (boundTouchArea != null)
                {
                    /* Check if boundTouchArea needs to be removed. */
                    switch (action.Action)
                    {
                    //TODO: this value was MotionEvent.ActionPointer1Up in Java.  Is it important to be pointer 1?
                    case MotionEventActions.PointerUp:
                    case MotionEventActions.Cancel:
                        touchAreaBindings.RemoveAt(pSceneTouchEvent.GetPointerID());
                        break;
                    }
                    /* final */
                    bool?handled = this.OnAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, boundTouchArea);
                    if (handled != null && handled.Value)
                    {
                        return(true);
                    }
                }
            }

            /* final */
            Scene childScene = this.mChildScene;

            if (childScene != null)
            {
                /* final */
                bool handledByChild = this.OnChildSceneTouchEvent(pSceneTouchEvent);
                if (handledByChild)
                {
                    return(true);
                }
                else if (this.mChildSceneModalTouch)
                {
                    return(false);
                }
            }

            /* First give the layers a chance to handle their TouchAreas. */
            {
                /* final */
                int layerCount = this.mLayerCount;
                /* final */
                ILayer[] layers = this.mLayers;
                if (this.mOnAreaTouchTraversalBackToFront)
                { /* Back to Front. */
                    for (int i = 0; i < layerCount; i++)
                    {
                        /* final */
                        ILayer layer = layers[i];
                        /* final */
                        //ArrayList<ITouchArea> layerTouchAreas = layer.getTouchAreas();
                        var layerTouchAreas = layer.GetTouchAreas();
                        /* final */
                        int layerTouchAreaCount = layerTouchAreas.Count;
                        if (layerTouchAreaCount > 0)
                        {
                            for (int j = 0; j < layerTouchAreaCount; j++)
                            {
                                /* final */
                                ITouchArea layerTouchArea = layerTouchAreas[j];
                                if (layerTouchArea.Contains(sceneTouchEventX, sceneTouchEventY))
                                {
                                    /* final */
                                    bool?handled = this.OnAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, layerTouchArea);
                                    if (handled != null && handled.Value)
                                    {
                                        /* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
                                         *  bind this ITouchArea to the PointerID. */
                                        if (this.mTouchAreaBindingEnabled && isDownAction)
                                        {
                                            this.mTouchAreaBindings[pSceneTouchEvent.GetPointerID()] = layerTouchArea;
                                        }
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                { /* Front to back. */
                    for (int i = layerCount - 1; i >= 0; i--)
                    {
                        /* final */
                        ILayer layer = layers[i];
                        /* final */
                        //ArrayList<ITouchArea> layerTouchAreas = layer.getTouchAreas();
                        var layerTouchAreas = layer.GetTouchAreas();
                        /* final */
                        int layerTouchAreaCount = layerTouchAreas.Count;
                        if (layerTouchAreaCount > 0)
                        {
                            for (int j = layerTouchAreaCount - 1; j >= 0; j--)
                            {
                                /* final */
                                ITouchArea layerTouchArea = layerTouchAreas[j];
                                if (layerTouchArea.Contains(sceneTouchEventX, sceneTouchEventY))
                                {
                                    /* final */
                                    bool?handled = this.OnAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, layerTouchArea);
                                    if (handled != null && handled.Value)
                                    {
                                        /* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
                                         *  bind this ITouchArea to the PointerID. */
                                        if (this.mTouchAreaBindingEnabled && isDownAction)
                                        {
                                            this.mTouchAreaBindings[pSceneTouchEvent.GetPointerID()] = layerTouchArea;
                                        }
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            /* final */
            //ArrayList<ITouchArea> touchAreas = this.mTouchAreas;
            List <ITouchArea> touchAreas = this.mTouchAreas;
            /* final */
            int touchAreaCount = touchAreas.Count;

            if (touchAreaCount > 0)
            {
                if (this.mOnAreaTouchTraversalBackToFront)
                { /* Back to Front. */
                    for (int i = 0; i < touchAreaCount; i++)
                    {
                        /* final */
                        ITouchArea touchArea = touchAreas[i];
                        if (touchArea.Contains(sceneTouchEventX, sceneTouchEventY))
                        {
                            /* final */
                            bool?handled = this.OnAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, touchArea);
                            if (handled != null && handled.Value)
                            {
                                /* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
                                 *  bind this ITouchArea to the PointerID. */
                                if (this.mTouchAreaBindingEnabled && isDownAction)
                                {
                                    this.mTouchAreaBindings[pSceneTouchEvent.GetPointerID()] = touchArea;
                                }
                                return(true);
                            }
                        }
                    }
                }
                else
                { /* Front to back. */
                    for (int i = touchAreaCount - 1; i >= 0; i--)
                    {
                        /* final */
                        ITouchArea touchArea = touchAreas[i];
                        if (touchArea.Contains(sceneTouchEventX, sceneTouchEventY))
                        {
                            /* final */
                            bool?handled = this.OnAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, touchArea);
                            if (handled != null && handled.Value)
                            {
                                /* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
                                 *  bind this ITouchArea to the PointerID. */
                                if (this.mTouchAreaBindingEnabled && isDownAction)
                                {
                                    this.mTouchAreaBindings[pSceneTouchEvent.GetPointerID()] = touchArea;
                                }
                                return(true);
                            }
                        }
                    }
                }
            }
            /* If no area was touched, the Scene itself was touched as a fallback. */
            if (this.mOnSceneTouchListener != null)
            {
                return(this.mOnSceneTouchListener.OnSceneTouchEvent(this, pSceneTouchEvent));
            }
            else
            {
                return(false);
            }
        }