home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / plbin.zip / pl / src / pl-index.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  7KB  |  243 lines

  1. /*  pl-index.c,v 1.1.1.1 1992/05/26 11:52:21 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: indexing support
  8. */
  9.  
  10. #include "pl-incl.h"
  11.  
  12. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. Clause indexing.  Clauses store an  `index  structure',  which  provides
  14. summary information on the unification behaviour of the clause (e.i. its
  15. head  arguments.   This  structure  consists  of  two words: a key and a
  16. varmask.  Indexing can be done with upto 4 arguments.   Both  words  are
  17. divided  into  the  same  number  of  bit  groups  as  there are indexed
  18. arguments.  If an argument  is  indexable  (atom,  integer  or  compound
  19. term),  the  corresponding  bit group is filled with bits taken from the
  20. atom  pointer,  integer  or  functor  pointer.    In   this   case   all
  21. corresponding  bits  in  the varmask field are 1.  Otherwise the bits in
  22. both the varmask and the key are all 0.
  23.  
  24. To find a clause using indexing, we calculate an  index  structure  from
  25. the  calling arguments to the goal using the same rules.  Now, we can do
  26. a mutual `and' using the varmasks on the keys and  compare  the  result.
  27. If  equal  a  good  chance  for a possible unification exists, otherwise
  28. unification will definitely fail.  See matchIndex() and findClause().
  29.  
  30. Care has been taken to get this code as fast as  possible,  notably  for
  31. indexing only on the first argument as this is default.
  32. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33.  
  34. /* 1 <= c <= 4 */
  35.  
  36. #define SHIFT(c, a)    ((32/(c)) * a)
  37. #define MASK(c)        (c == 1 ? ~0L : ((1L << (32/(c))) - 1))
  38. #define VM(c, a)    (~(MASK(c) << SHIFT(c, a)))
  39.  
  40. #define Shift(c, a)    (mask_shift[c][a])
  41. #define Mask(c)        (mask_mask[c])
  42. #define varMask(c, a)    (variable_mask[c][a])
  43.  
  44. #define matchIndex(i1, i2)    (((i1).key & (i2).varmask) ==\
  45.                   ((i2).key & (i1).varmask))
  46.  
  47. static ulong variable_mask[][4] =
  48.   { { 0,        0,        0,        0 }, 
  49.     { VM(1, 0), 0,        0,        0 }, 
  50.     { VM(2, 0), VM(2, 1), 0,        0 }, 
  51.     { VM(3, 0), VM(3, 1), VM(3, 2), 0 }, 
  52.     { VM(4, 0), VM(4, 1), VM(4, 2), VM(4, 3) }
  53.   };
  54.  
  55. static int mask_shift[][4] =
  56.   { { 0,           0,           0,           0 }, 
  57.     { SHIFT(1, 0), 0,           0,           0 }, 
  58.     { SHIFT(2, 0), SHIFT(2, 1), 0,           0 }, 
  59.     { SHIFT(3, 0), SHIFT(3, 1), SHIFT(3, 2), 0 }, 
  60.     { SHIFT(4, 0), SHIFT(4, 1), SHIFT(4, 2), SHIFT(4, 3) }
  61.   };
  62.  
  63. static ulong mask_mask[] =
  64.   { 0, MASK(1), MASK(2), MASK(3), MASK(4)
  65.   };
  66.  
  67. /*  Determine cardinality (= # 1's) of bit pattern.
  68.  
  69.  ** Sun Sep 11 13:19:41 1988  jan@swivax.UUCP (Jan Wielemaker)  */
  70.  
  71. int
  72. cardinalityPattern(pattern)
  73. register ulong pattern;
  74. { register int result = 0;
  75.  
  76.   for(; pattern; pattern >>= 1)
  77.     if (pattern & 0x1)
  78.       result++;
  79.  
  80.   return result;
  81. }
  82.  
  83. struct index
  84. getIndex(argv, pattern, card)
  85. register Word argv;
  86. register ulong pattern;
  87. int card;
  88. { static struct index result;
  89.  
  90.   if ( pattern == 0x1L )
  91.   { deRef(argv);
  92.     if (isVar(*argv) || isIndirect(*argv) )
  93.     { result.key = result.varmask = 0;
  94.       return result;
  95.     }
  96.     result.key = (isTerm(*argv) ? (word) functorTerm(*argv) : *argv);
  97.     result.varmask = ~0L;
  98.  
  99.     return result;
  100.   } else
  101.   { register Word k;
  102.     register word key;
  103.     register int a;
  104.  
  105.     result.key = 0;
  106.     result.varmask = ~0L;            /* all 1s */
  107.  
  108.     for(a = 0; a < card; a++, pattern >>= 1, argv++)
  109.     { for(;(pattern & 0x1) == 0; pattern >>= 1)
  110.     argv++;
  111.  
  112.       deRef2(argv, k);
  113.       if (isVar(*k) || isIndirect(*k) )
  114.       { result.varmask &= varMask(card, a);
  115.     continue;
  116.       }
  117.       key = (isTerm(*k) ? (word) functorTerm(*k) : *k);
  118.       key = key >> 2;
  119.       result.key |= ((key & Mask(card)) << Shift(card, a) );
  120.     }
  121.   }
  122.  
  123.   return result;
  124. }
  125.  
  126. Clause
  127. findClause(cl, argv, def, deterministic)
  128. register Clause cl;
  129. register Word argv;
  130. register Definition def;
  131. bool *deterministic;
  132. { *deterministic = FALSE;
  133.  
  134.   if ( def->indexPattern == 0x0L )
  135.   { DEBUG(9, printf("Not indexed.\n"));
  136.     while(cl && true(cl, ERASED))
  137.     { DEBUG(9, printf("Skipping erased clause.\n"));
  138.       cl = cl->next;
  139.     }
  140.     DEBUG(9, printf("Returning clause 0x%lx\n", cl));
  141.     if ( cl && !cl->next )
  142.       *deterministic = TRUE;
  143.     return cl;
  144.   } else if ( def->indexPattern == 0x1L )
  145.   { register word key;
  146.  
  147.     deRef(argv);
  148.     if (isVar(*argv) || isIndirect(*argv))
  149.     { while(cl && true(cl, ERASED))
  150.     cl = cl->next;
  151.       if ( cl && !cl->next )
  152.     *deterministic = TRUE;
  153.       return cl;
  154.     }
  155.     key = (isTerm(*argv) ? (word) functorTerm(*argv) : *argv);
  156.     for(;cl ; cl = cl->next)
  157.     { if ((key & cl->index.varmask) == cl->index.key && false(cl, ERASED))
  158.       { Clause result = cl;
  159.       
  160.     for( cl = cl->next; cl; cl = cl->next )
  161.     { if ((key & cl->index.varmask) == cl->index.key && false(cl, ERASED))
  162.         return result;
  163.     }
  164.     *deterministic = TRUE;
  165.  
  166.     return result;
  167.       }
  168.     }
  169.     return (Clause) NULL;
  170.   } else
  171.   { struct index argIndex;
  172.  
  173.     argIndex = getIndex(argv, def->indexPattern, def->indexCardinality);
  174.     for(; cl; cl = cl->next)
  175.     { if (matchIndex(argIndex, cl->index) && false(cl, ERASED))
  176.       { Clause result = cl;
  177.       
  178.     for( cl = cl->next; cl; cl = cl->next )
  179.     { if (matchIndex(argIndex, cl->index) && false(cl, ERASED))
  180.         return result;
  181.     }
  182.     *deterministic = TRUE;
  183.  
  184.     return result;
  185.       }
  186.     }
  187.     return (Clause) NULL;
  188.   }
  189. }
  190.  
  191.  
  192. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  193. Recalculate the index of  a  clause  after  the  index  pattern  on  the
  194. predicate  has been changed.  The head of the clause is decompiled.  The
  195. resulting term is simply discarded as it cannot have links to any  other
  196. part of the stacks (e.g. backtrailing is not needed).
  197. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  198.  
  199. bool
  200. reindexClause(clause)
  201. Clause clause;
  202. { word head;
  203.   Procedure proc = clause->procedure;
  204.   mark m;
  205.  
  206.   if (proc->definition->indexPattern == 0x0)
  207.     succeed;
  208.  
  209.   Mark(m);
  210.   setVar(head);
  211.   if (decompileHead(clause, &head) == FALSE)
  212.   { sysError("Failed to decompile head of %s", procedureName(proc));
  213.     fail;
  214.   }
  215.  
  216.   clause->index = getIndex(argTermP(head, 0), proc->definition->indexPattern, 
  217.                           proc->definition->indexCardinality);
  218.   Undo(m);
  219.  
  220.   succeed;
  221. }
  222.  
  223. bool
  224. indexPatternToTerm(proc, value)
  225. Procedure proc;
  226. Word value;
  227. { Word argp;
  228.   ulong pattern = proc->definition->indexPattern;
  229.   int n, arity = proc->functor->arity;
  230.  
  231.   if (pattern == 0)
  232.     fail;
  233.  
  234.   deRef(value);
  235.   TRY(unifyFunctor(value, proc->functor) );
  236.   argp = argTermP(*value, 0);
  237.  
  238.   for(n=0; n<arity; n++, argp++, pattern >>= 1)
  239.     TRY(unifyAtomic(argp, consNum((pattern & 0x1) ? 1 : 0) ));
  240.  
  241.   succeed;
  242. }
  243.