Ejemplo n.º 1
0
void fileObj::display() {
    cout << "obj size is " << objSize << endl;
    for(int i=0; i< objSize; i++) {
        cout << " Num: " << i
             << " ID: " << getID(i)
             << " PAR: " << getPar(i)
             << " NAME: " << getName(i)
             <<  " PATH: " << getPath(i) << endl;
    }
}
Ejemplo n.º 2
0
int main(){
    int n;
    scanf("%d", &n);
    ll ans = 0;
    for (int i = 0; i*2 <= n; i++) {
        ans += getPar(n-i*2);
    }
    printf("%lld\n", ans);
    return 0;
}
Ejemplo n.º 3
0
bool Domain::removePar(const string &name)
{
    if(!contains(name))
        return false;

    Variable *var  = getPar(name);
    delete var;

    members.erase(members.find(name));
    return true;
}
Ejemplo n.º 4
0
void CorrelatedGaussianParameters::ParseCGP(std::vector<ModelParameter>& ModPars, 
                                            std::ifstream& ifile, 
                                            boost::tokenizer<boost::char_separator<char> >::iterator & beg,
                                            int rank)
{
    name = *beg;
    ++beg;
    int size = atoi((*beg).c_str());
    int nlines = 0;
    std::string line;
    bool IsEOF;
    boost::char_separator<char>sep(" \t");
    for (int i = 0; i < size; i++) {
        IsEOF = getline(ifile, line).eof();
        if (line.empty() || line.at(0) == '#') {
            if (rank == 0) std::cout << "ERROR: no comments or empty lines in CorrelatedGaussianParameters please!"
                    << std::endl;
            exit(EXIT_FAILURE);
        }
        lineNo++;
        boost::tokenizer<boost::char_separator<char> > tok(line, sep);
        beg = tok.begin();
        std::string type = *beg;
        ++beg;
        if (type.compare("ModelParameter") != 0)
            if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + filename + ", expecting a ModelParameter type here...\n");
        ModelParameter tmpMP;
        beg = tmpMP.ParseModelParameter(beg);
        if (beg != tok.end())
                if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
        tmpMP.setCgp_name(name);
        AddPar(tmpMP);
        nlines++;
    }
    if (nlines > 1) {
        gslpp::matrix<double> myCorr(gslpp::matrix<double>::Id(nlines));
        int ni = 0;
        for (int i = 0; i < size; i++) {
            IsEOF = getline(ifile, line).eof();
            if (line.empty() || line.at(0) == '#') {
                if (rank == 0) std::cout << "ERROR: no comments or empty lines in CorrelatedGaussianParameters please!"
                        << std::endl;
                exit(EXIT_FAILURE);
            }
            lineNo++;
            boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
            beg = mytok.begin();
            int nj = 0;
            for (int j = 0; j < size; j++) {
                if ((*beg).compare(0, 1, "0") == 0
                        || (*beg).compare(0, 1, "1") == 0
                        || (*beg).compare(0, 1, "-") == 0) {
                    if (std::distance(mytok.begin(), mytok.end()) < size && rank == 0) throw std::runtime_error(("ERROR: Correlation matrix is of wrong size in Correlated Gaussian Parameters: " + name).c_str());
                    myCorr(ni, nj) = atof((*beg).c_str());
                    nj++;
                    beg++;
                } else {
                    if (rank == 0) std::cout << "ERROR: invalid correlation matrix for "
                            << name << ". Check element (" << ni + 1 << "," << nj + 1 << ") in line number " + boost::lexical_cast<std::string>(lineNo) << std::endl;
                    exit(EXIT_FAILURE);
                }
            }
            ni++;
        }
        DiagonalizePars(myCorr);
        ModPars.insert(ModPars.end(), getDiagPars().begin(), getDiagPars().end());

    } else {
        if (rank == 0) std::cout << "\nWARNING: Correlated Gaussian Parameters " << name.c_str() << " defined with less than two correlated parameters. The set is being marked as normal Parameters." << std::endl;
        if (getPars().size() == 1) ModPars.push_back(ModelParameter(getPar(0)));
        for (int i = 0; i < size; i++) {
            IsEOF = getline(ifile, line).eof();
            lineNo++;
        }
    }
}
Ejemplo n.º 5
0
//Main program
int main(void) {
	int receivedBytes;//, sentBytes;
	//unsigned char outBuff[BUFFER_MAX];
	unsigned char inBuff[BUFFER_MAX];
	//Reserve memory
	char* cmd = malloc((BUFFER_MAX+1) * sizeof(char));
	char* par = malloc((BUFFER_MAX+1) * sizeof(char));
	struct commandStructure command;
	
	//Threads variables
	pthread_t logThread;
	pthread_t emailThread;
	//pthread_t debugThread;
	pthread_t webcamThread;
	pthread_t emailPhotoThread;
	
	//Empty buffers
	init();
	
	// Open serial file descriptor
	int fd = openSerial();
		
	//Loop to scan
	while(1){
		receivedBytes = read(fd,inBuff,BUFFER_MAX);
		if(receivedBytes > 0){						// Data found!
			
			if(DEBUG){ 
				printf("\nPayload size: %d\n",receivedBytes);
				int i;
				for(i=0;i<receivedBytes;i++){
					printf("%c",inBuff[i]);
				}
				printf("\n");
			}

			getCmd(inBuff,cmd);
			getPar(inBuff,par);
			int pars = parseParameters(par);
			
			if(!validCommand(cmd)){
				printf("Invalid Command: %s\n\n",cmd);
				continue;
			}else{	
				//printf("Command: %s\n",cmd);
				//int i = 0;
				//printf("Parameters found: %d\n",pars);
				//for(i=0;i<pars;i++) printf("Parameter %d - %s\n",i,PARAMETERS[i]);			
			}
			
			if(compareText(cmd,"Debug")){ //thread is detached so resources can be recycled.
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				
				time_t now = time(NULL);
				printf("%s - %s",PARAMETERS[0],ctime(&now));

				/*
				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&debugThread,&attr,debugFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
				*/
			}

			if(compareText(cmd,"Log")){
				if(pars < 1){
					printf("Error: No message sent\n");
					continue;
				}
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&logThread,&attr,logFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Email")){
				if(pars < 3){  //Need at least the email address and a subject
					printf("Error: Need 3 parameters: address, subject and message\n");
					continue;
				}
				
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];
				
				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailThread,&attr,emailFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Webcam")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&webcamThread,&attr,webcamFunc,&command))){
					fprintf(stderr,"Error: Could not create webcam thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);				
			}

			if(compareText(cmd,"EmailPhoto")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailPhotoThread,&attr,emailPhotoFunc,&command))){
					fprintf(stderr,"Error: Could not create emailPhoto thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
		}else if(receivedBytes == 0){				//No data yet! go back to loop
			continue;					
		}else if(receivedBytes < 0){				//Error reading, exit.
			printf("Error reading from file!\n");
			perror("Error: " );
			close(fd);
			return -1;
		}
		usleep(UDOONEO_POLL_DELAY);	// poll time approx 50mS (faster crashes the app)
	}

	//Free reserved memory
	free((void*)cmd);
	free((void*)par);

	//Close serial's file descriptor
	close(fd);	
}