示例#1
0
文件: trie.hpp 项目: JonahMania/Trie
void removeRecursive( trieNode<T>* curr, std::string key, int level )
{
    //Create a node to keep track of the root 
    trieNode<T>* currRoot = curr;
    //Set the leaf of the key to false
    if( level == key.size() - 1 )
        curr->children[key[level] - 97]->isLeaf = false;
    //If the current node is not NULL and our current level is not past the end of the key
    if( curr != NULL && level != key.size() - 1 )
    {
        //Set curr to the next letter in the key
        curr = curr->children[ key[level] - 97 ];
        //Recursivly call removeRecursive while incrementing the level by 1
        removeRecursive( curr, key, ++level );
        //If the node corresponding to the next letter in the key has no children delete it
        if( checkChildren( curr->children[key[level] - 97] ) )
        {
            delete curr->children[key[level] - 97];
            curr->children[key[level] - 97] = NULL;
        }
    }
    //If the node corresponding to the first letter in the key has no children delete it
    if( level - 1 == 0 )
    {
        if( checkChildren( currRoot->children[key[0] - 97] ) )
        {
            delete currRoot->children[ key[0] - 97 ];
            currRoot->children[ key[0] - 97 ] = NULL;
        }
    }
}
示例#2
0
static void
processRoot (Node *strongRoot) 
{
	Node *temp, *strongNode = strongRoot, *weakNode;
	Arc *out;

	strongRoot->nextScan = strongRoot->childList;

	if ((out = findWeakNode (strongRoot, &weakNode)))
	{
		merge (weakNode, strongNode, out);
		pushExcess (strongRoot);
		return;
	}

	checkChildren (strongRoot);
	
	while (strongNode)
	{
		while (strongNode->nextScan) 
		{
			temp = strongNode->nextScan;
			strongNode->nextScan = strongNode->nextScan->next;
			strongNode = temp;
			strongNode->nextScan = strongNode->childList;

			if ((out = findWeakNode (strongNode, &weakNode)))
			{
				merge (weakNode, strongNode, out);
				pushExcess (strongRoot);
				return;
			}

			checkChildren (strongNode);
		}

		if ((strongNode = strongNode->parent))
		{
			checkChildren (strongNode);
		}
	}

	addToStrongBucket (strongRoot, strongRoots[strongRoot->label].end);

	++ highestStrongLabel;
}
/*! After the children have been stopped, it also deletes them. Any
	current solutions of the children are lost.
*/
void
GuidedPlanner::stopPlanner()
{
	for (int i=0; i<(int)mChildPlanners.size(); i++) {
		mChildPlanners[i]->stopPlanner();
	}
	checkChildren();
	SimAnnPlanner::stopPlanner();
}
示例#4
0
/* --------------------------------------------------
     This function prints the prompt that indicates to the user it is waiting for input.
     It calls checkChildren() first to see if any background processes have completed.
     The prompt keeps track of how many jobs are being run in the background and displays it for the user.
   -------------------------------------------------- */
void printPrompt(){

     //CURDIR = getcwd(BUFFER,1024);    //legacy code
     checkChildren();
     printf("%s active jobs - %d: ",getenv("LOGNAME"), JOBS);
     fflush(stdout);

     return;
}
void
GuidedPlanner::pausePlanner()
{
	SimAnnPlanner::pausePlanner();
	//stop all the children for good; it's just simpler
	for (int i=0; i<(int)mChildPlanners.size(); i++) {
		mChildPlanners[i]->stopPlanner();
	}
	checkChildren();
	mCurrentState->getObject()->showFrictionCones(true);
}
示例#6
0
//
// Kill with prejudice.
// This will make a serious attempt to *synchronously* kill the process before
// returning. If that doesn't work for some reason, abandon the child.
// This is one thing you can do in the destructor of your subclass to legally
// dispose of your Child's process.
//
void Child::kill()
{
	// note that we mustn't hold the lock across these calls
	if (this->state() == alive) {
		this->kill(SIGTERM);				// shoot it once
		checkChildren();					// check for quick death
		if (this->state() == alive) {
			usleep(200000);					// give it some time to die
			if (this->state() == alive) {	// could have been reaped by another thread
				checkChildren();			// check again
				if (this->state() == alive) {	// it... just... won't... die...
					this->kill(SIGKILL);	// take THAT!
					checkChildren();
					if (this->state() == alive) // stuck zombie
						this->abandon();	// leave the body behind
				}
			}
		}
	} else
		secdebug("unixchild", "%p (pid %d) not alive; ignoring request to kill it", this, pid());
}
示例#7
0
/*!
    Tests model's implementation of QAbstractItemModel::parent()
 */
void ModelTest::parent()
{
    // Make sure the model wont crash and will return an invalid QModelIndex
    // when asked for the parent of an invalid index.
    QVERIFY(model->parent(QModelIndex()) == QModelIndex());

    if (model->rowCount() == 0)
    {
        return;
    }

    // Column 0                | Column 1    |
    // QModelIndex()           |             |
    //    \- topIndex          | topIndex1   |
    //         \- childIndex   | childIndex1 |

    // Common error test #1, make sure that a top level index has a parent
    // that is a invalid QModelIndex.
    QModelIndex topIndex = model->index(0, 0, QModelIndex());
    QVERIFY(model->parent(topIndex) == QModelIndex());

    // Common error test #2, make sure that a second level index has a parent
    // that is the first level index.
    if (model->rowCount(topIndex) > 0)
    {
        QModelIndex childIndex = model->index(0, 0, topIndex);
        QVERIFY(model->parent(childIndex) == topIndex);
    }

    // Common error test #3, the second column should NOT have the same children
    // as the first column in a row.
    // Usually the second column shouldn't have children.
    QModelIndex topIndex1 = model->index(0, 1, QModelIndex());

    if (model->rowCount(topIndex1) > 0)
    {
        QModelIndex childIndex = model->index(0, 0, topIndex);
        QModelIndex childIndex1 = model->index(0, 0, topIndex1);
        QVERIFY(childIndex != childIndex1);
    }

    // Full test, walk n levels deep through the model making sure that all
    // parent's children correctly specify their parent.
    checkChildren(QModelIndex());
}
/*! Does the usual main loop of a SimAnn planner, but checks if the current
	state is good enough to be placed in the list of seeds to be used for
	children. The list of seeds is also pruned to remove similar state,
	and only keep a list of "unique" seeds.
*/
void
GuidedPlanner::mainLoop()
{
	// call main simann iteration
	SimAnn::Result r = mSimAnn->iterate(mCurrentState, mEnergyCalculator);
	if (r==SimAnn::FAIL) return;

	//put result in list
	double bestEnergy;
	if ((int)mChildSeeds.size() < mChildSeedSize) {
		//only queue good states to begin with
		bestEnergy = mMinChildEnergy;
	} else {
		bestEnergy = mChildSeeds.back()->getEnergy();
	}
	if (r==SimAnn::JUMP && mCurrentState->getEnergy() < bestEnergy) {
		GraspPlanningState *insertState = new GraspPlanningState(mCurrentState);
		DBGP("New solution. Is it a candidate?");
		if (!addToListOfUniqueSolutions(insertState,&mChildSeeds,mDistanceThreshold)) {
			DBGP("No.");
			delete insertState;
		} else {
			DBGP("Yes");
			//place a visual marker in the world
			mHand->getWorld()->getIVRoot()->addChild( insertState->getIVRoot() );			
			mChildSeeds.sort(GraspPlanningState::compareStates);
			DBGP("Queued...");
			while ((int)mChildSeeds.size() > mChildSeedSize) {
				delete(mChildSeeds.back());
				mChildSeeds.pop_back();
			}
			DBGP("Done.");
		}
	}

	mCurrentStep = mSimAnn->getCurrentStep();
	render();

	if (mCurrentStep % 100 == 0) {
		emit update();
		checkChildren();
	}
}
示例#9
0
/*!
    Called from the parent() test.

    A model that returns an index of parent X should also return X when asking
    for the parent of the index.

    This recursive function does pretty extensive testing on the whole model in an
    effort to catch edge cases.

    This function assumes that rowCount(), columnCount() and index() already work.
    If they have a bug it will point it out, but the above tests should have already
    found the basic bugs because it is easier to figure out the problem in
    those tests then this one.
 */
void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
{
    // First just try walking back up the tree.
    QModelIndex p = parent;
    while ( p.isValid() )
        p = p.parent();

    // For models that are dynamically populated
    if ( model->canFetchMore ( parent ) ) {
        fetchingMore = true;
        model->fetchMore ( parent );
        fetchingMore = false;
    }

    int rows = model->rowCount ( parent );
    int columns = model->columnCount ( parent );

    if ( rows > 0 )
        Q_ASSERT ( model->hasChildren ( parent ) );

    // Some further testing against rows(), columns(), and hasChildren()
    Q_ASSERT ( rows >= 0 );
    Q_ASSERT ( columns >= 0 );
    if ( rows > 0 )
        Q_ASSERT ( model->hasChildren ( parent ) == true );

    //qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows
    //         << "columns:" << columns << "parent column:" << parent.column();

    Q_ASSERT ( model->hasIndex ( rows + 1, 0, parent ) == false );
    for ( int r = 0; r < rows; ++r ) {
        if ( model->canFetchMore ( parent ) ) {
            fetchingMore = true;
            model->fetchMore ( parent );
            fetchingMore = false;
        }
        Q_ASSERT ( model->hasIndex ( r, columns + 1, parent ) == false );
        for ( int c = 0; c < columns; ++c ) {
            Q_ASSERT ( model->hasIndex ( r, c, parent ) == true );
            QModelIndex index = model->index ( r, c, parent );
            // rowCount() and columnCount() said that it existed...
            Q_ASSERT ( index.isValid() == true );

            // index() should always return the same index when called twice in a row
            QModelIndex modifiedIndex = model->index ( r, c, parent );
            Q_ASSERT ( index == modifiedIndex );

            // Make sure we get the same index if we request it twice in a row
            QModelIndex a = model->index ( r, c, parent );
            QModelIndex b = model->index ( r, c, parent );
            Q_ASSERT ( a == b );

            // Some basic checking on the index that is returned
            Q_ASSERT ( index.model() == model );
            Q_ASSERT ( index.row() == r );
            Q_ASSERT ( index.column() == c );
            // While you can technically return a QVariant usually this is a sign
            // of an bug in data()  Disable if this really is ok in your model.
//            Q_ASSERT ( model->data ( index, Qt::DisplayRole ).isValid() == true );

            // If the next test fails here is some somewhat useful debug you play with.

            if (model->parent(index) != parent) {
                qDebug() << r << c << currentDepth << model->data(index).toString()
                         << model->data(parent).toString();
                qDebug() << index << parent << model->parent(index);
//                 And a view that you can even use to show the model.
//                 QTreeView view;
//                 view.setModel(model);
//                 view.show();
            }

            // Check that we can get back our real parent.
//             qDebug() << model->parent ( index ) << parent ;
            Q_ASSERT ( model->parent ( index ) == parent );

            // recursively go down the children
            if ( model->hasChildren ( index ) && currentDepth < 10 ) {
                //qDebug() << r << c << "has children" << model->rowCount(index);
                checkChildren ( index, ++currentDepth );
            }/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/

            // make sure that after testing the children that the index doesn't change.
            QModelIndex newerIndex = model->index ( r, c, parent );
            Q_ASSERT ( index == newerIndex );
        }
    }
}
void pop(char * str, Node* root) //pop element due to end_tag e.g</d>
{
    int i,j,begin,next;
    int flag=0;
    Node * n;
    int isoutput=0;
    int k;
    for(j=machineCount;j>=1;j=j-2)
    {
        if(strcmp(str,stateMachine[j].str)==0)
        {
            break;
		}
	}
	if(j>=1)
	{
		begin=stateMachine[j].start;
		next=stateMachine[j].end;
		j=begin;
		if(j<=stateCount)
		{
			n=root->children[j]->children[next];  //for state j
			if(n!=NULL&&n->state==next)
			{
				if(root->children[j]->hasOutput==1)
				{					
					if(n->hasOutput==1)
					    n->output=strcat(n->output," ");
					else n->hasOutput=1;					
					n->output=strcat(n->output,root->children[j]->output);
					if(root->children[j]->output!=NULL) free(root->children[j]->output);
					root->children[j]->output=NULL;
					root->children[j]->hasOutput=0;
				}
				n->parent=NULL;
				root->children[j]->children[next]=NULL;
				add_node(n,root);

				if(checkChildren(root->children[j])==-1)
				{
					if(root->children[j]!=NULL) free(root->children[j]);
					root->children[j]=NULL;
					flag=1;
				}
				if(root->children[0]!=NULL)
				{
					for(i=stateCount;i>=0;i--)  //for state0
				    {
				        	
					    if(root->children[0]->children[i]!=NULL)
					    {

					        n=root->children[0]->children[i];
					        n->parent=NULL;
					        root->children[0]->children[i]=NULL;
					        if(i==0){
					            if(root->children[0]!=NULL) {
								    free(root->children[0]);
								}
					            root->children[0]=NULL;
							}
					        add_node(n,root);
						}
			     	}
			   } 
			}
			else if(flag==0) //not in final tree, add it into the start tree
			   {
			   	    n=root->children[begin]->start_node;
			   	    if(n!=NULL)
				    {
				    	if(root->children[next]->start_node!=NULL)
				    	{
				            Node* ns=(Node*)malloc(sizeof(Node));
                            ns->state=begin;
                            ns->parent=n->parent;
                            ns->children=NULL;
                            ns->start_node=NULL;
				    		n->children[next]=root->children[next]->start_node;  //for pop node
				    		root->children[next]->start_node->parent->children[next]=NULL;
                            root->children[next]->start_node->parent=n;
                            ns->finish_node=root->children[begin];
                            if(root->children[begin]->hasOutput==1)
				            {
					            root->children[next]->hasOutput=1;
					            root->children[next]->output=strcpy(ns->finish_node->output,root->children[begin]->output);
					            
					            root->children[begin]->hasOutput=0;
				            }
                            root->children[begin]->start_node=ns;
                            //for pop node 0
                            n=root->children[0]->start_node;
                            n->children=(Node**)malloc(sizeof(Node));
                            for(k=0;k<=stateCount;k++)
                            {
                            	n->children[k]=NULL;
							}
                            ns=NULL;
                            
                            if(n!=NULL)
                            {
                            	for(i=0;i<=stateCount;i++)
                            	{
                            		if(i==next)
									{
										continue;
									} 
                            		if(i==0)
                            		{
                            			ns=(Node*)malloc(sizeof(Node));
                                        ns->state=i;
                                        ns->parent=n;
                                        ns->children=NULL;
                                        ns->start_node=NULL;
                                        n->children[i]=ns;
                                        ns->finish_node=root->children[0];
                                        root->children[0]->start_node=ns;
									}
										
									if(i!=0){
                                        if(i!=begin)
										{
                                        	root->children[i]->start_node->parent->children[i]=NULL;
										}
                                        root->children[i]->start_node->parent=n;
										n->children[i]=root->children[i]->start_node;  //for next state
                                    }
								}
							}
						}
					}
				}
		}
	}      
}