template <class ReplyT> long Redox::freeAllCommandsOfType() { lock_guard<mutex> lg(free_queue_guard_); lock_guard<mutex> lg2(queue_guard_); lock_guard<mutex> lg3(command_map_guard_); auto &command_map = getCommandMap<ReplyT>(); long len = command_map.size(); for (auto &pair : command_map) { Command<ReplyT> *c = pair.second; c->freeReply(); // Stop the libev timer if this is a repeating command if ((c->repeat_ != 0) || (c->after_ != 0)) { lock_guard<mutex> lg3(c->timer_guard_); ev_timer_stop(c->rdx_->evloop_, &c->timer_); } delete c; } command_map.clear(); commands_deleted_ += len; return len; }
int DirPage::deleteRecords(Where *where, int* attrType, int numOfAttr){ if(DEcount == 0){ lg2("@DirPage_"<<pageid<<" : No Records to delete."); return 0; } DataPage* datapage; long noOfRecs = 0; long dataPid; long maxTFS; DirEntry *de; // dirEntries; for(vector<DirEntry*>::size_type i = 0; i<dirEntries.size() ; i++){ //create DataPage for every entry and try retreiving records. de = dirEntries[i]; dataPid = de->getPageID(); datapage = new DataPage(dataPid); noOfRecs += datapage->deleteRecords(where,attrType,numOfAttr); maxTFS = datapage->getTotalFreeSize(); de->setTFS(maxTFS); if(maxSpaceAvailable < maxTFS) maxSpaceAvailable = maxTFS; de->writeDE(p,i); writeToPage(); delete datapage; } return noOfRecs; }
int DirPage::retrieveRecords(RecordSet* rs, Select *select, Where *where){ if(DEcount == 0){ lg2("@DirPage_"<<pageid<<" : No Records to retrieve."); return 0; } DataPage* datapage; long noOfRecs = 0; int dataPid; // dirEntries; for(vector<DirEntry*>::size_type i = select->getStartDirEntryID(); i<dirEntries.size() ; i++){ //create DataPage for every entry and try retreiving records. dataPid = dirEntries[i]->getPageID(); datapage = new DataPage(dataPid); noOfRecs += datapage->retrieveRecords(rs,select,where); if(select->isThereMore()){ select->setStartDirEntryID(i); delete datapage; return noOfRecs; } delete datapage; } return noOfRecs; }
int main() { #ifdef STACK_TESTS_ON /* [STACK] */ std::thread stack_thread(stack_test_suite_main); if (stack_thread.joinable()) { stack_thread.detach(); // F&F } std::cout << stack_thread.get_id() << std::endl; #endif #ifdef QUEUE_TESTS_ON /* [QUEUE] */ std::thread queue_thread(queue_test_suite_main); //queue_thread.detach(); if (queue_thread.joinable()) { queue_thread.join(); } #endif std::thread __ttt; std::cout << __ttt.get_id() << std::endl; std::cout << std::thread::hardware_concurrency() << std::endl; std::cout << std::this_thread::get_id() << std::endl; std::mutex m1; std::mutex m2; //std::lock_guard<std::mutex> g(m1); { std::lock(m1, m2); std::lock_guard<std::mutex> lg1(m1, std::adopt_lock); std::lock_guard<std::mutex> lg2(m2, std::adopt_lock); } std::lock(m1, m2); std::lock_guard<std::mutex> lg1(m1, std::adopt_lock); std::lock_guard<std::mutex> lg2(m2, std::adopt_lock); }
int main(void) { int i; printf("%016llx\n",pw2(5,0x1ull)); printf("%016llx\n",pop(0x11100ull)); printf("%016llx\n",rev(0x1ull)); printf("%016llx\n",lg2(0x5ull)); return 0; }
void DirPage::printDetails(){ lg2("Directory Page"); lg2("Page : "<< pageid); lg2("Prev Page : " << prevPage); lg2("Next Page : " << nextPage); lg2("Max space in a DE : "<<maxSpaceAvailable); lg2("Totals DEs : "<<DEcount); lg2(""); }
void DirPage::deleteAll(){ if(DEcount == 0){ lg2("@DirPage_"<<pageid<<" : No Pages to delete."); return; } Cache *cache = Cache::getInstance(); // dirEntries; for(vector<DirEntry*>::size_type i = 0; i<dirEntries.size() ; i++){ //create DataPage for every entry and try retreiving records. cache->deletePage(dirEntries[i]->getPageID()); } }
void HighlightButton::paintEvent(QPaintEvent *) { QPainter painter(this); painter.translate(width() / 2, height() / 2); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::transparent); painter.fillRect(-width(), -height(), width() * 2, height() * 2, Qt::gray); int radius = 100; // 外边框 QLinearGradient lg1(0, -radius, 0, radius); lg1.setColorAt(0, QColor(255, 255, 255)); lg1.setColorAt(1, QColor(166, 166, 166)); painter.setBrush(lg1); painter.drawEllipse(-radius, -radius, radius << 1, radius << 1); // 内边框 radius -= 13; QLinearGradient lg2(0, -radius, 0, radius); lg2.setColorAt(0, QColor(155, 155, 155)); lg2.setColorAt(1, QColor(255, 255, 255)); painter.setBrush(lg2); painter.drawEllipse(-radius, -radius, radius << 1, radius << 1); // 内部的圆 radius -= 4; QRadialGradient rg(0, 0, radius); rg.setColorAt(0, QColor(245, 0, 0)); rg.setColorAt(0.6, QColor(210, 0, 0)); rg.setColorAt(1, QColor(140, 0, 0)); painter.setBrush(rg); painter.drawEllipse(-radius, -radius, radius << 1, radius << 1); // 高光 radius -= 3; QPainterPath path; path.addEllipse(-radius, -radius - 2, radius << 1, radius << 1); QPainterPath bigEllipse; radius *= 2; bigEllipse.addEllipse(-radius, -radius + 140, radius << 1, radius << 1); path -= bigEllipse; QLinearGradient lg3(0, -radius / 2, 0, 0); lg3.setColorAt(0, QColor(255, 255, 255, 220)); lg3.setColorAt(1, QColor(255, 255, 255, 30)); painter.setBrush(lg3); painter.drawPath(path); }
void CustomWidget::paintEvent ( QPaintEvent * event ) { QPainter p(this); p.drawText(20, 100, "Hello World"); QLinearGradient lg(10, 10, 210, 210); lg.setColorAt(0, QColor(0, 0, 255, 255)); lg.setColorAt(1, QColor(255, 255, 255, 0)); p.fillRect(10, 10, 200, 200, lg); p.drawText(230, 100, "Hello World"); QLinearGradient lg2(220, 10, 420, 210); lg2.setColorAt(0, QColor(0, 0, 255, 255)); lg2.setColorAt(1, QColor(255, 255, 255, 255)); p.fillRect(220, 10, 200, 200, lg2); }
/** * @pre * for all i ws[i][0]>ws[i+1][0] and ws[i][0]>0 ws[i][1]>0 * @param ws: * ws[i][0] = weight i * ws[i][1] = number of weight i in constraint * @return SearchMetaData* */ SearchMetaData* bnb_oddEven_Cost_search(unsigned int weights[][2],int length,std::vector<unsigned int>& primes,unsigned int cutoff,bool nonPrimePossible,bool abstraction){ SearchMetaData* md = new SearchMetaData(lg2(weights[0][0]),cutoff,weights[0][0],length,"BNB_oddEven_comp"); //preperations THeap que; std::map<unsigned int,bss> homomorphism; unsigned int sum[length+1]; sum[0] = 0; unsigned long long temp=0; for(int i=0;i<length;i++) { sum[i+1]=sum[i]+weights[i][1]; temp+=weights[i][0]*weights[i][1]; } temp = oddEvenCount(temp); BaseSearchState* startState=new BaseSearchState(0,length,1,0,0,0); BaseSearchState* bestStateFound(0); que.offer(startState); unsigned long long bestFound = oddEvenCountEval(weights, md->base,length); if (bestFound>temp) { // relaxs betwin The empty base and the Binary base bestFound =temp; bestStateFound = startState; } std::vector<bss> old; while(!que.isEmpty() && que.peek()->hCost < bestFound ) { //main A star loop; md->nodesExpended++; bss father = que.poll(); old.push_back(father); for(unsigned int i=0;i<primes.size() && father->baseMul*primes[i]<=weights[0][0];i++){ createChild(father,primes[i],weights,sum,&bestFound,&bestStateFound,que,*md,old,homomorphism,abstraction); //create regular chiled if (nonPrimePossible && father->parent!=0) //create compresion chiled createChild(father->parent,(father->baseMul / father->parent->baseMul)*primes[i],weights,sum,&bestFound,&bestStateFound,que,*md,old,homomorphism,abstraction); } } if (bestStateFound!=0) { std::vector<int> &base = md->base; base.clear(); BaseSearchState* temp = bestStateFound; while (temp->parent!=0){ base.insert(base.begin(),temp->baseMul / temp->parent->baseMul); temp = temp->parent; } } md->finalize(bestFound); for(unsigned int i=0;i<old.size();i++) delete old[i]; return md; }
void checkArray(int A[], int n, int s) { int *B; // temporary buffer int nerrs; #ifdef DEBUG int checkPre, checkPost; // checksums for unsorted and sorted // arrays #endif int i; B = (int *) malloc(n * sizeof(int)); assert(B != NULL); genArray(B, n, s); #ifdef DEBUG checkPre = 0; for (i = 0; i < n; i++) checkPre += B[i]; #endif #ifdef CHECK_PIVOT_QUALITY printf("average quick sort call depth = %ld (ideal=%d)\n", qCalls == 0 ? 0 : qDepthSum / qCalls, lg2(n)); #endif qsort(B, n, sizeof(int), compint); #ifdef DEBUG checkPost = n > 0 ? B[0] : 0; nerrs = 0; for (i = 1; i < n; i++) { nerrs += (B[i - 1] > B[i]); checkPost += B[i]; } if (nerrs) printf("Error in sort() detected: %d out-of-order elements\n", nerrs); if (checkPost != checkPre) printf("Error in sort() detected: unexpected values present\n"); #endif nerrs = 0; for (i = 0; i < n; i++) nerrs += (B[i] != A[i]); printf("check of sort of array of length %d from seed %d: %d errors\n", n, s, nerrs); free(B); } // checkArray()
void UnicornVolumeSlider::resizeEvent( QResizeEvent* event ) { if( event->oldSize().height() == event->size().height() ) return; QPalette p = palette(); QLinearGradient lg( 0, 0, 0, event->size().height() / 2); lg.setColorAt( 0, 0x404040 ); lg.setColorAt( 1, 0x222222 ); p.setBrush( QPalette::Window, lg ); QLinearGradient lg2( 0, event->size().height() / 2, 0, event->size().height()); lg2.setColorAt( 0, 0x0e0e0e ); lg2.setColorAt( 1, 0x1b1a1a ); p.setBrush( QPalette::AlternateBase, lg2 ); setPalette( p ); }
DirPage::DirPage(long pid):Page(pid){ DirPage* dp; char* buf = new char[sizeof(DirPage)]; memcpy(buf,p,sizeof(DirPage)); dp = (DirPage*)buf; nextPage = dp->nextPage; prevPage = dp->prevPage; DEcount = dp->DEcount; maxSpaceAvailable = dp->maxSpaceAvailable; dirEntries = DirEntry::getAllEntries(p,DEcount); for(unsigned long i = 0;i<DEcount;i++){ lg2("DEs: "<< i<<" " << dirEntries[i]->getPageID() << " "<<dirEntries[i]->getTFS()); } printDetails(); delete []buf; }
// create a partition of A[0..n-1] that is likely to be balanced for // random and near-sorted arrays. int partition(int A[], int n) { int i, j, pivot; if (n <= 1) return 0; else if (n < MIN_PIVOT_SEARCH) pivot = A[n / 2]; else { // select median from a small portion of // A[] // note: it is essential that partition() be thread-safe for // quickThread(), // so sA must not be in a static memory area, i.e. be on the stack // int sz = 2 * lg2(n), stride = n / sz, sA[sz], i; for (i = 0; i < sz; i++) sA[i] = A[i * stride]; pivot = median(sA, sz, sz / 2); } i = 0, j = n - 1; while (i <= j) { // invariant: A[lo..i-1] <= pivot <= // A[j+1..hi] while (A[i] < pivot) i++; while (pivot < A[j]) j--; if (i <= j) { int t = A[i]; A[i] = A[j], A[j] = t; i++; j--; } } // while(...) if (j < 0) j = 0; return (j); } // partition()
/** * Probably one of the more interesting functions. */ SearchMetaData* searchForBase(vec<Int>& inCoeffs, vec<int>& outputBase,PrimesLoader& pl, PBOptions* options) { std::map<unsigned int,unsigned int> multiSet; vecToMultiSet(inCoeffs,multiSet); unsigned int weights[multiSet.size()][2]; coeffsToDescendingWeights(multiSet, weights); SearchMetaData* data = 0; unsigned int cutof = pl.loadPrimes(weights[0][0],options->opt_max_generator); std::vector<unsigned int>& pri = pl.primeVector(); if (options->opt_base == base_Forward) data = findBaseFWD(weights, multiSet.size(), pri,cutof); else if (options->opt_base == base_SOD) data = bnb_SOD_Carry_Cost_search(weights, multiSet.size(),pri,cutof,false,true,true); else if (options->opt_base == base_Carry) data = bnb_SOD_Carry_Cost_search(weights, multiSet.size(), pri,cutof,options->opt_non_prime,options->opt_abstract); else if (options->opt_base == base_Comp) data = bnb_Comp_Cost_search(weights, multiSet.size(), pri,cutof,options->opt_non_prime,options->opt_abstract); else if (options->opt_base == base_oddEven) data = bnb_oddEven_Cost_search(weights, multiSet.size(), pri,cutof,options->opt_non_prime,options->opt_abstract); else if (options->opt_base == base_Rel) data = bnb_Relative_search(weights, multiSet.size() , pri,cutof,options->opt_non_prime,options->opt_abstract); else if (options->opt_base == base_Bin) { data = new SearchMetaData(lg2(weights[0][0]),cutof,weights[0][0],multiSet.size(),"BinaryBase"); data->finalize(0); } else if (options->opt_base == base_M) { vec<Int> dummy; int cost; data = optimizeBase(inCoeffs, dummy, cost, outputBase,weights,multiSet.size(), cutof); } else printf("Unknown option!"), exit(-1); data->inputCountCost = inputCountEval(weights,data->base,multiSet.size()); data->carryOnlyCost = carryOnlyEval(weights,data->base,multiSet.size()); data->compCost = compCountEval(weights,data->base,multiSet.size()); for (unsigned int j=0;j<multiSet.size();j++){ data->emptyBaseNOI += weights[j][0]*weights[j][1]; data->numOfCoffes += weights[j][1]; } carryVecEval(weights,data->base,multiSet.size(),data->carry); inputVecEval(weights,data->base,multiSet.size(),data->inputs); if (options->opt_verbosity >= 1) { data->print(); printf("Run time is in micro seconds!!!\n"); } if (options->opt_base != base_M) metaDataToBase(data, outputBase); options->baseMetaData.push_back(data); return data; }
long DirPage::updateRecords(Where *where, Modify *modify, RecordSet* rs){ if(DEcount == 0){ lg2("@DirPage_"<<pageid<<" : No Records to update."); return 0; } DataPage* datapage; long noOfRecs = 0; long dataPid; long maxTFS; DirEntry *de; // dirEntries; for(vector<DirEntry*>::size_type i = modify->getStartDirEntry(); i<dirEntries.size() ; i++){ //create DataPage for every entry and try retreiving records. de = dirEntries[i]; dataPid = de->getPageID(); datapage = new DataPage(dataPid); noOfRecs += datapage->updateRecords(where,modify,rs); maxTFS = datapage->getTotalFreeSize(); de->setTFS(maxTFS); if(maxSpaceAvailable < maxTFS) maxSpaceAvailable = maxTFS; de->writeDE(p,i); if(modify->isThereMore()){ writeToPage(); modify->setStartDirEntry(i); delete datapage; return noOfRecs; } delete datapage; } writeToPage(); return noOfRecs; }
/** * 重置迭代器 */ static void __set_it_reset(void *it) { if (it && ((set_it_p)it)->set) { set_it_p iterator = (set_it_p)it; set_p set = iterator->set; unsigned int len = lg2(set->size + 1); len = len << 1; // 红黑树最大树高度小于2*lg2(size+1) rbt_node_p *stack = (rbt_node_p *)malloc(len * sizeof(rbt_node_p)); if (stack) { free(iterator->stack); iterator->stack = stack; iterator->top = iterator->stack; pthread_mutex_lock(&set->mut); iterator->changes = set->changes; rbt_node_p current = set->root; while (current != NULL) { __it_push(iterator, current); current = iterator->asc ? current->left : current->right; } pthread_mutex_unlock(&set->mut); } } }
static set_it_p __set_iterator(set_p set, int dir) { set_it_p ret = (set_it_p)malloc(sizeof(set_it_t)); unsigned int len = lg2(set->size + 1); len = len << 1; // 红黑树最大树高度小于2*lg2(size+1) rbt_node_p *stack = (rbt_node_p *)malloc(len * sizeof(rbt_node_p)); if (ret && stack) { ret->asc = dir; ret->set = set; ret->stack = stack; ret->top = ret->stack; ret->changes = set->changes; rbt_node_p current = set->root; while (current != NULL) { __it_push(ret, current); current = ret->asc ? current->left : current->right; } } else { free(ret); free(stack); ret = NULL; } return ret; }
int index(int l, int r) { int b = lg2(r - l); return cmp(rmq[b][l], rmq[b][r - bin(b)]); }
double BigInteger::doubleValueOf() const { // todo: there's got to be a quicker/smaller code alg for this. if (numWords == 1) return (double)wordBuffer[0]; // determine how many of bits are used by the top word int bitsInTopWord=1; for(uint32 topWord = wordBuffer[numWords-1]; topWord > 1; topWord >>= 1) bitsInTopWord++; // start result with top word. We will accumulate the most significant 53 bits of data into it. int nextWord = numWords-1; // used for rounding: bool bit53 = false; bool bit54 = false; bool rest = false; const uint64_t ONEL = 1UL; uint64_t resultMantissa = 0; uint64_t w = 0; int pos = 53; int bits = bitsInTopWord; int wshift = 0; while(pos > 0) { // extract word and | it in w = wordBuffer[nextWord--]; resultMantissa |= (w >> wshift); pos -= bits; if (pos > 0) { if (nextWord > -1) { // ready for next word bits = (pos > 31) ? 32 : pos; wshift = (pos > 31) ? 0 : 32-bits; resultMantissa <<= bits; } else { break; // not enough data for full 53 bits } } } // rounding if (pos > 0) { ; // insufficient data for rounding } else { bit53 = ( resultMantissa & 0x1 ) ? true : false; if (bits == 32) { // last extract was exactly 32 bits, so rest of bits are in next word if there is one if (nextWord > -1) { w = wordBuffer[nextWord--]; bit54 = ( w & (ONEL<<31) ) ? true : false; rest = ( w & ((ONEL<<31)-1) ) ? true : false; } } else { // we know bit54 is in this word but the rest of the data may be in the next AvmAssert(bits < 32 && wshift > 0); bit54 = ( w & (ONEL<<(wshift-1)) ) ? true : false; if (wshift > 1) rest = ( w & ((ONEL<<(wshift-1))-1) ) ? true : false; // pick up any residual bits if (nextWord > -1) rest = rest || (wordBuffer[nextWord--] != 0); } } /** * ieee rounding is to nearest even value (bit54 is 2^-1) * x...1 1.. -> round up since odd (but53 set) * x...0 1...1.. -> round up since value > 1/2 */ if (bit54 && (bit53 || rest)) resultMantissa += 1; double result=0; int32 expBase2 = lg2() + 1 - 53; // if negative, then we've already captured all the data in the mantissaResult and we can ignore. result = (double)resultMantissa; if (expBase2 > 0) { if (expBase2 < 64) { uint64_t uint64_1 = 1; result *= (double)(uint64_1 << expBase2); } else { result *= MathUtils::pow(2,expBase2); } } return result; }
int Cache::writePage(long pageid, char* page){ db.seekp(pageSize*pageid,ios_base::beg); lg2("Writing page : "<<pageid); db.write(page,pageSize); return 1; }