You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2014/08/03 17:47:12 UTC

[5/9] git commit: Implement LFReg#Clone

Implement LFReg#Clone


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/c1d89c3a
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/c1d89c3a
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/c1d89c3a

Branch: refs/heads/clone_class_registry
Commit: c1d89c3a3ab7c152da1165ae190ead024aafb9d5
Parents: fd30d70
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Aug 3 14:53:13 2014 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Aug 3 17:38:04 2014 +0200

----------------------------------------------------------------------
 runtime/core/Clownfish/LockFreeRegistry.c       | 23 ++++++++++++++++++++
 runtime/core/Clownfish/LockFreeRegistry.cfh     |  9 +++++---
 .../core/Clownfish/Test/TestLockFreeRegistry.c  |  9 +++++++-
 3 files changed, 37 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c1d89c3a/runtime/core/Clownfish/LockFreeRegistry.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/LockFreeRegistry.c b/runtime/core/Clownfish/LockFreeRegistry.c
index c95a791..fab4f54 100644
--- a/runtime/core/Clownfish/LockFreeRegistry.c
+++ b/runtime/core/Clownfish/LockFreeRegistry.c
@@ -106,6 +106,29 @@ LFReg_Fetch_IMP(LockFreeRegistry *self, Obj *key) {
     return NULL;
 }
 
+LockFreeRegistry*
+LFReg_Clone_IMP(LockFreeRegistry *self) {
+    size_t             capacity = self->capacity;
+    LFRegEntry       **entries  = (LFRegEntry**)self->entries;
+    LockFreeRegistry  *twin     = LFReg_new(capacity);
+
+    for (size_t i = 0; i < capacity; ++i) {
+        LFRegEntry *entry = entries[i];
+
+        while (entry) {
+            Obj *key   = Obj_Clone(entry->key);
+            Obj *value = Obj_Clone(entry->value);
+            LFReg_Register(twin, key, value);
+            DECREF(key);
+            DECREF(value);
+
+            entry = entry->next;
+        }
+    }
+
+    return twin;
+}
+
 void
 LFReg_Destroy_IMP(LockFreeRegistry *self) {
     LFRegEntry **entries = (LFRegEntry**)self->entries;

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c1d89c3a/runtime/core/Clownfish/LockFreeRegistry.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/LockFreeRegistry.cfh b/runtime/core/Clownfish/LockFreeRegistry.cfh
index bb41fbd..2061a23 100644
--- a/runtime/core/Clownfish/LockFreeRegistry.cfh
+++ b/runtime/core/Clownfish/LockFreeRegistry.cfh
@@ -29,17 +29,20 @@ class Clownfish::LockFreeRegistry nickname LFReg inherits Clownfish::Obj {
     inert LockFreeRegistry*
     init(LockFreeRegistry *self, size_t capacity);
 
-    public void
-    Destroy(LockFreeRegistry *self);
-
     bool
     Register(LockFreeRegistry *self, Obj *key, Obj *value);
 
     nullable Obj*
     Fetch(LockFreeRegistry *self, Obj *key);
 
+    public incremented LockFreeRegistry*
+    Clone(LockFreeRegistry *self);
+
     void*
     To_Host(LockFreeRegistry *self);
+
+    public void
+    Destroy(LockFreeRegistry *self);
 }
 
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c1d89c3a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c b/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
index 46d725e..fb69065 100644
--- a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
+++ b/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
@@ -66,6 +66,13 @@ test_all(TestBatchRunner *runner) {
     TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)baz) == NULL,
               "Fetch() non-existent key returns NULL");
 
+    LockFreeRegistry *twin = LFReg_Clone(registry);
+    Obj *twin_entry = LFReg_Fetch(twin, (Obj*)foo);
+    TEST_TRUE(runner, twin_entry != NULL, "Fetch() from clone");
+    TEST_TRUE(runner, Obj_Equals(twin_entry, (Obj*)foo),
+              "Fetch() from clone returns equal entry");
+    TEST_TRUE(runner, twin_entry != (Obj*)foo, "Clone() performs deep clone");
+
     DECREF(foo_dupe);
     DECREF(baz);
     DECREF(bar);
@@ -75,7 +82,7 @@ test_all(TestBatchRunner *runner) {
 
 void
 TestLFReg_Run_IMP(TestLockFreeRegistry *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 6);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 9);
     test_all(runner);
 }