void add(T evnt) override { if(data.empty()) { data.push_back(evnt); } else { //try to add it at the end auto riter = data.rbegin(); auto iter = data.begin(); if(evnt >= (*riter)) { //aka, the event being added is further off than the //oldest event currently in the queue. data.push_back(evnt); } else if (evnt <= (*iter)) { //aka, the event being added is younger than the next event in the queue. data.push_front(evnt); } else { //gutted. Now have to find the place for it to go. iter = upper_bound(data.begin(), data.end(), evnt); data.insert(iter, evnt); } } }
void insert_readyq(Event temp){ deque<Event>::iterator eq_it = ready_queue.begin(); while (eq_it != ready_queue.end() && temp.dynamic_p <= eq_it->dynamic_p) { eq_it++; } eq_it = ready_queue.insert(eq_it, temp); return; }
void insert_rq(Event temp){ //insert end process into result queue deque<Event>::iterator rq_it = result_queue.begin(); while (rq_it != result_queue.end() && temp.p.id > rq_it->p.id) { rq_it++; } rq_it = result_queue.insert(rq_it, temp); return; }
void insert_expiredq(Event temp){ temp.dynamic_p = temp.p.priority + 1; deque<Event>::iterator eq_it = expired_queue.begin(); while (eq_it != expired_queue.end() && temp.dynamic_p <= eq_it->dynamic_p) { eq_it++; } eq_it = expired_queue.insert(eq_it, temp); return; }
/** * reads char stream to bit stream using previous method */ void bin2str(ifstream & ifs,deque<char> & ss) { char aux = 0; while(ifs.good()) { aux=ifs.get(); if(!ifs.good()) break; string tmp = ascii2bin(aux); ss.insert(ss.end(),tmp.data(),tmp.data()+tmp.length()); } }
void normal_insert_by_insert() //insert the element at front of the iterator { vector<int>::iterator vec_iter = vec.begin(); list<int>::iterator ls_iter = ls.begin(); deque<int>::iterator de_iter = de.begin(); vec_iter = vec.insert ( vec_iter, 3 );//return is the iterator of newly insertion element ls_iter = ls.insert ( ls_iter, 33 ); de_iter = de.insert ( de_iter, 333 ); cout << "normal_insert_by_insert" << *vec_iter << " " << *ls_iter << " " << *de_iter << endl; }
int main() { freopen("C.INP","r",stdin); freopen("C.OUT","w",stdout); int tcs; scanf(" %d ",&tcs); for(int t = 1 ; t <= tcs ; t++) { dq.clear(); printf("Case %d:\n",t); int n; scanf(" %d ",&n); int a; scanf(" %d ",&a); dq.pb(a); puts("0"); for(int i = 1 ; i < n ; i++) { scanf(" %d ",&a); if(a >= dq.back()) { printf("%d\n",dq.size()); dq.pb(a); } else if(a <= dq.front()) { int cnt = 0; while(dq[cnt++] == a); printf("%d\n",cnt-1); dq.pf(a); } else { int pos = bSearch(a); if(dq[pos] == a) { while(dq[pos] == a) pos++; } else if(dq[pos] < a) pos++; printf("%d\n",pos); dq.insert(dq.begin()+pos,a); } //for(int i = 0 ; i < dq.size() ; i++) // printf("%d ",dq[i]); // printf("\n"); } } return 0; }
void insert_eq(Event temp){ deque<Event>::iterator eq_it = event_queue.begin(); while (eq_it != event_queue.end() && temp.time_stamp >= eq_it->time_stamp) { if (temp.time_stamp > eq_it->time_stamp) { eq_it++; } else{ if (eq_it != event_queue.end() && temp.randval <= eq_it->randval) { eq_it++; } } } eq_it = event_queue.insert(eq_it, temp); return; }
bool reduce(deque<double>& nums) { if (nums.size() == 1 && abs(nums.front() - target) < EPS) { return true; } for (int i = 1; i < nums.size(); i++) { for (int j = 0; j < i; j++) { auto num_i = nums[i], num_j = nums[j]; nums.erase(nums.begin() + i); nums.erase(nums.begin() + j); nums.push_back(num_i + num_j); if (reduce(nums)) return true; nums.pop_back(); nums.push_back(num_i * num_j); if (reduce(nums)) return true; nums.pop_back(); nums.push_back(num_i - num_j); if (reduce(nums)) return true; nums.pop_back(); nums.push_back(num_j - num_i); if (reduce(nums)) return true; nums.pop_back(); if (num_i != 0) { nums.push_back(num_j / num_i); if (reduce(nums)) return true; nums.pop_back(); } if (num_j != 0) { nums.push_back(num_i / num_j); if (reduce(nums)) return true; nums.pop_back(); } nums.insert(nums.begin() + j, num_j); nums.insert(nums.begin() + i, num_i); } } return false; }
void f(int n){ if(n == N){ for(int i=0;i<N;i++) printf("%c",D[i]); printf("\n"); return; } long long ways = ff(n+2,N); long long pos = ways; int ii = 0; while(pos < idx) pos += ways, ii++; // printf("[%d] pos = %lld idx = %lld ways = %lld\n",n,pos,idx,ways); // pos >= idx // pos-ways < idx idx -= (pos-ways); assert(idx > 0); D.insert(D.begin()+ii,str[n]); f(n+1); }
//write Huffman tree to file, also make encoder mapping void print(node*cur, map<char,string> &encoder,deque<char> & ss) { if(cur->left==0 )//only left and right links of leaf may be NULL { ss.push_back('1'); string tmp = ascii2bin(cur->ASCII); ss.insert(ss.end(),tmp.data(),tmp.data()+tmp.length()); //cout<<cur->ASCII<<" "<<cur->freq<<endl; encoder[cur->ASCII]=cur->code; } else { ss.push_back('0'); cur->left->code.append(cur->code); cur->left->code.append("0"); cur->right->code.append(cur->code); cur->right->code.append("1"); print(cur->left,encoder,ss); print(cur->right,encoder,ss); } }
void displayModel_t::renderText(const RECT& rect, const int minHorizontalWhitespace, const int minVerticalWhitespace, const bool stripOuterWhitespace, wstring& text, deque<RECT>& characterLocations) { RECT tempCharLocation; RECT tempRect; wstring curLineText; deque<RECT> curLineCharacterLocations; int curLineMinTop=-1; int curLineMaxBottom=-1; int curLineBaseline=-1; int lastChunkRight=rect.left; HWND lastChunkHwnd=NULL; int lastLineBottom=rect.top; //Walk through all the chunks looking for any that intersect the rectangle displayModelChunksByPointMap_t::iterator chunkIt=chunksByYX.begin(); while(chunkIt!=chunksByYX.end()) { displayModelChunk_t* chunk=NULL; BOOL isTempChunk=FALSE; if(IntersectRect(&tempRect,&rect,&(chunkIt->second->rect))) { chunk=chunkIt->second; //If this chunk is not fully covered by the rectangle //Copy it and truncate it so that it is fully covered if(chunk->rect.left<tempRect.left||chunk->rect.right>tempRect.right) { chunk=new displayModelChunk_t(*chunk); isTempChunk=TRUE; if(chunk->rect.left<tempRect.left) chunk->truncate(tempRect.left,TRUE); if(chunk->rect.right>tempRect.right) chunk->truncate(tempRect.right,FALSE); } } //Find out the current line's baseline curLineBaseline=chunkIt->first.first; //Iterate to the next possible chunk ++chunkIt; //If we have a valid chunk then add it to the current line if(chunk&&chunk->text.length()>0) { //Update the maximum height of the line if(curLineText.length()==0) { curLineMinTop=chunk->rect.top; curLineMaxBottom=chunk->rect.bottom; } else { if(chunk->rect.top<curLineMinTop) curLineMinTop=chunk->rect.top; if(chunk->rect.bottom>curLineMaxBottom) curLineMaxBottom=chunk->rect.bottom; } //Add space before this chunk if necessary if(((chunk->rect.left-lastChunkRight)>=minHorizontalWhitespace)&&(lastChunkRight>rect.left||!stripOuterWhitespace)) { generateWhitespaceXML((chunk->hwnd==lastChunkHwnd)?lastChunkHwnd:hwnd,curLineBaseline,curLineText); tempCharLocation.left=lastChunkRight; tempCharLocation.top=curLineBaseline-1; tempCharLocation.right=chunk->rect.left; tempCharLocation.bottom=curLineBaseline+1; curLineCharacterLocations.push_back(tempCharLocation); } //Add text from this chunk to the current line chunk->generateXML(curLineText); //Copy the character X positions from this chunk in to the current line deque<long>::const_iterator cxaIt=chunk->characterXArray.begin(); deque<RECT>::iterator curLineCharacterLocationsOldEnd=curLineCharacterLocations.end(); while(cxaIt!=chunk->characterXArray.end()) { tempCharLocation.left=*cxaIt; tempCharLocation.top=chunk->rect.top; ++cxaIt; tempCharLocation.right=(cxaIt!=chunk->characterXArray.end())?*cxaIt:chunk->rect.right; tempCharLocation.bottom=chunk->rect.bottom; curLineCharacterLocations.push_back(tempCharLocation); } lastChunkRight=chunk->rect.right; lastChunkHwnd=chunk->hwnd; } if((chunkIt==chunksByYX.end()||chunkIt->first.first>curLineBaseline)&&curLineText.length()>0) { //This is the end of the line if(((curLineMinTop-lastLineBottom)>=minVerticalWhitespace)&&(lastLineBottom>rect.top||!stripOuterWhitespace)) { //There is space between this line and the last, //Insert a blank line in between. generateWhitespaceXML(hwnd,-1,text); tempCharLocation.left=rect.left; tempCharLocation.top=lastLineBottom; tempCharLocation.right=rect.right; tempCharLocation.bottom=curLineMinTop; characterLocations.push_back(tempCharLocation); } //Insert this line in to the output. text.append(curLineText); characterLocations.insert(characterLocations.end(),curLineCharacterLocations.begin(),curLineCharacterLocations.end()); //Add a linefeed to complete the line if(!stripOuterWhitespace) { generateWhitespaceXML(hwnd,curLineBaseline,text); tempCharLocation.left=lastChunkRight; tempCharLocation.top=curLineBaseline-1; tempCharLocation.right=rect.right; tempCharLocation.bottom=curLineBaseline+1; characterLocations.push_back(tempCharLocation); } //Reset the current line values curLineText.clear(); curLineCharacterLocations.clear(); lastChunkRight=rect.left; lastChunkHwnd=NULL; lastLineBottom=curLineMaxBottom; if(chunk&&isTempChunk) delete chunk; } } if(!stripOuterWhitespace&&(rect.bottom-lastLineBottom)>=minVerticalWhitespace) { //There is a gap between the bottom of the final line and the bottom of the requested rectangle, //So add a blank line. generateWhitespaceXML(hwnd,-1,text); tempCharLocation.left=rect.left; tempCharLocation.top=lastLineBottom; tempCharLocation.right=rect.right; tempCharLocation.bottom=rect.bottom; characterLocations.push_back(tempCharLocation); } }
int putIntoTour(int length, int **adjacencyMatrix, const int &n, int vertex) { tour.insert(findClosest(vertex, adjacencyMatrix),vertex); return 0; }