public void TC_SimpleQueue()
        {
            var queue = new SimpleQueue <double>();

            var input = GetDoubleTestList();

            foreach (var item in input)
            {
                queue.Add(item, item);
            }
            Assert.AreEqual(input.Count, queue.GetSize());

            List <double> poppedItems = new List <double>();

            while (queue.GetSize() > 0)
            {
                poppedItems.Add(queue.RemoveMin());
            }
            Assert.IsTrue(CollectionsEquality.Equals(input, poppedItems));

            queue.Clear();
            Assert.AreEqual(0, queue.GetSize());
            Assert.AreEqual("Simple Queue", queue.GetName());
        }
Exemplo n.º 2
0
        static private void WriteLine(LogLevel level, string format, object [] args, Exception ex)
        {
            if (level != LogLevel.Always && cutoff_level < level)
            {
                return;
            }

            string msg_string = null;

            if (format != null)
            {
                msg_string = String.Format(format, args);
            }
            if (ex != null)
            {
                msg_string = String.Concat(msg_string,
                                           (msg_string != null ? "\n" : String.Empty),
                                           ex);
            }

            // Use the buffer only for multiline messages, which will mainly happen with exceptions
            if (ex != null && !msg_buffer.Add(msg_string))
            {
                msg_string = "(Repeated)";
                if (format != null)
                {
                    msg_string = String.Format(format, args);
                    int newline = msg_string.IndexOf('\n');
                    if (newline != -1)
                    {
                        msg_string  = msg_string.Substring(0, newline);
                        msg_string += " ...";
                    }
                }
                else
                {
                    msg_string = ex.Message;
                }

                msg_string = "(Repeated) " + msg_string;
            }

            // This only happens if Log.Initialize was never called.
            if (running_in_foreground && foreground_echo_writer == null)
            {
                foreground_echo_writer = Console.Out;
            }

            if (foreground_echo_writer != null)
            {
                foreground_echo_writer.Write(level);
                foreground_echo_writer.Write(": ");

                if (msg_string != null)
                {
                    foreground_echo_writer.WriteLine(msg_string);
                }
                foreground_echo_writer.Flush();
            }

            if (log_writer == null)             // i.e. if Log.Initialize has not been called
            {
                return;
            }

            StringBuilder prefix_builder;

            prefix_builder = new StringBuilder();
            prefix_builder.Append('\n');              // start w/ a newline
            prefix_builder.AppendFormat("{0:yyyyMMdd HH:mm:ss.ffff} {1:00000} ",
                                        DateTime.Now, Process.GetCurrentProcess().Id);
            prefix_builder.Append(program_identifier_truncated);

            prefix_builder.Append(' ');
            switch (level)
            {
            case LogLevel.Error:
                prefix_builder.Append("ERROR");
                break;

            case LogLevel.Warn:
                prefix_builder.Append(" WARN");
                break;

            case LogLevel.Debug:
                prefix_builder.Append("DEBUG");
                break;

            case LogLevel.Always:
                prefix_builder.Append(" INFO");
                break;

            default:
                prefix_builder.Append(" HUH?");
                break;
            }

            if (ex != null)
            {
                prefix_builder.Append(" EX");
            }
            prefix_builder.Append(": ");

            string prefix;

            prefix = prefix_builder.ToString();

            StringBuilder message;

            message = new StringBuilder();
            message.Append(prefix);
            message.Remove(0, 1);              // remove leading \n
            if (msg_string != null)
            {
                message.Append(msg_string);
            }
            message.Replace("\n", prefix);

            string message_str;

            message_str = message.ToString();

            lock (write_lock) {
                log_writer.WriteLine(message_str);
                if (ex != null && exception_writer != null)
                {
                    exception_writer.WriteLine(message_str);
                }
            }
        }
        const int SIMULATION_DURATION = 60 * 12; // one shift

        public void processPatients()
        {
            IQueue <Patient> waitingRoom = null;
            bool             usePriority = false;//orig false

            // create an array or list of ERTables
            ERTable[] tables = new ERTable[NUM_TABLES];

            // TODO -- populate the array with tables
            for (int i = 0; i < NUM_TABLES; i++)/////////////colin
            {
                tables[i] = new ERTable();
            }

            // this for loop will run twice, once with a SimpleQueue and once with the PriorityQueue
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("\nUsing Priority Queue: {0}", usePriority);
                int  totalExpired     = 0;
                int  totalPatients    = 0;
                int  maxWait          = 0;
                int  tmpMaxWait       = 0;
                int  totalWait        = 0;
                int  totalStay        = 0;
                int  maxWaitingInLine = 0;
                int  tmpWaitingInLine = 0;
                bool stillWorking     = false;


                // TODO -- create triage unit
                TriageUnit      triageUnit = new TriageUnit();
                Queue <Patient> frontDoor  = triageUnit.getNewPatients();

                // TODO set the waitingQueue to one or the other type Queue, depending on the value of usePriority
                // you will need to instantiate the appropriate Queue in the if/else statement.
                if (usePriority == true)
                {
                    waitingRoom = new PriorityQueue <Patient>();
                    while (frontDoor.Count > 0)
                    {
                        Patient p = frontDoor.Dequeue();
                        waitingRoom.Add(p.LastPossibleMoment, p);
                    }
                }
                else if (usePriority == false)
                {
                    waitingRoom = new SimpleQueue <Patient>();
                    while (frontDoor.Count > 0)
                    {
                        Patient p = frontDoor.Dequeue();
                        waitingRoom.Add(p.LastPossibleMoment, p);
                    }
                }
                // Reset Hospital clock
                Hospital.CurrentTime = 0;

                while (Hospital.CurrentTime < SIMULATION_DURATION || waitingRoom.Count > 0 || stillWorking)
                {
                    // TODO empty tables that are free
                    // look for table where ETC <= currentTime
                    // do not look for expired patients; if they made it to an ER table, they lived
                    for (int j = 0; j < NUM_TABLES; j++)
                    {
                        if (tables[j].ETC <= Hospital.CurrentTime)
                        {
                            tables[j].Clear();
                        }
                    }
                    // NOTE: do the following *ONLY* if currentTime < simulation duration, otherwise you'll never finish
                    // TODO: get list of new patients from triage unit
                    if (Hospital.CurrentTime < SIMULATION_DURATION)
                    {
                        // for each patient in the triage queue
                        // set IntakeTime for each patient to 'currentTime'
                        // place new patients into waiting room
                        // when placing in waiting room, priority is the patient's last possible moment
                        frontDoor = triageUnit.getNewPatients();
                        foreach (Patient p in frontDoor)
                        {
                            p.IntakeTime = Hospital.CurrentTime;
                        }
                        while (frontDoor.Count > 0)
                        {
                            Patient p = frontDoor.Dequeue();
                            waitingRoom.Add(p.LastPossibleMoment, p);
                        }
                    }
                    // TODO: check for maximum number in waiting room
                    if (waitingRoom.Count > 0)
                    {
                        tmpWaitingInLine = waitingRoom.Count;
                        if (tmpWaitingInLine > maxWaitingInLine)
                        {
                            maxWaitingInLine = tmpWaitingInLine;
                        }
                    }
                    // TODO: for every EMPTY tables, be careful here
                    // remove next patient from waiting room
                    // check for expired patients (count them)
                    // (if the patient has expired, you'll need to get another one)
                    // placing living patients on empty ER table
                    // update any accumulators, maximums, etc.
                    for (int d = 0; d < NUM_TABLES; d++)
                    {
                        if (tables[d].IsFree && waitingRoom.Count > 0)
                        {
                            Patient p = waitingRoom.Remove();
                            //currenttime > intaketime + timetolive
                            if (Hospital.CurrentTime > p.IntakeTime + p.TimeToLive)
                            {
                                //died
                                totalExpired++;
                                p = null;
                            }
                            else if (Hospital.CurrentTime < p.IntakeTime + p.TimeToLive)
                            {
                                tmpMaxWait = Hospital.CurrentTime - p.IntakeTime;
                                if (tmpMaxWait > maxWait)
                                {
                                    maxWait = tmpMaxWait;
                                }
                                totalWait += Hospital.CurrentTime - p.IntakeTime;
                                //totalStay =
                                p.TimeEnteringER = Hospital.CurrentTime;
                                totalStay       += p.TimeEnteringER + p.TimeForProcedure - p.IntakeTime;
                                tables[d].AddPatient(p);
                                totalPatients++;
                            }
                        }
                    }

                    stillWorking = false;
                    // TODO: Make certain ALL of the tables are free
                    // if any table is not free, set stillWorking to true
                    for (int d = 0; d < NUM_TABLES; d++)
                    {
                        if (!tables[d].IsFree)
                        {
                            stillWorking = true;
                        }
                    }

                    // set add 10 minutes to hospital time
                    Hospital.CurrentTime += 10;
                }

                // print parameters (num tables, duration, using priority queue)
                // print time, total patients, max waiting, average waiting, expired patients
                Console.WriteLine("Total Elapsed Time: {0,7}", Hospital.CurrentTime);
                Console.WriteLine("Total patients:     {0,7}", totalPatients);
                Console.WriteLine("Total Expired:      {0,7}", totalExpired);
                Console.WriteLine("Longest Wait:       {0,7}", maxWait);
                Console.WriteLine("Average Wait:       {0,7:N2}", (double)totalWait / totalPatients);
                Console.WriteLine("Average Stay:       {0,7:N2}", (double)totalStay / totalPatients);
                Console.WriteLine("Maximum waiting:    {0,7:N2}", maxWaitingInLine);

                usePriority = true;
            } // end for
        }     // end processPatients