/*===========================================================================* * alloc_pages * *===========================================================================*/ static phys_bytes alloc_pages(int pages, int memflags) { phys_bytes boundary16 = 16 * 1024 * 1024 / VM_PAGE_SIZE; phys_bytes boundary1 = 1 * 1024 * 1024 / VM_PAGE_SIZE; phys_bytes mem = NO_MEM, i; /* page number */ int maxpage = NUMBER_PHYSICAL_PAGES - 1; static int lastscan = -1; int startscan, run_length; if(memflags & PAF_LOWER16MB) maxpage = boundary16 - 1; else if(memflags & PAF_LOWER1MB) maxpage = boundary1 - 1; else { /* no position restrictions: check page cache */ if(pages == 1) { while(free_page_cache_size > 0) { i = free_page_cache[free_page_cache_size-1]; if(page_isfree(i)) { free_page_cache_size--; mem = i; assert(mem != NO_MEM); run_length = 1; break; } free_page_cache_size--; } } } if(lastscan < maxpage && lastscan >= 0) startscan = lastscan; else startscan = maxpage; if(mem == NO_MEM) mem = findbit(0, startscan, pages, memflags, &run_length); if(mem == NO_MEM) mem = findbit(0, maxpage, pages, memflags, &run_length); if(mem == NO_MEM) return NO_MEM; /* remember for next time */ lastscan = mem; for(i = mem; i < mem + pages; i++) { UNSET_BIT(free_pages_bitmap, i); } if(memflags & PAF_CLEAR) { int s; if ((s= sys_memset(NONE, 0, CLICK_SIZE*mem, VM_PAGE_SIZE*pages)) != OK) panic("alloc_mem: sys_memset failed: %d", s); } return mem; }
void addface(Face *f) /* always adds at 0 */ { Face **ofaces; Rectangle r0, r1, r; int y, nx, ny; if(f == nil) return; if(first != 0){ first = 0; eresized(0); } findbit(f); nx = nacross; ny = (nfaces+(nx-1)) / nx; lockdisplay(display); for(y=ny; y>=0; y--){ /* move them along */ r0 = facerect(y*nx+0); r1 = facerect(y*nx+1); r = r1; r.max.x = r.min.x + (nx - 1)*(Facesize+Facesep); draw(screen, r, screen, nil, r0.min); /* copy one down from row above */ if(y != 0){ r = facerect((y-1)*nx+nx-1); draw(screen, r0, screen, nil, r.min); } } ofaces = faces; faces = emalloc((nfaces+1)*sizeof(Face*)); memmove(faces+1, ofaces, nfaces*(sizeof(Face*))); free(ofaces); nfaces++; setlast(); drawarrows(); faces[0] = f; drawface(f, 0); flushimage(display, 1); unlockdisplay(display); }
int main() { char a; char choice; nd* start; int flag; nd* T; start = '\0'; printf("Enter the character: \n"); scanf("%c", &a); if(!start) { start = (nd*)malloc(sizeof(nd)); start->info = a; start->bitindex = findbit('\0', a); // null has got all 0s in all the 7 bits start->lc = '\0'; start->rc = start; //printf("Info at root: %c\n", start->rc->info); printf("bitindex of the node %c is %d\n", a, start->bitindex); } printf("Enter another node? answer in y/n "); scanf(" %c", &choice); // the leading whitespace in scanf tells the function to skip any whitespace fflush(stdin); T = start; root = start; while(choice != 'n') { printf("Enter the character: \n"); scanf(" %c", &a); shift = 6 - T->bitindex; traverse(T, a); printf("Enter another node? answer in y/n "); scanf(" %c", &choice); } free(start); exit(EXIT_SUCCESS); }
void traverse(nd* T, char a) { int d, x, d1; nd* closest_node; nd* current; int flag; nd* new_node = (nd*) malloc(sizeof(nd)); new_node->info = a; new_node->rc = new_node->lc = '\0'; d = a << shift; // skip the pointer to the bit_index of the current node if(0100 & d) { //if the new node has 1 in current node's bit_index postion go to right if(T->rc->bitindex >= T->bitindex) { // search finds an uplink closest_node = T->rc; new_node->bitindex = findbit(closest_node->info, a); printf("bitindex of new node: %d\n", new_node->bitindex); //if(new_node->bitindex < closest_node->bitindex){ if(new_node->bitindex < T->bitindex) { // we check whether new_node should be a child of current node T->rc = new_node; x = a << (6 - new_node->bitindex); if(0100 & x) { // right link to itself, left link to closest node printf("the info at closest_node: %c\n", closest_node->info); new_node->rc = new_node; new_node->lc = closest_node; } else { // left link to itself, righf(new_node->bitindex < T->bitindex){ // we check whether new_node should be a child of current node T->rc = new_node; x = a << (6 - new_node->bitindex); t link to closest node printf("the info at closest_node: %c\n", closest_node->info); new_node->lc = new_node; new_node->rc = closest_node; } } else { // the new node can't be a child of the current node,find right place shift = 6 - (T->bitindex); d = a << shift; T = root; while(T->bitindex > new_node->bitindex) { // finds the proper parent of new node if(0100 & d) { // new node to be inserted as right child flag = 1; if(T->rc->bitindex < new_node->bitindex) break; else ; } else { // new node to be inserted as left child flag = 0; if(T->lc->bitindex < new_node->bitindex) break; else ; } } if(flag) { // inserting the previous child as right child of new node if(0100 & (T->rc->info << (6 - new_node->bitindex))) { new_node->rc = T->rc; if(0100 & (a << (6 - new_node->bitindex))) new_node->lc = closest_node; else new_node->lc = new_node; } else { // inserting the previous child as left child of new node new_node->lc = T->rc; if(0100 & (a << (6 - new_node->bitindex))) new_node->rc = new_node; else new_node->rc = closest_node; } } } } else { // normal link to a child node, so we call the routine recursively free(new_node); new_node = '\0'; shift = 6 - (T->rc->bitindex); traverse(T->rc, a); } } else { // if the new node has 0 in its current node's bit_index position go to left if(T->lc == '\0') { // search hits null new_node->bitindex = findbit('\0', a); new_node->lc = '\0'; new_node->rc = new_node; printf("bitindex of new node: %d\n", new_node->bitindex); if(new_node->bitindex < T->bitindex) { T->lc = new_node; new_node->lc = '\0'; new_node->rc = new_node; printf("the info at closest node: %c\n", T->info); } else { // new node can't be a child node of present node shift = 6 - (T->bitindex); d = a << shift; T = root; while(T->bitindex > new_node->bitindex) { // finds the proper parent of new node if(0100 & d) { // new node to be inserted as right child flag = 1; if(T->rc->bitindex < new_node->bitindex) break; else ; } else { // new node to be inserted as left child flag = 0; if(T->lc->bitindex < new_node->bitindex) break; else ; } } if(flag) { // inserting the previous child as right child of new node if(0100 & (T->rc->info << (6 - new_node->bitindex))) { new_node->rc = T->rc; } else { // inserting the previous child as left child of new node new_node->lc = T->rc; } } } } else if(T->lc->bitindex >= T->bitindex) { // search finds an uplink closest_node = T->lc; new_node->bitindex = findbit(closest_node->info, a); printf("bitindex of new node: %d\n", new_node->bitindex); if(new_node->bitindex < T->bitindex) { // we check whether new_node should be a child of current node T->lc = new_node; x = a << (6 - new_node->bitindex); if(0100 & x) { // right link to itself, left link to closest node new_node->rc = new_node; new_node->lc = closest_node; printf("the info at closest_node: %c\n", closest_node->info); } else { // left link to itself, right link to closest node new_node->lc = new_node; new_node->rc = closest_node; printf("the info at closest_node: %c\n", closest_node->info); } } else { // when the new node cannot be a child of the current node shift = 6 - (T->bitindex); d = a << shift; T = root; while(T->bitindex > new_node->bitindex) { // finds the proper parent of new node if(0100 & d) { // new node to be inserted as right child flag = 1; if(T->rc->bitindex < new_node->bitindex) break; else ; } else { // new node to be inserted as left child flag = 0; if(T->lc->bitindex < new_node->bitindex) break; else ; } } if(flag) { // inserting the previous child as right child to new node if(0100 & (T->rc->info << (6 - new_node->bitindex))) { new_node->rc = T->rc; if(0100 & (a << (6 - new_node->bitindex))) new_node->lc = closest_node; else new_node->lc = new_node; } else { // inserting the previous child as the left child to new node new_node->lc = T->rc; if(0100 & (a << (6 - new_node->bitindex))) new_node->rc = new_node; else new_node->rc = closest_node; } } } } else { // normal link to a child node free(new_node); new_node = '\0'; shift = 6 - (T->lc->bitindex); traverse(T->lc, a); } } }
int Calu(int x, int y, int n, int idx) { int Fy = oo, Sy = oo, Ty = -oo, Fthy = -oo; int i, j, Ft, St, Tt, Ftht; Ft = St = Tt = Ftht = 0; for(i = idx+1; i <= n; i++) { if(Point[i].y > y) { if(Point[i].y < Fy) { Fy = Point[i].y; First[Ft++] = Point[i].num; } } else { if(Point[i].y > Fthy) { Fthy = Point[i].y; Fourth[Ftht++] = Point[i].num; } } } for(i = idx-1; i >= 0; i--) { if(Point[i].y > y) { if(Point[i].y < Sy) { Sy = Point[i].y; Second[St++] = Point[i].num; } } else { if(Point[i].y > Ty) { Ty = Point[i].y; Third[Tt++] = Point[i].num; } } } int nodex = Point[idx].num, nodey; int Ans = Ft + St + Tt + Ftht; for(i = 0; i < Ft; i++) { nodey = First[i]; modifybit((nodey<<13)|nodex); } for(i = 0; i < St; i++) { nodey = Second[i]; modifybit((nodey<<13)|nodex); } for(i = 0; i < Tt; i++) { nodey = Third[i]; modifybit((nodex<<13)|nodey); } for(i = 0; i < Ftht; i++) { nodey = Fourth[i]; modifybit((nodex<<13)|nodey); } for(i = 0; i < Ft; i++) { for(j = 0; j < Tt; j++) { nodex = First[i], nodey = Third[j]; if(findbit((nodex<<13)|nodey)) Ans--, modifybit((nodex<<13)|nodey); } } for(i = 0; i < St; i++) { for(j = 0; j < Ftht; j++) { nodex = Second[i], nodey = Fourth[j]; if(findbit((nodex<<13)|nodey)) Ans--, modifybit((nodex<<13)|nodey); } } return Ans; }