void LRUCache::fetchPage(int pageNumber) { 
	/* find the page in the map */
	unordered_map< int, Node* >::const_iterator it = directAccess.find(pageNumber); 
		
	/* if the page is found in the map */
	if (it != directAccess.end()) {   
		/* move the page on to the head of the doubly list */
		dlist.moveToHead( (Node*)it->second); 
	} 
	else { 
		/* if size of list is full */
		if (dlist.size() == cacheSize-1) 
		   dlist.removeTail(); 
			
		/* add the node to the head of doubly list */
		Node* node = dlist.addNode(pageNumber); 
		/* add the node in the map */
		directAccess.insert(pair< int, Node* >(pageNumber,node)); 
	}
		
	dlist.print(); 
}
Exemple #2
0
void processFile (string filename) {
    stringstream ss;
    int value = 0;
    DLList* list = NULL;
    char firstCharacter;
    ifstream fin(filename.c_str());
    if(!fin.fail()) {
        string nextline;
        while(!fin.eof()) {
            getline(fin, nextline);
            firstCharacter = nextline[0];
            switch(firstCharacter) {
            case '#':
                break;
            case 'C':
                if(list != NULL) { //if there was a previous list delete it
                    delete list;
                }
                else {
                    //creates a new list of type DLList
                    list = new DLList();
                    cout << "LIST CREATED" << endl;
                }
                break;
            case 'X':
                //if no list was created yet create list first
                if(list == NULL) {
                    cout << "MUST CREAT LIST INSTANCE" << endl;
                } //if there was a list clear it with the clear function
                else {
                    list->clear();
                    cout << "LIST CLEARED" << endl;
                }
                break;
            case 'D':
                //if there is a list deleted it
                if(list!= NULL) {
                    delete list;
                    cout << "LIST DELETED" << endl;
                }//if there isn't show warning
                else
                    cout << "MUST CREAT LIST INSTANCE" << endl;
                break;
            case 'I':
                if(list == NULL) {
                    cout << "MUST CREAT LIST INSTANCE" << endl;
                }
                else {
                    ss.str(nextline.substr(2));//gets the next whole value after the first value on a line
                    ss >> value;//puts the from input into an int
                    list->insert(value);//inserts the value into a node on the list
                    cout << "VALUE " << value << " INSERTED" << endl;
                    ss.clear();
                    ss.str("");
                }
                break;
            case 'F':
                ss.str(nextline.substr(2));
                ss >> value;
                list->insertHead(value);//inserts the value from input into the first node
                cout << "VALUE " << value << " ADDED TO HEAD" << endl;
                ss.clear();
                ss.str("");
                break;
            case 'B':
                ss.str(nextline.substr(2));
                ss >> value;
                list->insertTail(value);
                cout << "VALUE " << value << " ADDED TO TAIL" << endl;
                ss.clear();
                ss.str("");
                break;
            case 'A':
                if(list == NULL) {
                    cout << "VALUE NULL AT HEAD" << endl;
                }
                else {
                    list->getHead();
                    cout << "VALUE " << list->getHead() << " AT HEAD" << endl;
                }
                break;
            case 'Z':
                list->getTail();
                cout << "VALUE "  << list->getTail() << " AT TAIL" << endl;
                break;
            case 'T':
                if(list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                }
                else if (list->getSize() > 0) {
                    list->removeHead();
                    cout << "REMOVED HEAD" << endl;
                }
                else {
                    cout << "LIST EMPTY" << endl;

                }
                break;
            case 'K':
                if(list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                }
                else if(list->getSize() > 0) {
                    list->removeTail();
                    cout << "REMOVED TAIL" << endl;
                }
                else
                    cout << "LIST EMPTY" << endl;
                break;
            case 'E':
                ss.str(nextline.substr(2));
                ss >> value;
                list->removeFirst(value);
                cout << "VALUE " << value << " ELIMINATED" << endl;
                ss.clear();
                ss.str("");
                break;
            case 'R':
                break;
            case 'G':
                break;
            case 'N':
                break;
            case 'P':
                list->toString();
                break;
            }
        }
        cout << "INPUT FINISHED" << endl;
        fin.close();
    }