trie_search.c

00001 /* File:      trie_search.c
00002 ** Author(s): Ernie Johnson
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: trie_search.c,v 1.9 2005/12/22 23:34:01 tswift Exp $
00022 ** 
00023 */
00024 
00025 
00026 #include "xsb_config.h"
00027 #include "xsb_debug.h"
00028 
00029 #include "debugs/debug_tries.h"
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 
00034 #include "auxlry.h"
00035 #include "cell_xsb.h"
00036 #include "error_xsb.h"
00037 #include "deref.h"
00038 #include "psc_xsb.h"
00039 #include "trie_internals.h"
00040 #include "tst_aux.h"
00041 #include "debug_xsb.h"
00042 #include "flags_xsb.h"
00043 
00044 /****************************************************************************
00045 *
00046 *   In XSB parlance, "search" routines look for something in an entity
00047 *   and insert it if it doesn't already exist.
00048 *
00049 *   The following routines search different flavors of tries for sets
00050 *   of terms using either variant or subsumptive match criteria.
00051 *
00052 *     TSTNptr subsumptive_tst_search(TSTNptr,int,CPtr,xsbBool,xsbBool *)
00053 *     BTNptr  subsumptive_bt_search(BTNptr,int,CPtr,xsbBool *)
00054 *     TSTNptr variant_tst_search(TSTNptr,int,CPtr,xsbBool,xsbBool *)
00055 *     BTNptr  variant_bt_search(BTNptr,int,CPtr,xsbBool *)
00056 *
00057 *   They assume they are given a non-NULL trie pointer and accept a
00058 *   term set as an integer count and an array of terms.
00059 *
00060 *   As of 11/05, the only one of these used is subsumptive_tst_search()
00061 *
00062 ****************************************************************************/
00063 
00064 
00065 /*=========================================================================*/
00066 
00067 /*
00068  *                      Searching for an Escape Node
00069  *                      ============================
00070  *
00071  * Conditionally creates and returns the Escape node in the trie, given
00072  * as a non-NULL root pointer.  Indicates to the caller whether this
00073  * node was preexisting through the second argument.  Aborts the
00074  * computation if something other than an escape node is found in the
00075  * trie.
00076  *
00077  * These functions are intended for internal use by other search
00078  * routines.
00079  */
00080 
00081 
00082 /*-------------------------------------------------------------------------*/
00083 
00084 BTNptr bt_escape_search(CTXTdeclc BTNptr btRoot, xsbBool *isNew) {
00085 
00086   BTNptr btn;
00087 
00088   btn = BTN_Child(btRoot);
00089   if ( IsNULL(btn) ) {
00090     BT_InsertEscapeNode(btRoot,btn);
00091     *isNew = TRUE;
00092   }
00093   else if ( IsEscapeNode(btn) )
00094     *isNew = FALSE;
00095   else
00096     TrieError_AbsentEscapeNode(btRoot);
00097   return btn;
00098 }
00099 
00100 /*-------------------------------------------------------------------------*/
00101 
00102 inline static TSTNptr tst_escape_search(CTXTdeclc TSTNptr tstRoot, xsbBool *isNew) {
00103 
00104   TSTNptr tstn;
00105 
00106   tstn = TSTN_Child(tstRoot);
00107   if ( IsNULL(tstn) ) {
00108     TST_InsertEscapeNode(tstRoot,tstn);
00109     *isNew = TRUE;
00110   }
00111   else if ( IsEscapeNode(tstn) )
00112     *isNew = FALSE;
00113   else
00114     TrieError_AbsentEscapeNode(tstRoot);
00115   return tstn;
00116 }
00117 
00118 /*=========================================================================*/
00119 
00120 /*
00121  *                Subsumptive Search for a Set of Terms
00122  *                =====================================
00123  *
00124  * Given a trie (a non-NULL root pointer) and a (possibly empty) set of
00125  * terms, searches the trie for a subsuming term set.  If none is found,
00126  * inserts the given terms into the trie.  Returns a pointer to the leaf
00127  * node representing the given set of terms (if they were inserted) or
00128  * the discovered subsuming term set (otherwise).  A flag indicates to
00129  * the caller what the returned leaf represents.
00130  * 
00131  * This does not appear to be used (11/05)
00132  */
00133 
00134 
00135 /*-------------------------------------------------------------------------*/
00136 
00137 BTNptr subsumptive_bt_search(CTXTdeclc BTNptr btRoot, int nTerms, CPtr termVector,
00138                              xsbBool *isNew) {
00139 
00140   BTNptr btn;
00141   TriePathType path_type;
00142 
00143   
00144 #ifdef DEBUG_ASSERTIONS
00145   if ( IsNULL(btRoot) || (nTerms < 0) )
00146     TrieError_InterfaceInvariant("subsumptive_bt_search()");
00147 #endif
00148 
00149   if ( nTerms > 0 ) {
00150     Trail_ResetTOS;
00151     TermStack_ResetTOS;
00152     TermStack_PushHighToLowVector(termVector,nTerms);
00153     if ( IsEmptyTrie(btRoot) ) {
00154       btn = bt_insert(CTXTc btRoot,btRoot,NO_INSERT_SYMBOL);
00155       *isNew = TRUE;
00156     }
00157     else {
00158       TermStackLog_ResetTOS;
00159       btn = iter_sub_trie_lookup(CTXTc btRoot,&path_type);
00160       if ( path_type == NO_PATH ) {
00161         Trail_Unwind_All;
00162         btn = bt_insert(CTXTc btRoot,stl_restore_variant_cont(CTXT),
00163                         NO_INSERT_SYMBOL);
00164         *isNew = TRUE;
00165       }
00166       else
00167         *isNew = FALSE;
00168     }
00169     Trail_Unwind_All;
00170   }
00171   else
00172     btn = bt_escape_search(CTXTc btRoot,isNew);
00173   return btn;
00174 }
00175 
00176 /*-------------------------------------------------------------------------*/
00177 
00178 /*
00179  * An additional flag is supplied to this routine indicating whether
00180  * TSIs are being maintained for the given Time-Stamped Trie.  This
00181  * information is needed when the term set is inserted.
00182  */
00183 
00184 TSTNptr subsumptive_tst_search(CTXTdeclc TSTNptr tstRoot, int nTerms, CPtr termVector,
00185                                xsbBool maintainTSI, xsbBool *isNew) {
00186 
00187   TSTNptr tstn;
00188   TriePathType path_type;
00189 
00190   
00191 #ifdef DEBUG_ASSERTIONS
00192   if ( IsNULL(tstRoot) || (nTerms < 0) )
00193     TrieError_InterfaceInvariant("subsumptive_tst_search()");
00194 #endif
00195 
00196 #ifdef DEBUG_VERBOSE
00197   if (LOG_INTERN <= cur_log_level) {
00198     int i;
00199     xsb_dbgmsg((LOG_INTERN, 
00200                "Entered subsumptive_tst_search() with the following terms:"));
00201     for (i = 0; i < nTerms; i++) {
00202       xsb_dbgmsg((LOG_INTERN,"\t"));
00203       dbg_printterm(LOG_INTERN,stddbg, (Cell)(termVector - i),25);
00204       xsb_dbgmsg((LOG_INTERN,"\n"));
00205     }
00206   }
00207 #endif
00208 
00209   if ( nTerms > 0 ) {
00210     Trail_ResetTOS;
00211     TermStack_ResetTOS;
00212     TermStack_PushHighToLowVector(termVector,nTerms);
00213     if ( IsEmptyTrie(tstRoot) ) {
00214       tstn = tst_insert(CTXTc tstRoot,tstRoot,NO_INSERT_SYMBOL,maintainTSI);
00215       *isNew = TRUE;
00216     }
00217     else {
00218       TermStackLog_ResetTOS;
00219       tstn = iter_sub_trie_lookup(CTXTc tstRoot,&path_type);
00220       if ( path_type == NO_PATH ) {
00221         Trail_Unwind_All;
00222         tstn = tst_insert(CTXTc tstRoot, stl_restore_variant_cont(CTXT),
00223                           NO_INSERT_SYMBOL, maintainTSI);
00224         *isNew = TRUE;
00225       }
00226       else
00227         *isNew = FALSE;
00228     }
00229     Trail_Unwind_All;
00230   }
00231   else
00232     tstn = tst_escape_search(CTXTc tstRoot,isNew);
00233   return tstn;
00234 }
00235 
00236 /*=========================================================================*/
00237 
00238 /*
00239  *                  Variant Search for a Set of Terms
00240  *                  =================================
00241  *
00242  * Searches the given trie (a non-NULL root pointer) for the given
00243  * (possibly empty) term set.  The terms are inserted if they are not
00244  * already present.  Returns a pointer to the leaf node representing
00245  * these terms, and indicates in a flag whether the terms were inserted.
00246  */
00247 
00248 
00249 /*-------------------------------------------------------------------------*/
00250 
00251 BTNptr variant_bt_search(CTXTdeclc BTNptr btRoot, int nTerms, CPtr termVector,
00252                          xsbBool *isNew) {
00253 
00254   BTNptr btn;
00255   xsbBool wasFound;
00256   Cell symbol;
00257 
00258 
00259 #ifdef DEBUG_ASSERTIONS
00260   if ( IsNULL(btRoot) || (nTerms < 0) )
00261     TrieError_InterfaceInvariant("variant_bt_search()");
00262 #endif
00263 
00264   if ( nTerms > 0 ) {
00265     Trail_ResetTOS;
00266     TermStack_ResetTOS;
00267     TermStack_PushLowToHighVector(termVector,nTerms);
00268     if ( IsEmptyTrie(btRoot) ) {
00269       btn = bt_insert(CTXTc btRoot,btRoot,NO_INSERT_SYMBOL);
00270       *isNew = TRUE;
00271     }
00272     else {
00273       btn = var_trie_lookup(CTXTc btRoot,&wasFound,&symbol);
00274       if ( ! wasFound )
00275         btn = bt_insert(CTXTc btRoot,btn,symbol);
00276       *isNew = ( ! wasFound );
00277     }
00278     Trail_Unwind_All;
00279   }
00280   else
00281     btn = bt_escape_search(CTXTc btRoot,isNew);
00282   return btn;
00283 }
00284 
00285 /*-------------------------------------------------------------------------*/
00286 
00287 /*
00288  * An additional flag is supplied to this routine indicating whether
00289  * TSIs are being maintained for the given Time-Stamped Trie.  This
00290  * information is needed when the term set is inserted.
00291  */
00292 
00293 TSTNptr variant_tst_search(CTXTdeclc TSTNptr tstRoot, int nTerms, CPtr termVector,
00294                            xsbBool maintainTSI, xsbBool *isNew) {
00295 
00296   TSTNptr tstn;
00297   xsbBool wasFound;
00298   Cell symbol;
00299 
00300 
00301 #ifdef DEBUG_ASSERTIONS
00302   if ( IsNULL(tstRoot) || (nTerms < 0) )
00303     TrieError_InterfaceInvariant("variant_tst_search()");
00304 #endif
00305 
00306   if ( nTerms > 0 ) {
00307     Trail_ResetTOS;
00308     TermStack_ResetTOS;
00309     TermStack_PushHighToLowVector(termVector,nTerms);
00310     if ( IsEmptyTrie(tstRoot) ) {
00311       tstn = tst_insert(CTXTc tstRoot,tstRoot,NO_INSERT_SYMBOL,maintainTSI);
00312       *isNew = TRUE;
00313     }
00314     else {
00315       tstn = var_trie_lookup(CTXTc tstRoot,&wasFound,&symbol);
00316       if ( ! wasFound )
00317         tstn = tst_insert(CTXTc tstRoot,tstn,symbol,maintainTSI);
00318       *isNew = ( ! wasFound );
00319     }
00320     Trail_Unwind_All;
00321   }
00322   else
00323     tstn = tst_escape_search(CTXTc tstRoot,isNew);
00324   return tstn;
00325 }
00326 
00327 /*=========================================================================*/

Generated on Wed Jul 26 13:30:43 2006 for XSB by  doxygen 1.4.5