You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2012/06/30 06:54:44 UTC

[lucy-commits] svn commit: r1355633 - in /lucy/trunk/core/Lucy: Object/VArray.c Object/VArray.cfh Test/Object/TestVArray.c

Author: marvin
Date: Sat Jun 30 04:54:42 2012
New Revision: 1355633

URL: http://svn.apache.org/viewvc?rev=1355633&view=rev
Log:
Add Slice() method to VArray.

Modified:
    lucy/trunk/core/Lucy/Object/VArray.c
    lucy/trunk/core/Lucy/Object/VArray.cfh
    lucy/trunk/core/Lucy/Test/Object/TestVArray.c

Modified: lucy/trunk/core/Lucy/Object/VArray.c
URL: http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/VArray.c?rev=1355633&r1=1355632&r2=1355633&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Object/VArray.c (original)
+++ lucy/trunk/core/Lucy/Object/VArray.c Sat Jun 30 04:54:42 2012
@@ -343,4 +343,28 @@ VA_gather(VArray *self, Lucy_VA_Gather_T
     return gathered;
 }
 
+VArray*
+VA_slice(VArray *self, uint32_t offset, uint32_t length) {
+    // Adjust ranges if necessary.
+    if (offset >= self->size) {
+        offset = 0;
+        length = 0;
+    }
+    else if (length > UINT32_MAX - offset
+             || offset + length > self->size
+            ) {
+        length = self->size - offset;
+    }
+
+    // Copy elements.
+    VArray *slice = VA_new(length);
+    slice->size = length;
+    Obj **slice_elems = slice->elems;
+    Obj **my_elems    = self->elems;
+    for (uint32_t i = 0; i < length; i++) {
+        slice_elems[i] = INCREF(my_elems[offset + i]);
+    }
+
+    return slice;
+}
 

Modified: lucy/trunk/core/Lucy/Object/VArray.cfh
URL: http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/VArray.cfh?rev=1355633&r1=1355632&r2=1355633&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Object/VArray.cfh (original)
+++ lucy/trunk/core/Lucy/Object/VArray.cfh Sat Jun 30 04:54:42 2012
@@ -149,6 +149,16 @@ class Lucy::Object::VArray cnick VA inhe
     public incremented VArray*
     Gather(VArray *self, Lucy_VA_Gather_Test_t test, void *data);
 
+    /** Return a new array consisting of elements from a contiguous slice.  If
+     * the specified range is out of bounds, return an array with fewer
+     * elements -- potentially none.
+     *
+     * @param offset The index of the element to start at.
+     * @param length The maximum number of elements to slice.
+     */
+    public incremented VArray*
+    Slice(VArray *self, uint32_t offset, uint32_t length);
+
     public bool_t
     Equals(VArray *self, Obj *other);
 

Modified: lucy/trunk/core/Lucy/Test/Object/TestVArray.c
URL: http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Test/Object/TestVArray.c?rev=1355633&r1=1355632&r2=1355633&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Test/Object/TestVArray.c (original)
+++ lucy/trunk/core/Lucy/Test/Object/TestVArray.c Sat Jun 30 04:54:42 2012
@@ -216,6 +216,51 @@ test_Push_VArray(TestBatch *batch) {
 }
 
 static void
+test_Slice(TestBatch *batch) {
+    VArray *array = VA_new(0);
+    for (uint32_t i = 0; i < 10; i++) { VA_Push(array, (Obj*)CB_newf("%u32", i)); }
+    {
+        VArray *slice = VA_Slice(array, 0, 10);
+        TEST_TRUE(batch, VA_Equals(array, (Obj*)slice), "Slice entire array");
+        DECREF(slice);
+    }
+    {
+        VArray *slice = VA_Slice(array, 0, 11);
+        TEST_TRUE(batch, VA_Equals(array, (Obj*)slice),
+            "Exceed length");
+        DECREF(slice);
+    }
+    {
+        VArray *wanted = VA_new(0);
+        VA_Push(wanted, (Obj*)CB_newf("9"));
+        VArray *slice = VA_Slice(array, 9, 11);
+        TEST_TRUE(batch, VA_Equals(slice, (Obj*)wanted),
+            "Exceed length, start near end");
+        DECREF(slice);
+        DECREF(wanted);
+    }
+    {
+        VArray *slice = VA_Slice(array, 0, 0);
+        TEST_TRUE(batch, VA_Get_Size(slice) == 0, "empty slice");
+        DECREF(slice);
+    }
+    {
+        VArray *slice = VA_Slice(array, 20, 1);
+        TEST_TRUE(batch, VA_Get_Size(slice) ==  0, "exceed offset");
+        DECREF(slice);
+    }
+    {
+        VArray *wanted = VA_new(0);
+        VA_Push(wanted, (Obj*)CB_newf("9"));
+        VArray *slice = VA_Slice(array, 9, UINT32_MAX - 1);
+        TEST_TRUE(batch, VA_Get_Size(slice) == 1, "guard against overflow");
+        DECREF(slice);
+        DECREF(wanted);
+    }
+    DECREF(array);
+}
+
+static void
 test_Clone_and_Shallow_Copy(TestBatch *batch) {
     VArray *array = VA_new(0);
     VArray *twin;
@@ -271,7 +316,7 @@ test_serialization(TestBatch *batch) {
 
 void
 TestVArray_run_tests() {
-    TestBatch *batch = TestBatch_new(39);
+    TestBatch *batch = TestBatch_new(45);
 
     TestBatch_Plan(batch);
 
@@ -282,6 +327,7 @@ TestVArray_run_tests() {
     test_Resize(batch);
     test_Excise(batch);
     test_Push_VArray(batch);
+    test_Slice(batch);
     test_Clone_and_Shallow_Copy(batch);
     test_Dump_and_Load(batch);
     test_serialization(batch);