/* $Id: AssociativeArray,v 1.7 2009/01/04 20:28:39 kiesling Exp $ -*-c-*-*/

/*
  This file is part of Ctalk.
  Copyright  2005 - 2009  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(__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, __keyString, FALSE);
  if (key_object) {
    return key_object;
  } else {
    return __ctalkCreateObjectInit ("result", "String", "Character",
				    LOCAL_VAR, NULLSTR);
  }
}

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

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

  fn = (OBJECT *(*)())__ctalkRtGetMethodFn ();
  rcvr_obj = __ctalkRtReceiverObject ();
  /*
   *  The rcvr_value_obj's class is used to look up the receiver method
   *  in case the receiver is an instance or class variable.
   */
  if ((rcvr_value_obj = 
       __ctalkGetInstanceVariable (rcvr_obj, "value", 0)) == NULL) 
    rcvr_value_obj = rcvr_obj;

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

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

  for (t = rcvr_obj -> instancevars; t; t = t -> next) {
    if (!strcmp (t -> __o_name, "value"))
      continue;
    __ctalkInlineMethod (t, arg_method);
  }

  methodReturnNULL
}

AssociativeArray instanceMethod keyExists (char *__key) {

  OBJECT *key_object;
  
  returnObjectClass Integer;

  if ((key_object = 
       __ctalkGetInstanceVariable (self, __key, FALSE)) != NULL) {
    methodReturnTrue;
  } else {
    methodReturnFalse;
  }
}
