/* This file is part of cqual.
   Copyright (C) 2000-2001 The Regents of the University of California.

cqual is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

cqual is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with cqual; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */

/*
 * Hash info
 *
 * Hash keys and hash data.  Used by qenv.
 *
 */

#include "hash_info.h"
#include "qtype.h"
#include "util.h"

hash_key id_to_key(data_declaration data) {
  return ((hash_key) {kind: key_id, u: {id: data} });
}

hash_key field_to_key(field_declaration field) {
  return ((hash_key) {kind: key_field, u: {field: field} });
}

hash_key aloc_to_key(aloc loc) {
  return ((hash_key) {kind: key_loc, u: {loc: loc} });
}

hash_data qtype_to_data(qtype qt) {
  return ((hash_data) {kind: data_qtype, u: {qt: qt} });
}

qtype data_to_qtype(hash_data hd) {
  assert(hd.kind == data_qtype);
  return hd.u.qt;
}

/* Return true iff k1 and k2 are the same */
bool key_eq(hash_key k1, hash_key k2)
{
  return (k1.kind == k2.kind && k1.u.generic == k2.u.generic);
}

/* Return -1, 0, or 1 if k1 is less than, equal to, or greater than
   k2 (in some consistent sense) */
int keycmp(hash_key k1, hash_key k2)
{
  unsigned int
    d1 = (unsigned int) k1.u.generic,
    d2 = (unsigned int) k2.u.generic;
  if (d1 < d2) return -1;
  else if (d1 > d2) return 1;
  else return 0;
}

/* Return a hash of d */
int key_to_hash(hash_key k)
{
  return (int) k.u.generic;
}

/* Print hash key */
void print_hash_key(printf_func pf, hash_key k)
{
  switch(k.kind)
    {
    case key_field:
      pf("(field) %s", k.u.field->name);
      break;
    case key_id:
      pf("%s", k.u.id->name);
      break;
    default:
      fail("Unexpected key kind %x\n", k.kind);
    }
}

/* Print hash data */
void print_hash_data(printf_func pf, hash_data d)
{
  switch(d.kind)
    {
    case data_qtype:
      print_qtype_raw(pf, d.u.qt);
      break;
    default:
      fail("Unexpected data kind %x\n", d.kind);
    }
}

/*
 * Hash key lists
 */

/* Make a new hash key list */
hash_key_list new_hash_key_list(region r)
{
  return NULL;
}

/* Add to the front of hkl */
void insert_hash_key_list(region r, hash_key_list *hkl, hash_key k)
{
  hash_key_list new;

  new = ralloc(r, struct Hash_key_list);
  new->key = k;
  new->next = *hkl;
  *hkl = new;
}

/* Destructively add hkl2 to the end of hkl1. */
void append_hash_key_list(hash_key_list *hkl1, hash_key_list hkl2)
{
  hash_key_list *cur = hkl1;
  while (*cur)
    cur = &(*cur)->next;
  *cur = hkl2;
}

/* Scan the list.  List must not be modified during scanning */
#define scan_hash_key_list(hkl, var) for (var = hkl; var; var = var->next)

/* Return the length of hkl */
int length_hash_key_list(hash_key_list hkl)
{
  int i;
  hash_key_list cur;

  i = 0;
  cur = hkl;
  scan_hash_key_list(hkl, cur)
    i++;
  return i;
}

/* Return TRUE iff k is in hkl */
bool in_hash_key_list(hash_key_list hkl, hash_key k)
{
  hash_key_list cur;

  cur = hkl;
  scan_hash_key_list(hkl, cur)
    if (key_eq(cur->key, k))
      return TRUE;
  return FALSE;
}
