random-accessible 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/BSDL ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2011, Natsuki Kawai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -56,11 +56,7 @@ module RandomAccessible
56
56
  # See the docment of RandomReadable#collect.
57
57
  def collect!(&block)
58
58
  if block.nil?
59
- Enumerator.new do |y|
60
- size.times do |i|
61
- self[i] = y.yield(at(i))
62
- end
63
- end
59
+ return Enumerator.new(self, :collect!)
64
60
  else
65
61
  replace(collect(&block))
66
62
  end
@@ -140,16 +136,7 @@ module RandomAccessible
140
136
  # if the class provides no size-provider.
141
137
  def reject!(&block)
142
138
  if block.nil?
143
- return Enumerator.new do |y|
144
- deleted = 0
145
- size.times do |i|
146
- val = self[i - deleted]
147
- if y.yield(val)
148
- delete_at(i - deleted)
149
- deleted += 1
150
- end
151
- end
152
- end
139
+ return Enumerator.new(self, :reject!)
153
140
  else
154
141
  deleted = 0
155
142
  size.times do |i|
@@ -174,15 +161,7 @@ module RandomAccessible
174
161
  # if the class provides no size-provider.
175
162
  def keep_if(&block)
176
163
  if block.nil?
177
- e = reject!
178
- return Enumerator.new do |y|
179
- i = 0
180
- e.each do
181
- res = !y.yield(self[i])
182
- i += 1 unless res
183
- res
184
- end
185
- end
164
+ return Enumerator.new(self, :keep_if)
186
165
  else
187
166
  reject! do |el|
188
167
  !block.call(el)
@@ -313,17 +292,7 @@ module RandomAccessible
313
292
  def sort_by!(&block)
314
293
  # TODO: Optimize me.
315
294
  if block.nil?
316
- data = []
317
- Enumerator.new do |y|
318
- each do |el|
319
- data << y.yield(el)
320
- end
321
- i = -1
322
- sort_by! do
323
- i += 1
324
- data[i]
325
- end
326
- end
295
+ return Enumerator.new(self, :sort_by!)
327
296
  else
328
297
  replace(sort_by(&block))
329
298
  end
@@ -22,11 +22,9 @@ module RandomReadable
22
22
  # This method raises NotImplementedError
23
23
  # if the class provides neither size nor length method.
24
24
  def to_ary
25
- Enumerator.new do |y|
26
- size.times do |i|
27
- y << at(i)
28
- end
29
- end.to_a
25
+ Array.new(size) do |i|
26
+ at(i)
27
+ end
30
28
  end
31
29
 
32
30
  alias :to_a :to_ary
@@ -113,17 +111,19 @@ module RandomReadable
113
111
  if has_size?
114
112
  return nil if start < -size || size < start
115
113
  return nil if len < 0
116
- return Enumerator.new do |y|
117
- len.times do |i|
118
- y << at(start + i) if start + i < size
119
- end
120
- end.to_a
114
+ res = []
115
+ len.times do |i|
116
+ res << at(start + i) if start + i < size
117
+ end
118
+ return res
121
119
  else
122
- return Enumerator.new do |y|
123
- len.times do |i|
124
- y << at(start + i)
120
+ if len < 0
121
+ return []
122
+ else
123
+ return Array.new(len) do |i|
124
+ at(start + i)
125
125
  end
126
- end.to_a
126
+ end
127
127
  end
128
128
  elsif args[0].is_a? Range
129
129
  range = args[0]
@@ -139,11 +139,11 @@ module RandomReadable
139
139
  elsif first < 0 || size < first
140
140
  return nil
141
141
  end
142
- return Enumerator.new do |y|
143
- (first..last).each do |i|
144
- y << at(i) if 0 <= i && i < size
145
- end
146
- end.to_a
142
+ res = []
143
+ (first..last).each do |i|
144
+ res << at(i) if 0 <= i && i < size
145
+ end
146
+ return res
147
147
  else
148
148
  range.map do |i|
149
149
  at(i)
@@ -202,11 +202,11 @@ module RandomReadable
202
202
  # This method raises NotImplementedError
203
203
  # if the class provides neither size nor length method.
204
204
  def compact
205
- Enumerator.new do |y|
206
- each do |el|
207
- y << el unless el.nil?
208
- end
209
- end.to_a
205
+ res = []
206
+ each do |el|
207
+ res << el unless el.nil?
208
+ end
209
+ return res
210
210
  end
211
211
 
212
212
  # Same as Array's.
@@ -229,11 +229,7 @@ module RandomReadable
229
229
  # if the class provides neither size nor length method.
230
230
  def each(&block)
231
231
  if block.nil?
232
- return Enumerator.new do |y|
233
- size.times do |i|
234
- y << self[i]
235
- end
236
- end
232
+ return Enumerator.new(self, :each)
237
233
  else
238
234
  size.times do |i|
239
235
  block.call(self[i])
@@ -378,14 +374,12 @@ module RandomReadable
378
374
  # if the class provides neither size nor length method.
379
375
  def last(n = nil)
380
376
  if n.nil?
381
- at(size - 1)
377
+ return at(size - 1)
382
378
  else
383
379
  n = size if n > size
384
- Enumerator.new do |y|
385
- n.times do |i|
386
- y << at(size - n + i)
387
- end
388
- end.to_a
380
+ return Array.new(n) do |i|
381
+ at(size - n + i)
382
+ end
389
383
  end
390
384
  end
391
385
 
@@ -458,11 +452,7 @@ module RandomReadable
458
452
  def reverse_each(&block)
459
453
  # Needs size.
460
454
  if block.nil?
461
- Enumerator.new do |y|
462
- (size - 1).downto 0 do |i|
463
- y << at(i)
464
- end
465
- end
455
+ return Enumerator.new(self, :reverse_each)
466
456
  else
467
457
  (size - 1).downto 0 do |i|
468
458
  yield at(i)
@@ -560,23 +550,23 @@ module RandomReadable
560
550
  # The arguments must not be negative values
561
551
  # if the class does not provide size or length method.
562
552
  def values_at(*selectors)
563
- Enumerator.new do |y|
564
- selectors.each do |s|
565
- if s.is_a? Range
566
- subary = self[s]
567
- unless subary.nil?
568
- self[s].each do |el|
569
- y << el
570
- end
553
+ res = []
554
+ selectors.each do |s|
555
+ if s.is_a? Range
556
+ subary = self[s]
557
+ unless subary.nil?
558
+ self[s].each do |el|
559
+ res << el
571
560
  end
572
- if has_size? && !s.exclude_end? && s.include?(size)
573
- y << nil
574
- end
575
- else
576
- y << self[s.to_int]
577
561
  end
562
+ if has_size? && !s.exclude_end? && s.include?(size)
563
+ res << nil
564
+ end
565
+ else
566
+ res << self[s.to_int]
578
567
  end
579
- end.to_a
568
+ end
569
+ return res
580
570
  end
581
571
 
582
572
  # Same as Array's.