[svn:parrot] r40240 - branches/orderedhash_revamp/src/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Thu Jul 23 23:07:31 UTC 2009


Author: bacek
Date: Thu Jul 23 23:07:30 2009
New Revision: 40240
URL: https://trac.parrot.org/parrot/changeset/40240

Log:
[pmc] Implement restoring order after OrderedHash clone/thaw.

Modified:
   branches/orderedhash_revamp/src/pmc/orderedhash.pmc

Modified: branches/orderedhash_revamp/src/pmc/orderedhash.pmc
==============================================================================
--- branches/orderedhash_revamp/src/pmc/orderedhash.pmc	Thu Jul 23 23:06:42 2009	(r40239)
+++ branches/orderedhash_revamp/src/pmc/orderedhash.pmc	Thu Jul 23 23:07:30 2009	(r40240)
@@ -88,6 +88,25 @@
     return ret;
 };
 
+/* Find first/last in cloned/thawed OrderedHash */
+/* Parameter C<pmc_hash> is Hash, not OrderedHash */
+static void
+find_bounds(PARROT_INTERP, PMC *pmc_hash, PMC **first, PMC **last) {
+    PMC *iter = VTABLE_get_iter(interp, pmc_hash);
+    while (VTABLE_get_bool(interp, iter)) {
+        PMC *item  = VTABLE_shift_pmc(interp, iter);
+        PMC *entry = VTABLE_get_pmc_keyed(interp, pmc_hash, item);
+        /* First entry doesn't have prev */
+        PMC *tmp   = VTABLE_get_pmc_keyed_int(interp, entry, ORDERED_HASH_ITEM_PREV);
+        if (PMC_IS_NULL(tmp))
+            *first = entry;
+        /* Last entry doesn't have next */
+        tmp   = VTABLE_get_pmc_keyed_int(interp, entry, ORDERED_HASH_ITEM_NEXT);
+        if (PMC_IS_NULL(tmp))
+            *last = entry;
+    }
+}
+
 /* Helpers for boxing values */
 static PMC*
 box_string(PARROT_INTERP, STRING *str) {
@@ -141,10 +160,6 @@
         PObj_custom_mark_destroy_SETALL(SELF);
     }
 
-    VTABLE void init_pmc() {
-        PARROT_ASSERT(!"Bah");
-    }
-
 /*
 
 =item C<void mark()>
@@ -597,6 +612,11 @@
 
     VTABLE PMC *clone() {
         PMC  * const dest   = pmc_new(INTERP, SELF->vtable->base_type);
+        Parrot_OrderedHash_attributes *clone_attrs =
+                PARROT_ORDEREDHASH(dest);
+
+        clone_attrs->hash   = VTABLE_clone(INTERP, PARROT_ORDEREDHASH(SELF)->hash);
+        find_bounds(INTERP, clone_attrs->hash, &clone_attrs->first, &clone_attrs->last);
 
         return dest;
     }
@@ -614,9 +634,6 @@
 
 Used to unarchive the hash.
 
-Freeze/thaw are inherited from hash.  Only thaw.visit is special, as we have to
-preserve key/value order.
-
 =cut
 
 */
@@ -627,6 +644,13 @@
         (info->visit_pmc_now)(INTERP, *hash, info);
         SUPER(info);
     }
+
+    VTABLE void thaw(visit_info *info) {
+        Parrot_OrderedHash_attributes *attrs;
+        SUPER(info);
+        attrs = PARROT_ORDEREDHASH(SELF);
+        find_bounds(INTERP, attrs->hash, &attrs->first, &attrs->last);
+    }
 }
 
 /*


More information about the parrot-commits mailing list