summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/mrblib/array.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-05-14 10:51:38 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-05-15 17:26:36 +0900
commitae4c952d2ebf73fb24688097c9e675b0d07b3cd7 (patch)
tree5bf2cafeb570a11949992e0eb8505dddb55fd10d /mrbgems/mruby-array-ext/mrblib/array.rb
parent9422fdbc87cc5310d7f0d5b1b9039fae1b6aa425 (diff)
downloadmruby-ae4c952d2ebf73fb24688097c9e675b0d07b3cd7.tar.gz
mruby-ae4c952d2ebf73fb24688097c9e675b0d07b3cd7.zip
mruby-array-ext/array.c: implement `Array#rotate` in C.
The Ruby version of `Array#rotate!` generated a rotated array and replaced the receiver, but the C version rotates the receiver array in-place. So the performance is improved a lot both in speed and memory consumption. Look for the comments in `array.c` for the in-place rotating algorithm, if you are interested.
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb51
1 files changed, 0 insertions, 51 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index 79fcca34e..a44976606 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -450,57 +450,6 @@ class Array
##
# call-seq:
- # ary.rotate(count=1) -> new_ary
- #
- # Returns a new array by rotating +self+ so that the element at +count+ is
- # the first element of the new array.
- #
- # If +count+ is negative then it rotates in the opposite direction, starting
- # from the end of +self+ where +-1+ is the last element.
- #
- # a = [ "a", "b", "c", "d" ]
- # a.rotate #=> ["b", "c", "d", "a"]
- # a #=> ["a", "b", "c", "d"]
- # a.rotate(2) #=> ["c", "d", "a", "b"]
- # a.rotate(-3) #=> ["b", "c", "d", "a"]
-
- def rotate(count=1)
- ary = []
- len = self.length
-
- if len > 0
- idx = (count < 0) ? (len - (~count % len) - 1) : (count % len) # rotate count
- len.times do
- ary << self[idx]
- idx += 1
- idx = 0 if idx > len-1
- end
- end
- ary
- end
-
- ##
- # call-seq:
- # ary.rotate!(count=1) -> ary
- #
- # Rotates +self+ in place so that the element at +count+ comes first, and
- # returns +self+.
- #
- # If +count+ is negative then it rotates in the opposite direction, starting
- # from the end of the array where +-1+ is the last element.
- #
- # a = [ "a", "b", "c", "d" ]
- # a.rotate! #=> ["b", "c", "d", "a"]
- # a #=> ["b", "c", "d", "a"]
- # a.rotate!(2) #=> ["d", "a", "b", "c"]
- # a.rotate!(-3) #=> ["a", "b", "c", "d"]
-
- def rotate!(count=1)
- self.replace(self.rotate(count))
- end
-
- ##
- # call-seq:
# ary.delete_if { |item| block } -> ary
# ary.delete_if -> Enumerator
#