Replace VA_Unshift with VA_Insert
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/ea40da6d Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/ea40da6d Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/ea40da6d Branch: refs/heads/master Commit: ea40da6d91a558b316468d74d7855b3fbeca3e25 Parents: 5124e36 Author: Nick Wellnhofer <[email protected]> Authored: Sun Apr 26 12:25:59 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Sun Apr 26 19:39:58 2015 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Test/TestVArray.c | 22 +++++++++++----------- runtime/core/Clownfish/VArray.c | 11 ++++++++--- runtime/core/Clownfish/VArray.cfh | 4 ++-- 3 files changed, 21 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ea40da6d/runtime/core/Clownfish/Test/TestVArray.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestVArray.c b/runtime/core/Clownfish/Test/TestVArray.c index e6729dc..7b35549 100644 --- a/runtime/core/Clownfish/Test/TestVArray.c +++ b/runtime/core/Clownfish/Test/TestVArray.c @@ -140,7 +140,7 @@ test_Store_Fetch(TestBatchRunner *runner) { } static void -test_Push_Pop_Unshift(TestBatchRunner *runner) { +test_Push_Pop_Insert(TestBatchRunner *runner) { VArray *array = VA_new(0); String *elem; @@ -160,19 +160,19 @@ test_Push_Pop_Unshift(TestBatchRunner *runner) { TEST_INT_EQ(runner, VA_Get_Size(array), 2, "size after Pop"); DECREF(elem); - VA_Unshift(array, (Obj*)Str_newf("foo")); + VA_Insert(array, 0, (Obj*)Str_newf("foo")); elem = (String*)CERTIFY(VA_Fetch(array, 0), STRING); - TEST_TRUE(runner, Str_Equals_Utf8(elem, "foo", 3), "Unshift"); - TEST_INT_EQ(runner, VA_Get_Size(array), 3, "size after Shift"); + TEST_TRUE(runner, Str_Equals_Utf8(elem, "foo", 3), "Insert"); + TEST_INT_EQ(runner, VA_Get_Size(array), 3, "size after Insert"); for (int i = 0; i < 256; ++i) { VA_Push(array, (Obj*)Str_newf("flotsam")); } for (int i = 0; i < 512; ++i) { - VA_Unshift(array, (Obj*)Str_newf("jetsam")); + VA_Insert(array, i, (Obj*)Str_newf("jetsam")); } TEST_INT_EQ(runner, VA_Get_Size(array), 3 + 256 + 512, - "size after exercising Pop and Unshift"); + "size after exercising Push and Insert"); DECREF(array); } @@ -374,12 +374,12 @@ S_overflow_Push(void *context) { } static void -S_overflow_Unshift(void *context) { +S_overflow_Insert(void *context) { UNUSED_VAR(context); VArray *array = VA_new(0); array->cap = SIZE_MAX; array->size = array->cap; - VA_Unshift(array, (Obj*)CFISH_TRUE); + VA_Insert(array, 38911, (Obj*)CFISH_TRUE); } static void @@ -417,8 +417,8 @@ test_exceptions(TestBatchRunner *runner) { } S_test_exception(runner, S_overflow_Push, "Push throws on overflow"); - S_test_exception(runner, S_overflow_Unshift, - "Unshift throws on overflow"); + S_test_exception(runner, S_overflow_Insert, + "Insert throws on overflow"); S_test_exception(runner, S_overflow_Push_VArray, "Push_VArray throws on overflow"); S_test_exception(runner, S_overflow_Store, @@ -547,7 +547,7 @@ TestVArray_Run_IMP(TestVArray *self, TestBatchRunner *runner) { TestBatchRunner_Plan(runner, (TestBatch*)self, 63); test_Equals(runner); test_Store_Fetch(runner); - test_Push_Pop_Unshift(runner); + test_Push_Pop_Insert(runner); test_Delete(runner); test_Resize(runner); test_Excise(runner); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ea40da6d/runtime/core/Clownfish/VArray.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/VArray.c b/runtime/core/Clownfish/VArray.c index 312811c..6b7ed41 100644 --- a/runtime/core/Clownfish/VArray.c +++ b/runtime/core/Clownfish/VArray.c @@ -128,12 +128,17 @@ VA_Pop_IMP(VArray *self) { } void -VA_Unshift_IMP(VArray *self, Obj *elem) { +VA_Insert_IMP(VArray *self, size_t tick, Obj *elem) { + if (tick >= self->size) { + VA_Store(self, tick, elem); + return; + } if (self->size == self->cap) { SI_grow_by(self, 1); } - memmove(self->elems + 1, self->elems, self->size * sizeof(Obj*)); - self->elems[0] = elem; + memmove(self->elems + tick + 1, self->elems + tick, + (self->size - tick) * sizeof(Obj*)); + self->elems[tick] = elem; self->size++; } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ea40da6d/runtime/core/Clownfish/VArray.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/VArray.cfh b/runtime/core/Clownfish/VArray.cfh index 8b8c991..140b536 100644 --- a/runtime/core/Clownfish/VArray.cfh +++ b/runtime/core/Clownfish/VArray.cfh @@ -60,10 +60,10 @@ class Clownfish::VArray nickname VA inherits Clownfish::Obj { incremented nullable Obj* Pop(VArray *self); - /** Unshift an item onto the front of a VArray. + /** Insert an element at `tick` moving the following elements. */ void - Unshift(VArray *self, decremented Obj *element = NULL); + Insert(VArray *self, size_t tick, decremented Obj *element = NULL); /** Ensure that the VArray has room for at least `capacity` * elements.
