Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
7b8d7840
Commit
7b8d7840
authored
Jul 25, 2017
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reimplement sort method to reduce array copying.
parent
7f9e3336
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
47 deletions
+40
-47
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
+1
-1
mrblib/array.rb
mrblib/array.rb
+38
-1
mrblib/enum.rb
mrblib/enum.rb
+1
-45
No files found.
mrbgems/mruby-enum-ext/mrblib/enum.rb
View file @
7b8d7840
...
...
@@ -201,7 +201,7 @@ module Enumerable
ary
.
push
([
block
.
call
(
e
),
i
])
}
if
ary
.
size
>
1
__sort_sub__
(
ary
,
::
Array
.
new
(
ary
.
size
),
0
,
0
,
ary
.
size
-
1
)
do
|
a
,
b
|
__sort_sub__
(
ary
,
0
,
ary
.
size
-
1
)
do
|
a
,
b
|
a
<=>
b
end
end
...
...
mrblib/array.rb
View file @
7b8d7840
# coding: utf-8
##
# Array
#
...
...
@@ -192,10 +193,46 @@ class Array
# ISO 15.2.12.3
include
Enumerable
##
# Quick sort
# a : the array to sort
# left : the beginning of sort region
# right : the end of sort region
def
__sort_sub__
(
a
,
left
,
right
,
&
block
)
if
left
<
right
i
=
left
j
=
right
pivot
=
a
[
i
+
(
j
-
i
)
/
2
]
while
true
while
((
block
)?
block
.
call
(
a
[
i
],
pivot
):
(
a
[
i
]
<=>
pivot
))
<
0
i
+=
1
end
while
((
block
)?
block
.
call
(
pivot
,
a
[
j
]):
(
pivot
<=>
a
[
j
]))
<
0
j
-=
1
end
break
if
(
i
>=
j
)
tmp
=
a
[
i
];
a
[
i
]
=
a
[
j
];
a
[
j
]
=
tmp
;
i
+=
1
j
-=
1
end
__sort_sub__
(
a
,
left
,
i
-
1
,
&
block
)
__sort_sub__
(
a
,
j
+
1
,
right
,
&
block
)
end
end
# private :__sort_sub__
##
# Sort all elements and replace +self+ with these
# elements.
def
sort!
(
&
block
)
self
.
replace
(
self
.
sort
(
&
block
))
size
=
self
.
size
if
size
>
1
__sort_sub__
(
self
,
0
,
size
-
1
,
&
block
)
end
self
end
def
sort
(
&
block
)
self
.
dup
.
sort!
(
&
block
)
end
end
mrblib/enum.rb
View file @
7b8d7840
...
...
@@ -315,45 +315,6 @@ module Enumerable
# ISO 15.3.2.2.18
alias
select
find_all
##
# TODO
# Does this OK? Please test it.
def
__sort_sub__
(
sorted
,
work
,
src_ary
,
head
,
tail
,
&
block
)
if
head
==
tail
sorted
[
head
]
=
work
[
head
]
if
src_ary
==
1
return
end
# on current step, which is a src ary?
if
src_ary
==
0
src
,
dst
=
sorted
,
work
else
src
,
dst
=
work
,
sorted
end
key
=
src
[
head
]
# key value for dividing values
i
,
j
=
head
,
tail
# position to store on the dst ary
(
head
+
1
).
upto
(
tail
){
|
idx
|
if
((
block
)?
block
.
call
(
src
[
idx
],
key
):
(
src
[
idx
]
<=>
key
))
>
0
# larger than key
dst
[
j
]
=
src
[
idx
]
j
-=
1
else
dst
[
i
]
=
src
[
idx
]
i
+=
1
end
}
sorted
[
i
]
=
key
# sort each sub-array
src_ary
=
(
src_ary
+
1
)
%
2
# exchange a src ary
__sort_sub__
(
sorted
,
work
,
src_ary
,
head
,
i
-
1
,
&
block
)
if
i
>
head
__sort_sub__
(
sorted
,
work
,
src_ary
,
i
+
1
,
tail
,
&
block
)
if
i
<
tail
end
# private :__sort_sub__
##
# Return a sorted array of all elements
# which are yield by +each+. If no block
...
...
@@ -364,12 +325,7 @@ module Enumerable
#
# ISO 15.3.2.2.19
def
sort
(
&
block
)
ary
=
[]
self
.
each
{
|*
val
|
ary
.
push
(
val
.
__svalue
)}
if
ary
.
size
>
1
__sort_sub__
(
ary
,
::
Array
.
new
(
ary
.
size
),
0
,
0
,
ary
.
size
-
1
,
&
block
)
end
ary
self
.
map
{
|*
val
|
val
.
__svalue
}.
sort
end
##
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment