void bsTree<T>::remove(const T & item) { TNode<T> *tN = search(root, item); //Returns node of value, item. if(!tN) //if node is not found, remove nothing. return; TNode<T> *pN= tN->getParent(); //Points to tN's parent. if(tN->getLeft() == NULL && tN->getRight() == NULL) //node is leaf { removeLink(tN, pN, NULL); } else if(tN->hasOnlyLeft()) //Node has only left child { removeLink(tN, pN, tN->getLeft()); } else if(tN->hasOnlyRight()) //Node has only right child { removeLink(tN, pN, tN->getRight()); } else // has two children { if(tN->getValue() > root->getValue()) { getSmallest(tN->getRight(), tN, tN); } else { getBiggest(tN->getLeft(), tN, tN); } } }
string QueryEvaluator::fillResult(TNode * node, int value) { string name = node->getValue(); string type = table.getType(name); // special case if (type==KEYWORD_CALL) { if (node->getNumChildren()!=0) { TNode * attr = node->getChildAtIndex(0); if (attr->getType()==Attr && attr->getValue()=="procName") { value = PKB::getCalledProc(value); type=KEYWORD_PROCEDURE; } } } if (type==KEYWORD_PROCEDURE) { return PKB::getProcName(value); } else if (type==KEYWORD_VAR) { return PKB::getVarName(value); } else if (type==KEYWORD_CONST) { return PKB::getConstName(value); } else if (type==KEYWORD_PROG_LINE || SyntaxHelper::isStmtSymbol(type)) { return to_string((long long)value); } else { } return "-1"; }
vector<int> QueryEvaluator::getAttrIndex(TNode * node, string attr_value) { vector<int> result; string symbol_name = node->getValue(); Symbol symbol_type = SyntaxHelper::getSymbolType(table.getType(symbol_name)); if (symbol_type==CallStmt) { TNode * attr = node->getChildAtIndex(0); if (attr->getValue()=="procName") { result = PKB::getCallingStmt(PKB::getProcIndex(attr_value).front()); return result; } } switch(symbol_type) { case Procedure: { result.push_back(PKB::getProcIndex(attr_value).front()); break; } case Var: { result.push_back(PKB::getVarIndex(attr_value)); break; } case Const: { result.push_back(PKB::getConstIndex(attr_value)); break; } case Prog_line: case Stmt: case Assign: case If: case While: case CallStmt: { int value = atoi(attr_value.c_str()); if ((value>0 && value<PKB::getStatTableSize()) && (table.getType(symbol_name)==KEYWORD_STMT || table.getType(symbol_name)==KEYWORD_PROG_LINE || table.getType(symbol_name)==PKB::getStmtName(value))) { result.push_back(value); } break; } default: break; } //remove all values = -1 if (result.size()==1 && result[0]==-1) { result.clear(); } return result; }
string QueryEvaluator::getAttrValue(TNode * node, int index) { string result = "-1"; if (index==-1) return result; string symbol_name = node->getValue(); Symbol symbol_type = SyntaxHelper::getSymbolType(table.getType(symbol_name)); switch(symbol_type) { case Procedure: { result = PKB::getProcName(index); break; } case Var: { result = PKB::getVarName(index); break; } case Const: { result = PKB::getConstName(index); break; } case Stmt: case Prog_line: case Assign: case While: case If: case CallStmt: { if (symbol_type==CallStmt) { TNode * attr = node->getChildAtIndex(0); if (attr->getValue()=="procName") { result = PKB::getProcName(PKB::getCalledProc(index)); } else { result = to_string((long long)index); } } else { result = to_string((long long)index); } break; } default: break; } return result; }
string QueryEvaluator::fillResult(TNode * result_node, vector<int> values, ResultTable * table) { string result = ""; int size = result_node->getNumChildren(); for (size_t i=0; i<size; i++) { TNode * node = result_node->getChildAtIndex(i); int value_index = table->getSymbolIndex(node->getValue()); string str = fillResult(node, values[value_index]); if (i<size-1) { result += str + " "; } else { result += str; } } return result; }
vector<string> QueryEvaluator::getSymbolsUsedBy(TNode * node) { vector<string> results; int size = node->getNumChildren(); for (int i=0; i<size; i++) { TNode * child = node->getChildAtIndex(i); if (child->getType()==QuerySymbol) { results.push_back(child->getValue()); } vector<string> sub_results = getSymbolsUsedBy(child); for (size_t j=0; j<sub_results.size(); j++) { if (find(results.begin(), results.end(), sub_results[j])==results.end()) results.push_back(sub_results[j]); } } return results; }