// 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);
            }
        }
        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);
        }
Example #3
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);
        }
        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());
        }
        // 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);
            }
        }
        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 Test1()
        {
            var t  = SplayHeap <string> .Empty;
            var x1 = SplayHeap <string> .Insert("C", t);

            var x2 = SplayHeap <string> .Insert("B", x1);

            Assert.AreEqual("[B, [C]]", DumpHeap(x2));
        }
        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 FindMinTest2()
        {
            var t0 = SplayHeap <int> .Empty;
            var t1 = SplayHeap <int> .Insert(5, t0);

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

            Assert.AreEqual(5, result);
        }
        private int InsertAction(int count)
        {
            for (var i = 0; i < count; i++)
            {
                var word = NextWord(10);
                _set = SplayHeap <string> .Insert(word, _set);
            }

            return(count);
        }
        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));
        }
        public void MergeTest()
        {
            const string data1 = "What's in a name?";
            var          ts1   = data1.Split().Aggregate(SplayHeap <string> .Empty, (current, word) => SplayHeap <string> .Insert(word, current));

            const string data2 = "That which we call a rose by any other name would smell as sweet";
            var          ts2   = data2.Split().Aggregate(SplayHeap <string> .Empty, (current, word) => SplayHeap <string> .Insert(word, current));

            var t = SplayHeap <string> .Merge(ts1, ts2);

            Assert.AreEqual("[[[[a], a, [any, [as]]], by, [[call], in, [name]]], name?, [other, [[[[rose], smell], sweet, [That, [we]]], What's, [[which], would]]]]", DumpHeap(t));
        }
        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));
        }
 private void InsertAction()
 {
     for (var i = 0; i < Count; i++)
     {
         var word = NextWord(10);
         _lockObject.EnterWriteLock();
         try
         {
             _set = SplayHeap <string> .Insert(word, _set);
         }
         finally
         {
             _lockObject.ExitWriteLock();
         }
     }
 }
        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));
        }
Example #16
0
        public bool Insert(string word)
        {
            if (_token.IsCancellationRequested)
            {
                return(false);
            }

            // 132 ms, 3,000 calls
            lock (_lockObject)
            {
                // 13 ms, 3,000 calls
                _set = SplayHeap <string> .Insert(word, _set);

                // 2 ms, 3,000 calls
                Monitor.Pulse(_lockObject);
            }

            return(true);
        }
        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);
        }
        // 132 ms, 10 calls
        private void InsertAction()
        {
            for (var i = 0; i < Count; i++)
            {
                // 18 ms, 3,000 calls
                var word = NextWord(10);
                while (true)
                {
                    var localCopy = _set;
                    Thread.MemoryBarrier();
                    // 99 ms, 13,072 calls
                    var newSet = SplayHeap <string> .Insert(word, localCopy);

                    // 3 ms, 13,072 calls
                    var oldSet = Interlocked.CompareExchange(ref _set, newSet, localCopy);
                    if (ReferenceEquals(oldSet, localCopy))
                    {
                        // 3,000 calls
                        break;
                    }
                }
            }
        }
        // 158 ms, 10 calls
        private void InsertAction()
        {
            for (var i = 0; i < Count; i++)
            {
                // 4 ms, 3,000 calls
                var word = NextWord(10);

                // 106 ms, 3,000 calls
                _semaphore.Wait();
                try
                {
                    Interlocked.MemoryBarrier();
                    // 43 ms, 3,000 calls
                    var newSet = SplayHeap <string> .Insert(word, _set);

                    Interlocked.Exchange(ref _set, newSet);
                }
                finally
                {
                    _semaphore.Release();
                }
            }
        }
        public void FindMinTest1()
        {
            var t = SplayHeap <int> .Empty;

            AssertThrows <ArgumentNullException>(() => SplayHeap <int> .FindMin(t));
        }
        public void Test2()
        {
            const string words = "What's in a name? That which we call a rose by any other name would smell as sweet";
            var          ts    = words.Split().Aggregate(SplayHeap <string> .Empty, (current, word) => SplayHeap <string> .Insert(word, current));

            Assert.AreEqual("[[[[[[a], a], any], as, [by, [[call, [in]], name, [name?]]]], other, [[rose], smell]], sweet, [[That, [[we], What's, [which]]], would]]", DumpHeap(ts));
        }