/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

#include <poliqarp-config.h>

#include <bp/common.h>
#include "occurrences.h"

int occurrences_compare(const void *x_, const void *y_)
{
   const struct occurrence *x = (const struct occurrence *)x_,
                           *y = (const struct occurrence *)y_;
   int res = x->term - y->term;
   if (res)
      return res;
   return x->document - y->document;
}

void occurrences_create(struct occurrences *occurrences, size_t size,
   FILE *file)
{
   occurrences->data = bp_malloc(size * sizeof *occurrences->data);
   occurrences->size = size;
   occurrences->used = 0;
   occurrences->file = file;

   occurrences->frequencies = bp_malloc(sizeof(struct vector));
   vector_create(occurrences->frequencies, VECTOR_ZERO);

   vector_create(&occurrences->offsets, VECTOR_DEFAULT);
   vector_set(&occurrences->offsets, 0, 0);

   file_obs_create(&occurrences->bs_backend, 65536, file);
   file_obs_set_backend(&occurrences->bs, &occurrences->bs_backend);
}

void occurrences_flush(struct occurrences *occurrences)
{
   size_t i;
   uint32_t last_term = 0, last_document = 0;

   if (occurrences->used == 0)
      return;
   qsort(occurrences->data, occurrences->used, sizeof(struct occurrence),
         occurrences_compare);
   for (i = 0; i < occurrences->used; i++) {
      uint32_t diff_term = occurrences->data[i].term - last_term;
      encode_unary(&occurrences->bs, diff_term + 1);
      if (diff_term)
         encode_delta(&occurrences->bs, occurrences->data[i].document + 1);
      else {
         uint32_t diff_document = occurrences->data[i].document - last_document;
         if (i == 0) diff_document = occurrences->data[i].document + 1;
         encode_delta(&occurrences->bs, diff_document);
      }
      last_term = occurrences->data[i].term;
      last_document = occurrences->data[i].document;
   }
   occurrences->remaining = occurrences->used;
   occurrences->used = 0;
   file_obs_flush(occurrences->bs.data);
   vector_append(&occurrences->offsets, ftello(occurrences->file));
   if (occurrences->bs_backend.block_size * vector_length(&occurrences->offsets)
      > occurrences->size * sizeof(struct occurrence))
   {
      file_obs_halve_block(&occurrences->bs_backend);
   }
}

void occurrences_destroy(struct occurrences *occurrences)
{
   free(occurrences->data);
   occurrences->data = NULL;
   file_obs_destroy(&occurrences->bs_backend);
   vector_destroy(&occurrences->offsets);
}

void occurrences_insert(struct occurrences *occurrences, uint32_t document,
   uint32_t term)
{
   size_t i = occurrences->used++;
   occurrences->data[i].term = term;
   occurrences->data[i].document = document;
   if (occurrences->used == occurrences->size) {
      occurrences_flush(occurrences);
   }
}

void occurrences_dump_segments(struct occurrences *occurrences, uint32_t document,
   struct bst_node *t)
{
   if (t == NULL)
      return;
   occurrences_dump_segments(occurrences, document, t->left);
   vector_inc(occurrences->frequencies, t->val);
   occurrences_insert(occurrences, document, t->val);
   occurrences_dump_segments(occurrences, document, t->right);
}

