- C++ Radix Tree Implementation
- 16 Mar 2011 01:30:43 am
- Last edited by elfprince13 on 13 Apr 2011 07:24:34 pm; edited 2 times in total
Hey everyone, I'm implementing a radix tree to handle the cache lookups and insertions in FreeBuild. Anyone mind lending a second set of eyes to my find (and the other ones as I go) method to look for logic errors?
[edit]
I think this is working correctly =)
Code:
[edit]
I think this is working correctly =)
Code:
LDCacheNode * LDCacheNode::find(string nodeName, int depth) {
string ds("");
const char * dsc;
if(depth >= 0){
depth++;
ds = string(depth,' ');
}
dsc = ds.c_str();
string commonPrefix = findCommonPrefix(mPrefix,nodeName);
int cpS = commonPrefix.size();
int nnS = nodeName.size();
int mpS = mPrefix.size();
LDCacheNode * retNode;
if(depth >= 0){
Con::printf("%sLooking for %s, mPrefix is \"%s\"",dsc, nodeName.c_str(), mPrefix.c_str());
Con::printf("%s- Node has children - ",dsc);
for(set<LDCacheNode*,STLDereferencer>::iterator it = mSuffixes.begin(); (*it) != NULL && it != mSuffixes.end(); it++){
Con::printf("%s-- --- %s",dsc,(*it)->mPrefix.c_str());
}
}
if(nnS == mpS && mpS == cpS){ // Same
if(depth >= 0) Con::printf("%s - Found it",dsc);
retNode = this;
} else if((cpS == nnS) || mSuffixes.size() == 0){ // Not in the tree.
if(depth >= 0) Con::printf("%s - the search is the full common prefix, but we don't match this",dsc);
retNode = NULL;
} else {
string suffix = nodeName.substr(cpS, nnS - cpS);
retNode = NULL;
for(set<LDCacheNode*,STLDereferencer>::iterator checkNode = mSuffixes.begin();
(*checkNode) != NULL && checkNode != mSuffixes.end();
checkNode++) {
if(findCommonPrefix((*checkNode)->mPrefix,suffix) != ""){
if(depth >= 0) Con::printf("%s - found matching prefix \"%s\"",dsc,(*checkNode)->mPrefix.c_str());
retNode = (*checkNode)->find(suffix,depth);
break;
}
}
}
return retNode;
}