/*
  Copyright (c) 2007-2010 Stefan Kurtz <kurtz@zbh.uni-hamburg.de>
  Copyright (c) 2007-2010 Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

static bool GT_APPENDINT(binarysearchpage)(const GT_SPECIALTABLETYPE *leftptr,
                                           const GT_SPECIALTABLETYPE *rightptr,
                                           GT_SPECIALTABLETYPE pos)
{
  const GT_SPECIALTABLETYPE *midptr;

  while (leftptr <= rightptr)
  {
    midptr = leftptr + GT_DIV2((GtUword) (rightptr-leftptr));
    if (pos < *midptr)
    {
      rightptr = midptr-1;
    } else
    {
      if (pos > *midptr)
      {
        leftptr = midptr + 1;
      } else
      {
        return true;
      }
    }
  }
  return false;
}

static bool GT_APPENDINT(checkspecial)(const GT_APPENDINT(GtSWtable) *swtable,
                                       GtUword pos)
{
  GtUword pagenumber = GT_POS2PAGENUM(pos);

  if (pagenumber == 0)
  {
    if (swtable->endidxinpage[0] >= 1UL)
    {
      if (GT_APPENDINT(binarysearchpage)(swtable->positions,
                                         swtable->positions
                                          + swtable->endidxinpage[0] - 1,
                                          (GT_SPECIALTABLETYPE) pos))
      {
        return true;
      }
    }
  } else
  {
    if (swtable->endidxinpage[pagenumber-1] <
        swtable->endidxinpage[pagenumber])
    {
      if (GT_APPENDINT(binarysearchpage)(
               swtable->positions +
               swtable->endidxinpage[pagenumber-1],
               swtable->positions +
               swtable->endidxinpage[pagenumber] - 1,
               (GT_SPECIALTABLETYPE) pos))
      {
        return true;
      }
    }
  }
  return false;
}

/*@null@*/ static GtUword GT_APPENDINT(sepnum2pageoffset)(
                                       const GT_APPENDINT(GtSWtable) *swtable,
                                       GtUword sepnum)
{
  GtUword *leftptr, *midptr, *rightptr, *found = NULL;

  leftptr = swtable->endidxinpage;
  rightptr = swtable->endidxinpage + swtable->numofpages - 1;
  while (leftptr <= rightptr)
  {
    midptr = leftptr + GT_DIV2((GtUword) (rightptr-leftptr));
    if (sepnum < *midptr)
    {
      rightptr = midptr - 1;
    } else
    {
      found = midptr;
      leftptr = midptr + 1;
    }
  }
  if (found != NULL)
  {
    return (GtUword) (found - swtable->endidxinpage + 1);
  }
  return 0;
}

static GtUword GT_APPENDINT(gt_encseq_seqstartposSW)(
                           const GT_APPENDINT(GtSWtable) *swtable,
                           GtUword seqnum)
{
  return (seqnum > 0)
           ? GT_PAGENUM2OFFSET(GT_APPENDINT(sepnum2pageoffset)
                               (swtable,seqnum - 1)) +
             swtable->positions[seqnum - 1] + 1
           : 0;
}

static GtUword GT_APPENDINT(position2seqnum)(
                                   const GT_SPECIALTABLETYPE *recordseps,
                                   GtUword numofrecords,
                                   GT_SPECIALTABLETYPE position)
{
  GtUword left, mid, right, len;

  gt_assert(numofrecords > 0);
  if (numofrecords == 1UL || position <= recordseps[0])
  {
    return 0;
  }
  if (position > recordseps[numofrecords-2])
  {
    return numofrecords - 1;
  }
  left = 0;
  right = numofrecords - 2;
  while (left<=right)
  {
    len = right-left;
    mid = left + GT_DIV2(len);
    if (recordseps[mid] < position)
    {
      if (position <= recordseps[mid+1])
      {
        return mid + 1;
      }
      left = mid + 1;
    } else
    {
      if (recordseps[mid-1] < position)
      {
        return mid;
      }
      right = mid-1;
    }
  }
  fprintf(stderr,"gt_encseq_sep2seqnum: cannot find position "GT_WU"\n",
           (GtUword) position);
  exit(GT_EXIT_PROGRAMMING_ERROR);
}

static GtUword GT_APPENDINT(gt_encseq_seqnum)(
                                const GT_APPENDINT(GtSWtable) *swtable,
                                GtUword pos)
{
  GtUword pagenumber = GT_POS2PAGENUM(pos);

  if (pagenumber == 0)
  {
    if (swtable->endidxinpage[0] >= 1UL)
    {
      return GT_APPENDINT(position2seqnum)(swtable->positions,
                                           swtable->endidxinpage[0]+1,
                                           (GT_SPECIALTABLETYPE) pos);
    } else
    {
      return 0;
    }
  } else
  {
    if (swtable->endidxinpage[pagenumber-1] <
        swtable->endidxinpage[pagenumber])
    {
      return swtable->endidxinpage[pagenumber-1] +
             GT_APPENDINT(position2seqnum)(
               swtable->positions + swtable->endidxinpage[pagenumber-1],
               swtable->endidxinpage[pagenumber] -
               swtable->endidxinpage[pagenumber-1] + 1,
               (GT_SPECIALTABLETYPE) pos);
    } else
    {
      return swtable->endidxinpage[pagenumber-1];
    }
  }
}
