int main (int argc, const char* argv[]) { /* * argc should be 2 * arvg[1] is the size of the page table/ amount of memory avaiable/ the number of page frames */ if (argc != 2) { fprintf(stderr, "Not enough input arguments\n"); return 1; } int page_frames = atoi(argv[1]); char buffer[BUFFER_SIZE]; int page_faults = 0; //counter of page faults int memory_accesses = 0; //counter of memory accesses (lines in file) //create DEQUE DEQUE* page_table = createDeque(); //loop through the file and grab the numbers in there while (fgets(buffer, BUFFER_SIZE, stdin) != NULL ) { int access; //find the first integer in the line and store in x int n = sscanf(buffer, "%d", &access); //only try and access the page if the value is a number if (n > 0){ memory_accesses++; //if this page is already in the table, do nothing if (findNode(page_table, access) != NULL) { setRef(page_table,access); } //try and add the this page to the queue (oldest in front, newest in back) //if the queue is already at capacity, search for a page that we can remove else if (numItems(page_table) == page_frames) { /*FIND AND REPLACE PAGE*/ while ( getFirstRef(page_table) != 0) { //move the node with the value of first to the back of the deque moveToBack(page_table, getFirst(page_table)); } //remove the first node because it is unreferenced removeFirst(page_table); //add the new node at the end of the queue addLast(page_table, access); page_faults++; printf("PAGE FAULT: %d\n", access); } //if the page is not already in the list and there is room to add it, place it at the end of the list else { addLast(page_table, access); } } } //printf("memory accesses: %d\n",memory_accesses); //printf("page faults: %d\n",page_faults); }
bool Tracks::existsItemAt(int n) { return ((n > 0) && (n < numItems())); }
int main(void) { DEQUE **digit; /* index of deques */ digit = (DEQUE **) malloc(sizeof(DEQUE*) * RADIX); int i,j,k; /* generic counter variables */ /* allocate space for the deques; Complexity: O(1) */ for (i = 0; i < RADIX; i++) digit[i] = createDeque(); int iteration; /* number of iterations needed for sorting */ int num; /* placeholder for temporary integer values */ int max = 0; /* stores the maximum of numbers entered */ /* User Integer Insertion -- while Ctrl-D is not pressed... * Integer cannot be negative. It also keeps track of * the maximum value of the integers entered. * * Complexity: O(n); */ while (scanf("%d",&num) != EOF) { assert (num >= 0); addFirst(digit[0],num); if (num > max) max = num; } /* Calculating 'iteration' a.k.a. max # of digits to sort */ iteration = (int) ceil( log(max+1) / log(RADIX) ); /* THE ACTUAL SORTING ALGORITHM * Sorting by each digit, starting with the 'ones' digit (in a * base ten number system), it traverses each digit's deque, removing * the number of elements originally placed in them and reassigning * each number the deque corresponding to the next digit's value. * * [EXAMPLE: 112 is taken out of '2' deque and placed in the '1' deque * when sorting by the second digit(to the left)] * * Complexity: ~ O(n) */ for (i = 0; i < iteration; i++) { /* Divisor precalculated for ease */ int divisor = 1; /* will help determine which digit to eval */ for (j = 0; j < i; j++) divisor *= RADIX; /* Taking values out and placing them back... * Complexity: O(n) */ for (j = 0; j < RADIX; j++) { /* 'nItems' will help ensure only old values*/ int nItems = numItems(digit[j]); while (nItems > 0) { num = removeFirst(digit[j]); /* k is the appropriate digit of 'num' */ k = (num / divisor) % RADIX; addLast(digit[k],num); nItems--; } } } /* Time to print things out; Complexity: O(n) */ for (i = 0; i < RADIX; i++) { int nItems = numItems(digit[i]); for (j = 0; j < nItems; j++) { num = removeFirst(digit[i]); printf("%d ",num); } /* Deallocate memory for each deque */ destroyDeque(digit[i]); } /* Finally, free the index. */ free(digit); return 0; }
bool Chapters::existsItemAt(int n) { return ((n > 0) && (n < numItems())); }
int Chapters::find(int ID) { for (int n=0; n < numItems(); n++) { if (itemAt(n).ID() == ID) return n; } return -1; }
int main(void) { int i, input, m, digits, exp; DEQUE *list, *dp[r]; //1 deque for each digit //make a deque to hold a list of numbers m = 0; list = createDeque(); //make an array of deques to sort by digit for(i = 0; i < r; i++) dp[i] = createDeque(); //read in non-negative intergers while(scanf("%d", &input) == 1) { if(input >= 0) { addLast(list, input); //keep track of max if(input > m) m = input; } else { printf("List cannot contain negative numbers.\n"); return -1; } } exp = 1; //number of iterations through loop digits = ceil(log(m + 1)/log(r)); //Sort list into separate deques by digit while(digits > 0) { //place in respective buckets while(numItems(list) > 0) { input = removeFirst(list); addLast(dp[(input/exp) % r], input); } //Take sorted numbers and place back into list for(i = 0; i < r; i++) { while(numItems(dp[i]) > 0) addLast(list, removeFirst(dp[i])); } exp *= r; //increase exp to check next digit digits--; //decrease after check each digit } //Print the list while(numItems(list) > 0) printf("%d\n", removeFirst(list)); return 1; }
void synexit() { clearItems(); printf( "alive : %d /%d.\n", _nIt(), numItems() ); // asio.unloadDriver(); }