int main(int nargs, char * args[]) {	
	if(nargs < 2) {
		fprintf(stderr, "USAGE: %s <symbol_string>\n", args[0]);
		exit(EXIT_FAILURE); 
	}
	//symbol index is symbol char val - 'a'
	//ASSUMES chars are all lowercase
	//determine frequency of symbols
	calcFrequency(args[1]);
	printFrequency();
	
	//build the priority q for each char/frequency in frequency array
	initQueue();
	buildQueue();
	
	//priority queue is done so build the huffman tree
	root = NULL;
	buildHuffmanTree();
	if(root == NULL) {
		freeQueue();//should already be empty, but just in case
		return(EXIT_FAILURE);
	}
	
	calcCodes();
	
	if(nargs > 2) 
		printHuffmanCode(args[2]);
	else
		printHuffmanCode(args[1]);
	
	freeTree();
	return(EXIT_SUCCESS);
}
Exemplo n.º 2
0
bool AI::run()
{
  realLight=(playerID())?player1Light():player0Light();
  cout<<"Turn: "<<turnNumber()<<" Player: "<<playerID()<<" Light: "<<realLight<<endl;
  cout<<"Plants: "<<plants.size()<<endl;
  if(plants.size()<2)
  {
    return true;
  }
  //if the queue is empty
  if(que.empty())
  {
  //  cout<<"First time on this turn"<<endl;
    resetDataStructures();
    buildQueue();
  }
  //cout<<"Queue Size: "<<que.size()<<endl;
  //runs once
  return doActions();
  /*
  while(!que.empty())
  {
    que.pop();
  }
  return true;
  */
  //otherwise
  //return doActions();
  /*
  while(!doActions()){};
  return true;
  */
}
Exemplo n.º 3
0
void buildTree(void) {
	buildQueue();
	printQueue();

	int lvalue,rvalue;
	if ( !head ) exit(EXIT_FAILURE);
	while ( head -> right ) {
		Node* temp = createNode();
		temp 	-> bottomleft = head;
		temp	-> bottomright = head -> right;
		head 	=  head  -> right -> right;

		temp	-> bottomleft	-> right = NULL;
		temp	-> bottomright	-> right = NULL;
		
		if ( temp -> bottomleft -> data.datatype.type == LETTER ) 
			lvalue	= characters[ temp -> bottomleft -> data.dataletter.letter ].count;
		else 	lvalue	= temp -> bottomleft -> data.datanum.num;

		if ( temp -> bottomright -> data.datatype.type == LETTER ) 
			rvalue = characters[ temp -> bottomright -> data.dataletter.letter ].count;
		else rvalue	= temp -> bottomright -> data.datanum.num;

		temp -> data.datatype.type = NUMBER;
		temp -> data.datanum.num= lvalue + rvalue;

		if ( !head ) head	= temp;
		else addToQueue( temp );
		}
	buffer	= (unsigned char*)malloc	( currsize );
	traverseTree(head,0UL);
	
	//creation of code is done.

	// tester started..
	for ( int var = 0; var <128 ;++ var ) {
		if ( characters[var].count ){
			printf("%c\t%s\n",var,characters[var].bitstring);
		//	writeBits( characters[var].bitstring );	
			}
		}
	}
Exemplo n.º 4
0
/*!
 * \brief IPProcessGrid::execute
 */
void IPProcessGrid::execute(bool forcedUpdate /* = false*/)
{
    // if no processes yet, then exit
    if(_scene->steps()->size() < 1)
    {
        return;
    }

    // if already running or nothing has changed, exit
    if(_isRunning || !_updateNeeded)
    {
        return;
    }
    // prevent user changes during execution
    _mainWindow->lockScene();
    _isRunning = true;
    _sequenceCount = 0;

    buildQueue();

    int totalDurationMs = 0;

    // execute the processes
    int counter = 0;
    int limit = 10000;

    QList<IPProcessStep*> afterProcessingList;

    QListIterator<IPProcessStep *> it(_processList);
    while (it.hasNext() && counter < limit)
    {
        if(_stopExecution)
            return;

        IPProcessStep* step = it.next();
        _currentStep = step;

        // make sure the progress bar gets filled
        updateProgress(1);

        // source processes don't have inputs
        if(step->process()->isSource())
        {
            // check if is sequence
            //IPLLoadImageSequence* sequenceProcess = dynamic_cast<IPLLoadImageSequence*>(step->process());

            // update if index has changed
            /*if(sequenceProcess && (_sequenceIndex != _lastSequenceIndex))
            {
                sequenceProcess->setSequenceIndex(_sequenceIndex);
                propagateNeedsUpdate(sequenceProcess);
            }*/

            // execute thread
            if(!step->process()->isResultReady() || forcedUpdate)
            {
                step->process()->resetMessages();
                step->process()->beforeProcessing();
                int durationMs = executeThread(step->process());
                //step->process()->afterProcessing();

                // afterProcessing will be called later
                afterProcessingList.append(step);

                totalDurationMs += durationMs;
                step->setDuration(durationMs);

                if(!step->process()->hasErrors())
                    step->updateThumbnail();

                // update error messages
                _mainWindow->updateProcessMessages();
            }

            /*if(sequenceProcess)
            {
                int currentSequenceCount = sequenceProcess->sequenceCount();
                _sequenceCount = std::max(_sequenceCount, currentSequenceCount);
                emit sequenceChanged(_sequenceIndex, _sequenceCount);
            }*/
        }
        else
        {
            if(!step->process()->isResultReady() || forcedUpdate)
            {
                // execute process once for every input
                for(int i=0; i < step->edgesIn()->size(); i++)
                {
                    IPProcessEdge* edge = step->edgesIn()->at(i);
                    int indexFrom = edge->indexFrom();
                    int indexTo = edge->indexTo();
                    IPProcessStep* stepFrom = edge->from();

                    IPLImage* result = static_cast<IPLImage*>(stepFrom->process()->getResultData(indexFrom));

                    // invalid result, stopp the execution
                    if(!result)
                    {
                        QString msg("Invalid operation at step: ");
                        msg.append(QString::fromStdString(stepFrom->process()->title()));
                        _mainWindow->showMessage(msg, MainWindow::MESSAGE_ERROR);
                        break;
                    }

                    // execute thread
                    step->process()->resetMessages();
                    step->process()->beforeProcessing();
                    int durationMs = executeThread(step->process(), result, indexTo, mainWindow()->useOpenCV());
                    //step->process()->afterProcessing();

                    // afterProcessing will be called later
                    afterProcessingList.append(step);

                    totalDurationMs += durationMs;
                    step->setDuration(durationMs);

                    step->updateThumbnail();

                    // update error messages
                    _mainWindow->updateProcessMessages();
                }
            }
        }

        // make sure the progress bar gets filled
        updateProgress(100);

        counter++;
    }

    if(_stopExecution)
        return;

    // update images
    _mainWindow->imageViewer()->updateImage();
    _mainWindow->imageViewer()->showProcessDuration(totalDurationMs);


    // update process graph
    _mainWindow->updateGraphicsView();
    _mainWindow->unlockScene();

    _isRunning = false;
    _currentStep = NULL;

    // if sequence, then run execute next step
    /*if(_sequenceCount > 0)
    {
        // notify GUI
        emit sequenceChanged(_sequenceIndex, _sequenceCount);

        if(_isSequenceRunning)
        {
            //setParamsHaveChanged();
            _mainWindow->execute(true);
        }
    }

    // find sequence processes
    //bool graphNeedsUpdate = false;
    for(auto it = _scene->steps()->begin(); it < _scene->steps()->end(); ++it)
    {
        IPProcessStep* step = (IPProcessStep*) *it;
        IPLProcess* process = step->process();

        if(process->isSequence())
        {
            process->requestUpdate();
            propertyChanged(process);
            requestUpdate();
        }
    }*/

    //if(_updateID > _currentUpdateID)
    //    _mainWindow->execute(false);

    // only for testing the camera
    //if(graphNeedsUpdate)
    //    _mainWindow->execute(false);

    _updateNeeded = false;

    // call afterProcessing of all steps which were executed this time
    // processes like the camera might request another execution
    QListIterator<IPProcessStep *> it2(_processList);
    while (it2.hasNext())
    {
        IPProcessStep* step = it2.next();
        step->process()->afterProcessing();
    }
}
Exemplo n.º 5
0
/* main: this is supposedly required for a C program
 * 1. Create threads to handle clients
 * 2. Add clients from the file
 * 3. Join threads and leave
 * Returns 0 if everything was done correctly.
 */
int main(int argc, char** argv)
{	
	int num_queries, num_threads;
	long i;
	pthread_t * tids;

	if(argc != 3)
	{	
		fprintf(stderr,
			"USAGE: %s [input_file_path] [num_threads]\n",
			argv[0]);
		exit(1);
	}	

	// do preliminary work (database, queue, etc.)
	setup_queue();
	populate_database("TwitterDB.txt");

	num_threads = atoi(argv[2]);

	sem_init(&queue_access, 0, 1);
	sem_init(&empty, 0, 0);
	sem_init(&served, 0, 1);
	sem_init(&full, 0, num_threads);


	// create threads to handle clients
	tids = calloc(num_threads, sizeof(pthread_t));
	for(i = 0; i < num_threads; i++)
	{
	 	if(pthread_create(&tids[i], NULL, serveClients, (void *)(i + 1)))
		{
			perror("main: could not create create thread");
		    exit(1);
		}
	}	

	// populate queue
	num_queries = buildQueue(argv[1]);

	while(num_served != num_queries);
	

   	// cancel threads
	for (i = 0; i < num_threads; i++) 
	{
		if(pthread_cancel(tids[i])) 
		{
			perror("main: could not join thread");
		    exit(1);
		}
	}

	// clean up
	destroy_database();
	destroy_queue();

	free(tids);

	printf("Finished handling all clients\n");
	return 0;
}