private void RemoveAction()
        {
            for (var i = 0; i < Count; i++)
            {
                SplayHeap <string> .Heap localCopy;
                while (true)
                {
                    _lockObject.EnterWriteLock();
                    try
                    {
                        if (!SplayHeap <string> .IsEmpty(_set))
                        {
                            localCopy = _set;
                            _set      = SplayHeap <string> .DeleteMin(localCopy);

                            break;
                        }
                    }
                    finally
                    {
                        _lockObject.ExitWriteLock();
                    }

                    Thread.Yield();
                }

                var unused = SplayHeap <string> .FindMin(localCopy);
            }
        }
        public void DeleteLotsOfMinsTest()
        {
            const int size   = 1000;
            var       random = new Random(3456);
            var       heap   = SplayHeap <int> .Empty;

            for (var i = 0; i < size; i++)
            {
                heap = SplayHeap <int> .Insert(random.Next(size), heap);
            }

            var last  = 0;
            var count = 0;

            while (!SplayHeap <int> .IsEmpty(heap))
            {
                var next = SplayHeap <int> .FindMin(heap);

                heap = SplayHeap <int> .DeleteMin(heap);

                Assert.IsTrue(last <= next);
                last = next;
                count++;
            }
            Assert.AreEqual(size, count);
        }
        // 91 ms, 10 calls
        private void RemoveAction()
        {
            for (var i = 0; i < Count; i++)
            {
                SplayHeap <string> .Heap localCopy;
                while (true)
                {
                    localCopy = _set;
                    Thread.MemoryBarrier();
                    // 13 ms, 66,042 calls
                    if (SplayHeap <string> .IsEmpty(localCopy))
                    {
                        Thread.Yield();
                        continue;
                    }

                    // 15 ms, 7,594 calls
                    var newSet = SplayHeap <string> .DeleteMin(localCopy);

                    // 2 ms, 7,594 calls
                    var oldSet = Interlocked.CompareExchange(ref _set, newSet, localCopy);
                    if (ReferenceEquals(oldSet, localCopy))
                    {
                        break;
                    }
                }

                // 3,000 calls
                var unused = SplayHeap <string> .FindMin(localCopy);
            }
        }
Beispiel #4
0
        public bool Remove(out string item)
        {
            // 58 ms, 3,000 calls
            SplayHeap <string> .Heap localCopy;
            lock (_lockObject)
            {
                while (!_token.IsCancellationRequested && SplayHeap <string> .IsEmpty(_set))
                {
                    // 33 ms, 1,609 calls
                    Monitor.Wait(_lockObject);
                }

                if (SplayHeap <string> .IsEmpty(_set))
                {
                    item = default(string);
                    return(false);
                }

                // 2 ms, 3,000 calls
                localCopy = _set;
                _set      = SplayHeap <string> .DeleteMin(localCopy);
            }

            // 2 ms, 3,000 calls
            item = SplayHeap <string> .FindMin(localCopy);

            return(true);
        }
        // 329 ms, 10 calls
        private void RemoveAction()
        {
            for (var i = 0; i < Count; i++)
            {
                SplayHeap <string> .Heap localCopy;
                while (true)
                {
                    // 294 ms, 3,000 calls
                    _semaphore.Wait();
                    try
                    {
                        localCopy = _set;
                        Interlocked.MemoryBarrier();
                        if (SplayHeap <string> .IsEmpty(localCopy))
                        {
                            Thread.Yield();
                            continue;
                        }

                        // 4 ms, 3,000 calls
                        var newSet = SplayHeap <string> .DeleteMin(localCopy);

                        Interlocked.Exchange(ref _set, newSet);
                        break;
                    }
                    finally
                    {
                        _semaphore.Release();
                    }
                }

                // 3 ms, 3,000 calls
                var unused = SplayHeap <string> .FindMin(localCopy);
            }
        }
        public void DeleteMinTest2()
        {
            var t0 = SplayHeap <int> .Empty;
            var t1 = SplayHeap <int> .Insert(5, t0);

            var result = SplayHeap <int> .DeleteMin(t1);

            Assert.AreEqual("∅", DumpHeap(result));
        }
        public void DeleteMinTest5()
        {
            var t0 = SplayHeap <int> .Empty;
            var t1 = SplayHeap <int> .Insert(3, t0);

            var t2 = SplayHeap <int> .Insert(5, t1);

            var t3 = SplayHeap <int> .Insert(6, t2);

            var result = SplayHeap <int> .DeleteMin(t3);

            Assert.AreEqual("[5, [6]]", DumpHeap(result));
        }
        public void Test3()
        {
            var heap = SplayHeap <int> .Empty;

            for (var i = 1; i < 8; i++)
            {
                heap = SplayHeap <int> .Insert(i, heap);
            }
            Assert.AreEqual("[[[[[[[1], 2], 3], 4], 5], 6], 7]", DumpHeap(heap));

            var x = SplayHeap <int> .Insert(0, heap);

            Assert.AreEqual("[0, [[[[1], 2, [3]], 4, [5]], 6, [7]]]", DumpHeap(x));

            var y = SplayHeap <int> .DeleteMin(x);

            Assert.AreEqual("[[[[1], 2, [3]], 4, [5]], 6, [7]]", DumpHeap(y));
        }
        private int RemoveAction(int count)
        {
            var i = 0;

            while (i < count)
            {
                if (SplayHeap <string> .IsEmpty(_set))
                {
                    return(i);
                }

                var localCopy = _set;
                _set = SplayHeap <string> .DeleteMin(localCopy);

                var unused = SplayHeap <string> .FindMin(localCopy);

                i++;
            }

            return(i);
        }
        public void DeleteMinTest1()
        {
            var t = SplayHeap <int> .Empty;

            AssertThrows <ArgumentNullException>(() => SplayHeap <int> .DeleteMin(t));
        }