/*
  Copyright (c) 2007 Stefan Kurtz <kurtz@zbh.uni-hamburg.de>
  Copyright (c) 2007 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.
*/

#ifndef FMI_OCC_GEN
#define FMI_OCC_GEN

#define ACCESSBWTTEXT(POS)\
        gt_encseq_get_encoded_char(fm->bwtformatching,POS,\
                                          GT_READMODE_FORWARD)

static GtUword fmoccurrence (const Fmindex *fm,GtUchar cc,
                                   GtUword pos)
{
  GtUword bwtidx,
         bwtlastidx,
         numofocc,
         idx,
         maxbfreqidx,
         ctimesnumofblocks = (GtUword) (cc * fm->nofblocks),
         ctimesnumofsuperblocks = (GtUword) (cc * fm->nofsuperblocks),
         posshiftbsizepow = pos >> (GtUword) fm->log2bsize,
         posshiftsuperbsizepow = pos >> (GtUword) fm->log2superbsize;

#ifdef SKDEBUG
  printf("bwtlength=%u,",(unsigned int) fm->bwtlength);
  printf("longestpos=%u,",(unsigned int) fm->longestsuffixpos);
  printf("bsize=%u,",(unsigned int) fm->bsize);
  printf("pos=%u,cc=%u\n",pos,cc);
#endif
  if ((pos <= fm->longestsuffixpos &&
       fm->longestsuffixpos - pos < (GtUword) fm->bsize) ||
      (pos >  fm->longestsuffixpos &&
       pos - fm->longestsuffixpos < (GtUword) fm->bsize))
  {                                    /* dollar in block */
    maxbfreqidx = ctimesnumofblocks + posshiftbsizepow;
    numofocc = fm->superbfreq[ctimesnumofsuperblocks + posshiftsuperbsizepow];
#ifdef SKDEBUG
    printf("case 0: numofocc = %u\n",numofocc);
#endif
    for (idx = ctimesnumofblocks +
               ((pos & fm->negatesuperbsizeones) >> fm->log2bsize);
         idx < maxbfreqidx; idx++)
    {
      numofocc += fm->bfreq[idx];
#ifdef SKDEBUG
      printf("case 1: numofocc = %u\n",numofocc);
#endif
    }
    for (bwtidx = (pos & fm->negatebsizeones); bwtidx < pos; bwtidx++)
    {
#ifdef SKDEBUG
      printf("access(%u)=%u,cc=%u\n",bwtidx,ACCESSBWTTEXT(bwtidx),cc);
#endif
      if (bwtidx != fm->longestsuffixpos && ACCESSBWTTEXT(bwtidx) == cc)
      {
        numofocc++;
#ifdef SKDEBUG
        printf("case 2: numofocc = %u\n",numofocc);
#endif
      }
    }
#ifdef SKDEBUG
    printf("(0) return %u\n",numofocc);
#endif
    return numofocc;
  }
  if ((pos & (fm->superbsize >> 1)) &&       /* first halve of superbuckets */
      fm->bwtlength - pos > (GtUword) fm->superbsize)
  {
    maxbfreqidx = ctimesnumofblocks +
                  ((posshiftsuperbsizepow + 1) <<
                   fm->log2superbsizeminuslog2bsize);
    numofocc = fm->superbfreq[ctimesnumofsuperblocks +
                              posshiftsuperbsizepow + 1];
#ifdef SKDEBUG
    printf("case 3: numofocc %u\n",numofocc);
#endif
    for (idx = ctimesnumofblocks + posshiftbsizepow; idx < maxbfreqidx; idx++)
    {
      numofocc -= fm->bfreq[idx];
#ifdef SKDEBUG
      printf("case 4: numofocc %u\n",numofocc);
#endif
    }
    if (pos & fm->bsizehalve)            /* second halve of bucket */
    {
      numofocc += fm->bfreq[ctimesnumofblocks + posshiftbsizepow];
#ifdef SKDEBUG
      printf("case 5: numofocc %u\n",numofocc);
#endif
      bwtlastidx = (posshiftbsizepow + 1) << (GtUword) fm->log2bsize;
      for (bwtidx = pos; bwtidx < bwtlastidx; bwtidx++)
      {
        if (ACCESSBWTTEXT(bwtidx) == cc)
        {
          numofocc--;
#ifdef SKDEBUG
          printf("case 6: numofocc = %u\n",numofocc);
#endif
        }
      }
    } else                               /* first halve of bucket */
    {
      for (bwtidx = (pos & fm->negatebsizeones); bwtidx < pos; bwtidx++)
      {
        if (ACCESSBWTTEXT(bwtidx) == cc)
        {
          numofocc++;
#ifdef SKDEBUG
          printf("case 7: numofocc = %u\n",numofocc);
#endif
        }
      }
    }
  } else                                 /* second halve of superbucket */
  {
    maxbfreqidx = ctimesnumofblocks + posshiftbsizepow;
    numofocc = fm->superbfreq[ctimesnumofsuperblocks + posshiftsuperbsizepow];
#ifdef SKDEBUG
    printf("case 8: numofocc = %u\n",numofocc);
#endif
    for (idx = ctimesnumofblocks +
               ((pos & fm->negatesuperbsizeones) >> fm->log2bsize);
         idx < maxbfreqidx; idx++)
    {
      numofocc += fm->bfreq[idx];
#ifdef SKDEBUG
      printf("case 9: numofocc = %u\n",numofocc);
#endif
    }
    if ((pos & fm->bsizehalve)           /* second halve of bucket */
        && (fm->bwtlength - pos > (GtUword) fm->bsize))
    {
      numofocc += fm->bfreq[ctimesnumofblocks + posshiftbsizepow];
#ifdef SKDEBUG
      printf("case 10: numofocc = %u\n",numofocc);
#endif
      bwtlastidx = (pos & fm->negatebsizeones) + fm->bsize;
      for (bwtidx = pos; bwtidx < bwtlastidx; bwtidx++)
      {
        if (ACCESSBWTTEXT(bwtidx) == cc)
        {
          numofocc--;
#ifdef SKDEBUG
          printf("case 11: numofocc = %u\n",numofocc);
#endif
        }
      }
    } else                               /* first halve of bucket */
    {
      for (bwtidx = (pos & fm->negatebsizeones); bwtidx < pos; bwtidx++)
      {
        if (ACCESSBWTTEXT(bwtidx) == cc)
        {
          numofocc++;
#ifdef SKDEBUG
          printf("case 12: numofocc = %u\n",numofocc);
#endif
        }
      }
    }
  }
#ifdef SKDEBUG
  printf("(1) return %u\n",numofocc);
#endif
  return numofocc;
}

#endif
