--- trunk/src/selection/NameFinder.cpp 2005/04/05 23:09:48 452 +++ trunk/src/selection/NameFinder.cpp 2005/12/02 15:38:03 770 @@ -45,26 +45,26 @@ namespace oopse { #include "utils/StringUtils.hpp" namespace oopse { -TreeNode::~TreeNode(){ + TreeNode::~TreeNode(){ std::map::iterator i; for ( i = children.begin(); i != children.end(); ++i) { - i->second->~TreeNode(); + i->second->~TreeNode(); } children.clear(); -} + } -NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ + NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies(); loadNames(); -} + } -NameFinder::~NameFinder(){ + NameFinder::~NameFinder(){ delete root_; -} + } -void NameFinder::loadNames() { + void NameFinder::loadNames() { std::map::iterator foundIter; SimInfo::MoleculeIterator mi; @@ -80,156 +80,156 @@ void NameFinder::loadNames() { for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { - std::string molName = mol->getMoleculeName(); - TreeNode* currentMolNode = createNode(root_, molName); + std::string molName = mol->getMoleculeName(); + TreeNode* currentMolNode = createNode(root_, molName); - for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { - std::string atomName = atom->getType(); - TreeNode* currentAtomNode = createNode(currentMolNode, atomName); + for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { + std::string atomName = atom->getType(); + TreeNode* currentAtomNode = createNode(currentMolNode, atomName); - currentMolNode->bs.setBitOn(atom->getGlobalIndex()); - currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); - } + currentMolNode->bs.setBitOn(atom->getGlobalIndex()); + currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); + } - for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { - std::string rbName = rb->getType(); - TreeNode* currentRbNode = createNode(currentMolNode, rbName); + for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { + std::string rbName = rb->getType(); + TreeNode* currentRbNode = createNode(currentMolNode, rbName); - currentMolNode->bs.setBitOn(rb->getGlobalIndex()); - currentRbNode->bs.setBitOn(rb->getGlobalIndex()); + currentMolNode->bs.setBitOn(rb->getGlobalIndex()); + currentRbNode->bs.setBitOn(rb->getGlobalIndex()); - //create nodes for atoms belong to this rigidbody - for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { - std::string rbAtomName = atom->getType(); - TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; + //create nodes for atoms belong to this rigidbody + for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { + std::string rbAtomName = atom->getType(); + TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; - currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); - } + currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); + } - } + } } -} + } -TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { + TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { TreeNode* node; std::map::iterator foundIter; foundIter = parent->children.find(name); if ( foundIter == parent->children.end()) { - node = new TreeNode; - node->name = name; - node->bs.resize(nStuntDouble_); - parent->children.insert(std::make_pair(name, node)); + node = new TreeNode; + node->name = name; + node->bs.resize(nStuntDouble_); + parent->children.insert(std::make_pair(name, node)); }else { - node = foundIter->second; + node = foundIter->second; } return node; -} + } -BitSet NameFinder::match(const std::string& name){ - BitSet bs(nStuntDouble_); + OOPSEBitSet NameFinder::match(const std::string& name){ + OOPSEBitSet bs(nStuntDouble_); StringTokenizer tokenizer(name, "."); std::vector names; while(tokenizer.hasMoreTokens()) { - names.push_back(tokenizer.nextToken()); + names.push_back(tokenizer.nextToken()); } int size = names.size(); switch(size) { - case 1 : - //could be molecule name, atom name and rigidbody name - matchMolecule(names[0], bs); - matchStuntDouble("*", names[0], bs); + case 1 : + //could be molecule name, atom name and rigidbody name + matchMolecule(names[0], bs); + matchStuntDouble("*", names[0], bs); - break; - case 2: - //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) + break; + case 2: + //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) - if (!isInteger(names[1])){ - matchRigidAtoms("*", names[0], names[1], bs); - matchStuntDouble(names[0], names[1], bs); - } else { - int internalIndex = lexi_cast(names[1]); - if (internalIndex < 0) { - std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; - } else { - matchInternalIndex(names[0], internalIndex, bs); - } - } + if (!isInteger(names[1])){ + matchRigidAtoms("*", names[0], names[1], bs); + matchStuntDouble(names[0], names[1], bs); + } else { + int internalIndex = lexi_cast(names[1]); + if (internalIndex < 0) { + std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; + } else { + matchInternalIndex(names[0], internalIndex, bs); + } + } - break; - case 3: - //must be molecule.rigidbody.* - matchRigidAtoms(names[0], names[1], names[2], bs); - break; - default: - std::cerr << "invalid name: " << name << std::endl; - break; + break; + case 3: + //must be molecule.rigidbody.* + matchRigidAtoms(names[0], names[1], names[2], bs); + break; + default: + std::cerr << "invalid name: " << name << std::endl; + break; } return bs; -} + } -void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { + void NameFinder::matchMolecule(const std::string& molName, OOPSEBitSet& bs) { std::vector molNodes = getMatchedChildren(root_, molName); std::vector::iterator i; for( i = molNodes.begin(); i != molNodes.end(); ++i ) { - bs |= (*i)->bs; + bs |= (*i)->bs; } -} + } -void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, BitSet& bs){ + void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, OOPSEBitSet& bs){ std::vector molNodes = getMatchedChildren(root_, molName); std::vector::iterator i; for( i = molNodes.begin(); i != molNodes.end(); ++i ) { - std::vector sdNodes = getMatchedChildren(*i, sdName); - std::vector::iterator j; - for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { - bs |= (*j)->bs; - } + std::vector sdNodes = getMatchedChildren(*i, sdName); + std::vector::iterator j; + for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { + bs |= (*j)->bs; + } } -} + } -void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, BitSet& bs){ + void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, OOPSEBitSet& bs){ std::vector molNodes = getMatchedChildren(root_, molName); std::vector::iterator i; for( i = molNodes.begin(); i != molNodes.end(); ++i ) { - std::vector rbNodes = getMatchedChildren(*i, rbName); - std::vector::iterator j; - for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { - std::vector rbAtomNodes = getMatchedChildren(*j, rbAtomName); - std::vector::iterator k; - for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ - bs |= (*k)->bs; - } - } + std::vector rbNodes = getMatchedChildren(*i, rbName); + std::vector::iterator j; + for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { + std::vector rbAtomNodes = getMatchedChildren(*j, rbAtomName); + std::vector::iterator k; + for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ + bs |= (*k)->bs; + } + } } -} + } -std::vector NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { + std::vector NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { std::vector matchedNodes; std::map::iterator i; for (i = node->children.begin(); i != node->children.end(); ++i) { - if (isMatched( i->first, name)) { - matchedNodes.push_back(i->second); - } + if (isMatched( i->first, name)) { + matchedNodes.push_back(i->second); + } } return matchedNodes; -} + } -bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { + bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); -} + } -void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, BitSet& bs){ + void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, OOPSEBitSet& bs){ std::map::iterator foundIter; SimInfo::MoleculeIterator mi; @@ -237,31 +237,31 @@ void NameFinder::matchInternalIndex(const std::string& for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { - if (isMatched(mol->getMoleculeName(), name) ) { - int natoms = mol->getNAtoms(); - int nrigidbodies = mol->getNRigidBodies(); - if (internalIndex >= natoms + nrigidbodies) { - continue; - } else if (internalIndex < natoms) { - bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); - continue; - } else if ( internalIndex < natoms + nrigidbodies) { - bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); - } - } + if (isMatched(mol->getMoleculeName(), name) ) { + int natoms = mol->getNAtoms(); + int nrigidbodies = mol->getNRigidBodies(); + if (internalIndex >= natoms + nrigidbodies) { + continue; + } else if (internalIndex < natoms) { + bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); + continue; + } else if ( internalIndex < natoms + nrigidbodies) { + bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); + } + } } -} + } - bool NameFinder::isInteger(const std::string str) { + bool NameFinder::isInteger(const std::string str) { for(int i =0; i < str.size(); ++i){ - if (!std::isdigit(str[i])) { - return false; - } + if (!std::isdigit(str[i])) { + return false; + } } return true; - } + } }