Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of STL Queues

Status
Not open for further replies.

icpsvt

Programmer
Aug 10, 2004
7
US
Hi, small problem I'm having.

I've created an array of queues:
Code:
queue<Process> pqueue[NUM_PRIORITY_LEVELS];
I know I could have used a vector of queues, but I think this will be easier to debug.

I can insert elements into the first element of the array:
Code:
pqueue[0].push(p);
But when I attempt to push into any other element, it doesn't put them in.
ie,
Code:
pqueue[2].push(p);  //executes, but it doesn't actually appear
I'm using Visual Studio .NET 2003. In the debugger, it shows pqueue[0].c's _MAP as being referenced, but any of the other elements _MAP is shown as an undefined value.

Why? And How do I fix it?

Thanks for any help,
Jason

Code:
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include "EXPQueue.h"
#include "Process.h"
#include "state.h"
#include "SchedulerProcess.h"
using namespace std;


//EXPQueue::EXPQueue() : ReadyState("Ready State (EXP)"){
EXPQueue::EXPQueue() : state("Ready State (EXP)"){
    for(int i=0; i<NUM_PRIORITY_LEVELS; i++)
        this->pqueue[i].size();
}

EXPQueue::~EXPQueue(){
}


// What to do when the clock ticks
void EXPQueue::clockTick(long currentTime) {
    this->currentTime = currentTime;


}


// Store an existing process in the right queue
void EXPQueue::storeIntoQueue(Process p) {
    //first determine what the new priority level should be and set it
    int level = p.getTimeSliceLevel();
    int start = p.getTimeSliceStart();
    int end = p.getTimeSliceEnd();
    int runTime = (int)(pow(2,level)*10);
    int actualTime = end - start;
    //runTime is how long it could have run for
    if(runTime == actualTime) {
        if(level<NUM_PRIORITY_LEVELS)
            p.setTimeSliceLevel(++level);
    } 
    else if(((double)actualTime/(double)runTime)<.5)
        if(level>0)
            p.setTimeSliceLevel(--level);


    //then store it in the right level at the end
    this->pqueue[p.getTimeSliceLevel()].push(p);
}

// Load what should be the next process from the Queue
 Process EXPQueue::loadFromQueue() {
    Process p;
    // If the queues are not empty, load the first process, and store a copy in copy
    if(!this->isEmpty()) {
        p = this->getFront();    
        this->copy = p;
    }
    return p;
}

 //Check if there are any processes
 bool EXPQueue::isEmpty() {
     for(int i=0; i<NUM_PRIORITY_LEVELS; i++) 
        if(!this->pqueue[i].empty()) 
            return false;

    return true;
 }

 //Check if there is more work to do (Keeps syntax similar in opening loop)
 bool EXPQueue::getMoreToDo() {
    for(int i=0; i<NUM_PRIORITY_LEVELS; i++) 
            if(!this->pqueue[i].empty()) 
                return true;

    return false;
 }

 //Return a copy of what's up front
 Process EXPQueue::getFront() {
    Process p;
    //Get the first available process
    for(int i=0; i<NUM_PRIORITY_LEVELS; i++) 
        if(!this->pqueue[i].empty()) {
            p = this->pqueue[i].front();
            this->pqueue[i].pop();
            return p;
        }
    return p;
 }

// Describe the state
string EXPQueue::toString() {
    char temp[ARRAY_SIZE];
    char temp2[20];
    int nums[8];
    
    int size = 0;
    for(int i=0; i<NUM_PRIORITY_LEVELS; i++)
        size += this->pqueue[i].size();

    string info = this->name + ": Queued Processes = ";
    info += itoa(size, temp, NUM_BASE);

    if(!this->isEmpty()) {
        info += this->getFront().toString();
        for(int i=0; i<NUM_PRIORITY_LEVELS; i++)
            nums[i] = this->pqueue[i].size();
    }
    sprintf(temp2, "%d %d %d %d %d %d %d %d",nums[0],nums[1],nums[2],nums[3],nums[4],nums[5],nums[6],nums[7]);
    info += temp2;

    return info + "\n";
}
 
Your getFront() member function has (unwilling?) side effect: it removes the last element of the queue by pop() member function. May be you want top() member function?..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top