//+----------------------------------------------------------------------+
//|								    DAPsim								 |
//| Dean Morrison												11/30/06 |
//| University of Florida												 |
//| Department of Electrical and Computer Engineering					 |
//+----------------------------------------------------------------------+
//| Description: DAPsim is a trace-driven simulator developed in C++ for |
//| the purpose of designing, testing, and evaluating demand prepaging 	 |
//| policies.  It currently implements my proposed concept of Dynamically|
//| Adaptive Prepaging, but may be modified to evaluate many other 		 |
//| policies.															 |
//|																		 |
//| Reuse Policy: Use of this software for personal or instructional	 |
//| purposes is granted without fee provided that the source bears a	 |
//| clear and complete citation.										 |
//+----------------------------------------------------------------------+


#include "main.h"
#include "LRU_pageQ.h"

LRU_pageQ::LRU_pageQ()
{
	size = 0;
	maxSize = DEFAULT_MAX_MEM_SIZE;
}

LRU_pageQ::LRU_pageQ(int newMaxSize)
{
	size = 0;
	maxSize = newMaxSize;
}

void LRU_pageQ::setMaxSize(int newMaxSize)
{
	maxSize = newMaxSize;
}

int LRU_pageQ::getSize(void)
{
	size = queue.size();
	return size;
}

int LRU_pageQ::getMaxSize(void)
{
	return maxSize;
}

LRU_pageQ_entry LRU_pageQ::getLRU_entry(void)
{
	return queue.back();
}

LRU_pageQ_entry LRU_pageQ::getMRU_entry(void)
{
	return queue.front();
}

void LRU_pageQ::pushMRU(unsigned long int newPage)
{
	if ((queue.size() + 1) > maxSize) return;	
	LRU_pageQ_entry newEntry;
	newEntry.setPageNumber(newPage);
	newEntry.setLRUValue(0);
	newEntry.setResidency(true);
	//newEntry.setHistValue(0);
	queue.push_front(newEntry);

	// record new queue size
	size = queue.size(); 
}

LRU_pageQ_entry LRU_pageQ::popLRU(void)
{
	LRU_pageQ_entry LRUentry = queue.back();
	queue.pop_back();

	// record new queue size
	size = queue.size();

	return LRUentry;
}

void LRU_pageQ::evictPage(void)
{
	popLRU();

	// record new queue size
	size = queue.size(); 
}

list<LRU_pageQ_entry> * LRU_pageQ::getQueue(void)
{
	return &queue;
}

/*
void LRU_pageQ::printQueueEntries(void)
{
	cout << "\tPage #\tLRU Value\tHist Value\n";
	cout << "\t------\t---------\t----------\n";
	
	int elementNum = 1;
	list<LRU_pageQ_entry>::iterator position;
    for (position = queue.begin(); position != queue.end(); ++position) {
		if (elementNum == 1) cout << "    MRU\t";
		else if (elementNum == queue.size()) cout << "    LRU\t";
		else cout << "\t";
		cout << (*position).getPageNumber() << "\t";
		cout << (*position).getLRUValue() << "\t\t";
		//cout << (*position).getHistValue() << "\n";
		cout << histogram[elementNum - 1] << "\n";
		elementNum++;
	}
}
*/

/*
void LRU_pageQ::clearHistogram(void)
{
	int index;
	for (index = 0; index < maxSize; index++) {
		histogram[index] = 0;
	}
}

void LRU_pageQ::incHistogram(int index)
{
	histogram[index] += 1;
}
*/

bool LRU_pageQ::pagePresentInQ(unsigned long int pageNumber)
{
	bool pagePresent = false;
	list<LRU_pageQ_entry>::iterator position;
    for (position = queue.begin(); position != queue.end(); ++position) {
        if ((*position).getPageNumber() == pageNumber) {
			pagePresent = true;
			break;
		}
    }
	
	return pagePresent;
}