/* $Id: AssociativeArray,v 1.1.1.1 2008/11/28 17:57:26 kiesling Exp $ -*-c-*-*/

/*
  This file is part of ctalk.
  Copyright  2005 - 2008  Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

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

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/*
 *    AssociativeArray class.  
 */

List class AssociativeArray;

AssociativeArray instanceMethod new (char *__newArrayName) {

  AssociativeArray super new __newArrayName;
  OBJECT *a_obj;

  a_obj = __ctalk_get_object(__ctalk_to_c_char_ptr(__newArrayName), 
			     "AssociativeArray");
  if (a_obj -> instancevars) __ctalkDeleteObject (a_obj -> instancevars);
  a_obj -> instancevars = NULL;
#ifdef __DJGPP__
  methodReturnObjectName(__newArrayName)
#else
  methodReturnObject(__newArrayName)
#endif
}

AssociativeArray instanceMethod keyAt (char *__keyString) {

  OBJECT *key_object;
  
  returnObjectClass Key;

  key_object = __ctalkGetInstanceVariable (self, 
					   __ctalk_to_c_char_ptr(ARG(0))
					   , FALSE);
  if (key_object) {
    return key_object;
  } else {
    return __ctalkCreateObjectInit ("result", "String", "Character",
				    LOCAL_VAR, NULLSTR);
  }
}

AssociativeArray instanceMethod map (OBJECT *(*methodfn)()) {

  OBJECT *rcvr_obj, *(*fn)(), *t;
  METHOD *self_method, *arg_method;
  Integer new i;

  fn = (OBJECT *(*)())__ctalkRtGetMethodFn ();
  rcvr_obj = __ctalkRtReceiverObject ();

  if (((self_method = __ctalkFindInstanceMethodByFn (&rcvr_obj, fn, 0))
      == NULL) &&
      ((self_method = __ctalkFindClassMethodByFn (&rcvr_obj, fn, 0))
       == NULL)) {
    __ctalkCriticalExceptionInternal (NULL, undefined_method_x, 
				      "from map (Class List)");
    return NULL;
  }

  if (((arg_method = __ctalkFindInstanceMethodByName (&rcvr_obj, 
			      self_method->args[0] -> __o_name, 0))
      == NULL) &&
      ((arg_method = __ctalkFindClassMethodByName (&rcvr_obj, 
				 self_method->args[0]->__o_name, 0))
       == NULL)) {
    __ctalkCriticalExceptionInternal (NULL, undefined_method_x, 
				      "from map (Class List)");
    return NULL;
  }


  for (t = rcvr_obj -> instancevars; t; t = t -> next) {
    __ctalkInlineMethod (t, arg_method);
  }

  methodReturnNULL
}

AssociativeArray instanceMethod keyExists (char *__key) {

  OBJECT *key_object;
  
  returnObjectClass Integer;

  if ((key_object = __ctalkGetInstanceVariable (__ctalk_self_internal (), 
					   __ctalk_to_c_char_ptr(ARG(0))
						, FALSE)) != NULL) {
    methodReturnTrue;
  } else {
    methodReturnFalse;
  }
}
