Skip to content

Commit

Permalink
Pattern.instanceof() now uses PatternPointer where possible, should m…
Browse files Browse the repository at this point in the history
…ake it a bit faster. Should solve part of issue #18
  • Loading branch information
proycon committed Feb 24, 2016
1 parent 3370362 commit 2b9de15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class Pattern {
/**
* Tests whether the pattern is an instantiation of the specified skipgram
*/
bool instanceof(const Pattern & skipgram) const;
bool instanceof(const PatternPointer & skipgram) const;


/**
Expand Down Expand Up @@ -530,7 +530,7 @@ class PatternPointer {
*/
int flexcollapse(unsigned char * collapseddata) const;

bool instanceof(const Pattern & skipgram) const;
bool instanceof(const PatternPointer & skipgram) const;

operator Pattern() { return Pattern(*this); } //cast overload
Pattern pattern() const { return Pattern(*this); } //cast overload
Expand Down
22 changes: 10 additions & 12 deletions src/pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,52 +1624,50 @@ Pattern Pattern::extractskipcontent(const Pattern & instance) const {
return pattern;
}

bool Pattern::instanceof(const Pattern & skipgram) const { //TODO: refactor for v2
bool Pattern::instanceof(const PatternPointer & skipgram) const {
//Is this an instantiation of the skipgram?
//Instantiation is not necessarily full, aka: A ? B C is also an instantiation
//of A ? ? C
if (this->category() == FLEXGRAM) return false;
if (skipgram.category() == NGRAM) return (*this) == skipgram;

if (skipgram.category() == FLEXGRAM) {
//DYNAMIC SKIPGRAM
//TODO: NOT IMPLEMENTED YET!!
//TODO: Implement flexgram support!!!
return false;
} else {
//FIXED SKIPGRAM
const unsigned int _n = n();
if (skipgram.n() != _n) return false;

for (unsigned int i = 0; i < _n; i++) {
const Pattern token1 = Pattern(skipgram, i, 1);
const Pattern token2 = Pattern(*this, i, 1);
if ((token1 != token2) && (token1.category() != SKIPGRAM)) return false;
const PatternPointer reftoken = PatternPointer(skipgram, i, 1);
const PatternPointer token = PatternPointer(*this, i, 1);
if ((reftoken != token) && (reftoken.category() != SKIPGRAM)) return false;
}
return true;
}

}

bool PatternPointer::instanceof(const Pattern & skipgram) const { //TODO: refactor for v2
bool PatternPointer::instanceof(const PatternPointer & skipgram) const {
//Is this an instantiation of the skipgram?
//Instantiation is not necessarily full, aka: A ? B C is also an instantiation
//of A ? ? C
if (this->category() == FLEXGRAM) return false;
if (skipgram.category() == NGRAM) return (*this) == skipgram;

if (skipgram.category() == FLEXGRAM) {
//DYNAMIC SKIPGRAM
//TODO: NOT IMPLEMENTED YET!!
//TODO: Implement flexgram support!!!
return false;
} else {
//FIXED SKIPGRAM
const unsigned int _n = n();
if (skipgram.n() != _n) return false;

for (unsigned int i = 0; i < _n; i++) {
const Pattern token1 = Pattern(skipgram, i, 1);
const PatternPointer token2 = PatternPointer(*this, i, 1);
if ((token2 != token1) && (token1.category() != SKIPGRAM)) return false;
const PatternPointer reftoken = PatternPointer(skipgram, i, 1);
const PatternPointer token = PatternPointer(*this, i, 1);
if ((token != reftoken) && (reftoken.category() != SKIPGRAM)) return false;
}
return true;
}
Expand Down

0 comments on commit 2b9de15

Please sign in to comment.