llvm.org GIT mirror llvm / cdd0417
Define addString() and lookup() out-of-line to dissuade the C++ compiler from inlining it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45068 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 12 years ago
1 changed file(s) with 77 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
177177
178178 inline Node* getRoot() const { return Nodes[0]; }
179179
180 bool addString(const std::string& s, const Payload& data) {
181 Node* cNode = getRoot();
182 Node* tNode = NULL;
183 std::string s1(s);
184
185 while (tNode == NULL) {
186 char Id = s1[0];
187 if (Node* nNode = cNode->getEdge(Id)) {
188 typename Node::QueryResult r = nNode->query(s1);
189
190 switch (r) {
191 case Node::Same:
192 case Node::StringIsPrefix:
193 // Currently we don't allow to have two strings in the trie one
194 // being a prefix of another. This should be fixed.
195 assert(0 && "FIXME!");
196 return false;
197 case Node::DontMatch:
198 assert(0 && "Impossible!");
199 return false;
200 case Node::LabelIsPrefix:
201 s1 = s1.substr(nNode->label().length());
202 cNode = nNode;
203 break;
204 default:
205 nNode = splitEdge(cNode, Id, r);
206 tNode = addNode(data, s1.substr(r));
207 nNode->addEdge(tNode);
208 }
209 } else {
210 tNode = addNode(data, s1);
211 cNode->addEdge(tNode);
212 }
213 }
214
215 return true;
216 }
217
218 const Payload& lookup(const std::string& s) const {
219 Node* cNode = getRoot();
220 Node* tNode = NULL;
221 std::string s1(s);
222
223 while (tNode == NULL) {
224 char Id = s1[0];
225 if (Node* nNode = cNode->getEdge(Id)) {
226 typename Node::QueryResult r = nNode->query(s1);
227
228 switch (r) {
229 case Node::Same:
230 tNode = nNode;
231 break;
232 case Node::StringIsPrefix:
233 return Empty;
234 case Node::DontMatch:
235 assert(0 && "Impossible!");
236 return Empty;
237 case Node::LabelIsPrefix:
238 s1 = s1.substr(nNode->label().length());
239 cNode = nNode;
240 break;
241 default:
242 return Empty;
243 }
244 } else
180 bool addString(const std::string& s, const Payload& data);
181 const Payload& lookup(const std::string& s) const;
182
183 };
184
185 // Define this out-of-line to dissuade the C++ compiler from inlining it.
186 template
187 bool Trie::addString(const std::string& s, const Payload& data) {
188 Node* cNode = getRoot();
189 Node* tNode = NULL;
190 std::string s1(s);
191
192 while (tNode == NULL) {
193 char Id = s1[0];
194 if (Node* nNode = cNode->getEdge(Id)) {
195 typename Node::QueryResult r = nNode->query(s1);
196
197 switch (r) {
198 case Node::Same:
199 case Node::StringIsPrefix:
200 // Currently we don't allow to have two strings in the trie one
201 // being a prefix of another. This should be fixed.
202 assert(0 && "FIXME!");
203 return false;
204 case Node::DontMatch:
205 assert(0 && "Impossible!");
206 return false;
207 case Node::LabelIsPrefix:
208 s1 = s1.substr(nNode->label().length());
209 cNode = nNode;
210 break;
211 default:
212 nNode = splitEdge(cNode, Id, r);
213 tNode = addNode(data, s1.substr(r));
214 nNode->addEdge(tNode);
215 }
216 } else {
217 tNode = addNode(data, s1);
218 cNode->addEdge(tNode);
219 }
220 }
221
222 return true;
223 }
224
225 template
226 const Payload& Trie::lookup(const std::string& s) const {
227 Node* cNode = getRoot();
228 Node* tNode = NULL;
229 std::string s1(s);
230
231 while (tNode == NULL) {
232 char Id = s1[0];
233 if (Node* nNode = cNode->getEdge(Id)) {
234 typename Node::QueryResult r = nNode->query(s1);
235
236 switch (r) {
237 case Node::Same:
238 tNode = nNode;
239 break;
240 case Node::StringIsPrefix:
245241 return Empty;
246 }
247
248 return tNode->data();
249 }
250
251 };
242 case Node::DontMatch:
243 assert(0 && "Impossible!");
244 return Empty;
245 case Node::LabelIsPrefix:
246 s1 = s1.substr(nNode->label().length());
247 cNode = nNode;
248 break;
249 default:
250 return Empty;
251 }
252 } else
253 return Empty;
254 }
255
256 return tNode->data();
257 }
252258
253259 template
254260 struct GraphTraits > {