[svn:parrot] r39739 - branches/tt761_keys_revamp/src/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Tue Jun 23 11:14:34 UTC 2009


Author: bacek
Date: Tue Jun 23 11:14:33 2009
New Revision: 39739
URL: https://trac.parrot.org/parrot/changeset/39739

Log:
[pmc] Implement (get|set)_value_type in Hash.

Modified:
   branches/tt761_keys_revamp/src/pmc/hash.pmc

Modified: branches/tt761_keys_revamp/src/pmc/hash.pmc
==============================================================================
--- branches/tt761_keys_revamp/src/pmc/hash.pmc	Tue Jun 23 11:14:12 2009	(r39738)
+++ branches/tt761_keys_revamp/src/pmc/hash.pmc	Tue Jun 23 11:14:33 2009	(r39739)
@@ -458,7 +458,9 @@
 /*
 =item C<void set_integer(INTVAL type)>
 
-Reset Hash to use non-string keys. See enum C<Hash_key_type> for possible values.
+Reset Hash to use differen keys. See enum C<Hash_key_type> for possible values.
+
+NB: this method will destroy all old data!
 
 =cut
 */
@@ -494,6 +496,61 @@
     METHOD set_key_type(INTVAL type) {
         SELF.set_integer_native(type);
     }
+
+    METHOD get_key_type() {
+        INTVAL ret = ((Hash *)SELF.get_pointer())->key_type;
+        RETURN(INTVAL ret);
+    }
+
+/*
+
+=item C<METHOD set_value_type(INTVAL type)>
+
+Reset Hash to use different value-type for stored items. If there is no
+previous _hash was set defaults to STRING* keys.
+
+NB: this method will destroy all old data!
+
+=cut
+*/
+    METHOD set_value_type(INTVAL type) {
+        Hash *old_hash = (Hash *)SELF.get_pointer();
+        Hash *new_hash = 0;
+
+        /*
+        If someone called Hash.set_pointer with NULL pointer...
+        It will create STRING* keys hash. Because we can't use STRING_compare
+        directly - it declared static in F<src/hash.c>
+        */
+        if (!old_hash)
+            old_hash = parrot_new_hash(INTERP);
+
+        switch (type) {
+            case enum_type_INTVAL:
+            case enum_type_STRING:
+            case enum_type_PMC:
+                new_hash = parrot_create_hash(interp,
+                        type,
+                        old_hash->key_type,
+                        old_hash->compare,
+                        old_hash->hash_val);
+                break;
+            default:
+                Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
+                        "Hash: unsupported entry_type");
+        }
+
+        PARROT_HASH(SELF)->hash = new_hash;
+        new_hash->container     = SELF;
+
+        parrot_hash_destroy(INTERP, old_hash);
+    }
+
+    METHOD get_value_type() {
+        INTVAL ret = ((Hash *)SELF.get_pointer())->entry_type;
+        RETURN(INTVAL ret);
+    }
+
 /*
 
 =item C<void *get_pointer()>


More information about the parrot-commits mailing list