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
e89676a6
Unverified
Commit
e89676a6
authored
Oct 18, 2018
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Oct 18, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4142 from iij/mergesort
replace quicksort with mergesort.
parents
fdd5ce8f
91444c47
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
36 deletions
+58
-36
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
+1
-3
mrblib/array.rb
mrblib/array.rb
+51
-33
test/t/array.rb
test/t/array.rb
+6
-0
No files found.
mrbgems/mruby-enum-ext/mrblib/enum.rb
View file @
e89676a6
...
@@ -201,9 +201,7 @@ module Enumerable
...
@@ -201,9 +201,7 @@ module Enumerable
ary
.
push
([
block
.
call
(
e
),
i
])
ary
.
push
([
block
.
call
(
e
),
i
])
}
}
if
ary
.
size
>
1
if
ary
.
size
>
1
ary
.
__sort_sub__
(
0
,
ary
.
size
-
1
)
do
|
a
,
b
|
ary
.
sort!
a
<=>
b
end
end
end
ary
.
collect
{
|
e
,
i
|
orig
[
i
]}
ary
.
collect
{
|
e
,
i
|
orig
[
i
]}
end
end
...
...
mrblib/array.rb
View file @
e89676a6
...
@@ -193,43 +193,61 @@ class Array
...
@@ -193,43 +193,61 @@ class Array
include
Enumerable
include
Enumerable
##
##
# Quick sort
# Sort all elements and replace +self+ with these
# left : the beginning of sort region
# elements.
# right : the end of sort region
def
sort!
(
&
block
)
def
__sort_sub__
(
left
,
right
,
&
block
)
stack
=
[
[
0
,
self
.
size
-
1
]
]
stack
=
[
[
left
,
right
]
]
until
stack
.
empty?
until
stack
.
empty?
left
,
right
=
stack
.
pop
left
,
mid
,
right
=
stack
.
pop
if
left
<
right
if
right
==
nil
i
=
left
right
=
mid
j
=
right
# sort self[left..right]
pivot
=
self
[
i
+
(
j
-
i
)
/
2
]
if
left
<
right
while
true
if
left
+
1
==
right
while
((
block
)?
block
.
call
(
self
[
i
],
pivot
):
(
self
[
i
]
<=>
pivot
))
<
0
lval
=
self
[
left
]
i
+=
1
rval
=
self
[
right
]
end
if
(
block
&
.
call
(
lval
,
rval
)
||
(
lval
<=>
rval
))
>
0
while
((
block
)?
block
.
call
(
pivot
,
self
[
j
]):
(
pivot
<=>
self
[
j
]))
<
0
self
[
left
]
=
rval
j
-=
1
self
[
right
]
=
lval
end
else
mid
=
((
left
+
right
+
1
)
/
2
).
floor
stack
.
push
[
left
,
mid
,
right
]
stack
.
push
[
mid
,
right
]
stack
.
push
[
left
,
(
mid
-
1
)
]
if
left
<
mid
-
1
end
end
break
if
(
i
>=
j
)
tmp
=
self
[
i
];
self
[
i
]
=
self
[
j
];
self
[
j
]
=
tmp
;
i
+=
1
j
-=
1
end
end
stack
.
push
[
left
,
i
-
1
]
else
stack
.
push
[
j
+
1
,
right
]
lary
=
self
[
left
,
mid
-
left
]
end
lsize
=
lary
.
size
end
end
# private :__sort_sub__
##
# The entity sharing between lary and self may cause a large memory
# Sort all elements and replace +self+ with these
# copy operation in the merge loop below. This harmless operation
# elements.
# cancels the sharing and provides a huge performance gain.
def
sort!
(
&
block
)
lary
[
0
]
=
lary
[
0
]
size
=
self
.
size
if
size
>
1
# merge
__sort_sub__
(
0
,
size
-
1
,
&
block
)
lidx
=
0
ridx
=
mid
(
left
..
right
).
each
{
|
i
|
if
lidx
>=
lsize
break
elsif
ridx
>
right
self
[
i
,
lsize
-
lidx
]
=
lary
[
lidx
,
lsize
-
lidx
]
break
else
lval
=
lary
[
lidx
]
rval
=
self
[
ridx
]
if
(
block
&
.
call
(
lval
,
rval
)
||
(
lval
<=>
rval
))
<=
0
self
[
i
]
=
lval
lidx
+=
1
else
self
[
i
]
=
rval
ridx
+=
1
end
end
}
end
end
end
self
self
end
end
...
...
test/t/array.rb
View file @
e89676a6
...
@@ -386,6 +386,12 @@ assert("Array#rindex") do
...
@@ -386,6 +386,12 @@ assert("Array#rindex") do
assert_equal
0
,
$a
.
rindex
(
1
)
assert_equal
0
,
$a
.
rindex
(
1
)
end
end
assert
(
'Array#sort!'
)
do
a
=
[
3
,
2
,
1
]
assert_equal
a
,
a
.
sort!
# sort! returns self.
assert_equal
[
1
,
2
,
3
],
a
# it is sorted.
end
assert
(
'Array#freeze'
)
do
assert
(
'Array#freeze'
)
do
a
=
[].
freeze
a
=
[].
freeze
assert_raise
(
RuntimeError
)
do
assert_raise
(
RuntimeError
)
do
...
...
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