Author: leo
Date: Thu Jan 12 08:08:51 2006
New Revision: 11123
Modified:
trunk/src/classes/orderedhash.pmc
trunk/t/pmc/orderedhash.t
Log:
OrderedHash - freeze/thaw
* implement freeze, inherit other freez/thaw stuff
* tests
Modified: trunk/src/classes/orderedhash.pmc
==============================================================================
--- trunk/src/classes/orderedhash.pmc (original)
+++ trunk/src/classes/orderedhash.pmc Thu Jan 12 08:08:51 2006
@@ -556,6 +556,57 @@ were deleted
}
return dest;
}
+/*
+
+=item C<void visit(visit_info *info)>
+
+Used during archiving to visit the elements in the hash.
+
+=item C<void freeze(visit_info *info)>
+
+Used to archive the hash.
+
+=item C<void thaw(visit_info *info)>
+
+Used to unarchive the hash.
+
+Freeze/thaw are inherited from hash, onyl thaw.visit is special, as
+we have to preserver key/value order.
+
+=cut
+
+*/
+
+ void visit(visit_info *info) {
+ UINTVAL i, n;
+ HashBucket *b;
+ void *key;
+ IMAGE_IO *io = info->image_io;
+ Hash *hash = (Hash *)PMC_struct_val(SELF);
+
+ info->container = SELF;
+ switch (info->what) {
+ case VISIT_THAW_NORMAL:
+ case VISIT_THAW_CONSTANTS:
+ SUPER(info);
+ break;
+ case VISIT_FREEZE_NORMAL:
+ case VISIT_FREEZE_AT_DESTRUCT:
+ for (i = 0; i <= hash->mask; i++) {
+ b = hash->bs + i;
+ key = b->key;
+ if (!key)
+ continue;
+ io->vtable->push_string(interpreter, io, key);
+ (info->visit_pmc_now)(interpreter, b->value, info);
+ }
+ break;
+ default:
+ internal_exception(1, "unhandled visit action (%d)",
+ info->what);
+ }
+ }
+
}
/*
Modified: trunk/t/pmc/orderedhash.t
==============================================================================
--- trunk/t/pmc/orderedhash.t (original)
+++ trunk/t/pmc/orderedhash.t Thu Jan 12 08:08:51 2006
@@ -6,7 +6,7 @@ use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
-use Parrot::Test tests => 25;
+use Parrot::Test tests => 27;
=head1 NAME
@@ -752,3 +752,60 @@ CODE
00
OUTPUT
+output_is(<<'CODE', <<'OUTPUT', "freeze/thaw 1");
+ new P0, .OrderedHash
+ set P0["a"], "Foo\n"
+ set P0["b"], "Bar\n"
+
+ freeze S0, P0
+ thaw P1, S0
+ set P2, P1["a"]
+ print P2
+ set P2, P1[0]
+ print P2
+ set P2, P1["b"]
+ print P2
+ set P2, P1[1]
+ print P2
+
+ end
+CODE
+Foo
+Foo
+Bar
+Bar
+OUTPUT
+
+output_is(<<'CODE', <<'OUTPUT', "freeze/thaw 2");
+ new P0, .OrderedHash
+ set P0["a"], "Foo\n"
+ new P1, .Hash
+ set P1['foo'], "bar\n"
+ set P0["b"], P1
+ set P0['b'; 'quux'], "xyzzy\n"
+
+ freeze S0, P0
+ thaw P1, S0
+ set P2, P1["a"]
+ print P2
+ set P2, P1[0]
+ print P2
+ set P2, P1["b";"foo"]
+ print P2
+ set P2, P1[1; "foo"]
+ print P2
+ set P2, P1["b";"quux"]
+ print P2
+ set P2, P1[1; "quux"]
+ print P2
+
+ end
+CODE
+Foo
+Foo
+bar
+bar
+xyzzy
+xyzzy
+OUTPUT
+