Beispiel #1
0
        public static IEnumerable <Job> AssignJobs(int threadCount, long[] jobs)
        {
            long currentTime = 0;
            var  queue       = new MinBinaryHeap <Job>(threadCount);
            var  initials    = new Queue <int>(Enumerable.Range(0, threadCount));

            //Process Results
            var results = new List <Job>();
            int id;

            foreach (var processingTime in jobs)
            {
                var q = queue.Max();
                if (initials.Count > 0 && (q == null || q.EndOfProcessing > currentTime))
                {
                    //Process job on unused thread
                    //Do not Dequeue max since it does not exist or has not finished processing yet
                    id = initials.Dequeue();
                }
                else
                {
                    //Dequeue Max and use that thread to process new job
                    queue.ExtractMax();
                    currentTime = q.EndOfProcessing;
                    id          = q.ThreadId;
                }
                //adding job now since reporting on start time not end of processing time
                var job = new Job(id, currentTime, processingTime);
                results.Add(job);
                queue.Insert(job);
            }

            return(results);
        }
Beispiel #2
0
        public static IEnumerable <Job> AssignJobs(int threadCount, long[] jobs)
        {
            long currentTime = 0;
            var  queue       = new MinBinaryHeap <Job>(threadCount);
            var  initials    = new Queue <int>(Enumerable.Range(0, threadCount));

            //Process Results
            var results = new List <Job>();
            int id;

            foreach (var processingTime in jobs)
            {
                var q = queue.Max();
                if (initials.Count > 0 && (q == null || q.EndOfProcessing > currentTime))
                {   //Add job as un used initial thread
                    id = initials.Dequeue();
                }
                else
                {
                    queue.ExtractMax();
                    currentTime = q.EndOfProcessing;
                    id          = q.ThreadId;
                }
                var job = new Job(id, currentTime, processingTime);
                results.Add(job);
                queue.Insert(job);
            }

            return(results);
        }
        public static BinaryHeap BuildHeap(int n, int[] data)
        {
            var current = data.Take(n).ToArray();
            var heap    = new MinBinaryHeap(current);

            for (int i = n / 2; i >= 0; i--)
            {
                heap.SiftDown(i);
            }
            return(heap);
        }
Beispiel #4
0
        public static IList <string> Answer(IList <string> inputs)
        {
            var n    = int.Parse(inputs[0]);
            var data = inputs[1]
                       .Split(new[] { ' ' })
                       .Take(n)
                       .Select(int.Parse)
                       .ToArray();

            var heap        = MinBinaryHeap.Build(n, data);
            var countResult = new[] { heap.Swaps.Count.ToString() };
            var swapResults = heap.Swaps.Select((pair) => string.Format("{0} {1}", pair.Item1, pair.Item2));

            return(countResult.Concat(swapResults).ToArray());
        }