Helper function to copy array elements

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

Branch: refs/heads/master
Commit: 5b9e31643c53c72b1fb8ddba32fed034102855aa
Parents: 5eb638e
Author: Nick Wellnhofer <[email protected]>
Authored: Tue Jul 14 12:58:03 2015 +0200
Committer: Nick Wellnhofer <[email protected]>
Committed: Tue Jul 14 13:54:13 2015 +0200

----------------------------------------------------------------------
 runtime/core/Clownfish/Vector.c | 41 ++++++++++++------------------------
 1 file changed, 14 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5b9e3164/runtime/core/Clownfish/Vector.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Vector.c b/runtime/core/Clownfish/Vector.c
index 1108ceb..4cdfa96 100644
--- a/runtime/core/Clownfish/Vector.c
+++ b/runtime/core/Clownfish/Vector.c
@@ -27,6 +27,9 @@
 #include "Clownfish/Util/SortUtils.h"
 
 static CFISH_INLINE void
+SI_copy_and_incref(Obj **dst, Obj **src, size_t num);
+
+static CFISH_INLINE void
 SI_add_grow_and_oversize(Vector *self, size_t addend1, size_t addend2);
 
 static void
@@ -76,13 +79,7 @@ Vector*
 Vec_Clone_IMP(Vector *self) {
     Vector *twin = Vec_new(self->size);
     twin->size = self->size;
-
-    // Copy and incref.
-    Obj **elems      = self->elems;
-    Obj **twin_elems = twin->elems;
-    for (size_t i = 0, max = self->size; i < max; i++) {
-        twin_elems[i] = INCREF(elems[i]);
-    }
+    SI_copy_and_incref(twin->elems, self->elems, self->size);
 
     return twin;
 }
@@ -97,14 +94,7 @@ Vec_Push_IMP(Vector *self, Obj *element) {
 void
 Vec_Push_All_IMP(Vector *self, Vector *other) {
     SI_add_grow_and_oversize(self, self->size, other->size);
-
-    // Copy and incref.
-    Obj **dest        = self->elems + self->size;
-    Obj **other_elems = other->elems;
-    for (size_t i = 0, max = other->size; i < max; i++) {
-        dest[i] = INCREF(other_elems[i]);
-    }
-
+    SI_copy_and_incref(self->elems + self->size, other->elems, other->size);
     self->size += other->size;
 }
 
@@ -144,13 +134,7 @@ Vec_Insert_All_IMP(Vector *self, size_t tick, Vector 
*other) {
                (tick - self->size) * sizeof(Obj*));
     }
 
-    // Copy and incref.
-    Obj **dest        = self->elems + tick;
-    Obj **other_elems = other->elems;
-    for (size_t i = 0, max = other->size; i < max; i++) {
-        dest[i] = INCREF(other_elems[i]);
-    }
-
+    SI_copy_and_incref(self->elems + tick, other->elems, other->size);
     self->size = tick + other->size;
 }
 
@@ -292,15 +276,18 @@ Vec_Slice_IMP(Vector *self, size_t offset, size_t length) 
{
     // Copy elements.
     Vector *slice = Vec_new(length);
     slice->size = length;
-    Obj **slice_elems = slice->elems;
-    Obj **my_elems    = self->elems;
-    for (size_t i = 0; i < length; i++) {
-        slice_elems[i] = INCREF(my_elems[offset + i]);
-    }
+    SI_copy_and_incref(slice->elems, self->elems + offset, length);
 
     return slice;
 }
 
+static CFISH_INLINE void
+SI_copy_and_incref(Obj **dst, Obj **src, size_t num) {
+    for (size_t i = 0; i < num; i++) {
+        dst[i] = INCREF(src[i]);
+    }
+}
+
 // Ensure that the vector's capacity is at least (addend1 + addend2).
 // If the vector must be grown, oversize the allocation.
 static CFISH_INLINE void

Reply via email to