Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort methods to Indexable::Mutable #11195

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 8 additions & 138 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1577,96 +1577,20 @@ class Array(T)
dup.shuffle!(random)
end

# Returns a new array with all elements sorted based on the return value of
# their comparison method `#<=>`
#
# ```
# a = [3, 1, 2]
# a.sort # => [1, 2, 3]
# a # => [3, 1, 2]
# ```
def sort : Array(T)
dup.sort!
end

# :ditto:
#
# This method does not guarantee stability between equally sorting elements.
# Which results in a performance advantage over stable sort.
def unstable_sort : Array(T)
dup.unstable_sort!
end

# Returns a new array with all elements sorted based on the comparator in the
# given block.
#
# The block must implement a comparison between two elements *a* and *b*,
# where `a < b` returns `-1`, `a == b` returns `0`, and `a > b` returns `1`.
# The comparison operator `<=>` can be used for this.
#
# ```
# a = [3, 1, 2]
# b = a.sort { |a, b| b <=> a }
#
# b # => [3, 2, 1]
# a # => [3, 1, 2]
# ```
def sort(&block : T, T -> U) : Array(T) forall U
{% unless U <= Int32? %}
{% raise "expected block to return Int32 or Nil, not #{U}" %}
{% end %}

dup.sort! &block
end

# :ditto:
#
# This method does not guarantee stability between equally sorting elements.
# Which results in a performance advantage over stable sort.
def unstable_sort(&block : T, T -> U) : Array(T) forall U
{% unless U <= Int32? %}
{% raise "expected block to return Int32 or Nil, not #{U}" %}
{% end %}

dup.unstable_sort!(&block)
end

# Modifies `self` by sorting all elements based on the return value of their
# comparison method `#<=>`
#
# ```
# a = [3, 1, 2]
# a.sort!
# a # => [1, 2, 3]
# ```
def sort! : Array(T)
# :inherit:
def sort! : self
to_unsafe_slice.sort!
self
end

# :ditto:
#
# This method does not guarantee stability between equally sorting elements.
# Which results in a performance advantage over stable sort.
def unstable_sort! : Array(T)
# :inherit:
def unstable_sort! : self
to_unsafe_slice.unstable_sort!
self
end

# Modifies `self` by sorting all elements based on the comparator in the given
# block.
#
# The given block must implement a comparison between two elements
# *a* and *b*, where `a < b` returns `-1`, `a == b` returns `0`,
# and `a > b` returns `1`.
# The comparison operator `<=>` can be used for this.
#
# ```
# a = [3, 1, 2]
# a.sort! { |a, b| b <=> a }
# a # => [3, 2, 1]
# ```
def sort!(&block : T, T -> U) : Array(T) forall U
# :inherit:
def sort!(&block : T, T -> U) : self forall U
{% unless U <= Int32? %}
{% raise "expected block to return Int32 or Nil, not #{U}" %}
{% end %}
Expand All @@ -1675,11 +1599,8 @@ class Array(T)
self
end

# :ditto:
#
# This method does not guarantee stability between equally sorting elements.
# Which results in a performance advantage over stable sort.
def unstable_sort!(&block : T, T -> U) : Array(T) forall U
# :inherit:
def unstable_sort!(&block : T, T -> U) : self forall U
{% unless U <= Int32? %}
{% raise "expected block to return Int32 or Nil, not #{U}" %}
{% end %}
Expand All @@ -1688,57 +1609,6 @@ class Array(T)
self
end

# Returns a new array with all elements sorted. The given block is called for
# each element, then the comparison method #<=> is called on the object
# returned from the block to determine sort order.
#
# ```
# a = %w(apple pear fig)
# b = a.sort_by { |word| word.size }
# b # => ["fig", "pear", "apple"]
# a # => ["apple", "pear", "fig"]
# ```
def sort_by(&block : T -> _) : Array(T)
dup.sort_by! { |e| yield(e) }
end

# :ditto:
#
# This method does not guarantee stability between equally sorting elements.
# Which results in a performance advantage over stable sort.
def unstable_sort_by(&block : T -> _) : Array(T)
dup.unstable_sort_by! { |e| yield(e) }
end

# Modifies `self` by sorting all elements. The given block is called for
# each element, then the comparison method #<=> is called on the object
# returned from the block to determine sort order.
#
# ```
# a = %w(apple pear fig)
# a.sort_by! { |word| word.size }
# a # => ["fig", "pear", "apple"]
# ```
def sort_by!(&block : T -> _) : Array(T)
sorted = map { |e| {e, yield(e)} }.sort! { |x, y| x[1] <=> y[1] }
@size.times do |i|
@buffer[i] = sorted.to_unsafe[i][0]
end
self
end

# :ditto:
#
# This method does not guarantee stability between equally sorting elements.
# Which results in a performance advantage over stable sort.
def unstable_sort_by!(&block : T -> _) : Array(T)
sorted = map { |e| {e, yield(e)} }.unstable_sort! { |x, y| x[1] <=> y[1] }
@size.times do |i|
@buffer[i] = sorted.to_unsafe[i][0]
end
self
end

def to_a
self
end
Expand Down
Loading