MemHandle MEM_AllocatePages(Bitu pages,bool sequence) { MemHandle ret; if (!pages) return 0; if (sequence) { Bitu index=BestMatch(pages); if (!index) return 0; MemHandle * next=&ret; while (pages) { *next=index; next=&memory.mhandles[index]; index++;pages--; } *next=-1; } else { if (MEM_FreeTotal()<pages) return 0; MemHandle * next=&ret; while (pages) { Bitu index=BestMatch(1); if (!index) E_Exit("MEM:corruption during allocate"); while (pages && (!memory.mhandles[index])) { *next=index; next=&memory.mhandles[index]; index++;pages--; } *next=-1; //Invalidate it in case we need another match } } return ret; }
/* should get: black white blue red grey greyblue ltgrey */ void map_colors() { int x; #if 1 int tmpmap[] = { 0, 2, 3, 7, 4, 5, 8, 9, 10, 11, 13, 15, 12, 1, 14, 6 }; /* still not right: gray green yellow lost somewhere? */ #else int tmpmap[16]; int x, y; for (x = 0; x < 16; x++) tmpmap[x] = -1; /* set not assigned yet */ tmpmap[BestMatch(0, 0, 0)] = CX_BLACK; tmpmap[BestMatch(255, 255, 255)] = CX_WHITE; tmpmap[BestMatch(255, 0, 0)] = CX_RED; tmpmap[BestMatch(0, 255, 0)] = CX_GREEN; tmpmap[BestMatch(0, 0, 255)] = CX_BLUE; /* clean up the rest */ for (x = 0; x < 16; x++) { for (y = 0; y < 16; y++) if (tmpmap[y] == x) goto outer_cont; for (y = 0; y < 16; y++) if (tmpmap[y] == -1) { tmpmap[y] = x; break; } if (y == 16) panic("too many colors?"); outer_cont: ; } for (x = 0; x < 16; x++) if (tmpmap[y] == -1) panic("lost color?"); #endif for (x = 0; x < 16; x++) { #ifdef DBG printf("final: c[%d]=%d (target: %d)\n", x, tmpmap[x], colrmap[x]); #endif colrmap[x] = tmpmap[x]; } }
int main() { int ncase; scanf("%d",&ncase); for(int ii=1;ii<=ncase;ii++) { int tem; scanf("%d",&n); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { scanf("%d",&tem); tem--; g[tem][i]=j; } for(int i=0;i<n;i++) for(int j=0;j<n;j++) { scanf("%d",&tem); tem--; g[i][tem]+=j; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { // printf("%d ",map[i][j]); g[i][j]=100-g[i][j]; } // puts(""); } M=N=n; limit=BestMatch(); double ans=limit*0.5/n; printf("Data Set %d, Best average difference: %.6lf\n",ii,ans); for(int i=0;i<n;i++) for(int j=0;j<n;j++) g[i][j]=100-g[i][j]; memset(cy,0,sizeof(cy)); top=1; DFS_Trace(0,0,limit); puts(""); } return 0; }
void GetBestMatch4Single(const vector<CandidatePosition>& ranked_results, const int ranked_results_size, const Genome& genome, const uint32_t& read_len, const string& read_name, const string& read_seq, const string& read_score, const uint32_t& max_mismatches, BestMatch& best_match) { for (int i = ranked_results_size - 1; i >= 0; --i) { const CandidatePosition& r = ranked_results[i]; if (r.mismatch < best_match.mismatch) { best_match = BestMatch(r.genome_pos, 1, r.strand, r.mismatch); } else if (r.mismatch == best_match.mismatch) { if (best_match.genome_pos == r.genome_pos) { continue; } else { best_match.genome_pos = r.genome_pos; best_match.strand = r.strand; best_match.times++; } } else { break; } } }
MemHandle MEM_GetNextFreePage(void) { return (MemHandle)BestMatch(1); }
//Function to compute texture from sample void SynthesizeTextureEfrosLeung(const ImgGray& texture, const ImgBinary &mask, int window_width, int window_height,int out_width, int out_height, ImgGray* out) { int height=texture.Height(); int width=texture.Width(); bool img_filled=false; int win_wid_c=static_cast<int>(blepo_ex::Ceil(window_width/2.0f)); int win_wid_f=static_cast<int>(blepo_ex::Floor(window_width/2.0f)); int win_ht_c=static_cast<int>(blepo_ex::Ceil(window_height/2.0f)); int win_ht_f=static_cast<int>(blepo_ex::Floor(window_height/2.0f)); int i,j,m,n,k; static bool hasnbr=false; static int nbr=0; ImgGray templates; templates.Reset(window_width,window_height); ImgBinary img_bin; img_bin = mask; //Find neighbors while(1) { for(i=win_wid_c;i<(out_width-win_wid_c);i++) { for(j=win_ht_c;j<(out_height-win_ht_c);j++) { hasnbr=false; nbr=0; if(img_bin(i,j)==false) { for (m=-win_wid_f;m<win_wid_c;m++) { for (n=-win_ht_f;n<win_ht_c;n++) { if(img_bin(i+m,j+n)==true) { nbr=nbr+1; hasnbr=true; } } } } if(hasnbr==true) { struct PixelInfo pix; pix.xc=i; pix.yc=j; pix.totalnbr=nbr; g_pixel.push_back(pix); } } } if(g_pixel.size()==0) { break; } //Sort list of neighbors qsort(&g_pixel[0],g_pixel.size(),sizeof(g_pixel[0]),Compare); //Compute best match for window around given pixel while(g_pixel.size()!=0) { struct PixelInfo temp; temp=g_pixel.back(); g_pixel.pop_back(); for (i=0;i<window_width;i++) for(j=0;j<window_height;j++) templates(i,j)=(*out)(temp.xc+i-win_wid_f,temp.yc+j-win_ht_f); k=BestMatch(texture,&templates,window_width,window_height,temp.xc,temp.yc,img_bin); (*out)(temp.xc,temp.yc)=k; img_bin(temp.xc,temp.yc)=true; } } }
/* merge the mapping results from paired reads */ void MergePairedEndResults( const Genome& genome, const string& read_name, const string& read_seq1, const string& read_score1, const string& read_seq2, const string& read_score2, const vector<vector<CandidatePosition> >& ranked_results, const vector<int>& ranked_results_size, const int& frag_range, const uint32_t& max_mismatches, const bool& SAM, StatPairedReads& stat_paired_reads, FILE * fout) { #ifdef DEBUG for (int i = ranked_results_size[0] - 1; i >= 0; --i) { const CandidatePosition& r1 = ranked_results[0][i]; uint32_t chr_id1 = getChromID(genome.start_index, r1.genome_pos); uint32_t start_pos = r1.genome_pos - genome.start_index[chr_id1]; if ('-' == r1.strand) { start_pos = genome.length[chr_id1] - start_pos - read_seq1.size(); } uint32_t end_pos = start_pos + read_seq1.size(); fprintf(stderr, "%u %s %u %u %c %u\n", r1.genome_pos, genome.name[chr_id1].c_str(), start_pos, end_pos, r1.strand, r1.mismatch); } for (int j = ranked_results_size[1] - 1; j >= 0; --j) { const CandidatePosition& r2 = ranked_results[1][j]; uint32_t chr_id2 = getChromID(genome.start_index, r2.genome_pos); uint32_t start_pos = r2.genome_pos - genome.start_index[chr_id2]; if ('-' == r2.strand) { start_pos = genome.length[chr_id2] - start_pos - read_seq2.size(); } uint32_t end_pos = start_pos + read_seq2.size(); fprintf(stderr, "%u %s %u %u %c %u\n", r2.genome_pos, genome.name[chr_id2].c_str(), start_pos, end_pos, r2.strand, r2.mismatch); } #endif uint32_t read_len1 = read_seq1.size(); uint32_t read_len2 = read_seq2.size(); pair<int, int> best_pair(-1, -1); uint32_t min_num_of_mismatch = max_mismatches; uint64_t best_pos = 0; uint32_t best_times = 0; for (int i = ranked_results_size[0] - 1; i >= 0; --i) { for (int j = ranked_results_size[1] - 1; j >= 0; --j) { const CandidatePosition& r1 = ranked_results[0][i]; const CandidatePosition& r2 = ranked_results[1][j]; if (r1.strand == r2.strand) continue; uint32_t num_of_mismatch = r1.mismatch + r2.mismatch; if (num_of_mismatch > min_num_of_mismatch) break; uint32_t chr_id1 = getChromID(genome.start_index, r1.genome_pos); uint32_t chr_id2 = getChromID(genome.start_index, r2.genome_pos); if (chr_id1 != chr_id2) continue; int frag_size = GetFragmentLength(r1, r2, frag_range, read_len1, read_len2, genome, chr_id1, chr_id2); if (frag_size <= 0 || frag_size > frag_range) continue; uint64_t cur_pos = r1.genome_pos; cur_pos <<= 32; cur_pos += r2.genome_pos; if (num_of_mismatch < min_num_of_mismatch) { best_pair = make_pair(i, j); best_times = 1; min_num_of_mismatch = num_of_mismatch; best_pos = cur_pos; } else if (num_of_mismatch == min_num_of_mismatch && cur_pos != best_pos) { best_pair = make_pair(i, j); best_times++; } } } BestMatch best_match_1(0, 0, '+', max_mismatches); BestMatch best_match_2(0, 0, '+', max_mismatches); bool is_paired_mapped = false; int len = 0; if (best_times == 1) { stat_paired_reads.unique_mapped_pairs++; len = OutputBestPairedResults(ranked_results[0][best_pair.first], ranked_results[1][best_pair.second], frag_range, read_len1, read_len2, genome, read_name, read_seq1, read_score1, read_seq2, read_score2, SAM, fout); if (SAM) { // SAM is_paired_mapped = true; const CandidatePosition& r1 = ranked_results[0][best_pair.first]; const CandidatePosition& r2 = ranked_results[1][best_pair.second]; best_match_1 = BestMatch(r1.genome_pos, 1, r1.strand, r1.mismatch); best_match_2 = BestMatch(r2.genome_pos, 1, r2.strand, r2.mismatch); } } else { if (best_times >= 2) { stat_paired_reads.ambiguous_mapped_pairs++; } else { stat_paired_reads.unmapped_pairs++; } GetBestMatch4Single(ranked_results[0], ranked_results_size[0], genome, read_len1, read_name, read_seq1, read_score1, max_mismatches, best_match_1); GetBestMatch4Single(ranked_results[1], ranked_results_size[1], genome, read_len2, read_name, read_seq2, read_score2, max_mismatches, best_match_2); StatInfoUpdate(best_match_1.times, stat_paired_reads.stat_single_reads_1); StatInfoUpdate(best_match_2.times, stat_paired_reads.stat_single_reads_2); if (!SAM) { OutputSingleResults(best_match_1, read_name, read_seq1, read_score1, genome, false, stat_paired_reads.stat_single_reads_1, fout); OutputSingleResults(best_match_2, read_name, read_seq2, read_score2, genome, true, stat_paired_reads.stat_single_reads_2, fout); } } if (SAM) { // Output SAM int flag_1 = GetSAMFLAG(true, is_paired_mapped, best_match_1.times == 0, best_match_2.times == 0, best_match_1.strand == '-', best_match_2.strand == '-', true, false, best_match_1.times >= 2); int flag_2 = GetSAMFLAG(true, is_paired_mapped, best_match_2.times == 0, best_match_1.times == 0, best_match_2.strand == '-', best_match_1.strand == '-', false, true, best_match_2.times >= 2); OutputPairedSAM(best_match_1, best_match_2, genome, read_name, read_seq1, read_score1, read_seq2, read_score2, len, flag_1, flag_2, stat_paired_reads, fout); } }