/** * Lfu class */ class Lfu { java.util.Vector stack; int[] distanceString; /** * Lfu constructor */ public Lfu() { super(); stack = new java.util.Vector(); distanceString = new int[65]; // 64 different string space // distanceString[64] for page that have // not been referenced, distance infinity for (int i=0; i<65; i++) { distanceString[i] = 0; } } /** * To check is the page already in stack * yes: return the position * no: return -1 */ public int isInStack(int referencePage) { Page page; for (int j = 0; j < stack.size(); j++) { page = (Page) stack.elementAt(j); if (referencePage == page.getId()) { // reference page is j position away from // the top of the stack distanceString[j] ++; return j; } } return -1; } /** * For testing: to print out the contents in stack */ public void printStack() { Page page; for (int j=0; j= 0){ page = (Page) stack.elementAt(j); // increment the page count int count = page.getCount(); count++; page.setCount(count); // remove the page and push it on top of the stack stack.removeElementAt(j); stack.insertElementAt(page, 0); // sort the rest of the stack sortStack(1, j); } else { // add the page on the top of the stack page = new Page(pageReference[i], 1); stack.insertElementAt(page, 0); // sort the rest of the stack sortStack(1, stack.size()-1); // page have not been referenced, distance infinity distanceString[64]++; } //printStack(); i++; } // calculate page fault for (j=0; j= tmp.getCount()) { stack.insertElementAt(page, j); bEnd = false; break; } } if (bEnd) { stack.insertElementAt(page, end); } } } }