Skip to content

Commit

Permalink
Merge pull request #598 from kostya/sort
Browse files Browse the repository at this point in the history
sort tags
  • Loading branch information
alex committed Apr 10, 2013
2 parents 946205f + bb45905 commit 15bb0ec
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
4 changes: 4 additions & 0 deletions lib-topaz/array.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def sort(&block)
dup.sort!(&block) dup.sort!(&block)
end end


def sort_by(&block)
dup.sort_by!(&block)
end

def ==(other) def ==(other)
if self.equal?(other) if self.equal?(other)
return true return true
Expand Down
8 changes: 8 additions & 0 deletions lib-topaz/enumerable.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -313,4 +313,12 @@ def each_slice(num, &block)
yield buf unless buf.empty? yield buf unless buf.empty?
nil nil
end end

def sort(&block)
to_a.sort!(&block)
end

def sort_by(&block)
to_a.sort_by!(&block)
end
end end
7 changes: 0 additions & 7 deletions spec/tags/core/array/sort_by_tags.txt

This file was deleted.

9 changes: 1 addition & 8 deletions spec/tags/core/array/sort_tags.txt
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,3 @@
fails:Array#sort properly handles recursive arrays fails:Array#sort properly handles recursive arrays
fails:compares values returned by block with 0
fails:Array#sort! sorts array in place using block value if a block given
fails:Array#sort! properly handles recursive arrays
fails:Array#sort! does not call #<=> on contained objects when invoked with a block
fails:Array#sort! does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)
fails:Array#sort! raises a RuntimeError on a frozen array
fails:Array#sort sorts an array that has a value shifted off without a block
fails:Array#sort sorts an array that has a value shifted off with a block
fails:Array#sort compares values returned by block with 0 fails:Array#sort compares values returned by block with 0
fails:Array#sort! properly handles recursive arrays
3 changes: 0 additions & 3 deletions spec/tags/core/enumerable/sort_by_tags.txt
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1 @@
fails:Enumerable#sort_by returns an array of elements ordered by the result of block
fails:Enumerable#sort_by sorts the object by the given attribute
fails:Enumerable#sort_by returns an Enumerator when a block is not supplied
fails:Enumerable#sort_by gathers whole arrays as elements when each yields multiple fails:Enumerable#sort_by gathers whole arrays as elements when each yields multiple
4 changes: 0 additions & 4 deletions spec/tags/core/enumerable/sort_tags.txt
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,2 @@
fails:Enumerable#sort sorts by the natural order as defined by <=>
fails:Enumerable#sort yields elements to the provided block
fails:Enumerable#sort sorts enumerables that contain nils
fails:Enumerable#sort compare values returned by block with 0 fails:Enumerable#sort compare values returned by block with 0
fails:Enumerable#sort raises an error if objects can't be compared
fails:Enumerable#sort gathers whole arrays as elements when each yields multiple fails:Enumerable#sort gathers whole arrays as elements when each yields multiple
23 changes: 20 additions & 3 deletions topaz/objects/arrayobject.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ def __init__(self, space, list, listlength=None, sortblock=None):
self.space = space self.space = space
self.sortblock = sortblock self.sortblock = sortblock


def lt(self, a, b): def lt(self, w_a, w_b):
cmp_res = self.space.compare(a, b, self.sortblock) cmp_res = self.space.compare(w_a, w_b, self.sortblock)
return self.space.int_w(cmp_res) < 0

class RubySorterYielder(RubySorter):
def lt(self, w_a, w_b):
cmp_res = self.space.compare(
self.space.invoke_block(self.sortblock, [w_a]),
self.space.invoke_block(self.sortblock, [w_b])
)
return self.space.int_w(cmp_res) < 0 return self.space.int_w(cmp_res) < 0


class W_ArrayObject(W_Object): class W_ArrayObject(W_Object):
Expand Down Expand Up @@ -263,10 +271,19 @@ def method_clear(self, space):
return self return self


@classdef.method("sort!") @classdef.method("sort!")
def method_sort(self, space, block): @check_frozen()
def method_sort_i(self, space, block):
RubySorter(space, self.items_w, sortblock=block).sort() RubySorter(space, self.items_w, sortblock=block).sort()
return self return self


@classdef.method("sort_by!")
@check_frozen()
def method_sort_by_i(self, space, block):
if block is None:
return space.send(self, space.newsymbol("enum_for"), [space.newsymbol("sort_by!")])
RubySorterYielder(space, self.items_w, sortblock=block).sort()
return self

@classdef.method("reverse!") @classdef.method("reverse!")
@check_frozen() @check_frozen()
def method_reverse_i(self, space): def method_reverse_i(self, space):
Expand Down

0 comments on commit 15bb0ec

Please sign in to comment.