lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r887612 - in /lucene/lucy/trunk: core/Lucy/Object/ core/Lucy/Test/Object/ core/Lucy/Util/ perl/lib/Lucy/ perl/lib/Lucy/Object/ perl/t/core/
Date Sun, 06 Dec 2009 00:14:14 GMT
Author: marvin
Date: Sun Dec  6 00:14:12 2009
New Revision: 887612

URL: http://svn.apache.org/viewvc?rev=887612&view=rev
Log:
Commit LUCY-82, adding I32Array, an array of 32-bit integers that performs
bounds checking.

Added:
    lucene/lucy/trunk/core/Lucy/Object/I32Array.bp   (with props)
    lucene/lucy/trunk/core/Lucy/Object/I32Array.c   (with props)
    lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.bp   (with props)
    lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.c   (with props)
    lucene/lucy/trunk/perl/lib/Lucy/Object/I32Array.pm   (with props)
    lucene/lucy/trunk/perl/t/core/036-i32_array.t   (with props)
Modified:
    lucene/lucy/trunk/core/Lucy/Object/BitVector.bp
    lucene/lucy/trunk/core/Lucy/Object/BitVector.c
    lucene/lucy/trunk/core/Lucy/Test/Object/TestBitVector.c
    lucene/lucy/trunk/core/Lucy/Util/ToolSet.h
    lucene/lucy/trunk/perl/lib/Lucy/Object/BitVector.pm
    lucene/lucy/trunk/perl/lib/Lucy/Test.pm

Modified: lucene/lucy/trunk/core/Lucy/Object/BitVector.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/BitVector.bp?rev=887612&r1=887611&r2=887612&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/BitVector.bp (original)
+++ lucene/lucy/trunk/core/Lucy/Object/BitVector.bp Sun Dec  6 00:14:12 2009
@@ -132,6 +132,11 @@
     public u32_t
     Count(BitVector *self);
 
+    /** Return an array where each element represents a set bit.
+     */
+    public incremented I32Array*
+    To_Array(BitVector *self);
+
     public void
     Destroy(BitVector *self);
 

Modified: lucene/lucy/trunk/core/Lucy/Object/BitVector.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/BitVector.c?rev=887612&r1=887611&r2=887612&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/BitVector.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/BitVector.c Sun Dec  6 00:14:12 2009
@@ -371,6 +371,39 @@
     return count;
 }
 
+I32Array*
+BitVec_to_array(BitVector *self)
+{
+    u32_t count             = BitVec_Count(self);
+    u32_t num_left          = count;
+    const u32_t capacity    = self->cap;
+    u32_t *const array      = (u32_t *const)(u32_t*)CALLOCATE(count, sizeof(u32_t));
+    const size_t byte_size  = (size_t)ceil(self->cap / 8.0);
+    u8_t *const bits        = self->bits;
+    u8_t *const limit       = bits + byte_size;
+    u32_t num               = 0;
+    u32_t i                 = 0;
+
+    while (num_left) {
+        u8_t *ptr = bits + (num >> 3);
+        while (ptr < limit && *ptr == 0) {
+            num += 8;
+            ptr++;
+        }
+        do {
+            if (BitVec_Get(self, num)) {
+                array[i++] = num;
+                if (--num_left == 0)
+                    break;
+            }
+            if (num >= capacity)
+                THROW(ERR, "Exceeded capacity: %u32 %u32", num, capacity);
+        } while (++num % 8);
+    }
+
+    return I32Arr_new_steal((i32_t*)array, count);
+}
+
 /* Copyright 2009 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");

Added: lucene/lucy/trunk/core/Lucy/Object/I32Array.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/I32Array.bp?rev=887612&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/I32Array.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Object/I32Array.bp Sun Dec  6 00:14:12 2009
@@ -0,0 +1,45 @@
+parcel Lucy;
+
+class Lucy::Object::I32Array cnick I32Arr extends Lucy::Object::Obj {
+    i32_t    *ints;
+    u32_t     size;
+
+    inert incremented I32Array*
+    new(i32_t *ints, u32_t size);
+
+    inert incremented I32Array*
+    new_steal(i32_t *ints, u32_t size);
+
+    inert I32Array*
+    init(I32Array *self, i32_t *ints, u32_t size);
+
+    /** Return the value at <code>tick</code>, or throw an error if
+     * <code>tick</code> is out of bounds.
+     */
+    i32_t
+    Get(I32Array *self, u32_t tick);
+
+    /** Accessor for 'size' member.
+     */
+    u32_t 
+    Get_Size(I32Array *self);
+
+    public void
+    Destroy(I32Array *self);
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Object/I32Array.bp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Object/I32Array.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/I32Array.c?rev=887612&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/I32Array.c (added)
+++ lucene/lucy/trunk/core/Lucy/Object/I32Array.c Sun Dec  6 00:14:12 2009
@@ -0,0 +1,63 @@
+#define C_LUCY_I32ARRAY
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Object/I32Array.h"
+
+I32Array*
+I32Arr_new(i32_t *ints, u32_t size) 
+{
+    I32Array *self = (I32Array*)VTable_Make_Obj(I32ARRAY);
+    i32_t *ints_copy = (i32_t*)MALLOCATE(size * sizeof(i32_t));
+    memcpy(ints_copy, ints, size * sizeof(i32_t));
+    return I32Arr_init(self, ints_copy, size);
+}
+
+I32Array*
+I32Arr_new_steal(i32_t *ints, u32_t size) 
+{
+    I32Array *self = (I32Array*)VTable_Make_Obj(I32ARRAY);
+    return I32Arr_init(self, ints, size);
+}
+
+I32Array*
+I32Arr_init(I32Array *self, i32_t *ints, u32_t size) 
+{
+    self->ints = ints;
+    self->size = size;
+    return self;
+}
+
+void
+I32Arr_destroy(I32Array *self)
+{
+    FREEMEM(self->ints);
+    SUPER_DESTROY(self, I32ARRAY);
+}
+
+i32_t 
+I32Arr_get(I32Array *self, u32_t tick)
+{
+    if (tick >= self->size) {
+        THROW(ERR, "Out of bounds: %u32 >= %u32", tick, self->size);
+    }
+    return self->ints[tick];
+}
+
+u32_t
+I32Arr_get_size(I32Array *self) { return self->size; }
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Object/I32Array.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/core/Lucy/Test/Object/TestBitVector.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Object/TestBitVector.c?rev=887612&r1=887611&r2=887612&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Object/TestBitVector.c (original)
+++ lucene/lucy/trunk/core/Lucy/Test/Object/TestBitVector.c Sun Dec  6 00:14:12 2009
@@ -2,6 +2,7 @@
 #include "Lucy/Util/ToolSet.h"
 
 #include "Lucy/Test.h"
+#include "Lucy/Test/TestUtils.h"
 #include "Lucy/Test/Object/TestBitVector.h"
 
 static void
@@ -374,6 +375,53 @@
     DECREF(evil_twin);
 }
 
+static int
+S_compare_u64s(void *context, const void *va, const void *vb)
+{
+    u64_t a = *(u64_t*)va;
+    u64_t b = *(u64_t*)vb;
+    UNUSED_VAR(context);
+    return a == b ? 0 : a < b ? -1 : 1;
+}
+
+static void
+test_To_Array(TestBatch *batch)
+{
+    u64_t     *source_ints = TestUtils_random_u64s(NULL, 20, 0, 200);
+    BitVector *bit_vec = BitVec_new(0);
+    I32Array  *array;
+    long       num_unique = 0;
+    long       i;
+
+    /* Unique the random ints. */
+    Sort_quicksort(source_ints, 20, sizeof(u64_t), 
+        S_compare_u64s, NULL);
+    for (i = 0; i < 19; i++) {
+        if (source_ints[i] != source_ints[i + 1]) {
+            source_ints[num_unique] = source_ints[i];
+            num_unique++;
+        }
+    }
+
+    /* Set bits. */
+    for (i = 0; i < num_unique; i++) {
+        BitVec_Set(bit_vec, (u32_t)source_ints[i]);
+    }
+
+    /* Create the array and compare it to the source. */
+    array = BitVec_To_Array(bit_vec);
+    for (i = 0; i < num_unique; i++) {
+        if (I32Arr_Get(array, i) != (i32_t)source_ints[i]) { break; }
+    }
+    ASSERT_INT_EQ(batch, i, num_unique, "To_Array (%ld == %ld)", i, 
+        num_unique);
+
+    DECREF(array);
+    DECREF(bit_vec);
+    FREEMEM(source_ints);
+}
+
+
 /* Valgrind only - detect off-by-one error. */
 static void
 test_off_by_one_error()
@@ -389,7 +437,7 @@
 void
 TestBitVector_run_tests()
 {
-    TestBatch   *batch     = Test_new_batch("TestInStream", 1028, NULL);
+    TestBatch   *batch     = Test_new_batch("TestInStream", 1029, NULL);
 
     PLAN(batch);
 
@@ -407,6 +455,7 @@
     test_Next_Set_Bit(batch);
     test_Clear_All(batch);
     test_Clone(batch);
+    test_To_Array(batch);
     test_off_by_one_error();
 
     batch->destroy(batch);

Added: lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.bp?rev=887612&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.bp Sun Dec  6 00:14:12 2009
@@ -0,0 +1,22 @@
+parcel Lucy;
+
+inert class Lucy::Test::Object::TestI32Array cnick TestI32Arr {
+    inert void
+    run_tests();
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.bp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.c?rev=887612&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.c (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.c Sun Dec  6 00:14:12 2009
@@ -0,0 +1,70 @@
+#define C_LUCY_TESTI32ARRAY
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Test.h"
+#include "Lucy/Test/Object/TestI32Array.h"
+
+static i32_t source_ints[] = { -1, 0, I32_MIN, I32_MAX, 1 };
+static size_t num_ints = sizeof(source_ints) / sizeof(i32_t);
+
+static void
+test_all(TestBatch *batch)
+{
+    I32Array *i32_array = I32Arr_new(source_ints, num_ints);
+    i32_t    *ints_copy = (i32_t*)malloc(num_ints * sizeof(i32_t));
+    I32Array *stolen    = I32Arr_new_steal(ints_copy, num_ints);
+    size_t    num_matched;
+
+    memcpy(ints_copy, source_ints, num_ints * sizeof(i32_t));
+
+    ASSERT_TRUE(batch, I32Arr_Get_Size(i32_array) == num_ints,
+        "Get_Size");
+    ASSERT_TRUE(batch, I32Arr_Get_Size(stolen) == num_ints,
+        "Get_Size for stolen");
+
+    for (num_matched = 0; num_matched < num_ints; num_matched++) {
+        if (source_ints[num_matched] != I32Arr_Get(i32_array, num_matched)) {
+            break; 
+        }
+    }
+    ASSERT_INT_EQ(batch, num_matched, num_ints, 
+        "Matched all source ints with Get()");
+
+    for (num_matched = 0; num_matched < num_ints; num_matched++) {
+        if (source_ints[num_matched] != I32Arr_Get(stolen, num_matched)) { 
+            break; 
+        }
+    }
+    ASSERT_INT_EQ(batch, num_matched, num_ints, 
+        "Matched all source ints in stolen I32Array with Get()");
+
+    DECREF(i32_array);
+    DECREF(stolen);
+}
+
+void
+TestI32Arr_run_tests()
+{
+    TestBatch *batch = Test_new_batch("TestI32Array", 4, NULL);
+
+    PLAN(batch);
+    test_all(batch);
+
+    batch->destroy(batch);
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Object/TestI32Array.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/core/Lucy/Util/ToolSet.h
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Util/ToolSet.h?rev=887612&r1=887611&r2=887612&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Util/ToolSet.h (original)
+++ lucene/lucy/trunk/core/Lucy/Util/ToolSet.h Sun Dec  6 00:14:12 2009
@@ -28,6 +28,7 @@
 #include "Lucy/Object/CharBuf.h"
 #include "Lucy/Object/Err.h"
 #include "Lucy/Object/Hash.h"
+#include "Lucy/Object/I32Array.h"
 #include "Lucy/Object/Num.h"
 #include "Lucy/Object/Undefined.h"
 #include "Lucy/Object/VArray.h"

Modified: lucene/lucy/trunk/perl/lib/Lucy/Object/BitVector.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Object/BitVector.pm?rev=887612&r1=887611&r2=887612&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Object/BitVector.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Object/BitVector.pm Sun Dec  6 00:14:12 2009
@@ -12,6 +12,7 @@
     $bit_vec->set($_) for ( 0, 2, 4, 6 );
     $other->set($_)   for ( 1, 3, 5, 7 );
     $bit_vec->or($other);
+    print "$_\n" for @{ $bit_vec->to_array };    # prints 0 through 7.
 END_SYNOPSIS
 my $constructor = <<'END_CONSTRUCTOR';
     my $bit_vec = Lucy::Object::BitVector->new( 
@@ -34,6 +35,7 @@
             Flip
             Flip_Block
             Next_Set_Bit
+            To_Array
             Grow
             Count
             Get_Capacity
@@ -55,6 +57,7 @@
                 flip
                 flip_block
                 next_set_bit
+                to_array
                 grow
                 count
                 )

Added: lucene/lucy/trunk/perl/lib/Lucy/Object/I32Array.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Object/I32Array.pm?rev=887612&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Object/I32Array.pm (added)
+++ lucene/lucy/trunk/perl/lib/Lucy/Object/I32Array.pm Sun Dec  6 00:14:12 2009
@@ -0,0 +1,94 @@
+use Lucy;
+
+1;
+
+__END__
+
+__BINDING__
+
+my $xs_code = <<'END_XS_CODE';
+MODULE = Lucy PACKAGE = Lucy::Object::I32Array
+
+SV*
+new(either_sv, ...) 
+    SV *either_sv;
+CODE:
+{
+    SV *ints_sv = NULL;
+    AV *ints_av = NULL;
+    lucy_I32Array *self = NULL;
+
+    XSBind_allot_params( &(ST(0)), 1, items, 
+        "Lucy::Object::I32Array::new_PARAMS",
+        &ints_sv, "ints", 4,
+        NULL);
+    if (XSBind_sv_defined(ints_sv) && SvROK(ints_sv)) {
+        ints_av = (AV*)SvRV(ints_sv);
+    }
+
+    if (ints_av && SvTYPE(ints_av) == SVt_PVAV) {
+        chy_i32_t size  = av_len(ints_av) + 1;
+        chy_i32_t *ints = (chy_i32_t*)LUCY_MALLOCATE(size * sizeof(chy_i32_t));
+        chy_i32_t i;
+
+        for (i = 0; i < size; i++) {
+            SV **const sv_ptr = av_fetch(ints_av, i, 0);
+            ints[i] = (sv_ptr && XSBind_sv_defined(*sv_ptr)) 
+                    ? SvIV(*sv_ptr) 
+                    : 0;
+        }
+        self = (lucy_I32Array*)XSBind_new_blank_obj(either_sv);
+        lucy_I32Arr_init(self, ints, size);
+    }
+    else {
+        THROW(LUCY_ERR, "Required param 'ints' isn't an arrayref");
+    }
+    
+    RETVAL = LUCY_OBJ_TO_SV_NOINC(self);
+}
+OUTPUT: RETVAL
+
+SV*
+to_arrayref(self)
+    lucy_I32Array *self;
+CODE:
+{
+    AV *out_av = newAV();
+    chy_u32_t i;
+    chy_u32_t size = Lucy_I32Arr_Get_Size(self);
+
+    av_extend(out_av, size);
+    for (i = 0; i < size; i++) {
+        chy_i32_t result = Lucy_I32Arr_Get(self, i);
+        SV* result_sv = result == -1 ? newSV(0) : newSViv(result);
+        av_push(out_av, result_sv);
+    }
+    RETVAL = newRV_noinc((SV*)out_av);
+}
+OUTPUT: RETVAL
+END_XS_CODE
+
+Boilerplater::Binding::Perl::Class->register(
+    parcel       => "Lucy",
+    class_name   => "Lucy::Object::I32Array",
+    xs_code      => $xs_code,
+    bind_methods => [qw( Get Get_Size )],
+);
+
+__COPYRIGHT__
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/perl/lib/Lucy/Object/I32Array.pm
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/perl/lib/Lucy/Test.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Test.pm?rev=887612&r1=887611&r2=887612&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Test.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Test.pm Sun Dec  6 00:14:12 2009
@@ -30,6 +30,9 @@
     else if (strEQ(package, "TestHash")) {
         lucy_TestHash_run_tests();
     }
+    else if (strEQ(package, "TestI32Array")) {
+        lucy_TestI32Arr_run_tests();
+    }
     else if (strEQ(package, "TestNum")) {
         lucy_TestNum_run_tests();
     }

Added: lucene/lucy/trunk/perl/t/core/036-i32_array.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/t/core/036-i32_array.t?rev=887612&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/t/core/036-i32_array.t (added)
+++ lucene/lucy/trunk/perl/t/core/036-i32_array.t Sun Dec  6 00:14:12 2009
@@ -0,0 +1,6 @@
+use strict;
+use warnings;
+
+use Lucy::Test;
+Lucy::Test::run_tests("TestI32Array");
+

Propchange: lucene/lucy/trunk/perl/t/core/036-i32_array.t
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message