matcher/msetcmp.cc

Go to the documentation of this file.
00001 /* msetcmp.cc - MSetItem comparison functions and functors.
00002  *
00003  * Copyright (C) 2006 Olly Betts
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00018  * USA
00019  */
00020 
00021 #include <config.h>
00022 #include "msetcmp.h"
00023 
00024 /* We use templates to generate the 14 different comparison functions
00025  * which we need.  This avoids having to write them all out by hand.
00026  */
00027 
00028 // Order by did.  Helper comparison template function, which is used as the
00029 // last fallback by the others.
00030 template<bool FORWARD_DID, bool CHECK_DID_ZERO> inline bool
00031 msetcmp_by_did(const Xapian::Internal::MSetItem &a,
00032                const Xapian::Internal::MSetItem &b)
00033 {
00034     if (FORWARD_DID) {
00035         if (CHECK_DID_ZERO) {
00036             // We want dummy did 0 to compare worse than any other.
00037             if (a.did == 0) return false;
00038             if (b.did == 0) return true;
00039         }
00040         return (a.did < b.did);
00041     } else {
00042         return (a.did > b.did);
00043     }
00044 }
00045 
00046 // Order by relevance, then docid.
00047 template<bool FORWARD_DID> bool
00048 msetcmp_by_relevance(const Xapian::Internal::MSetItem &a,
00049                      const Xapian::Internal::MSetItem &b)
00050 {
00051     if (a.wt > b.wt) return true;
00052     if (a.wt < b.wt) return false;
00053     return msetcmp_by_did<FORWARD_DID, true>(a, b);
00054 }
00055 
00056 // Order by value, then docid.
00057 template<bool FORWARD_VALUE, bool FORWARD_DID> bool
00058 msetcmp_by_value(const Xapian::Internal::MSetItem &a,
00059                  const Xapian::Internal::MSetItem &b)
00060 {
00061     if (!FORWARD_VALUE) {
00062         // We want dummy did 0 to compare worse than any other.
00063         if (a.did == 0) return false;
00064         if (b.did == 0) return true;
00065     }
00066     if (a.sort_key > b.sort_key) return FORWARD_VALUE;
00067     if (a.sort_key < b.sort_key) return !FORWARD_VALUE;
00068     return msetcmp_by_did<FORWARD_DID, FORWARD_VALUE>(a, b);
00069 }
00070 
00071 // Order by value, then relevance, then docid.
00072 template<bool FORWARD_VALUE, bool FORWARD_DID> bool
00073 msetcmp_by_value_then_relevance(const Xapian::Internal::MSetItem &a,
00074                                 const Xapian::Internal::MSetItem &b)
00075 {
00076     if (!FORWARD_VALUE) {
00077         // two special cases to make min_item compares work when did == 0
00078         if (a.did == 0) return false;
00079         if (b.did == 0) return true;
00080     }
00081     if (a.sort_key > b.sort_key) return FORWARD_VALUE;
00082     if (a.sort_key < b.sort_key) return !FORWARD_VALUE;
00083     if (a.wt > b.wt) return true;
00084     if (a.wt < b.wt) return false;
00085     return msetcmp_by_did<FORWARD_DID, FORWARD_VALUE>(a, b);
00086 }
00087 
00088 // Order by relevance, then value, then docid.
00089 template<bool FORWARD_VALUE, bool FORWARD_DID> bool
00090 msetcmp_by_relevance_then_value(const Xapian::Internal::MSetItem &a,
00091                                 const Xapian::Internal::MSetItem &b)
00092 {
00093     if (!FORWARD_VALUE) {
00094         // two special cases to make min_item compares work when did == 0
00095         if (a.did == 0) return false;
00096         if (b.did == 0) return true;
00097     }
00098     if (a.wt > b.wt) return true;
00099     if (a.wt < b.wt) return false;
00100     if (a.sort_key > b.sort_key) return FORWARD_VALUE;
00101     if (a.sort_key < b.sort_key) return !FORWARD_VALUE;
00102     return msetcmp_by_did<FORWARD_DID, FORWARD_VALUE>(a, b);
00103 }
00104 
00105 static mset_cmp mset_cmp_table[] = {
00106     // Xapian::Enquire::Internal::REL
00107     msetcmp_by_relevance<false>,
00108     msetcmp_by_relevance<false>, // Shouldn't be needed.
00109     msetcmp_by_relevance<true>,
00110     msetcmp_by_relevance<true>, // Shouldn't be needed.
00111     // Xapian::Enquire::Internal::VAL
00112     msetcmp_by_value<false, false>,
00113     msetcmp_by_value<true, false>,
00114     msetcmp_by_value<false, true>,
00115     msetcmp_by_value<true, true>,
00116     // Xapian::Enquire::Internal::VAL_REL
00117     msetcmp_by_value_then_relevance<false, false>,
00118     msetcmp_by_value_then_relevance<true, false>,
00119     msetcmp_by_value_then_relevance<false, true>,
00120     msetcmp_by_value_then_relevance<true, true>,
00121     // Xapian::Enquire::Internal::REL_VAL
00122     msetcmp_by_relevance_then_value<true, true>,
00123     msetcmp_by_relevance_then_value<false, true>,
00124     msetcmp_by_relevance_then_value<true, false>,
00125     msetcmp_by_relevance_then_value<false, false>
00126 };
00127 
00128 mset_cmp get_msetcmp_function(Xapian::Enquire::Internal::sort_setting sort_by, bool sort_forward, bool sort_value_forward) {
00129     return mset_cmp_table[sort_by * 4 + sort_forward * 2 + sort_value_forward];
00130 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.