Skip to content

Commit

Permalink
moved Array.delete to setting
Browse files Browse the repository at this point in the history
It's not likely that 'undef' will work as intended for typed arrays but,
hey, all the old tests pass, plus two TODO'd ones.
  • Loading branch information
Carl Masak committed Oct 5, 2009
1 parent 1ca164a commit d91717d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 53 deletions.
55 changes: 2 additions & 53 deletions src/classes/Array.pir
Expand Up @@ -14,70 +14,19 @@ src/classes/Array.pir - Perl 6 Array class and related functions
arrayproto.'!MUTABLE'()

$P0 = get_hll_namespace ['Perl6Array']
'!EXPORT'('delete,exists,pop,push,shift,unshift', 'from'=>$P0, 'to_p6_multi'=>1)
'!EXPORT'('exists,pop,push,shift,unshift', 'from'=>$P0, 'to_p6_multi'=>1)
.end


=head2 Methods

=item delete

Remove items from an array.

=cut

.namespace ['Perl6Array']
.sub 'delete' :method :multi() :subid('array_delete')
.param pmc indices :slurpy
.local pmc result
result = new ['List']
null $P99

indices.'!flatten'()
indices_loop:
unless indices goto indices_end
$I0 = shift indices
$P0 = self[$I0]
push result, $P0
self[$I0] = $P99

shorten:
$I0 = self.'elems'()
dec $I0
shorten_loop:
if $I0 < 0 goto shorten_end
$P0 = self[$I0]
if null $P0 goto do_shorten
$I1 = $P0.'defined'()
if $I1 goto shorten_end
do_shorten:
delete self[$I0]
dec $I0
goto shorten_loop
shorten_end:
goto indices_loop

indices_end:
.return (result)
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_delete"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
signature."!add_param"("@indices", 1 :named("slurpy"))
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end


=item exists(indices :slurpy)

Return true if the elements at C<indices> have been assigned to.

=cut

.namespace ['Perl6Array']
.sub 'exists' :method :multi() :subid('array_exists')
.param pmc indices :slurpy
.local int test
Expand Down
20 changes: 20 additions & 0 deletions src/setting/Array.pm
@@ -1,6 +1,22 @@
# "is export" on Array does not work (it's Perl6Array internally)

class Array is also {
multi method delete(@array is rw: *@indices) {
my @result;
for @indices -> $index {
my $i = $index >= 0
?? $index
!! * + $index;
@result.push(do { my $workaround = @array[$i] });
undefine @array[$i];
if $index == (@array - 1) | -1 {
@array.pop;
}
}
@array.pop while @array && !defined @array[* - 1];
return @result;
}

multi method splice(@array is rw: $offset is copy = 0, $size? is copy, *@values) {
my @spliced;
my @deleted;
Expand All @@ -20,6 +36,10 @@ class Array is also {
}
}

multi delete(@array is rw, *@indices) {
@array.delete(|@indices);
}

multi splice(@array is rw, $offset?, $size?, *@values) {
@array.splice($offset,$size,@values);
}
Expand Down

0 comments on commit d91717d

Please sign in to comment.