From acd0b6a9ab5d9005fe92fcab77ae8023141ed3f8 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 17 May 2010 21:15:13 -0400 Subject: [PATCH] 1.9.2: Implement Array#repeated_combination, #repeated_permutation (v 1.17.0) --- CHANGELOG.rdoc | 8 +++++++- README.rdoc | 1 + VERSION.yml | 4 ++-- backports.gemspec | 4 ++-- lib/backports/1.9.2/array.rb | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index b6e30459..e129421f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,8 +1,14 @@ = Packable --- History +== Version 1.17.0 - May 17th, 2010 + +* Added some features of 1.9.2: + + * Array#repeated_combination, #repeated_permutation + == Version 1.16.2 - April 11th, 2010 -* Added a features of 1.9.2: +* Added some features of 1.9.2: * Array#uniq, #uniq! (with block) * Array#product (with block) diff --git a/README.rdoc b/README.rdoc index e1bdbbaf..f09946d4 100644 --- a/README.rdoc +++ b/README.rdoc @@ -110,6 +110,7 @@ but since it is only an imitation, it must be required explicitly: * rotate, rotate! * keep_if, select! * +product+ (with block) + * +repeated_combination+, +repeated_permutation+ * sort_by! * uniq, #uniq! (with block) diff --git a/VERSION.yml b/VERSION.yml index 182deb4c..e3279198 100644 --- a/VERSION.yml +++ b/VERSION.yml @@ -1,5 +1,5 @@ --- :major: 1 +:minor: 17 +:patch: 0 :build: -:minor: 16 -:patch: 7 diff --git a/backports.gemspec b/backports.gemspec index 88866eb0..da65d784 100644 --- a/backports.gemspec +++ b/backports.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = %q{backports} - s.version = "1.16.7" + s.version = "1.17.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Marc-Andr\303\251 Lafortune"] - s.date = %q{2010-05-03} + s.date = %q{2010-05-17} s.description = %q{ Essential backports that enable some of the really nice features of ruby 1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier. } s.email = %q{github@marc-andre.ca} diff --git a/lib/backports/1.9.2/array.rb b/lib/backports/1.9.2/array.rb index 38287d0b..b74cd01f 100644 --- a/lib/backports/1.9.2/array.rb +++ b/lib/backports/1.9.2/array.rb @@ -27,6 +27,43 @@ def product_with_block(*arg, &block) Backports.alias_method_chain self, :product, :block end + # Note: Combinations are not yielded in the same order as MRI. + # This is not a bug; the spec states that the order is implementation dependent + def repeated_combination(num, &block) + return to_enum :repeated_combination, num unless block_given? + num = Backports.coerce_to_int(num) + if num <= 0 + yield [] if num == 0 + else + indices = Array.new(num, 0) + indices[-1] = size + while dec = indices.find_index(&:nonzero?) + indices[0..dec] = Array.new dec+1, indices[dec]-1 + yield values_at(*indices) + end + end + self + end unless method_defined? :repeated_combination + + # Note: Permutations are not yielded in the same order as MRI. + # This is not a bug; the spec states that the order is implementation dependent + def repeated_permutation(num, &block) + return to_enum :repeated_permutation, num unless block_given? + num = Backports.coerce_to_int(num) + if num <= 0 + yield [] if num == 0 + else + indices = Array.new(num, 0) + indices[-1] = size + while dec = indices.find_index(&:nonzero?) + indices[0...dec] = Array.new dec, size-1 + indices[dec] -= 1 + yield values_at(*indices) + end + end + self + end unless method_defined? :repeated_permutation + def rotate(n=1) dup.rotate!(n) end unless method_defined? :rotate