Skip to content

Commit

Permalink
Implement Array#nitems(). Second try.
Browse files Browse the repository at this point in the history
Signed-off-by: Ted Reed <ted.reed@gmail.com>
  • Loading branch information
dtm authored and treed committed Aug 10, 2009
1 parent fa95b37 commit c445fca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -277,6 +277,7 @@ namespace :test do |ns|
test "array/intersection.t"
test "array/join.t"
test "array/mathop.t"
test "array/nitems.t"
test "array/pop.t"
test "array/push.t"
test "array/reject.t"
Expand All @@ -290,7 +291,7 @@ namespace :test do |ns|
test "array/values_at.t"
test "array/warray.t"

task :all => [:array, :assign, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :pop, :push, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
task :all => [:array, :assign, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :nitems, :pop, :push, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
end

namespace :file do
Expand Down
16 changes: 16 additions & 0 deletions src/classes/Array.pir
Expand Up @@ -1640,6 +1640,22 @@ The zip operator.
# TODO: pass warning
.end

.sub 'nitems' :method
.local int count
.local pmc it
count = 0
it = iter self
iter_loop:
unless it goto done
$P0 = shift it
$I0 = isa $P0, "NilClass"
if $I0 goto iter_loop
inc count
goto iter_loop
done:
.return ( count )
.end

.sub '_cmp' :vtable('cmp') :method
.param pmc other
.local int i, len, result
Expand Down
16 changes: 16 additions & 0 deletions t/array/nitems
@@ -0,0 +1,16 @@
require 'Test'
include Test
plan 4

a = [ ]
is a.nitems(), 0, 'nitems, no items'

a = [ 1, 2, 3, 4 ]
is a.nitems(), 4, 'nitems, 4 non-nil items'

a = [ 1, 2, 3, nil ]
is a.nitems(), 3, 'nitems, 3 non-nil items, 1 nil'

a = [ nil, nil, nil, nil ]
is a.nitems(), 0, 'nitems, 4 nil items'

0 comments on commit c445fca

Please sign in to comment.