Пример #1
0
        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);
        }
Пример #2
0
        private static string DumpHeap <T>(SplayHeap <T> .Heap heap) where T : IComparable <T>
        {
            if (SplayHeap <T> .IsEmpty(heap))
            {
                return("\u2205");
            }

            var result = new StringBuilder();

            result.Append("[");

            if (!SplayHeap <T> .IsEmpty(heap.A))
            {
                result.Append(DumpHeap(heap.A));
                result.Append(", ");
            }

            result.Append(heap.X);

            if (!SplayHeap <T> .IsEmpty(heap.B))
            {
                result.Append(", ");
                result.Append(DumpHeap(heap.B));
            }

            result.Append("]");
            return(result.ToString());
        }
        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);
            }
        }
        // 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);
            }
        }
Пример #5
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);
            }
        }
Пример #7
0
        public void EmptyTest()
        {
            var t = SplayHeap <string> .Empty;

            Assert.IsTrue(SplayHeap <string> .IsEmpty(t));
            Assert.AreEqual("∅", DumpHeap(t));

            var t1 = SplayHeap <string> .Insert("C", t);

            Assert.IsFalse(SplayHeap <string> .IsEmpty(t1));
        }
        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);
        }