/*
 * HashTableFunctions.java
 *
 * Copyright (C) 2002-2005 Peter Graves
 * $Id: HashTableFunctions.java,v 1.3 2005/07/11 17:20:18 piso Exp $
 *
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.armedbear.lisp;

public final class HashTableFunctions extends Lisp
{
    private static final LispObject FUNCTION_EQ =
        Symbol.EQ.getSymbolFunction();
    private static final LispObject FUNCTION_EQL =
        Symbol.EQL.getSymbolFunction();
    private static final LispObject FUNCTION_EQUAL =
        Symbol.EQUAL.getSymbolFunction();
    private static final LispObject FUNCTION_EQUALP =
        Symbol.EQUALP.getSymbolFunction();

    // ### %make-hash-table
    private static final Primitive _MAKE_HASH_TABLE =
        new Primitive("%make-hash-table", PACKAGE_SYS, false)
    {
        public LispObject execute(LispObject test, LispObject size,
                                  LispObject rehashSize, LispObject rehashThreshold)
            throws ConditionThrowable
        {
            int n;
            try {
                n = ((Fixnum)size).value;
            }
            catch (ClassCastException e) {
                return signal(new TypeError(size, Symbol.FIXNUM));
            }
            if (test == FUNCTION_EQL || test == NIL)
                return new EqlHashTable(n, rehashSize, rehashThreshold);
            if (test == FUNCTION_EQ)
                return new EqHashTable(n, rehashSize, rehashThreshold);
            if (test == FUNCTION_EQUAL)
                return new EqualHashTable(n, rehashSize, rehashThreshold);
            if (test == FUNCTION_EQUALP)
                return new EqualpHashTable(n, rehashSize, rehashThreshold);
            return signal(new LispError("Unknown test for MAKE-HASH-TABLE: " +
                                        test.writeToString()));
        }
    };

    // ### gethash
    // gethash key hash-table &optional default => value, present-p
    private static final Primitive GETHASH =
        new Primitive("gethash", "key hash-table &optional default")
    {
        public LispObject execute(LispObject key, LispObject ht)
            throws ConditionThrowable
        {
            try {
                return ((HashTable)ht).gethash(key);
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
        public LispObject execute(LispObject key, LispObject ht,
                                  LispObject defaultValue)
            throws ConditionThrowable
        {
            try {
                return ((HashTable)ht).gethash(key, defaultValue);
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
    };

    // ### gethash-2op-1ret key hash-table => value
    private static final Primitive GETHASH_2OP_1RET =
        new Primitive("gethash-2op-1ret", PACKAGE_SYS, true, "key hash-table")
    {
        public LispObject execute(LispObject key, LispObject ht)
            throws ConditionThrowable
        {
            try {
                return ((HashTable)ht).gethash_2op_1ret(key);
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
    };

    // ### puthash
    // puthash key hash-table new-value &optional default => value
    private static final Primitive PUTHASH =
        new Primitive("puthash", PACKAGE_SYS, true)
    {
        public LispObject execute(LispObject key, LispObject ht,
                                  LispObject value)
            throws ConditionThrowable
        {
            try {
                return ((HashTable)ht).puthash(key, value);
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
        public LispObject execute(LispObject key, LispObject ht,
                                  LispObject ignored, LispObject value)
            throws ConditionThrowable
        {
            try {
                return ((HashTable)ht).puthash(key, value);
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
    };

    // remhash key hash-table => generalized-boolean
    private static final Primitive REMHASH =
        new Primitive("remhash", "key hash-table")
    {
        public LispObject execute(LispObject key, LispObject ht)
            throws ConditionThrowable
        {
            try {
                return ((HashTable)ht).remhash(key);
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
    };

    // ### clrhash
    // clrhash hash-table => hash-table
    private static final Primitive CLRHASH =
        new Primitive("clrhash", "hash-table")
    {
        public LispObject execute(LispObject ht) throws ConditionThrowable
        {
            try {
                ((HashTable)ht).clear();
                return ht;
            }
            catch (ClassCastException e) {
                return signal(new TypeError(ht, Symbol.HASH_TABLE));
            }
        }
    };

    // ### hash-table-count
    private static final Primitive HASH_TABLE_COUNT =
        new Primitive("hash-table-count", "hash-table")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            try {
                return new Fixnum(((HashTable)arg).getCount());
            }
            catch (ClassCastException e) {
                return signal(new TypeError(arg, Symbol.HASH_TABLE));
            }
        }
    };

    // ### sxhash
    // sxhash object => hash-code
    private static final Primitive SXHASH = new Primitive("sxhash", "object")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            return new Fixnum(arg.sxhash());
        }
    };

    // ### psxhash
    // psxhash object => hash-code
    // For EQUALP hash tables.
    private static final Primitive PSXHASH =
        new Primitive("psxhash", PACKAGE_SYS, true, "object")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            return new Fixnum(arg.psxhash());
        }
    };

    // ### hash-table-p
    private static final Primitive HASH_TABLE_P =
        new Primitive("hash-table-p","object")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            return arg instanceof HashTable ? T : NIL;
        }
    };

    // ### hash-table-entries
    private static final Primitive HASH_TABLE_ENTRIES =
        new Primitive("hash-table-entries", PACKAGE_SYS, false)
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            try {
                return ((HashTable)arg).ENTRIES();
            }
            catch (ClassCastException e) {
                return signal(new TypeError(arg, Symbol.HASH_TABLE));
            }
        }
    };

    // ### hash-table-test
    private static final Primitive HASH_TABLE_TEST =
        new Primitive("hash-table-test", "hash-table")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            try {
                return ((HashTable)arg).getTest();
            }
            catch (ClassCastException e) {
                return signal(new TypeError(arg, Symbol.HASH_TABLE));
            }
        }
    };

    // ### hash-table-size
    private static final Primitive HASH_TABLE_SIZE =
        new Primitive("hash-table-size", "hash-table")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            try {
                return new Fixnum(((HashTable)arg).getSize());
            }
            catch (ClassCastException e) {
                return signal(new TypeError(arg, Symbol.HASH_TABLE));
            }
        }
    };

    // ### hash-table-rehash-size
    private static final Primitive HASH_TABLE_REHASH_SIZE =
        new Primitive("hash-table-rehash-size", "hash-table")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            try {
                return ((HashTable)arg).getRehashSize();
            }
            catch (ClassCastException e) {
                return signal(new TypeError(arg, Symbol.HASH_TABLE));
            }
        }
    };

    // ### hash-table-rehash-threshold
    private static final Primitive HASH_TABLE_REHASH_THRESHOLD =
        new Primitive("hash-table-rehash-threshold", "hash-table")
    {
        public LispObject execute(LispObject arg) throws ConditionThrowable
        {
            try {
                return ((HashTable)arg).getRehashThreshold();
            }
            catch (ClassCastException e) {
                return signal(new TypeError(arg, Symbol.HASH_TABLE));
            }
        }
    };
}
