backports 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.irbrc +1 -0
- data/README.rdoc +55 -3
- data/Rakefile +1 -0
- data/VERSION.yml +1 -1
- data/backports.gemspec +99 -118
- data/lib/backports/1.8.7/string.rb +1 -1
- data/lib/backports/1.9.1/array.rb +1 -2
- data/lib/backports/1.9.1/file.rb +20 -0
- data/lib/backports/1.9.1/float.rb +19 -0
- data/lib/backports/1.9.1/hash.rb +20 -3
- data/lib/backports/1.9.1/integer.rb +19 -0
- data/lib/backports/1.9.1/io.rb +18 -3
- data/lib/backports/1.9.1/numeric.rb +9 -0
- data/lib/backports/1.9.1/regexp.rb +1 -6
- data/lib/backports/1.9.1/stdlib/prime.rb +495 -0
- data/lib/backports/1.9.1/stdlib.rb +1 -0
- data/lib/backports/1.9.1/string.rb +2 -7
- data/lib/backports/1.9.2/array.rb +3 -4
- data/lib/backports/1.9.2/complex.rb +6 -0
- data/lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb +886 -0
- data/lib/backports/1.9.2/stdlib/matrix/lup_decomposition.rb +218 -0
- data/lib/backports/1.9.2/stdlib/matrix.rb +1872 -0
- data/lib/backports/1.9.2/stdlib/set.rb +13 -0
- data/lib/backports/1.9.2/stdlib.rb +1 -0
- data/lib/backports/1.9.3/io.rb +12 -0
- data/lib/backports/1.9.3.rb +5 -0
- data/lib/backports/1.9.rb +1 -1
- data/lib/backports/basic_object.rb +3 -2
- data/lib/backports/force/array_map.rb +1 -0
- data/lib/backports/force/enumerable_map.rb +3 -0
- data/lib/backports/force/hash_select.rb +9 -0
- data/lib/backports/force/string_length.rb +10 -0
- data/lib/backports/force/string_size.rb +1 -0
- data/lib/backports/tools.rb +137 -1
- data/test/README +13 -0
- metadata +25 -42
- data/.gitignore +0 -7
- data/test/_README +0 -1
- data/test/array_test.rb +0 -82
- data/test/basic_object_test.rb +0 -70
- data/test/binding_test.rb +0 -20
- data/test/enumerable_test.rb +0 -244
- data/test/enumerator_test.rb +0 -45
- data/test/hash_test.rb +0 -26
- data/test/kernel_test.rb +0 -31
- data/test/math_test.rb +0 -59
- data/test/method_missing_test.rb +0 -37
- data/test/method_test.rb +0 -73
- data/test/module_test.rb +0 -20
- data/test/object_test.rb +0 -35
- data/test/proc_test.rb +0 -116
- data/test/regexp_test.rb +0 -14
- data/test/string_test.rb +0 -74
- data/test/symbol_test.rb +0 -23
- data/test/test_helper.rb +0 -8
@@ -0,0 +1,495 @@
|
|
1
|
+
#
|
2
|
+
# = prime.rb
|
3
|
+
#
|
4
|
+
# Prime numbers and factorization library.
|
5
|
+
#
|
6
|
+
# Copyright::
|
7
|
+
# Copyright (c) 1998-2008 Keiju ISHITSUKA(SHL Japan Inc.)
|
8
|
+
# Copyright (c) 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
|
9
|
+
#
|
10
|
+
# Documentation::
|
11
|
+
# Yuki Sonoda
|
12
|
+
#
|
13
|
+
|
14
|
+
require "singleton"
|
15
|
+
require "forwardable"
|
16
|
+
|
17
|
+
class Integer
|
18
|
+
# Re-composes a prime factorization and returns the product.
|
19
|
+
#
|
20
|
+
# See Prime#int_from_prime_division for more details.
|
21
|
+
def Integer.from_prime_division(pd)
|
22
|
+
Prime.int_from_prime_division(pd)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the factorization of +self+.
|
26
|
+
#
|
27
|
+
# See Prime#prime_division for more details.
|
28
|
+
def prime_division(generator = Prime::Generator23.new)
|
29
|
+
Prime.prime_division(self, generator)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns true if +self+ is a prime number, false for a composite.
|
33
|
+
def prime?
|
34
|
+
Prime.prime?(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Iterates the given block over all prime numbers.
|
38
|
+
#
|
39
|
+
# See +Prime+#each for more details.
|
40
|
+
def Integer.each_prime(ubound, &block) # :yields: prime
|
41
|
+
Prime.each(ubound, &block)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# The set of all prime numbers.
|
47
|
+
#
|
48
|
+
# == Example
|
49
|
+
# Prime.each(100) do |prime|
|
50
|
+
# p prime #=> 2, 3, 5, 7, 11, ...., 97
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# == Retrieving the instance
|
54
|
+
# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can
|
55
|
+
# access it as +Prime+.instance.
|
56
|
+
#
|
57
|
+
# For convenience, each instance method of +Prime+.instance can be accessed
|
58
|
+
# as a class method of +Prime+.
|
59
|
+
#
|
60
|
+
# e.g.
|
61
|
+
# Prime.instance.prime?(2) #=> true
|
62
|
+
# Prime.prime?(2) #=> true
|
63
|
+
#
|
64
|
+
# == Generators
|
65
|
+
# A "generator" provides an implementation of enumerating pseudo-prime
|
66
|
+
# numbers and it remembers the position of enumeration and upper bound.
|
67
|
+
# Futhermore, it is a external iterator of prime enumeration which is
|
68
|
+
# compatible to an Enumerator.
|
69
|
+
#
|
70
|
+
# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
|
71
|
+
# There are few implementations of generator.
|
72
|
+
#
|
73
|
+
# [+Prime+::+EratosthenesGenerator+]
|
74
|
+
# Uses eratosthenes's sieve.
|
75
|
+
# [+Prime+::+TrialDivisionGenerator+]
|
76
|
+
# Uses the trial division method.
|
77
|
+
# [+Prime+::+Generator23+]
|
78
|
+
# Generates all positive integers which is not divided by 2 nor 3.
|
79
|
+
# This sequence is very bad as a pseudo-prime sequence. But this
|
80
|
+
# is faster and uses much less memory than other generators. So,
|
81
|
+
# it is suitable for factorizing an integer which is not large but
|
82
|
+
# has many prime factors. e.g. for Prime#prime? .
|
83
|
+
class Prime
|
84
|
+
include Enumerable
|
85
|
+
@the_instance = Prime.new
|
86
|
+
|
87
|
+
# obsolete. Use +Prime+::+instance+ or class methods of +Prime+.
|
88
|
+
def initialize
|
89
|
+
@generator = EratosthenesGenerator.new
|
90
|
+
extend OldCompatibility
|
91
|
+
warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."
|
92
|
+
end
|
93
|
+
|
94
|
+
class << self
|
95
|
+
extend Forwardable
|
96
|
+
include Enumerable
|
97
|
+
# Returns the default instance of Prime.
|
98
|
+
def instance; @the_instance end
|
99
|
+
|
100
|
+
def method_added(method) # :nodoc:
|
101
|
+
(class<< self;self;end).def_delegator :instance, method
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Iterates the given block over all prime numbers.
|
106
|
+
#
|
107
|
+
# == Parameters
|
108
|
+
# +ubound+::
|
109
|
+
# Optional. An arbitrary positive number.
|
110
|
+
# The upper bound of enumeration. The method enumerates
|
111
|
+
# prime numbers infinitely if +ubound+ is nil.
|
112
|
+
# +generator+::
|
113
|
+
# Optional. An implementation of pseudo-prime generator.
|
114
|
+
#
|
115
|
+
# == Return value
|
116
|
+
# An evaluated value of the given block at the last time.
|
117
|
+
# Or an enumerator which is compatible to an +Enumerator+
|
118
|
+
# if no block given.
|
119
|
+
#
|
120
|
+
# == Description
|
121
|
+
# Calls +block+ once for each prime number, passing the prime as
|
122
|
+
# a parameter.
|
123
|
+
#
|
124
|
+
# +ubound+::
|
125
|
+
# Upper bound of prime numbers. The iterator stops after
|
126
|
+
# yields all prime numbers p <= +ubound+.
|
127
|
+
#
|
128
|
+
# == Note
|
129
|
+
# +Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+
|
130
|
+
# in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
|
131
|
+
# by +Prime+::+OldCompatibility+#+each+.
|
132
|
+
#
|
133
|
+
# +Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply
|
134
|
+
# +Prime+.+each+.
|
135
|
+
def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
|
136
|
+
generator.upper_bound = ubound
|
137
|
+
generator.each(&block)
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
# Returns true if +value+ is prime, false for a composite.
|
142
|
+
#
|
143
|
+
# == Parameters
|
144
|
+
# +value+:: an arbitrary integer to be checked.
|
145
|
+
# +generator+:: optional. A pseudo-prime generator.
|
146
|
+
def prime?(value, generator = Prime::Generator23.new)
|
147
|
+
value = -value if value < 0
|
148
|
+
return false if value < 2
|
149
|
+
for num in generator
|
150
|
+
q,r = value.divmod num
|
151
|
+
return true if q < num
|
152
|
+
return false if r == 0
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# Re-composes a prime factorization and returns the product.
|
157
|
+
#
|
158
|
+
# == Parameters
|
159
|
+
# +pd+:: Array of pairs of integers. The each internal
|
160
|
+
# pair consists of a prime number -- a prime factor --
|
161
|
+
# and a natural number -- an exponent.
|
162
|
+
#
|
163
|
+
# == Example
|
164
|
+
# For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns
|
165
|
+
# p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
|
166
|
+
#
|
167
|
+
# Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
|
168
|
+
def int_from_prime_division(pd)
|
169
|
+
pd.inject(1){|value, (prime, index)|
|
170
|
+
value *= prime**index
|
171
|
+
}
|
172
|
+
end
|
173
|
+
|
174
|
+
# Returns the factorization of +value+.
|
175
|
+
#
|
176
|
+
# == Parameters
|
177
|
+
# +value+:: An arbitrary integer.
|
178
|
+
# +generator+:: Optional. A pseudo-prime generator.
|
179
|
+
# +generator+.succ must return the next
|
180
|
+
# pseudo-prime number in the ascendent
|
181
|
+
# order. It must generate all prime numbers,
|
182
|
+
# but may generate non prime numbers.
|
183
|
+
#
|
184
|
+
# === Exceptions
|
185
|
+
# +ZeroDivisionError+:: when +value+ is zero.
|
186
|
+
#
|
187
|
+
# == Example
|
188
|
+
# For an arbitrary integer
|
189
|
+
# n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
|
190
|
+
# prime_division(n) returns
|
191
|
+
# [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].
|
192
|
+
#
|
193
|
+
# Prime.prime_division(12) #=> [[2,2], [3,1]]
|
194
|
+
#
|
195
|
+
def prime_division(value, generator= Prime::Generator23.new)
|
196
|
+
raise ZeroDivisionError if value == 0
|
197
|
+
if value < 0
|
198
|
+
value = -value
|
199
|
+
pv = [[-1, 1]]
|
200
|
+
else
|
201
|
+
pv = []
|
202
|
+
end
|
203
|
+
for prime in generator
|
204
|
+
count = 0
|
205
|
+
while (value1, mod = value.divmod(prime)
|
206
|
+
mod) == 0
|
207
|
+
value = value1
|
208
|
+
count += 1
|
209
|
+
end
|
210
|
+
if count != 0
|
211
|
+
pv.push [prime, count]
|
212
|
+
end
|
213
|
+
break if value1 <= prime
|
214
|
+
end
|
215
|
+
if value > 1
|
216
|
+
pv.push [value, 1]
|
217
|
+
end
|
218
|
+
return pv
|
219
|
+
end
|
220
|
+
|
221
|
+
# An abstract class for enumerating pseudo-prime numbers.
|
222
|
+
#
|
223
|
+
# Concrete subclasses should override succ, next, rewind.
|
224
|
+
class PseudoPrimeGenerator
|
225
|
+
include Enumerable
|
226
|
+
|
227
|
+
def initialize(ubound = nil)
|
228
|
+
@ubound = ubound
|
229
|
+
end
|
230
|
+
|
231
|
+
def upper_bound=(ubound)
|
232
|
+
@ubound = ubound
|
233
|
+
end
|
234
|
+
def upper_bound
|
235
|
+
@ubound
|
236
|
+
end
|
237
|
+
|
238
|
+
# returns the next pseudo-prime number, and move the internal
|
239
|
+
# position forward.
|
240
|
+
#
|
241
|
+
# +PseudoPrimeGenerator+#succ raises +NotImplementedError+.
|
242
|
+
def succ
|
243
|
+
raise NotImplementedError, "need to define `succ'"
|
244
|
+
end
|
245
|
+
|
246
|
+
# alias of +succ+.
|
247
|
+
def next
|
248
|
+
raise NotImplementedError, "need to define `next'"
|
249
|
+
end
|
250
|
+
|
251
|
+
# Rewinds the internal position for enumeration.
|
252
|
+
#
|
253
|
+
# See +Enumerator+#rewind.
|
254
|
+
def rewind
|
255
|
+
raise NotImplementedError, "need to define `rewind'"
|
256
|
+
end
|
257
|
+
|
258
|
+
# Iterates the given block for each prime numbers.
|
259
|
+
def each(&block)
|
260
|
+
return self.dup unless block
|
261
|
+
if @ubound
|
262
|
+
last_value = nil
|
263
|
+
loop do
|
264
|
+
prime = succ
|
265
|
+
break last_value if prime > @ubound
|
266
|
+
last_value = block.call(prime)
|
267
|
+
end
|
268
|
+
else
|
269
|
+
loop do
|
270
|
+
block.call(succ)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
# see +Enumerator+#with_index.
|
276
|
+
alias with_index each_with_index
|
277
|
+
|
278
|
+
# see +Enumerator+#with_object.
|
279
|
+
def with_object(obj)
|
280
|
+
return enum_for(:with_object) unless block_given?
|
281
|
+
each do |prime|
|
282
|
+
yield prime, obj
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
# An implementation of +PseudoPrimeGenerator+.
|
288
|
+
#
|
289
|
+
# Uses +EratosthenesSieve+.
|
290
|
+
class EratosthenesGenerator < PseudoPrimeGenerator
|
291
|
+
def initialize
|
292
|
+
@last_prime = nil
|
293
|
+
super
|
294
|
+
end
|
295
|
+
|
296
|
+
def succ
|
297
|
+
@last_prime = @last_prime ? EratosthenesSieve.instance.next_to(@last_prime) : 2
|
298
|
+
end
|
299
|
+
def rewind
|
300
|
+
initialize
|
301
|
+
end
|
302
|
+
alias next succ
|
303
|
+
end
|
304
|
+
|
305
|
+
# An implementation of +PseudoPrimeGenerator+ which uses
|
306
|
+
# a prime table generated by trial division.
|
307
|
+
class TrialDivisionGenerator<PseudoPrimeGenerator
|
308
|
+
def initialize
|
309
|
+
@index = -1
|
310
|
+
super
|
311
|
+
end
|
312
|
+
|
313
|
+
def succ
|
314
|
+
TrialDivision.instance[@index += 1]
|
315
|
+
end
|
316
|
+
def rewind
|
317
|
+
initialize
|
318
|
+
end
|
319
|
+
alias next succ
|
320
|
+
end
|
321
|
+
|
322
|
+
# Generates all integer which are greater than 2 and
|
323
|
+
# are not divided by 2 nor 3.
|
324
|
+
#
|
325
|
+
# This is a pseudo-prime generator, suitable on
|
326
|
+
# checking primality of a integer by brute force
|
327
|
+
# method.
|
328
|
+
class Generator23<PseudoPrimeGenerator
|
329
|
+
def initialize
|
330
|
+
@prime = 1
|
331
|
+
@step = nil
|
332
|
+
super
|
333
|
+
end
|
334
|
+
|
335
|
+
def succ
|
336
|
+
loop do
|
337
|
+
if (@step)
|
338
|
+
@prime += @step
|
339
|
+
@step = 6 - @step
|
340
|
+
else
|
341
|
+
case @prime
|
342
|
+
when 1; @prime = 2
|
343
|
+
when 2; @prime = 3
|
344
|
+
when 3; @prime = 5; @step = 2
|
345
|
+
end
|
346
|
+
end
|
347
|
+
return @prime
|
348
|
+
end
|
349
|
+
end
|
350
|
+
alias next succ
|
351
|
+
def rewind
|
352
|
+
initialize
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
# Internal use. An implementation of prime table by trial division method.
|
360
|
+
class TrialDivision
|
361
|
+
include Singleton
|
362
|
+
|
363
|
+
def initialize # :nodoc:
|
364
|
+
# These are included as class variables to cache them for later uses. If memory
|
365
|
+
# usage is a problem, they can be put in Prime#initialize as instance variables.
|
366
|
+
|
367
|
+
# There must be no primes between @primes[-1] and @next_to_check.
|
368
|
+
@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
|
369
|
+
# @next_to_check % 6 must be 1.
|
370
|
+
@next_to_check = 103 # @primes[-1] - @primes[-1] % 6 + 7
|
371
|
+
@ulticheck_index = 3 # @primes.index(@primes.reverse.find {|n|
|
372
|
+
# n < Math.sqrt(@@next_to_check) })
|
373
|
+
@ulticheck_next_squared = 121 # @primes[@ulticheck_index + 1] ** 2
|
374
|
+
end
|
375
|
+
|
376
|
+
# Returns the cached prime numbers.
|
377
|
+
def cache
|
378
|
+
return @primes
|
379
|
+
end
|
380
|
+
alias primes cache
|
381
|
+
alias primes_so_far cache
|
382
|
+
|
383
|
+
# Returns the +index+th prime number.
|
384
|
+
#
|
385
|
+
# +index+ is a 0-based index.
|
386
|
+
def [](index)
|
387
|
+
while index >= @primes.length
|
388
|
+
# Only check for prime factors up to the square root of the potential primes,
|
389
|
+
# but without the performance hit of an actual square root calculation.
|
390
|
+
if @next_to_check + 4 > @ulticheck_next_squared
|
391
|
+
@ulticheck_index += 1
|
392
|
+
@ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2
|
393
|
+
end
|
394
|
+
# Only check numbers congruent to one and five, modulo six. All others
|
395
|
+
|
396
|
+
# are divisible by two or three. This also allows us to skip checking against
|
397
|
+
# two and three.
|
398
|
+
@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
|
399
|
+
@next_to_check += 4
|
400
|
+
@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
|
401
|
+
@next_to_check += 2
|
402
|
+
end
|
403
|
+
return @primes[index]
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
# Internal use. An implementation of eratosthenes's sieve
|
408
|
+
class EratosthenesSieve
|
409
|
+
include Singleton
|
410
|
+
|
411
|
+
BITS_PER_ENTRY = 16 # each entry is a set of 16-bits in a Fixnum
|
412
|
+
NUMS_PER_ENTRY = BITS_PER_ENTRY * 2 # twiced because even numbers are omitted
|
413
|
+
ENTRIES_PER_TABLE = 8
|
414
|
+
NUMS_PER_TABLE = NUMS_PER_ENTRY * ENTRIES_PER_TABLE
|
415
|
+
FILLED_ENTRY = (1 << NUMS_PER_ENTRY) - 1
|
416
|
+
|
417
|
+
def initialize # :nodoc:
|
418
|
+
# bitmap for odd prime numbers less than 256.
|
419
|
+
# For an arbitrary odd number n, @tables[i][j][k] is
|
420
|
+
# * 1 if n is prime,
|
421
|
+
# * 0 if n is composite,
|
422
|
+
# where i,j,k = indices(n)
|
423
|
+
@tables = [[0xcb6e, 0x64b4, 0x129a, 0x816d, 0x4c32, 0x864a, 0x820d, 0x2196].freeze]
|
424
|
+
end
|
425
|
+
|
426
|
+
# returns the least odd prime number which is greater than +n+.
|
427
|
+
def next_to(n)
|
428
|
+
n = (n-1).div(2)*2+3 # the next odd number to given n
|
429
|
+
table_index, integer_index, bit_index = indices(n)
|
430
|
+
loop do
|
431
|
+
extend_table until @tables.length > table_index
|
432
|
+
for j in integer_index...ENTRIES_PER_TABLE
|
433
|
+
if !@tables[table_index][j].zero?
|
434
|
+
for k in bit_index...BITS_PER_ENTRY
|
435
|
+
return NUMS_PER_TABLE*table_index + NUMS_PER_ENTRY*j + 2*k+1 if !@tables[table_index][j][k].zero?
|
436
|
+
end
|
437
|
+
end
|
438
|
+
bit_index = 0
|
439
|
+
end
|
440
|
+
table_index += 1; integer_index = 0
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
private
|
445
|
+
# for an odd number +n+, returns (i, j, k) such that @tables[i][j][k] represents primarity of the number
|
446
|
+
def indices(n)
|
447
|
+
# binary digits of n: |0|1|2|3|4|5|6|7|8|9|10|11|....
|
448
|
+
# indices: |-| k | j | i
|
449
|
+
# because of NUMS_PER_ENTRY, NUMS_PER_TABLE
|
450
|
+
|
451
|
+
k = (n & 0b00011111) >> 1
|
452
|
+
j = (n & 0b11100000) >> 5
|
453
|
+
i = n >> 8
|
454
|
+
return i, j, k
|
455
|
+
end
|
456
|
+
|
457
|
+
def extend_table
|
458
|
+
lbound = NUMS_PER_TABLE * @tables.length
|
459
|
+
ubound = lbound + NUMS_PER_TABLE
|
460
|
+
new_table = [FILLED_ENTRY] * ENTRIES_PER_TABLE # which represents primarity in lbound...ubound
|
461
|
+
(3..Integer(Math.sqrt(ubound))).step(2) do |p|
|
462
|
+
i, j, k = indices(p)
|
463
|
+
next if @tables[i][j][k].zero?
|
464
|
+
|
465
|
+
start = (lbound.div(p)+1)*p # least multiple of p which is >= lbound
|
466
|
+
start += p if start.even?
|
467
|
+
(start...ubound).step(2*p) do |n|
|
468
|
+
_, j, k = indices(n)
|
469
|
+
new_table[j] &= FILLED_ENTRY^(1<<k)
|
470
|
+
end
|
471
|
+
end
|
472
|
+
@tables << new_table.freeze
|
473
|
+
end
|
474
|
+
end
|
475
|
+
|
476
|
+
# Provides a +Prime+ object with compatibility to Ruby 1.8 when instantiated via +Prime+.+new+.
|
477
|
+
module OldCompatibility
|
478
|
+
# Returns the next prime number and forwards internal pointer.
|
479
|
+
def succ
|
480
|
+
@generator.succ
|
481
|
+
end
|
482
|
+
alias next succ
|
483
|
+
|
484
|
+
# Overwrites Prime#each.
|
485
|
+
#
|
486
|
+
# Iterates the given block over all prime numbers. Note that enumeration starts from
|
487
|
+
# the current position of internal pointer, not rewound.
|
488
|
+
def each(&block)
|
489
|
+
return @generator.dup unless block_given?
|
490
|
+
loop do
|
491
|
+
yield succ
|
492
|
+
end
|
493
|
+
end
|
494
|
+
end
|
495
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Backports::StdLib.extend_relative
|
@@ -3,8 +3,7 @@ class String
|
|
3
3
|
class << self
|
4
4
|
# Standard in Ruby 1.8.8. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
5
5
|
def try_convert(x)
|
6
|
-
|
7
|
-
x.to_str
|
6
|
+
Backports.try_convert(x, String, :to_str)
|
8
7
|
end unless method_defined? :try_convert
|
9
8
|
end
|
10
9
|
|
@@ -27,11 +26,7 @@ class String
|
|
27
26
|
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
28
27
|
def codepoints
|
29
28
|
return to_enum(:codepoints) unless block_given?
|
30
|
-
|
31
|
-
utf8 = s.each_byte.to_a
|
32
|
-
utf8[0] &= 0xff >> utf8.size # clear high bits (1 to 4, depending of number of bytes used)
|
33
|
-
yield utf8.inject{|result, b| (result << 6) + (b & 0x3f) }
|
34
|
-
end
|
29
|
+
unpack("U*").each{|cp| yield cp}
|
35
30
|
end unless method_defined? :codepoints
|
36
31
|
|
37
32
|
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
@@ -65,13 +65,12 @@ class Array
|
|
65
65
|
end unless method_defined? :repeated_permutation
|
66
66
|
|
67
67
|
def rotate(n=1)
|
68
|
-
|
68
|
+
Array.new(self).rotate!(n)
|
69
69
|
end unless method_defined? :rotate
|
70
70
|
|
71
71
|
def rotate!(n=1)
|
72
|
-
|
73
|
-
n
|
74
|
-
concat(slice!(0, n))
|
72
|
+
n = Backports.coerce_to_int(n) % (empty? ? 1 : size)
|
73
|
+
concat(shift(n))
|
75
74
|
end unless method_defined? :rotate!
|
76
75
|
|
77
76
|
def select!
|