BitArray AbstractNumberModel<T>::compressColumn(const string& columnStr, int column){ // Try to parse number, shared_ptr<AbstractNumber<T> > ptr = parseColumnStr(columnStr); int flag = 0; if(ptr->isIllegal()){ // Dispatch to dictModelPtr when fail BitArray flagCode = flagEncoderPtr->encode(int2Str(flag)); return flagCode + dictModelPtr->compressWord(columnStr); } else{ flag |= legalBit; BitArray numCode = compressNumber(ptr->getNumeric()); BitArray beforeCode; if(!ptr->getBefore().empty()){ flag |= beforeBit; beforeCode = dictModelPtr->compressWord(ptr->getBefore()); } BitArray afterCode; if(!ptr->getAfter().empty()){ flag |= afterBit; afterCode = dictModelPtr->compressWord(ptr->getAfter()); } BitArray flagCode = flagEncoderPtr->encode(int2Str(flag)); return flagCode + numCode + beforeCode + afterCode; } }
void traverse( struct TreeNode* root, struct TreeNode** stack, int top, char** sArr, int* count ) { if(root->left == NULL && root->right == NULL) { int size = top*3+1; sArr[*count] = (char*)malloc(sizeof(char)*size); sArr[*count][0] = '\0'; int index = 0; int i = 0; for(; i < top; i++) { char* s = int2Str(stack[i]->val); strcat(sArr[*count], s); strcat(sArr[*count], "->"); } char* s = int2Str(stack[i]->val); strcat(sArr[*count], s); (*count)++; } else { if(root->left != NULL) { stack[++top] = root->left; traverse(root->left, stack, top, sArr, count); } if(root->right != NULL) { if(root->left) --top; stack[++top] = root->right; traverse(root->right, stack, top, sArr, count); } } }
//AC - 0ms; char* getHint(char* secret, char* guess) { int map[10] = {0}; char* p = secret; while(*p) map[*(p++)-'0']++; #ifdef FOO for(int i = 0; i < 10; i++) printf("map[%d]: %d\n", i, map[i]); #endif int bulls = 0, cows = 0; p = secret; char *q = guess; while(*p) { int key = *q - '0'; if(*p == *q) { map[key]--; bulls++; } p++, q++; } p = secret; while(*p) { int key = *guess -'0'; if(*p != *guess && map[key] > 0) { cows++; map[key]--; } guess++, p++; } #ifdef FOO printf("bulls: %d\tcows: %d\n", bulls, cows); #endif int bSize, cSize; char *bullStr = int2Str(bulls, &bSize), *cowStr = int2Str(cows, &cSize); #ifdef FOO printf("bullsStr: %s\tcowsStr: %s\n", bullStr, cowStr); #endif char res[LEN] = ""; strcat(res, bullStr); res[bSize] = 'A'; res[bSize+1] = '\0'; strcat(res, cowStr); res[bSize+cSize+1] = 'B'; res[bSize+cSize+2] = '\0'; #ifdef FOO printf("result: %s\n", res); #endif return res; }
//AC - 8ms; //iterative calculation; int addDigits0(int num) { int len; char *s = int2Str(num, &len); while(len > 1) { num = 0; while(*s) num += *(s++) - '0'; s = int2Str(num, &len); } return num; }
/* 2.3 Method ~bool closeWorkerSocketCommunication~ * returns true - success */ bool DServerCmdWorkerCommunication::closeWorkerStreamCommunication() { return true; if (!m_workerIoStrOpen) { const string errMsg = "Cannot close worker cmd - connection " + m_worker -> getServerHostName() + ":" + int2Str(m_worker -> getServerPort()) + " : no stream opened!"; setCmdErrorText(errMsg); return false; } #ifdef DS_CMD_WORKER_COMM cout << "CLOSING WORKER connection " << m_worker -> getServerHostName() << ":" << m_worker -> getServerPort() << endl; #endif m_worker -> getServer() -> Close(); return true; }
void FixPrecisionNumber::initRegex(unsigned int precision){ if(FixPrecisionNumber::regex_initialized != precision){ FixPrecisionNumber::regex_initialized = precision; string precision_decimal_str = decimal_str + "{" + int2Str(precision) + "}"; regcomp(&FixPrecisionNumber::decimal_regex, precision_decimal_str.c_str(), cflags); } }
std::string LoggerSimp::timeToString(bool elapsed) { //----- // get the time in a pretty form. Also can get time elapsed // struct tm * timeinfo; char buffer [80]; time ( &mCurrentTime ); if(elapsed) { std::string tmp = ""; int tot_secs = (int)(difftime(mCurrentTime, mStartTime)); int tot_days = tot_secs / 86400; if(tot_days) { tmp += int2Str(tot_days)+"d "; tot_secs = tot_secs - (tot_days * 86400); } int tot_hours = tot_secs / 3600; if(tot_hours) { tmp += int2Str(tot_hours)+"h "; tot_secs = tot_secs - (tot_hours * 3600); } int tot_mins = tot_secs / 60; if(tot_mins) { tmp += int2Str(tot_mins)+"m "; tot_secs = tot_secs - (tot_mins * 60); } tmp += int2Str(tot_secs)+"s"; return tmp; } else { timeinfo = localtime ( &mCurrentTime ); strftime (buffer,80,"%d/%m/%Y_%I:%M",timeinfo); std::string tmp(buffer); return tmp; } }
string AbstractNumberModel<T>::dumpFlagEncoder() const{ if(flagEncoderPtr == NULL){ // in train stage unordered_map<string, int> dict; for(auto it = flag2FreqDict.begin(); it != flag2FreqDict.end(); it++) dict[int2Str(it->first)] = it->second; shared_ptr<Encoder> tmp = CompressorFactory::createDictEncoder(); tmp->buildCoder(dict); return tmp->dump(); } return flagEncoderPtr->dump(); }
TupleType::TupleType( const ListExpr typeInfo ): noAttributes(0), attrTypeArray(0),totalSize(0),refs(1),coreSize(0) { int i = 0; size_t offset = sizeof(uint16_t); try { const string errMsg("TupleType: Wrong list format! Line "); if ( nl->ListLength(typeInfo) != 2) { // ( <tuple> <attr_list> ) throw SecondoException(errMsg + int2Str(__LINE__)); } noAttributes = nl->ListLength( nl->Second( typeInfo ) ); ListExpr rest = nl->Second( typeInfo ); attrTypeArray = new AttributeType[noAttributes]; while( !nl->IsEmpty( rest ) ) { ListExpr list = nl->First( rest ); if (nl->ListLength(list) != 2){ //( <attr_name> <attr_desc> ) throw SecondoException(errMsg + int2Str(__LINE__)); } //list = (a b ...) ListExpr b = nl->Second( list ); rest = nl->Rest( rest ); int algId=0, typeId=0, clsSize=0; if( nl->IsAtom(b) ){ throw SecondoException(errMsg + int2Str(__LINE__)); } ListExpr b1 = nl->First( b ); if( nl->IsAtom( b1 ) ) //b = (b1 b2 ...) { if ( nl->ListLength(b) < 2 ){ throw SecondoException(errMsg + int2Str(__LINE__)); } //b = (algid typeid ...) algId = nl->IntValue( nl->First( b ) ), typeId = nl->IntValue( nl->Second( b ) ), clsSize = (am->SizeOfObj(algId, typeId))(); } else { if ( nl->ListLength(b1) < 2 ){ throw SecondoException(errMsg + int2Str(__LINE__)); } //b1 = ((algid typeid ...) ...) algId = nl->IntValue( nl->First(b1) ); typeId = nl->IntValue( nl->Second(b1) ); clsSize = (am->SizeOfObj(algId, typeId))(); } int currentCoreSize = 0; TypeConstructor* tc = am->GetTC(algId, typeId); int numOfFlobs = tc->NumOfFLOBs(); bool extStorage = false; if ( tc->GetStorageType() == Attribute::Extension ) { currentCoreSize = sizeof(uint32_t); extStorage = true; } else if ( tc->GetStorageType() == Attribute::Default ) { currentCoreSize = clsSize; } else { currentCoreSize = tc->SerializedFixSize(); } //totalSize += clsSize; totalSize += currentCoreSize; attrTypeArray[i++] = AttributeType( algId, typeId, numOfFlobs, clsSize, currentCoreSize, extStorage, offset ); coreSize += currentCoreSize; offset += currentCoreSize; } } catch (SecondoException e) { cerr << e.msg() << endl; cerr << "Input list: " << nl->ToString(typeInfo) << endl; cerr << "Assuming list: " << "(a1 (algid typeid) a2 (algid typeid) ....) or" << endl; cerr << " " << "(a1 ((algid typeid)) a2 ((algid typeid)) ....) " << endl; } }
void DServerCmdWrite::run() { #if DS_CMD_WRITE_DEBUG cout << "DServerCmdWrite::run" << getIndexStr() << endl; #endif if (!checkWorkerAvailable()) return; if (!startWorkerStreamCommunication()) { setErrorText("Could not initiate communication!"); return; } int algID,typID; arrayalgebra::extractIds(getWorker() -> getTType(),algID,typID); TypeConstructor* tc = am->GetTC(algID,typID); while (nextIndex()) { const int curIdx = getIndex(); string master_port =int2Str((1800+curIdx)); string sendCmd = "let r" + getWorker() -> getName() + int2Str(curIdx) + " = " + "receiveD(" + getWorker() -> getMasterHostIP_() + ",p" + master_port + ")"; #if DS_CMD_WRITE_DEBUG cout << "Sending:" << sendCmd << endl; #endif //The sendD-operator on the worker is started if (!sendSecondoCmdToWorkerSOS(sendCmd, true)) { string errMsg; if (hasCmdError()) errMsg = getCmdErrorText(); else errMsg = "Could not write data from worker!"; setErrorText(errMsg); waitForSecondoResultThreaded(); return; } // open communication with receiveD - TypeMap DServerCmdCallBackCommunication callBack(getWorker() ->getMasterHostIP(), master_port); callBack.startSocket(); callBack.startSocketCommunication(); // send TYPE to receiveD - TypeMap if (!callBack.sendTextToCallBack("TYPE", getWorker() -> getTTypeStr())) {; waitForSecondoResultThreaded(); return; } // await "CLOSE" tag if (!callBack.getTagFromCallBack("CLOSE")) { waitForSecondoResultThreaded(); return; } // stop communication w/ reveive TypeMap callBack.closeSocketCommunication(); // type map has finished //The callback connection from the value-mapping // is opened and stored if (!(callBack.startSocketCommunication())) { setErrorText(callBack.getErrorText()); waitForSecondoResultThreaded(); return; } // send TYPE to receiveD - TypeMap if (!callBack.sendTextToCallBack("TYPE", getWorker() -> getTTypeStr())) { setErrorText(callBack.getErrorText()); waitForSecondoResultThreaded(); return; } if (!callBack.getTagFromCallBack("GOTTYPE")) { setErrorText(callBack.getErrorText()); waitForSecondoResultThreaded(); return; } //The element is converted into a binary stream of data SmiRecordFile recF(false,0); SmiRecord rec; SmiRecordId recID; Cmd_Mutex.acquire(); recF.Open("send"); recF.AppendRecord(recID,rec); size_t size = 0; am->SaveObj(algID,typID,rec,size,getWorker() -> getTType(), (*(getInElements()))[curIdx]); char* buffer = new char[size]; rec.Read(buffer,size,0); //rec.Truncate(3); recF.DeleteRecord(recID); recF.Close(); Cmd_Mutex.release(); //Size of the binary data is sent if (!callBack.sendTextToCallBack("SIZE", size)) { waitForSecondoResultThreaded(); return; } #if DS_CMD_WRITE_DEBUG cout << "Send Size:" << size << endl; #endif //The actual data are sent if (!callBack.Write(buffer,size)) { waitForSecondoResultThreaded(); return; } delete [] buffer ; Attribute* a; if(tc->NumOfFLOBs() > 0 ) a = static_cast<Attribute*>((am->Cast(algID,typID)) (((*(getInElements()))[curIdx]).addr)); //Flobs are sent to worker for(int i = 0; i < tc->NumOfFLOBs(); i++) { //send FLOB Tag as info if (!callBack.sendTagToCallBack("FLOB")) { waitForSecondoResultThreaded(); return; } Flob* f = a->GetFLOB(i); //Flob is converted to binary data SmiSize si = f->getSize(); int n_blocks = si / 1024 + 1; char* buf = new char[n_blocks*1024]; memset(buf,0,1024*n_blocks); f->read(buf,si,0); #if DS_CMD_WRITE_DEBUG cout << "Send Flob - Size:" << si << endl; #endif //Size of the binary data is sent if (!callBack.sendTextToCallBack("FLOBSIZE", si)) { waitForSecondoResultThreaded(); return; } //Flob data is sent for(int j = 0; j<n_blocks;j++) callBack.Write(buf+j*1024,1024); delete [] buf; //send FLOB Tag as info bool noErr = true; if (!callBack.getTagFromCallBackTF("GOTFLOB", "ERROR", noErr)) { waitForSecondoResultThreaded(); return; } if (!noErr) { waitForSecondoResultThreaded(); return; } } // if (!callBack.sendTagToCallBack("CLOSE")) { waitForSecondoResultThreaded(); return; } if (!callBack.getTagFromCallBack("FINISH")) { waitForSecondoResultThreaded(); return; } callBack.closeCallBackCommunication(); if (!waitForSecondoResultThreaded()) { string errMsg; if (hasCmdError()) errMsg = getCmdErrorText(); else errMsg = "Could not write data to worker!"; setErrorText(errMsg); } } // while(nextIndex()) if (!closeWorkerStreamCommunication()) { setErrorText("Could not stop communication!"); return; } #if DS_CMD_WRITE_DEBUG cout << "DServerCmdWrite::run DONE" << endl; #endif } // run()