Example #1
0
        private void ProcessData(Tuple <Stack <byte>, Stack <Stack <int> > > data)
        {
            var sw = Stopwatch.StartNew();

            var commands  = data.Item1;
            var arguments = data.Item2;

            int argOffset  = 0;
            int listOffset = 0;

            // Prepare the heap
            var heap        = new FibonacciHeap <int, int>();
            int insertCount = GetNextArg(ref argOffset, arguments, ref listOffset);

            NodeItem <int, int>[] insertedNodes = new NodeItem <int, int> [insertCount];
            int deleteDepthCount = 0;
            int deleteCount      = 0;

            // Do insert commands
            int id = 0, key = 0;
            NodeItem <int, int> node;

            foreach (byte command in commands)
            {
                if (command != DelKey)
                {
                    id  = GetNextArg(ref argOffset, arguments, ref listOffset);
                    key = GetNextArg(ref argOffset, arguments, ref listOffset);
                }

                switch (command)
                {
                case InsKey:
                    node = heap.Add(key, id);
                    Debug.Assert(insertedNodes[id] == null);
                    insertedNodes[id] = node;
                    break;

                case DelKey:
                    node = heap.PeekMin();
                    Debug.Assert(insertedNodes[node.Value] != null);
                    insertedNodes[node.Value] = null;
                    heap.DeleteMin();

                    deleteDepthCount += heap.LastConsolidateDepth;
                    deleteCount++;
                    break;

                case DecKey:
                    node = insertedNodes[id];

                    if (node == null || key > node.Key)
                    {
                        break;
                    }

                    heap.DecreaseKey(node, key);
                    break;
                }
            }

            // Cleanup and store the measurements
            //heap.Clear(); // Disassembles tree node pointers .... not a good idea with a GC...

            sw.Stop();

            float avgDeleteDepthCount = 0;

            if (deleteCount > 0)
            {
                avgDeleteDepthCount = deleteDepthCount / (float)deleteCount;
            }

            lock (_results)
                _results.Add(deleteCount, avgDeleteDepthCount);

            Interlocked.Increment(ref _currentJobsDone);
            Log("{0}/{1} done/waiting :: {2:F} sec :: {3} inserts :: {4}/{5:F} deletes/delete depth average",
                _currentJobsDone,
                Buffer.WaitingItemCount,
                sw.ElapsedMilliseconds * 0.001,
                insertCount,
                deleteCount,
                avgDeleteDepthCount);
        }