ruby-changes:20230
From: marcandre <ko1@a...>
Date: Wed, 29 Jun 2011 10:09:51 +0900 (JST)
Subject: [ruby-changes:20230] marcandRe: r32278 (trunk): * lib/matrix.rb: Specialize Matrix#find_index to return [row, col]
marcandre 2011-06-29 10:09:46 +0900 (Wed, 29 Jun 2011) New Revision: 32278 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32278 Log: * lib/matrix.rb: Specialize Matrix#find_index to return [row, col] and accept the same optional argument as #each Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/matrix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 32277) +++ ChangeLog (revision 32278) @@ -1,3 +1,8 @@ +Wed Jun 29 10:09:35 2011 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb: Specialize Matrix#find_index to return [row, col] + and accept the same optional argument as #each + Wed Jun 29 10:07:32 2011 Marc-Andre Lafortune <ruby-core@m...> * lib/matrix.rb: Matrix#each{_with_index} can iterate over a subset Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 32277) +++ lib/matrix.rb (revision 32278) @@ -56,6 +56,7 @@ # * <tt> #map </tt> # * <tt> #each </tt> # * <tt> #each_with_index </tt> +# * <tt> #find_index </tt> # * <tt> #minor(*param) </tt> # # Properties of a matrix: @@ -490,7 +491,37 @@ self end + SELECTORS = {all: true, diagonal: true, off_diagonal: true, lower: true, strict_lower: true, strict_upper: true, upper: true}.freeze # + # :call-seq: + # index(value, selector = :all) -> [row, column] + # index(selector = :all){ block } -> [row, column] + # index(selector = :all) -> an_enumerator + # + # The index method is specialized to return the index as [row, column] + # It also accepts an optional +selector+ argument, see #each for details. + # + # Matrix[ [1,2], [3,4] ].index(&:even?) # => [0, 1] + # Matrix[ [1,1], [1,1] ].index(1, :strict_lower) # => [1, 0] + # + def index(*args) + raise ArgumentError, "wrong number of arguments(#{args.size} for 0-2)" if args.size > 2 + which = (args.size == 2 || SELECTORS.include?(args.last)) ? args.pop : :all + return to_enum :find_index, which, *args unless block_given? || args.size == 1 + if args.size == 1 + value = args.first + each_with_index(which) do |e, row_index, col_index| + return row_index, col_index if e == value + end + else + each_with_index(which) do |e, row_index, col_index| + return row_index, col_index if yield e + end + end + nil + end + alias_method :find_index, :index + # # Returns a section of the matrix. The parameters are either: # * start_row, nrows, start_col, ncols; OR # * row_range, col_range Index: NEWS =================================================================== --- NEWS (revision 32277) +++ NEWS (revision 32278) @@ -159,6 +159,8 @@ * matrix * extended methods: * Matrix#each and #each_with_index can iterate on a subset of the elements + * Matrix#find_index returns [row, column] and can iterate on a subset + of the elements * net/http * SNI (Server Name Indication) supported for HTTPS. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/