On Tuesday 23 September 2008 09:35:05 Moritz Lenz wrote:

> %hash.pairs.sort needs infix:<cmp> to work properly with Pair as input.
>
> It should be easy to add a multi that does this by first comparing the
> key, and if they are equal the value.
>
> When this is done most (or all) of the skip markers in
> t/spec/S29-hash/pairs.t can (hopefully) be removed.
Something like this?

-- c

=== src/builtins/cmp.pir
==================================================================
--- src/builtins/cmp.pir	(revision 31423)
+++ src/builtins/cmp.pir	(local)
@@ -140,7 +140,7 @@
 .end
 
 
-.sub 'infix:cmp'
+.sub 'infix:cmp' :multi(_,_)
     .param pmc a
     .param pmc b
     $I0 = cmp a, b
=== src/classes/Pair.pir
==================================================================
--- src/classes/Pair.pir	(revision 31423)
+++ src/classes/Pair.pir	(local)
@@ -93,7 +93,28 @@
     .return $P0.'new'('key'=>key, 'value'=>value)
 .end
 
+.sub 'infix:cmp' :multi(Pair, Pair)
+    .param pmc a
+    .param pmc b
+    .local pmc a_key
+    .local pmc b_key
+    a_key = a.'key'()
+    b_key = b.'key'()
+    $P0   = 'infix:<cmp>'(a_key, b_key)
 
+    unless $P0 goto compare_values
+    .return( $P0 )
+
+  compare_values:
+    .local pmc a_value
+    .local pmc b_value
+    a_value = a.'value'()
+    b_value = b.'value'()
+    $P0   = 'infix:<cmp>'(a_value, b_value)
+    .return ($P0)
+.end
+
+
 =back
 
 =cut

Reply via email to