int btr(int mask) { int i,j,k,res=INF,cur,r; if(dp[mask]>-1) return dp[mask]; if(mask==0) return dp[mask]=0; for(i=0;!(mask&(1<<i));i++); /* carry one patient */ res=f[n][i]*2+btr(mask-(1<<i)); /* carry two patients */ for(j=i+1;j<n;j++) if(mask&(1<<j)) { cur=f[n][i]+f[i][j]+f[j][n]+btr(mask-(1<<i)-(1<<j)); if(res>cur) res=cur; /* carry three patients */ for(k=j+1;k<n;k++) if(mask&(1<<k)) { /* try all permutations of pickup order up to mirror: ijk ikj jik */ r=btr(mask-(1<<i)-(1<<j)-(1<<k)); cur=f[n][i]+f[i][j]+f[j][k]+f[k][n]+r; if(res>cur) res=cur; cur=f[n][i]+f[i][k]+f[k][j]+f[j][n]+r; if(res>cur) res=cur; cur=f[n][j]+f[j][i]+f[i][k]+f[k][n]+r; if(res>cur) res=cur; } } return dp[mask]=res; }
int comms_create_port(unsigned int port) { //return duplicate port when the port exists if (bt(comms_bitmap,port) == 0) { return -ERR_IPC_DUPLICATE_PORT; } //return invalid port if port exceeds max ports if (port >= COMMS_MAX_PORTS) { return -ERR_IPC_INVALID_PORT; } //allocate memory for a struct for the port msg_port[port] = (struct str_msg_port*)mem_alloc(sizeof(struct str_msg_port)); //there will be no messages now msg_port[port]->queue = NULL; msg_port[port]->last = NULL; //there isn't a blocked process msg_port[port]->bq = NULL; msg_port[port]->server = NULL; //save the pcb of the server msg_port[port]->pcb = get_current()->pcb; //get the index in the page directory. this will be the same for all processes msg_port[port]->index = msg_port[port]->pcb->base >> 22; //set port as allocated btr(comms_bitmap,port); //return OK return OK; }
Code() { Xbyak::Label label; cmpss(xmm0, ptr[rip + label], 0); test(dword[rip + label], 33); bt(dword[rip + label ], 3); vblendpd(xmm0, dword[rip + label], 3); vpalignr(xmm0, qword[rip + label], 4); vextractf128(dword[rip + label], ymm3, 12); vperm2i128(ymm0, ymm1, qword[rip + label], 13); vcvtps2ph(ptr[rip + label], xmm2, 44); mov(dword[rip + label], 0x1234); shl(dword[rip + label], 3); shr(dword[rip + label], 1); shld(qword[rip + label], rax, 3); imul(rax, qword[rip + label], 21); rorx(rax, qword[rip + label], 21); test(dword[rip + label], 5); pextrq(ptr[rip + label], xmm0, 3); pinsrq(xmm2, ptr[rip + label], 5); pextrw(ptr[rip + label], xmm1, 4); adc(dword[rip + label], 0x12345); bt(byte[rip + label], 0x34); btc(word[rip + label], 0x34); btr(dword[rip + label], 0x34); rcl(dword[rip + label], 4); shld(qword[rip + label], rax, 4); palignr(mm0, ptr[rip + label], 4); aeskeygenassist(xmm3, ptr[rip + label], 4); vpcmpestrm(xmm2, ptr[rip + label], 7); ret(); L(label); dq(0x123456789abcdef0ull); };
TEST(KeyBtreeTest, put_overwrite) { KeyBtree<KeyInfo, int> btr(sizeof(KeyInfo)); KeyInfo k1(10, 1); KeyInfo k2(10, 2); KeyInfo key(1, 1); int value, old_value; EXPECT_EQ(btr.put(k1, 100), ERROR_CODE_OK); EXPECT_EQ(btr.put(k2, 101, true), ERROR_CODE_OK); { BtreeReadHandle handle; btr.get_read_handle(handle); btr.set_key_range(handle, btr.get_min_key(), 0, btr.get_max_key(), 0); EXPECT_EQ(btr.get_next(handle, key, value), ERROR_CODE_OK); EXPECT_EQ(value, 101); EXPECT_EQ(key.b_, 1); } // overwrite key EXPECT_EQ(btr.put(k2, 102, old_value, true, true), ERROR_CODE_OK); { BtreeReadHandle handle; btr.get_read_handle(handle); btr.set_key_range(handle, btr.get_min_key(), 0, btr.get_max_key(), 0); EXPECT_EQ(btr.get_next(handle, key, value), ERROR_CODE_OK); EXPECT_EQ(old_value, 101); EXPECT_EQ(value, 102); EXPECT_EQ(key.b_, 2); } }
int i8259_install_interrupt_handler(int vector, interrupt_handler handler, int p_level) { /*This function will be used to install an interrupt handler for the specified vector. The function must return OK if the installation was successful or otherwise an error code (see error.h). The interrupt mask register should be updated if the vector number refers to an entry associated with IRQ0..IRQ15. The function must return -ERR_I8259_INVALID_VECTOR if the vector is out of bounds or -ERR_I8259_INSTALLED if a handler is already present.*/ /* ----- Check if the IDT vector is valid ----------------------------------------------------- */ if ((vector < 0) || (vector >= MAX_IDT_ENTRIES)) { return -ERR_I8259_INVALID_VECTOR; } /* ----- Check if the IDT currently contains a handler for the specified vector --------------- */ if (i8259_installed(vector) == 0) { /* ----- Create a new gate descriptor and place it into the IDT ------------------------------- */ i386_initialize_idt_entry(vector, (int)(&jump_table[vector][0]),p_level); /* ----- Add the handler to our interrupt table ----------------------------------------------- */ interrupt_table[vector] = handler; /* ----- Update the IMR if the handler is associated with IRQ0..IRQ15 ------------------------- */ unsigned char initial_IMR_reading; if ((vector - IRQ0 >= 0) && (vector - IRQ0 < 8 )) // pic - 0 { initial_IMR_reading = inb(I8259_PIC0_PORTB); btr(initial_IMR_reading,vector - IRQ0); outb(initial_IMR_reading,I8259_PIC0_PORTB); } if ((vector - IRQ0 >= 8) && (vector - IRQ0 <= 15 )) // pic 1 { initial_IMR_reading = inb(I8259_PIC1_PORTB); btr(initial_IMR_reading,vector - IRQ8); outb(initial_IMR_reading,I8259_PIC1_PORTB); } /* ----- Mark the entry inside the bitmap ----------------------------------------------------- */ bts(idt_bitmap[vector/32],vector%32); return OK; } else { return -ERR_I8259_INSTALLED; } }
int i8259_remove_interrupt_handler(int vector) { /* ----- Check if the IDT vector is valid ----------------------------------------------------- */ if ((vector < 0) || (vector >= MAX_IDT_ENTRIES)) { return -ERR_I8259_INVALID_VECTOR; } /* ----- Check if the IDT currently contains a handler for the specified vector --------------- */ if (i8259_installed(vector) == 1) { /* ----- Update the entry inside the IDT ------------------------------------------------------ */ i386_initialize_idt_entry(vector, (int)(&jump_table[vector][0]),0); /* ----- Remove the handler from our interrupt table ------------------------------------------ */ interrupt_table[vector] = NULL; /* ----- Update the IMR if the handler is associated with IRQ0..IRQ15 ------------------------- */ unsigned char initial_IMR_reading; if ((vector - IRQ0 >= 0) && (vector - IRQ0 < 8 )) // pic - 0 { initial_IMR_reading = inb(I8259_PIC0_PORTB); btr(initial_IMR_reading,vector - IRQ0); outb(initial_IMR_reading,I8259_PIC0_PORTB); } if ((vector - IRQ0 >= 8) && (vector - IRQ0 <= 15 )) // pic 1 { initial_IMR_reading = inb(I8259_PIC1_PORTB); btr(initial_IMR_reading,vector - IRQ8); outb(initial_IMR_reading,I8259_PIC1_PORTB); } /* ----- Clear the entry inside the bitmap ---------------------------------------------------- */ btr(idt_bitmap[vector/32],vector%32); return OK; } else { return -ERR_I8259_REMOVED; } }
int btr(int at,int num) { int i; if(num==n) return g[at][0]; for(i=0;i<n;i++) if(g[at][i] && !taken[i]) { taken[i]=1; path[num]=i; if(btr(i,num+1)) return 1; taken[i]=0; } return 0; }
int btr(int x,int y) { int i; if(x==X) { x=0,y++; if(y==Y) { printgrid(); return 1; } } if(grid[x][y]<0) return btr(x+1,y); for(i=0;i<N;i++) if(!taken[i]) { grid[x][y]=i; taken[i]=1; if(!ok(x+1,y+1) || !ok(x,y+1) || !ok(x+1,y) || !ok(x,y)) goto fail; if(btr(x+1,y)) return 1; fail: taken[i]=0; grid[x][y]=255; } return 0; }
int main(){ Simple_window win(Point(100, 100), 1280, 720, "Binary_tree2"); Binary_tree bt(Point(50, 50), 800, 8, 50, 10); bt.set_fill_color(Color::dark_cyan); bt.set_style(Line_style(Line_style::solid, 2)); Binary_tree_rectangle btr(Point(850, 300), 400, 3, 100, 20); btr.set_fill_color(Color::dark_magenta); btr.set_style(Line_style(Line_style::dot, 2)); win.attach(bt); win.attach(btr); win.wait_for_button(); }
int main() { int i,j,k,a,b,c,cases; scanf("%d",&cases); while(cases--) { scanf("%d %d",&n,&m); for(i=0;i<=n;i++) for(j=0;j<=n;j++) f[i][j]=INF; for(i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); f[a][b]=f[b][a]=c; } for(k=0;k<=n;k++) for(i=0;i<=n;i++) for(j=0;j<=n;j++) if(f[i][j]>f[i][k]+f[k][j]) f[i][j]=f[i][k]+f[k][j]; memset(dp,-1,sizeof(int)*(1<<n)); printf("%d\n",btr((1<<n)-1)); } return 0; }
TEST(KeyBtreeTest, range_search_int) { KeyBtree<int64_t, char*> btr(sizeof(int64_t)); if (true) { BtreeWriteHandle handle; btr.get_write_handle(handle); btr.put(handle, 1, NULL); btr.put(handle, 2, NULL); btr.put(handle, 3, NULL); btr.put(handle, 4, NULL); handle.end(); } int64_t start = 4; int64_t end = 99; int64_t key; char *value; { BtreeReadHandle handle; btr.get_read_handle(handle); btr.set_key_range(handle, &start, 0, &end, 1); key = 0; EXPECT_EQ(btr.get_next(handle, key, value), ERROR_CODE_OK); EXPECT_EQ(key, start); EXPECT_EQ(btr.get_next(handle, key, value), ERROR_CODE_NOT_FOUND); } { BtreeReadHandle handle; btr.get_read_handle(handle); btr.set_key_range(handle, &start, 0, &end, 0); key = 0; EXPECT_EQ(btr.get_next(handle, key, value), ERROR_CODE_OK); EXPECT_EQ(key, start); EXPECT_EQ(btr.get_next(handle, key, value), ERROR_CODE_NOT_FOUND); } btr.clear(); }
int main() { char s[16]; int a,b,i; while(scanf("%d",&n)==1) { memset(g,0,sizeof(g)); while(1) { scanf("%s",s); if(s[0]=='%') break; a=strtol(s,0,10); scanf("%d",&b); --a; --b; g[a][b]=g[b][a]=1; } memset(taken,0,n); taken[0]=1; path[0]=0; if(!btr(0,1)) puts("N"); else { for(i=0;i<n;i++) printf("%d ",path[i]+1); printf("%d\n",path[0]+1); } } return 0; }
int main() { int i; for(i=0;i<N;i++) taken[i]=0; btr(0,0); return 0; }