Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a "reverse" method for the array types (except boolarray)

  • Loading branch information...
commit 47a64b4800122ccfa7e08cfa0bed74ff0c39ca7e 1 parent 1d8cf9f
@mlschroe mlschroe authored
View
27 src/pmc/fixedfloatarray.pmc
@@ -502,6 +502,33 @@ Returns the Parrot string representation C<key>
return str;
}
+/*
+
+=item C<METHOD reverse()>
+
+Reverse the contents of the array.
+
+=cut
+
+*/
+
+ METHOD reverse() {
+ INTVAL n;
+ GET_ATTR_size(INTERP, SELF, n);
+ if (n > 1) {
+ FLOATVAL val;
+ FLOATVAL *data;
+ INTVAL i;
+
+ GET_ATTR_float_array(INTERP, SELF, data);
+ for (i = 0; i <= --n; i++) {
+ val = data[i];
+ data[i] = data[n];
+ data[n] = val;
+ }
+ }
+ }
+
}
/*
View
27 src/pmc/fixedintegerarray.pmc
@@ -651,6 +651,33 @@ Sort the array and return self.
RETURN(PMC *SELF);
}
+/*
+
+=item C<METHOD reverse()>
+
+Reverse the contents of the array.
+
+=cut
+
+*/
+
+ METHOD reverse() {
+ INTVAL n;
+ GET_ATTR_size(INTERP, SELF, n);
+ if (n > 1) {
+ INTVAL val;
+ INTVAL *data;
+ INTVAL i;
+
+ GET_ATTR_int_array(INTERP, SELF, data);
+ for (i = 0; i <= --n; i++) {
+ val = data[i];
+ data[i] = data[n];
+ data[n] = val;
+ }
+ }
+ }
+
}
/*
View
26 src/pmc/fixedpmcarray.pmc
@@ -68,6 +68,32 @@ Sort this array, optionally using the provided cmp_func
RETURN(PMC *SELF);
}
+
+/*
+
+=item C<METHOD reverse()>
+
+Reverse the contents of the array.
+
+=cut
+
+*/
+ METHOD reverse() {
+ INTVAL n = SELF.elements();
+ if (n > 1) {
+ PMC *val;
+ PMC **data = PMC_array(SELF);
+ INTVAL i;
+
+ for (i = 0; i <= --n; i++) {
+ val = data[i];
+ data[i] = data[n];
+ data[n] = val;
+ }
+ }
+ }
+
+
/*
=back
View
28 src/pmc/fixedstringarray.pmc
@@ -626,6 +626,34 @@ Used to unarchive the string.
str_array[i] = VTABLE_shift_string(INTERP, info);
}
}
+
+/*
+
+=item C<METHOD reverse()>
+
+Reverse the contents of the array.
+
+=cut
+
+*/
+
+ METHOD reverse() {
+ UINTVAL n;
+ GET_ATTR_size(INTERP, SELF, n);
+ if (n > 1) {
+ STRING *val;
+ STRING **data;
+ UINTVAL i;
+
+ GET_ATTR_str_array(INTERP, SELF, data);
+ for (i = 0; i <= --n; i++) {
+ val = data[i];
+ data[i] = data[n];
+ data[n] = val;
+ }
+ }
+ }
+
}
/*
View
39 t/pmc/resizablefloatarray.t
@@ -16,7 +16,7 @@ out-of-bounds test. Checks INT and PMC keys.
=cut
-.const int TESTS = 55
+.const int TESTS = 63
.const num PRECISION = 1e-6
.sub 'test' :main
@@ -49,6 +49,7 @@ out-of-bounds test. Checks INT and PMC keys.
check_interface()
get_iter()
'clone'()
+ method_reverse()
.end
.sub 'creation'
@@ -458,6 +459,42 @@ out-of-bounds test. Checks INT and PMC keys.
nok(0, 'clone made an evil clone')
.end
+.sub method_reverse
+ .local pmc array
+ array = new ['ResizableFloatArray']
+ array."reverse"()
+ $I0 = elements array
+ is($I0, 0, "method_reverse - reverse of empty array")
+ push array, 3.
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "3", "method_reverse - reverse of array with one element")
+ push array, 1.
+ array."reverse"()
+ array."reverse"()
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "1", "method_reverse - reverse of array with two elements")
+ $S0 = array[1]
+ is($S0, "3", "method_reverse - reverse of array with two elements second element")
+ push array, 4.5
+ array."reverse"()
+ push array, 5.
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "5134.5", "method_reverse - four elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "4.5315", "method_reverse - four elements second reverse")
+ push array, 6.
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "65134.5", "method_reverse - five elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "4.53156", "method_reverse - five elements second reverse")
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100
View
39 t/pmc/resizableintegerarray.t
@@ -42,7 +42,7 @@ Coverage plan:
.sub main :main
.include 'test_more.pir'
- plan(47)
+ plan(55)
test_does_interfaces()
@@ -74,6 +74,7 @@ Coverage plan:
test_iterator()
test_clone()
test_freeze()
+ method_reverse()
.end
.sub test_does_interfaces
@@ -607,6 +608,42 @@ k0:
is( ria, th, 'freeze/thaw copy is equal to original' )
.end
+.sub method_reverse
+ .local pmc array
+ array = new ['ResizableIntegerArray']
+ array."reverse"()
+ $I0 = elements array
+ is($I0, 0, "method_reverse - reverse of empty array")
+ push array, 3
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "3", "method_reverse - reverse of array with one element")
+ push array, 1
+ array."reverse"()
+ array."reverse"()
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "1", "method_reverse - reverse of array with two elements")
+ $S0 = array[1]
+ is($S0, "3", "method_reverse - reverse of array with two elements second element")
+ push array, 4
+ array."reverse"()
+ push array, 5
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "5134", "method_reverse - four elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "4315", "method_reverse - four elements second reverse")
+ push array, 6
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "65134", "method_reverse - five elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "43156", "method_reverse - five elements second reverse")
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100
View
39 t/pmc/resizablepmcarray.t
@@ -22,7 +22,7 @@ out-of-bounds test. Checks INT and PMC keys.
.include 'fp_equality.pasm'
.include 'test_more.pir'
- plan(143)
+ plan(151)
init_tests()
resize_tests()
@@ -60,6 +60,7 @@ out-of-bounds test. Checks INT and PMC keys.
test_assign_from_another()
test_assign_self()
test_assign_non_array()
+ method_reverse()
.end
.sub init_negative
@@ -1178,6 +1179,42 @@ CODE
is(n,15,'assigning to ResizablePMCArray from another ResizablePMCArray')
.end
+.sub method_reverse
+ .local pmc array
+ array = new ['ResizablePMCArray']
+ array."reverse"()
+ $I0 = elements array
+ is($I0, 0, "method_reverse - reverse of empty array")
+ push array, 3
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "3", "method_reverse - reverse of array with one element")
+ push array, "1"
+ array."reverse"()
+ array."reverse"()
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "1", "method_reverse - reverse of array with two elements")
+ $S0 = array[1]
+ is($S0, "3", "method_reverse - reverse of array with two elements second element")
+ push array, 4
+ array."reverse"()
+ push array, 5
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "5134", "method_reverse - four elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "4315", "method_reverse - four elements second reverse")
+ push array, 6
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "65134", "method_reverse - five elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "43156", "method_reverse - five elements second reverse")
+.end
+
# don't forget to change the test plan
View
39 t/pmc/resizablestringarray.t
@@ -22,7 +22,7 @@ out-of-bounds test. Checks INT and PMC keys.
.include 'test_more.pir'
# set a test plan
- plan(263)
+ plan(271)
'size/resize'()
'clone'()
@@ -86,6 +86,7 @@ out-of-bounds test. Checks INT and PMC keys.
method_unshift_string()
method_unshift_integer()
method_unshift_float()
+ method_reverse()
.end
#
@@ -1818,6 +1819,42 @@ exception:
is($S0, "3.3", "method_unshift_float (shrink, grow) - value")
.end
+.sub method_reverse
+ .local pmc array
+ array = new ['ResizableStringArray']
+ array."reverse"()
+ $I0 = elements array
+ is($I0, 0, "method_reverse - reverse of empty array")
+ push array, "3"
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "3", "method_reverse - reverse of array with one element")
+ push array, "1"
+ array."reverse"()
+ array."reverse"()
+ array."reverse"()
+ $S0 = array[0]
+ is($S0, "1", "method_reverse - reverse of array with two elements")
+ $S0 = array[1]
+ is($S0, "3", "method_reverse - reverse of array with two elements second element")
+ push array, "4"
+ array."reverse"()
+ push array, "5"
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "5134", "method_reverse - four elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "4315", "method_reverse - four elements second reverse")
+ push array, "6"
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "65134", "method_reverse - five elements")
+ array."reverse"()
+ $S0 = join "", array
+ is($S0, "43156", "method_reverse - five elements second reverse")
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100
Please sign in to comment.
Something went wrong with that request. Please try again.