ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/selection/NameFinder.cpp
(Generate patch)

Comparing trunk/OOPSE-3.0/src/selection/NameFinder.cpp (file contents):
Revision 1967 by tim, Thu Feb 3 23:14:05 2005 UTC vs.
Revision 1972 by tim, Fri Feb 4 22:39:26 2005 UTC

# Line 39 | Line 39 | namespace oopse {
39   * such damages.
40   */
41   #include "selection/NameFinder.hpp"
42 + #include "utils/wildcards.hpp"
43 + #include "utils/StringTokenizer.hpp"
44 + #include "primitives/Molecule.hpp"
45   namespace oopse {
46  
47 < NameFinder::NameFinder(SimInfo* info) {
47 > TreeNode::~TreeNode(){
48 >    std::map<std::string, TreeNode*>::iterator i;
49 >    for ( i = children.begin(); i != children.end(); ++i) {
50 >        i->second->~TreeNode();
51 >    }
52 >    children.clear();
53 > }
54 >
55 >
56 > NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){
57 >    nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies();
58      loadNames();
59   }
60  
61  
62 + NameFinder::~NameFinder(){
63 +    delete root_;
64 + }
65  
66   void NameFinder::loadNames() {
67  
68 <    root_ = new NameNode;
53 <    root_.type = rootNode;
54 <
55 <    NameNode* newNode;            
68 >    std::map<std::string, TreeNode*>::iterator foundIter;
69      SimInfo::MoleculeIterator mi;
70      Molecule* mol;
71      Molecule::AtomIterator ai;
72      Atom* atom;
73      Molecule::RigidBodyIterator rbIter;
74      RigidBody* rb;
75 +
76 +    root_ = new TreeNode;
77 +    root_->bs.resize(nStuntDouble_);
78 +    root_->bs.setAll(); //
79 +    
80      for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
81 <        newNode = new NameNode;
82 <        newNode.type = molNode;
83 <        newNode.name = mol->getMoleculeName();
81 >          
82 >        std::string molName = mol->getMoleculeName();
83 >         TreeNode* currentMolNode = createNode(root_, molName);
84          
85          for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
86 <            atomNames_.insert(atom->getType());
86 >            std::string atomName = atom->getType();
87 >            TreeNode* currentAtomNode = createNode(currentMolNode, atomName);
88 >            
89 >            currentMolNode->bs.setBitOn(atom->getGlobalIndex());
90 >            currentAtomNode->bs.setBitOn(atom->getGlobalIndex());
91          }
92 <        
71 <        //change the positions of atoms which belong to the rigidbodies
92 >
93          for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
94 <            rbNames_.insert(rb->getType());
95 <        }        
94 >            std::string rbName = rb->getType();
95 >            TreeNode* currentRbNode = createNode(currentMolNode, rbName);
96 >            
97 >            currentMolNode->bs.setBitOn(rb->getGlobalIndex());
98 >            currentRbNode->bs.setBitOn(rb->getGlobalIndex());
99 >
100 >            //create nodes for atoms belong to this rigidbody
101 >            for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) {
102 >                std::string rbAtomName = atom->getType();
103 >                TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);;
104 >
105 >                currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex());
106 >            }
107 >
108 >        }
109 >        
110      }    
111 +
112   }
113  
114 < bool NameFinder::match(const std::string& name, BitSet& bs){
114 > TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) {
115 >    TreeNode* node;    
116 >    std::map<std::string, TreeNode*>::iterator foundIter;
117 >    foundIter = parent->children.find(name);
118 >    if ( foundIter  == parent->children.end()) {
119 >        node = new TreeNode;
120 >        node->name = name;
121 >        node->bs.resize(nStuntDouble_);
122 >        parent->children.insert(std::make_pair(name, node));
123 >    }else {
124 >        node = foundIter->second;
125 >    }
126 >    return node;
127 > }
128  
129 <    bool error = true;
129 > BitSet NameFinder::match(const std::string& name){
130 >    BitSet bs(nStuntDouble_);
131 >  
132      StringTokenizer tokenizer(name, ".");
133  
134      std::vector<std::string> names;
# Line 88 | Line 139 | bool NameFinder::match(const std::string& name, BitSet
139      int size = names.size();
140      switch(size) {
141          case 1 :
142 <            //could be molecule name, atom name, rigidbody name
143 <            isMolName();
144 <            isAtomName();
145 <            isRigidBodyName();
142 >            //could be molecule name, atom name and rigidbody name
143 >            if (names[0] == "*"){
144 >                //if all molecules are selected, we don't need to do the matching, just set all of the bits
145 >                bs.setAll();
146 >            } else{
147 >                matchMolecule(names[0], bs);
148 >                matchStuntDouble("*", names[0], bs);
149 >            }
150 >            
151              break;
152          case 2:
153 <            //could be molecule.*(include atoms and rigidbodies) or rigidbody.*
153 >            //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody)
154 >            matchRigidAtoms("*", names[0], names[1], bs);
155 >            matchStuntDouble(names[0], names[1], bs);
156 >            
157              break;
158          case 3:
159              //must be molecule.rigidbody.*
160 <            
160 >            matchRigidAtoms(names[0], names[1], names[2], bs);
161              break;
162 <        default:            
162 >        default:      
163 >            std::cerr << "invalid name: " << name << std::endl;
164              break;          
165      }
166  
167 <    return matched;
167 >    return bs;
168   }
169  
170 + void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) {
171 +    std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);            
172 +    std::vector<TreeNode*>::iterator i;
173 +    for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
174 +        bs |= (*i)->bs;
175 +    }    
176   }
177 +
178 + void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, BitSet& bs){
179 +    std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);            
180 +    std::vector<TreeNode*>::iterator i;
181 +    for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
182 +        std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName);  
183 +        std::vector<TreeNode*>::iterator j;
184 +        for (j = sdNodes.begin(); j != sdNodes.end(); ++j) {
185 +            bs |= (*j)->bs;
186 +        }
187 +    }
188 +
189 + }
190 +
191 + void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, BitSet& bs){
192 +    std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);            
193 +    std::vector<TreeNode*>::iterator i;
194 +    for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
195 +        std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName);  
196 +        std::vector<TreeNode*>::iterator j;
197 +        for (j = rbNodes.begin(); j != rbNodes.end(); ++j) {
198 +            std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName);
199 +            std::vector<TreeNode*>::iterator k;
200 +            for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){
201 +                bs |= (*k)->bs;
202 +            }
203 +        }
204 +    }
205 +
206 + }
207 +
208 +
209 + std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) {
210 +    std::vector<TreeNode*> matchedNodes;
211 +    std::map<std::string, TreeNode*>::iterator i;
212 +    for (i = node->children.begin(); i != node->children.end(); ++i) {
213 +        if (isMatched( i->first, name)) {
214 +            matchedNodes.push_back(i->second);
215 +        }
216 +    }
217 +
218 +    return matchedNodes;
219 + }
220 +
221 + bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) {
222 +    return Wildcard::wildcardfit (wildcard.c_str(), str.c_str());
223 + }
224 +
225 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines