cvsuser     04/12/29 06:12:20

  Modified:    dynclasses pydict.pmc pyslice.pmc
               languages/python README
  Log:
  Hash of objects
  
  Revision  Changes    Path
  1.17      +50 -81    parrot/dynclasses/pydict.pmc
  
  Index: pydict.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/dynclasses/pydict.pmc,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- pydict.pmc        28 Dec 2004 19:17:12 -0000      1.16
  +++ pydict.pmc        29 Dec 2004 14:12:18 -0000      1.17
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
  -$Id: pydict.pmc,v 1.16 2004/12/28 19:17:12 rubys Exp $
  +$Id: pydict.pmc,v 1.17 2004/12/29 14:12:18 rubys Exp $
   
   =head1 NAME
   
  @@ -21,7 +21,7 @@
   #include "parrot/parrot.h"
   #include "pyconsts.h"
   
  -STRING * hash_get_idx(Interp *interpreter, Hash *hash, PMC *key);
  +PMC * hash_get_idx(Interp *interpreter, Hash *hash, PMC *key);
   
   /*
   
  @@ -37,13 +37,7 @@
   static size_t
   key_hash_object(Interp *interpreter, Hash *hash, void *value)
   {
  -    STRING *s = value;
  -
  -    if (s->hashval) {
  -        return s->hashval ^ hash->seed;
  -    }
  -
  -    return string_hash(interpreter, s) ^ hash->seed;
  +    return VTABLE_hash(interpreter, (PMC*)value) ^ hash->seed;
   }
   
   /*
  @@ -62,7 +56,7 @@
   static int
   object_compare(Parrot_Interp interp, void *a, void *b)
   {
  -    return string_equal(interp, (STRING *)a, (STRING *) b);
  +    return !mmd_dispatch_i_pp(interp, (PMC *)a, (PMC *)b, MMD_EQ);
   }
   
   pmclass PyDict extends PyObject need_ext does hash dynpmc group python_group 
{
  @@ -131,16 +125,14 @@
   */
   
       void delete_keyed(PMC* key) {
  -        STRING * sx;
           Hash * h = (Hash *)PMC_struct_val(SELF);
           HashBucket *b;
           PMC *nextkey = key_next(INTERP, key);
  -        sx = key_string(INTERP, key);
  -        b = hash_get_bucket(INTERP, h, sx);
  +        b = hash_get_bucket(INTERP, h, key);
           if (b == NULL)
                   return;  /* no such key */
           else if (nextkey == NULL)
  -            hash_delete(INTERP, h, sx);
  +            hash_delete(INTERP, h, key);
           else
               VTABLE_delete_keyed(INTERP, (PMC*)b->value, nextkey);
       }
  @@ -170,12 +162,10 @@
   */
   
       INTVAL exists_keyed(PMC* key) {
  -        STRING * sx;
           Hash * h = (Hash *)PMC_struct_val(SELF);
           HashBucket *b;
           PMC *nextkey = key_next(INTERP, key);
  -        sx = key_string(INTERP, key);
  -        b = hash_get_bucket(INTERP, h, sx);
  +        b = hash_get_bucket(INTERP, h, key);
           if (b == NULL)
               return 0;       /* no such key */
           if (nextkey == NULL)
  @@ -270,35 +260,19 @@
   
       PMC* get_pmc_keyed (PMC* key) {
           PMC* valpmc;
  -        STRING* keystr;
           Hash *hash = PMC_struct_val(SELF);
           HashBucket *b;
           PMC* nextkey;
   
           if ((PObj_get_FLAGS(key) & KEY_type_FLAGS) == 
KEY_hash_iterator_FLAGS) {
  -            PMC *ret;
  -            STRING *s = hash_get_idx(INTERP, hash, key);
  -            if (s->obj.flags & KEY_integer_FLAG) {
  -                INTVAL i;
  -                ret = pmc_new(INTERP, PyBuiltin_PyInt);
  -                i = string_to_int(INTERP, s);
  -                VTABLE_set_integer_native(INTERP, ret, i);
  -            }
  -            else {
  -                ret = pmc_new(INTERP, PyBuiltin_PyString);
  -                VTABLE_set_string_native(INTERP, ret, s);
  -            }
  -            return ret;
  +            return hash_get_idx(INTERP, hash, key);
           }
   
  -        keystr = key_string(INTERP, key);
           b = hash_get_bucket(INTERP, (Hash*) PMC_struct_val(SELF),
  -                                        keystr);
  +                                        key);
           if (b == NULL) {
  -            STRING *message = Parrot_sprintf_c(INTERP, "KeyError: %s",
  -                string_to_cstring(INTERP, keystr));
               real_exception(INTERP, NULL, E_KeyError,
  -                string_to_cstring(INTERP, message));
  +                "KeyError: %Ss", VTABLE_get_string(INTERP, key));
           }
           nextkey = key_next(INTERP, key);
           if (!nextkey)
  @@ -315,15 +289,9 @@
   */
   
       PMC* get_pmc_keyed_str (STRING* key) {
  -        HashBucket *b = hash_get_bucket(INTERP, (Hash*) PMC_struct_val(SELF),
  -                                        key);
  -        if (b == NULL) {
  -            STRING *message = Parrot_sprintf_c(INTERP, "KeyError: %s",
  -                string_to_cstring(INTERP, key));
  -            real_exception(INTERP, NULL, E_KeyError,
  -                string_to_cstring(INTERP, message));
  -        }
  -        return b->value;
  +        PMC *pmckey = pmc_new(INTERP, PyBuiltin_PyString);
  +        VTABLE_set_string_native(INTERP, pmckey, key);
  +        return VTABLE_get_pmc_keyed(INTERP, SELF, pmckey);
       }
   
   /*
  @@ -375,8 +343,8 @@
   */
   
       STRING* get_string_keyed (PMC* key) {
  -        PMC* valpmc = VTABLE_get_pmc_keyed(INTERP, SELF, key);
  -        return VTABLE_get_string(INTERP, valpmc);
  +        PMC *value = VTABLE_get_pmc_keyed(INTERP, SELF, key);
  +        return VTABLE_get_string(INTERP, value);
       }
   
   /*
  @@ -437,9 +405,9 @@
   */
   
       void set_integer_keyed (PMC* key, INTVAL value) {
  -         PMC *ivalue = pmc_new(INTERP, PyBuiltin_PyInt);
  -         VTABLE_set_integer_native(INTERP, ivalue, value);
  -         VTABLE_set_pmc_keyed(INTERP, SELF, key, ivalue);
  +        PMC *ivalue = pmc_new(INTERP, PyBuiltin_PyInt);
  +        VTABLE_set_integer_native(INTERP, ivalue, value);
  +        VTABLE_set_pmc_keyed(INTERP, SELF, key, ivalue);
       }
   
   /*
  @@ -464,12 +432,12 @@
           if (VTABLE_elements(INTERP, value) != n)
               return 0;
           for (j = 0; j < n; ++j) {
  -            STRING *key = VTABLE_shift_string(INTERP, iter);
  +            PMC *key = VTABLE_shift_pmc(INTERP, iter);
               PMC *item1, *item2;
  -            if (!VTABLE_exists_keyed_str(INTERP, value, key))
  +            if (!VTABLE_exists_keyed(INTERP, value, key))
                   return 0;
  -            item1 = SELF.get_pmc_keyed_str(key);
  -            item2 = VTABLE_get_pmc_keyed_str(INTERP, value, key);
  +            item1 = SELF.get_pmc_keyed(key);
  +            item2 = VTABLE_get_pmc_keyed(INTERP, value, key);
               if (item1 == item2)
                   continue;
               if (!mmd_dispatch_i_pp(INTERP, item1, item2, MMD_EQ))
  @@ -480,20 +448,6 @@
   
   /*
   
  -=item C<INTVAL exists_keyed_str(STRING *key)>
  -
  -=cut
  -
  -*/
  -
  -    INTVAL exists_keyed_str(STRING* key) {
  -        HashBucket *b = hash_get_bucket(INTERP, (Hash*) PMC_struct_val(SELF),
  -                                        key);
  -        return b != NULL;
  -    }
  -
  -/*
  -
   =item C<void set_pmc_keyed(PMC *key, PMC *value)>
   
   =cut
  @@ -501,27 +455,42 @@
   */
   
       void set_pmc_keyed (PMC* key, PMC* value) {
  -        STRING* keystr;
           PMC* nextkey;
           PMC* box;
           PMC* val;
   
           if (!key) return;
  -        keystr = key_string(INTERP, key);
   
  -        /* XXX: set the integer flat on the keystr itself */
  -        keystr->obj.flags &= ~KEY_integer_FLAG;
  -        if (key->vtable->base_type == PyBuiltin_PyInt)
  -            keystr->obj.flags |= KEY_integer_FLAG;
  -        else if (key->vtable->base_type == enum_class_Key)
  -            keystr->obj.flags |= (key->obj.flags & KEY_integer_FLAG);
  +        if (key->vtable->base_type == enum_class_Key) {
  +            PMC *key2;
  +            switch (PObj_get_FLAGS(key) & KEY_type_FLAGS) {
  +                case KEY_integer_FLAG:
  +                    key2 = pmc_new(INTERP, PyBuiltin_PyInt);
  +                    VTABLE_set_integer_native(INTERP, key2, 
PMC_int_val(key));
  +                    break;
  +                case KEY_number_FLAG:
  +                    key2 = pmc_new(INTERP, PyBuiltin_PyFloat);
  +                    VTABLE_set_number_native(INTERP, key2, PMC_num_val(key));
  +                    break;
  +                case KEY_string_FLAG:
  +                    key2 = pmc_new(INTERP, PyBuiltin_PyString);
  +                    VTABLE_set_string_native(INTERP, key2, PMC_str_val(key));
  +                    break;
  +                case KEY_pmc_FLAG:
  +                    key2 = PMC_pmc_val(SELF);
  +                    break;
  +                default:
  +                    internal_exception(1, "Unknown key type");
  +            }
  +            key = key2;
  +        }
   
           nextkey = key_next(INTERP, key);
           if (nextkey == NULL) {
  -            hash_put(INTERP, PMC_struct_val(SELF), keystr, value);
  +            hash_put(INTERP, PMC_struct_val(SELF), key, value);
               return;
           }
  -        box = SELF.get_pmc_keyed_str(keystr);
  +        box = SELF.get_pmc_keyed(key);
           if (box == NULL) {
               /* autovivify an PyDict */
               box = pmc_new(INTERP, DYNSELF.type());
  @@ -538,9 +507,9 @@
   */
   
       void set_string_keyed (PMC* key, STRING* value) {
  -         PMC *svalue = pmc_new(INTERP, PyBuiltin_PyString);
  -         VTABLE_set_string_native(INTERP, svalue, value);
  -         VTABLE_set_pmc_keyed(INTERP, SELF, key, svalue);
  +        PMC *svalue = pmc_new(INTERP, PyBuiltin_PyString);
  +        VTABLE_set_string_native(INTERP, svalue, value);
  +        VTABLE_set_pmc_keyed(INTERP, SELF, key, svalue);
       }
   
   /*
  
  
  
  1.2       +19 -1     parrot/dynclasses/pyslice.pmc
  
  Index: pyslice.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/dynclasses/pyslice.pmc,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- pyslice.pmc       24 Dec 2004 23:08:30 -0000      1.1
  +++ pyslice.pmc       29 Dec 2004 14:12:18 -0000      1.2
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2004 The Perl Foundation.  All Rights Reserved.
  -$Id: pyslice.pmc,v 1.1 2004/12/24 23:08:30 rubys Exp $
  +$Id: pyslice.pmc,v 1.2 2004/12/29 14:12:18 rubys Exp $
   
   =head1 NAME
   
  @@ -89,6 +89,24 @@
   
   /*
   
  +=item <PMC* get_pmc_keyed(PMC* key)>
  +
  +Returns the PMC value of the element at index C<key>.
  +
  +=cut
  +
  +*/
  +
  +    PMC* get_pmc_keyed (PMC* key) {
  +        INTVAL next = VTABLE_get_integer(INTERP, SUPER(key));
  +        PMC *res = pmc_new(INTERP, PyBuiltin_PyInt);
  +        VTABLE_set_integer_native(INTERP, res, next);
  +        return res;
  +    }
  +
  +
  +/*
  +
   =item C<void* invoke(void*)>
   
   Invoke is not supported by instances.
  
  
  
  1.12      +2 -2      parrot/languages/python/README
  
  Index: README
  ===================================================================
  RCS file: /cvs/public/parrot/languages/python/README,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- README    23 Dec 2004 23:49:34 -0000      1.11
  +++ README    29 Dec 2004 14:12:20 -0000      1.12
  @@ -22,9 +22,9 @@
     Multiple inheritance (Parrot)
     MMD
     Bound vs Unbound methods
  -  Dictionaries with non-string keys
  +  "Const"-ness (particularly for dictionaries)
     isinstance VTABLE entry?
  -  DOES
  +  DOES for dynclasses
     bounds checking
   
   - Sam Ruby
  
  
  

Reply via email to