/*
 * 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 <errno.h>

#include <unibits/strcoll.h>

#include <sakura/common/string-hash.h>
#include <sakura/common/newdict.h>

#include <bp/common.h>
#include <bp/ng/binary-pool.h>

#define BINARY_POOL_ALIGNMENT 4

int binary_pool_compare_freq(const void *p1, const void *p2)
{
   const struct binary_pool_node *n1 = p1;
   const struct binary_pool_node *n2 = p2;

   return n1->count == n2->count ? n1->length - n2->length : 
      n2->count - n1->count;
}

int binary_pool_compare_alpha(const void *p1, const void *p2)
{
   const struct binary_pool_node *n1 = p1;
   const struct binary_pool_node *n2 = p2;
   
   return unibits_afronte_strcoll(n1->data, n2->data);
}

int binary_pool_compare_ialpha(const void *p1, const void *p2)
{
   const struct binary_pool_node *n1 = p1;
   const struct binary_pool_node *n2 = p2;

   return unibits_afronte_stricoll(n1->data, n2->data);
}

int binary_pool_compare_atergo(const void *p1, const void *p2)
{
   const struct binary_pool_node *n1 = p1;
   const struct binary_pool_node *n2 = p2;

   return unibits_atergo_strcoll(n1->data, n2->data);
}

int binary_pool_compare_iatergo(const void *p1, const void *p2)
{
   const struct binary_pool_node *n1 = p1;
   const struct binary_pool_node *n2 = p2;

   return unibits_atergo_stricoll(n1->data, n2->data);
}

void binary_pool_create(struct binary_pool *this, size_t size)
{
   size_t i;

   assert(this != NULL);
   assert(size > 0);
   assert(size < SIZE_MAX / sizeof *this->hash_array);
   marena_create(&this->arena);
   this->hash_array = bp_malloc(sizeof *this->hash_array * size);
   this->array_size = size;
   this->count = 0;
   this->dense_array = NULL;
   this->index = NULL;
   this->offset = NULL;
   for (i = 0; i < this->array_size; ++i)
      this->hash_array[i].node.count = 0;
}

static void binary_pool_resize(struct binary_pool *this, size_t size)
{
   struct binary_pool_chain *old_hash_array, *chain, *next_chain, *new_chain;
   size_t i, old_size, hash;

   assert(this != NULL);
   assert(this->dense_array == NULL);
   assert(size > this->array_size);
   if (size > SIZE_MAX / sizeof *this->hash_array) {
      errno = ENOMEM;
      bp_system_error("memory allocation error");
   }
   old_hash_array = this->hash_array;
   old_size = this->array_size;
   this->array_size = size;
   this->hash_array = bp_malloc(sizeof *this->hash_array * size);
   for (i = 0; i < this->array_size; ++i)
      this->hash_array[i].node.count = 0;
   for (i = 0; i < old_size; i++) {
      chain = old_hash_array + i;
      if (chain->node.count == 0)
         continue;
      while (chain != NULL) {
         next_chain = chain->next;
         hash = hash_string_n((const char*) chain->node.data, chain->node.length);
         new_chain = this->hash_array + hash % size;
         if (new_chain->node.count == 0) {
            new_chain->node = chain->node;
            new_chain->next = NULL;
         }
         else {
            while (new_chain->next != NULL)
               new_chain = new_chain->next;
            new_chain->next = chain;
            chain->next = NULL;
         }
         chain = next_chain;
      }
   }
   free(old_hash_array);
}

void binary_pool_destroy(struct binary_pool *this)
{
   if (this == NULL)
      return;
   free(this->hash_array);
   free(this->dense_array);
   free(this->index);
   free(this->offset);
   marena_destroy(&this->arena);
}

void binary_pool_add_dict(struct binary_pool *this, const char *image_pathname,
   const char *offset_pathname)
{
   struct newdict dict;
   int rc;
   size_t i;
   size_t num_items;

   assert(this != NULL);
   rc = newdict_open(&dict, image_pathname, offset_pathname, false);
   if (rc != 0)
      bp_system_error("unable to open dictionary %s", image_pathname);
   num_items = GET_NUM_ITEMS(&dict);
   for (i = 0; i < num_items; ++i)
      binary_pool_add(this, GET_ITEM(&dict, i), GET_LENGTH(&dict, i));
   newdict_close(&dict);
}

struct binary_pool_node *binary_pool_add(struct binary_pool *this,
   const void *buffer, size_t length)
{
   return binary_pool_add_freq(this, buffer, length, 1);
}

struct binary_pool_node *binary_pool_add_freq(struct binary_pool *this,
   const void *buffer, size_t length, size_t freq)
{
   size_t hash;
   struct binary_pool_chain *chain, *last;

   assert(this != NULL);
   assert(buffer != NULL);
   assert(length > 0);

   if (this->count / 3 > this->array_size / 4)
      /* Hash table is 75% full. Do a resize. */
      binary_pool_resize(this, this->array_size * 2);

   /* calculate hash */
   hash = hash_string_n((const char*) buffer, length);

   /* get initial node */
   chain = this->hash_array + hash % this->array_size;

   /* empty initial node check */
   if (chain->node.count == 0) {
      chain->next = NULL;
      this->last = chain;
      goto fill_values;
   }

   /* try to match the keys */
   for (last = chain; chain != NULL; chain = chain->next) {
      int cmp_result;

      /* non equal hash or data length -> skip */
      if (chain->node.length != length)
         continue;

      /* compare binaries here */
      cmp_result = memcmp(chain->node.data, buffer, length);

      /* identical keys -> increase count and return */
      if (cmp_result == 0) {
         chain->node.count += freq;
         return &chain->node;
      }
      last = chain;
   }
   /* create new node (between 'last' and 'node')  */
   this->last = chain = bp_marena_alloc(&this->arena, sizeof *chain);
   chain->next = last->next;
   last->next = chain;

fill_values:

   /* fill the values of node */
   chain->node.id = this->count++;
   chain->node.count = freq;
   chain->node.length = length;
   chain->node.data = bp_marena_alloc(&this->arena, length);
   memcpy(chain->node.data, buffer, length);
   return &chain->node;
}

struct binary_pool_node *binary_pool_add_string_n(struct binary_pool *this,
   const char *str, size_t length)
{
   char *dup_str;
   struct binary_pool_node *res;

   assert(str != NULL);
   dup_str = bp_malloc(length + 1);
   memcpy(dup_str, str, length);
   dup_str[length] = 0;
   res = binary_pool_add(this, dup_str, length + 1);
   free(dup_str);
   return res;
}

struct binary_pool_node *binary_pool_add_string(struct binary_pool *this,
   const char *str)
{
   assert(str != NULL);
   return binary_pool_add(this, str, strlen(str) + 1);
}

struct binary_pool_node *binary_pool_lookup(struct binary_pool *this,
   const void *buffer, size_t length)
{
   size_t hash;
   struct binary_pool_chain *chain;

   assert(this != NULL);
   assert(buffer != NULL);
   assert(length > 0);
   hash = hash_string_n(buffer, length);
   chain = this->hash_array + hash % this->array_size;
   if (chain->node.count == 0)
      return NULL;
   /* try to find match the keys */
   for (; chain != NULL; chain = chain->next) {
      int cmp_result;
      /* non-equal hash or data length -> skip */
      if (chain->node.length != length)
         continue;
      /* compare binary here */
      cmp_result = memcmp(chain->node.data, buffer, length);
      /* identical keys -> increase count and return */
      if (cmp_result == 0) {
         return &chain->node;
      }
   }
   return NULL;
}

void binary_pool_make_dense_array(struct binary_pool *this)
{
   size_t i;
   struct binary_pool_chain *chain;
   struct binary_pool_chain *hash_array;
   struct binary_pool_node *dense_array;

   assert(this != NULL);
   if (this->dense_array != NULL)
      return;

   /* allocate dense array */
   this->dense_array = bp_malloc(sizeof *this->dense_array * this->count);

   /* shortcuts */
   hash_array = this->hash_array;
   dense_array = this->dense_array;

   /* iterate over entire hash table and copy all elements */
   for (i = 0; i < this->array_size; ++i)
      for (chain = hash_array + i; chain && chain->node.count > 0; 
         chain = chain->next)
      {
         dense_array[chain->node.id] = chain->node;
      }

   /* allocate index and offset arrays */
   this->index = bp_malloc(sizeof *this->index * this->count);
   this->offset = bp_malloc(sizeof *this->offset * this->count);

   /* reset index to default values */
   for (i = 0; i < this->count; ++i)
      this->index[i] = en4(i);
}

struct binary_pool_node *binary_pool_lookup_string_n(struct binary_pool *this,
   const char *str, size_t length)
{
   struct binary_pool_node *res;
   char *dup_str;

   assert(str != NULL);
   dup_str = bp_malloc(length + 1);
   memcpy(dup_str, str, length);
   dup_str[length] = 0;
   res = binary_pool_lookup(this, dup_str, length + 1);
   free(dup_str);
   return res;
}

void binary_pool_sort_data(struct binary_pool *this, binary_pool_node_cmp_fn cmp)
{
   assert(this != NULL);
   assert(this->dense_array != NULL);
   qsort(this->dense_array, this->count, sizeof *this->dense_array,
      cmp);
}

void binary_pool_sort_index(struct binary_pool *this, binary_pool_node_cmp_fn cmp)
{
   size_t i;
   assert(this != NULL);
   assert(this->dense_array != NULL);
   qsort(this->dense_array, this->count, sizeof *this->dense_array, 
      cmp);
   for (i = 0; i < this->count; ++i)
      this->index[this->dense_array[i].id] = en4(i);
}

void binary_pool_write_index(struct binary_pool *this, FILE *stream)
{
   assert(this != NULL);
   assert(this->index != NULL);
   fwrite(this->index, sizeof *this->index, this->count, stream);
   if (ferror(stream))
      bp_fatal("write error");
}

void binary_pool_write_data(struct binary_pool *this, FILE *stream)
{
   struct binary_pool_node *item;
   size_t i;
   size_t offset;
   
   assert(this != NULL);
   assert(this->dense_array != NULL);
   assert(this->offset != NULL);
   for (offset = 0, i = 0; i < this->count; ++i) {
      uint32_t length;
      unsigned int misalignment;
      item = this->dense_array + i;
      offset += sizeof(item->length); /* remember about length */
      this->offset[item->id] = en4(offset);
      length = en4(item->length);
      fwrite(&length, sizeof length, 1, stream);
      fwrite(item->data, 1, item->length, stream);
      offset += item->length;
      misalignment = item->length % BINARY_POOL_ALIGNMENT;
      if (misalignment > 0) {
         while (misalignment < BINARY_POOL_ALIGNMENT) {
            fputc(0, stream);
            offset++;
            misalignment++;
         }
      }
      if (ferror(stream))
         bp_fatal("write error");
   }
}

void binary_pool_write_offsets(struct binary_pool *this, FILE *stream)
{
   assert(this != NULL);
   assert(this->offset != NULL);
   fwrite(this->offset, sizeof *this->offset, this->count, stream);
   if (ferror(stream))
      bp_fatal("write error");
}
