Skip to content

Commit

Permalink
Make ObjectIterator goTo absolute, and add skip function to triples a…
Browse files Browse the repository at this point in the history
…nd object iterator
  • Loading branch information
fernandez committed Sep 19, 2017
1 parent de06a43 commit 7b92caa
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions hdt-lib/include/Iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ class IteratorTripleID {
return false;
}
virtual void goTo(unsigned int /*pos*/) {
/* Absolute repositioning of index: ie. go to the index set in the given argument (pos) */
}
virtual void skip(unsigned int /*pos*/) {
/* Relative repositioning of index: ie. skip index by given argument places */
}
virtual bool findNextOccurrence(unsigned int /*value*/, unsigned char /*component*/) {
return false;
Expand Down
3 changes: 3 additions & 0 deletions hdt-lib/src/triples/BitmapTriples.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class BitmapTriplesSearchIterator : public IteratorTripleID {
TripleComponentOrder getOrder();
bool canGoTo();
void goTo(unsigned int pos);
void skip(unsigned int pos);
bool findNextOccurrence(unsigned int value, unsigned char component);
bool isSorted(TripleComponentRole role);
};
Expand Down Expand Up @@ -203,6 +204,7 @@ class MiddleWaveletIterator : public IteratorTripleID {
TripleComponentOrder getOrder();
bool canGoTo();
void goTo(unsigned int pos);
void skip(unsigned int pos);
bool findNextOccurrence(unsigned int value, unsigned char component);
bool isSorted(TripleComponentRole role);
};
Expand Down Expand Up @@ -265,6 +267,7 @@ class ObjectIndexIterator : public IteratorTripleID {
TripleComponentOrder getOrder();
bool canGoTo();
void goTo(unsigned int pos);
void skip(unsigned int pos);
bool findNextOccurrence(unsigned int value, unsigned char component);
bool isSorted(TripleComponentRole role);
};
Expand Down
31 changes: 26 additions & 5 deletions hdt-lib/src/triples/BitmapTriplesIterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,26 @@ bool BitmapTriplesSearchIterator::canGoTo() {
return true;
}

template <typename T>
std::string NumberToString ( T Number )
{
std::ostringstream ss;
ss << Number;
return ss.str();
}

void BitmapTriplesSearchIterator::goTo(unsigned int pos) {
if ((pos) >= maxZ) {
throw std::runtime_error("Cannot goTo on this pattern.");
throw std::runtime_error(string("Given index is ") + NumberToString(pos) + ". Cannot go beyond last element index: " + NumberToString(maxZ));
}
posZ = minZ+pos; // move the position of Z
posZ = pos; // move the position of Z
goToY(); // go to the correct Y
}

void BitmapTriplesSearchIterator::skip(unsigned int pos) {
goTo(posZ+pos);
}

TripleComponentOrder BitmapTriplesSearchIterator::getOrder() {
return triples->order;
}
Expand Down Expand Up @@ -439,6 +451,10 @@ void MiddleWaveletIterator::goTo(unsigned int pos) {
z = adjZ.get(posZ);
}

void MiddleWaveletIterator::skip(unsigned int pos) {
goTo(predicateOcurrence+pos);
}

size_t MiddleWaveletIterator::estimatedNumResults()
{
if (triples->predicateCount!=NULL){
Expand Down Expand Up @@ -902,10 +918,15 @@ bool ObjectIndexIterator::canGoTo()

void ObjectIndexIterator::goTo(unsigned int pos)
{
if(minIndex+pos>maxIndex) {
throw std::runtime_error("Cannot goto beyond last element");
if(pos>maxIndex) {
throw std::runtime_error(string("Given index: ") + NumberToString(pos) + ". Cannot go beyond last element index: " + NumberToString(maxIndex));
}
posIndex = minIndex+pos;
posIndex = pos;
}

void ObjectIndexIterator::skip(unsigned int pos)
{
goTo(minIndex+pos);
}

bool ObjectIndexIterator::findNextOccurrence(unsigned int value, unsigned char component) {
Expand Down
5 changes: 5 additions & 0 deletions hdt-lib/src/triples/TripleIterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ void SequentialSearchIteratorTripleID::goTo(unsigned int pos){
doFetchNext();
}
}
void SequentialSearchIteratorTripleID::skip(unsigned int pos){
for (int i=0;i<pos;i++){
doFetchNext();
}
}

void SequentialSearchIteratorTripleID::doFetchPrevious()
{
Expand Down
1 change: 1 addition & 0 deletions hdt-lib/src/triples/TripleIterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SequentialSearchIteratorTripleID : public IteratorTripleID {
size_t estimatedNumResults();
bool canGoTo();
void goTo(unsigned int pos);
void skip(unsigned int pos);

bool hasNext();
TripleID *next();
Expand Down
6 changes: 3 additions & 3 deletions hdt-lib/tests/goto.cpp → hdt-lib/tests/randomSolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ void signalHandler(int sig) {
}

void help() {
cout << "$ goto <HDT file> <subject> <predicate> <object> " << endl;
cout << "$ randomSolution <HDT file> <subject> <predicate> <object> " << endl;
cout << "\t-h\t\t\t\tThis help" << endl;
cout << "\t-q\t<query>\t\tLaunch query to search for a random solution. E.g. \"? http://www.w3.org/2000/01/rdf-schema#label ?\"" << endl;
cout << "\t-q\t<query>\t\tLaunch query and pick a random solution. E.g. \"? http://www.w3.org/2000/01/rdf-schema#label ?\"" << endl;
}

int main(int argc, char **argv) {
Expand Down Expand Up @@ -93,7 +93,7 @@ int main(int argc, char **argv) {
int randNumber = int(numResults * rand() / (RAND_MAX));
cout << "We pick the random solution #" << randNumber << " out of " << numResults << " results"<< endl;

it->goTo(randNumber);
it->skip(randNumber);
TripleID* result=it->next();
cout<<"The selected random triple ID is: "<<result->getSubject()<<" "<<result->getPredicate()<<" "<<result->getObject()<<endl;
TripleString resString;
Expand Down

0 comments on commit 7b92caa

Please sign in to comment.