Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/unobe/rakudo
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Mar 26, 2010
2 parents 4b7dbf3 + 3343b56 commit 26ba8bf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
27 changes: 0 additions & 27 deletions src/builtins/Array.pir
Expand Up @@ -29,33 +29,6 @@ Arrays are the mutable form of Lists.
.tailcall '&circumfix:<[ ]>'(values :flat)
.end


=item postcircumfix:<[ ]>(Int)

=cut

.sub 'postcircumfix:<[ ]>' :method :multi(_, ['Integer'])
.param int n
if n < 0 goto err_index
.local pmc values, elem
$I0 = n + 1
values = self.'!fill'($I0)
elem = values[n]
unless null elem goto have_elem
.local pmc key
key = box n
elem = new ['Proxy']
setattribute elem, '$!base', values
setattribute elem, '$!key', key
$P0 = get_hll_global ['Bool'], 'True'
setprop elem, 'rw', $P0
have_elem:
.return (elem)

err_index:
"&die"("Cannot use negative index on arrays")
.end

=back

=head2 Operators
Expand Down
35 changes: 35 additions & 0 deletions src/core/Array.pm
Expand Up @@ -4,6 +4,41 @@ augment class Array {
'[' ~ self.map({ $^a.perl }).join(', ') ~ ']';
}

our Bool multi method exists(Int *@indices) {
if !@indices.elems || (any(@indices) < 0 || any(@indices) > self.end) {
return False;
}
[?&] map { self.values[$^a] !~~ Proxy }, @indices;
}

our multi method postcircumfix:<[ ]> (Int $i) {
if $i < 0 { die "Cannot use negative index on arrays" }
#XXX: .exists calls postcircumfix<[ ]>, so can't perl6ify this for now...
return Q:PIR{
.local pmc self, i, values
self = find_lex 'self'
i = find_lex '$i'
$I0 = i
inc $I0
values = self.'!fill'($I0)
%r = values[i]
unless null %r goto have_elem
%r = new ['Proxy']
setattribute %r, '$!base', values
setattribute %r, '$!key', i
have_elem:
$P0 = get_hll_global ['Bool'], 'True'
setprop %r, 'rw', $P0
}
}

our multi method postcircumfix:<[ ]>(Block $b) {
return self.[$b.(self.elems)]
}

our multi method postcircumfix:<[ ]>(Whatever) {
return self.values
}

our method push(*@values) is export {
self!fill;
Expand Down
2 changes: 1 addition & 1 deletion t/spectest.data
Expand Up @@ -439,7 +439,7 @@ S32-array/create.t
S32-array/delete.t
S32-array/elems.t
S32-array/end.t
# S32-array/exists.t
S32-array/exists.t
S32-array/keys_values.t
S32-array/kv.t
# S32-array/pairs.t
Expand Down

0 comments on commit 26ba8bf

Please sign in to comment.