Skip to content

Commit

Permalink
Improve implementation of at, with tests. Simplify values_at by using…
Browse files Browse the repository at this point in the history
… at where appropriate

Signed-off-by: Ted Reed <ted.reed@gmail.com>
  • Loading branch information
dtm authored and treed committed Jul 27, 2009
1 parent 7171222 commit 2581153
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
36 changes: 15 additions & 21 deletions src/classes/Array.pir
Expand Up @@ -1166,8 +1166,20 @@ Retrieve the number of elements in C<self>

=cut
.sub 'at' :method
.param pmc i
$P0 = self[i]
.param int i

if i >= 0 goto do_access
# if the index is negative check its no out of bounds to avoid
# exception from underlying pmc - alternative is to catch the exception
$I1 = elements self
$I1 = i + $I1
if $I1 < 0 goto is_nil
do_access:
$P0 = self[ i ]
unless null $P0 goto skip
is_nil:
$P0 = get_hll_global 'nil'
skip:
.return($P0)
.end

Expand Down Expand Up @@ -1275,28 +1287,10 @@ The zip operator.
$I0 = isa item, 'CardinalRange'
if $I0 goto do_range

$I0=item

if $I0 < 0 goto negative

if $I0 >= length goto nil_item

valid:
val = self[$I0]
push_val:
val = self.'at'(item)
values.'push'(val)
goto loop_check

nil_item:
val = get_hll_global 'nil'
goto push_val

negative:
$I0=$I0+length
if $I0 < 0 goto nil_item
if $I0 >= length goto nil_item
goto valid

do_range:
.local int beg, end, count

Expand Down
20 changes: 15 additions & 5 deletions t/array/at.t
@@ -1,9 +1,19 @@
puts "1..4"
require 'Test'
include Test
plan 8

a = [ 1, 2 ]
puts "ok " + a[-2]
puts "ok " + a.at(-1)

is a.at(0), 1, 'at'
is a.at(1), 2, 'at'
is a.at(2), nil, 'at'
is a.at(-1), 2, 'at'
is a.at(-2), 1, 'at'
is a.at(-3), nil, 'at'

b = a.at(1) + 1
puts "ok " + b
is b, 3, 'at'

c = a.at(1) * 2
puts "ok " + c
is c, 4, 'at'

0 comments on commit 2581153

Please sign in to comment.