[Xapian-discuss] term iterators

Georges Dupret gdupret at dcc.uchile.cl
Tue Dec 14 15:36:53 GMT 2004


Hi!

I need to iterate over pairs of terms to compute the term correlation
matrix. My first attempt was:

for(term1 = db.allterms_begin(); term1 != db.allterms_end(); ++term1){
 for(term2 = term1 + 1; term2 != db.allterms_end(); ++term2){	
	...
	}
  }

this doesn't work because term1 + 1 is not defined, so I did

for(term1 = db.allterms_begin(); term1 != db.allterms_end(); ++term1){
term2 = term1;
++term2;
 for(; term2 != db.allterms_end(); ++term2){	
	...
	}
  }

and to my surprise, incrementing term2 incremented as well term1. Is
this what is really intended? Finally, I solved the problem with

for(term1 = db.allterms_begin(); term1 != db.allterms_end(); ++term1){
      term2 = db.allterms_begin();    
      term2.skip_to(*term1);
      if(term2 == db.allterms_end()){
	cerr << "term2 end of list while term1 is '" << *term1 << "'\n";
	exit(1);
      }
      else
	++term2;
      for(; term2 != db.allterms_end(); ++term2){	
	...
	}
  }

Note that if term2 is not set to db.allterms_begin(), the code crashes.

Is there a more elegant way to iterate over pairs of terms?

Regards, and thanks for this nice library and all the work invested.

Georges Dupret




More information about the Xapian-discuss mailing list