Skip to content

Commit

Permalink
Add speedup shortcut to TaxonomyExpression for a single tax identifier
Browse files Browse the repository at this point in the history
Skip the expression evaluation if only one number is given
  • Loading branch information
milot-mirdita committed Aug 4, 2022
1 parent 1d63172 commit 8ff7279
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/taxonomy/TaxonomyExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class TaxonomyExpression {
};

TaxonomyExpression(const std::string &expression, NcbiTaxonomy &taxonomy, CommaMeaning commaMeaning = COMMA_IS_OR) {
tc.t = &taxonomy;
if (Util::isNumber(expression)) {
shortcutAncestor = strtoull(expression.c_str(), NULL, 10);
parser = NULL;
return;
}
std::string bracketExpression;
bool inNumber = false;
for (size_t i = 0; i < expression.size(); i++) {
Expand Down Expand Up @@ -50,7 +56,6 @@ class TaxonomyExpression {
if (inNumber == true) {
bracketExpression.append(")");
}
tc.t = &taxonomy;
te_variable var;
var.name = "a";
// GCC 4.8 does not like casting functions to void*
Expand All @@ -66,13 +71,18 @@ class TaxonomyExpression {
}

~TaxonomyExpression() {
delete parser;
if (parser != NULL) {
delete parser;
}
}

bool isAncestor(TaxID taxId) {
tc.taxId = taxId;
const double result = parser->evaluate();
return (result != 0);
if (parser != NULL) {
tc.taxId = taxId;
const double result = parser->evaluate();
return (result != 0);
}
return tc.t->IsAncestor(shortcutAncestor, taxId);
}

private:
Expand All @@ -83,6 +93,7 @@ class TaxonomyExpression {
TaxContext tc;
ExpressionParser *parser;
std::vector<te_variable> vars;
TaxID shortcutAncestor;

static double acst(void *context, double a) {
TaxContext *o = (TaxContext *) context;
Expand Down

0 comments on commit 8ff7279

Please sign in to comment.