/*
  Copyright (c) 2010, Kevin Waugh.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * The name of the author may not be used to endorse or promote products
      derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * rank7.c
 * author: Kevin Waugh (waugh@cs.cmu.edu)
 */

#include <assert.h>
#include <stdlib.h>
#include <zlib.h>

#define RANK7_SET_RANKER_SIZE    8192
#define RANK7_RANK_INDEXER_SIZE  350350

int * RANK7_SET_RANK     = 0;
int * RANK7_RANK_INDEXER = 0;

void init_rank7() {
  gzFile stream;

  assert(!RANK7_SET_RANK);
  assert(!RANK7_RANK_INDEXER);

  RANK7_SET_RANK     = calloc(sizeof(int), RANK7_SET_RANKER_SIZE);
  RANK7_RANK_INDEXER = calloc(sizeof(int), RANK7_RANK_INDEXER_SIZE);

  assert(stream = gzopen("rank7.gz", "rb"));
  assert(gzread(stream, RANK7_SET_RANK, 4*RANK7_SET_RANKER_SIZE) == 4*RANK7_SET_RANKER_SIZE);
  assert(gzread(stream, RANK7_RANK_INDEXER, 4*RANK7_RANK_INDEXER_SIZE) == 4*RANK7_RANK_INDEXER_SIZE);
  gzclose(stream);
}

void free_rank7() {
  assert(RANK7_SET_RANK);
  assert(RANK7_RANK_INDEXER);

  free(RANK7_SET_RANK);
  free(RANK7_RANK_INDEXER);
}

inline int get_suit(int card) {
  return card%4;
}

inline int get_rank(int card) {
  return card/4;
}

inline int max(int a, int b) {
  return a>b?a:b;
}

int rank7_52(const int board[], const int player[]) {
  int set_index[4], rank_index, i;

  assert(board);
  assert(player);

  set_index[0] = set_index[1] = set_index[2] = set_index[3] = rank_index = 0;
  for(i=0; i<5; ++i) {
    set_index[get_suit(board[i])] |= 1<<get_rank(board[i]);
    rank_index                     = RANK7_RANK_INDEXER[rank_index+get_rank(board[i])];
  }
  for(i=0; i<2; ++i) {
    set_index[get_suit(player[i])] |= 1<<get_rank(player[i]);
    rank_index                      = RANK7_RANK_INDEXER[rank_index+get_rank(player[i])];
  }

  for(i=0; i<4; ++i) {
    rank_index = max(rank_index, RANK7_SET_RANK[set_index[i]]);
  }

  return rank_index;
}

int rank7_7(const int cards[]) {
  assert(cards);

  return rank7_52(cards, cards+5);
}
