[Bug 5620] fts:FormCombinations is incorrect

http://www.w3.org/Bugs/Public/show_bug.cgi?id=5620





------- Comment #1 from thomas.baby@oracle.com  2008-04-10 22:17 -------
Michael Dyck and I have fixed this issue and other issues in the FTTimes
neighborhood. We have added a new function called fts:FormCombinationsAtLeast,
which generates $k-or-more combinations of a sequence, by repeatedly invoking
fts:FormCombinations. fts:FormCombinations is an existing function that
generates $k combinations of a sequence. Some of the base case checks in
fts:FormCombinations have also been fixed. The new functions are included
below:

declare function fts:FormCombinations (
      $sms as element(fts:match)*, 
      $k as xs:integer ) 
   as element(fts:match)*
(:
   Find all combinations of exactly $k elements from $sms, and
   for each such combination, construct a match whose children are
   copies of all the children of all the elements in the combination.
   Return the sequence of all such matches.
:)
{
   if ($k eq 0) then <fts:match/>
   else if (fn:count($sms) lt $k) then ()
   else if (fn:count($sms) eq $k) then <fts:match>{$sms/*}</fts:match>
   else
      let $first := $sms[1],
          $rest  := fn:subsequence($sms, 2)
      return (
         (: all the combinations that don't involve $first :)
         fts:FormCombinations($rest, $k),

         (: and all the combinations that do involve $first :)
         for $combination in fts:FormCombinations($rest, $k - 1)
         return
            <fts:match>
            {
               $first/*,
               $combination/*
            }
            </fts:match>
      )
};

declare function fts:FormCombinationsAtLeast (
      $sms as element(fts:match)*,
      $times as xs:integer)
   as element(fts:match)*
(:
   Find all combinations of $times or more elements from $sms, and
   for each such combination, construct a match whose children are
   copies of all the children of all the elements in the combination.
   Return the sequence of all such matches.
:)
{
   for $k in $times to fn:count($sms)
   return fts:FormCombinations($sms, $k)
};

Received on Thursday, 10 April 2008 22:18:20 UTC