Skip to content
This repository
Browse code

Implement &sort

  • Loading branch information...
commit 9f1bf0e2a9b0704112497f70af92c77a1a137e94 1 parent ea8058e
sorear authored
37  lib/Kernel.cs
@@ -890,6 +890,18 @@ public class Kernel {
890 890
             return lv;
891 891
         }
892 892
 
  893
+        public static VarDeque SortHelper(Frame th, IP6 cb, VarDeque from) {
  894
+            Variable[] tmp = from.CopyAsArray();
  895
+            Array.Sort(tmp, delegate (Variable v1, Variable v2) {
  896
+                Frame end  = th.MakeChild(null, ExitRunloopSI);
  897
+                Frame call = cb.Invoke(end, new Variable[] { v1, v2 }, null);
  898
+                RunCore(call);
  899
+                return (int)(double)UnboxAny(
  900
+                    ((Variable)end.resultSlot).Fetch());
  901
+            });
  902
+            return new VarDeque(tmp);
  903
+        }
  904
+
893 905
         public static Variable ContextHelper(Frame th, string name) {
894 906
             object rt;
895 907
             while (th != null) {
@@ -1345,17 +1357,26 @@ public sealed class VarDeque {
1345 1357
             return d;
1346 1358
         }
1347 1359
 
  1360
+        private void CopyToArray(Variable[] tg) {
  1361
+            int z1 = data.Length - head;
  1362
+            if (z1 >= count) {
  1363
+                Array.Copy(data, head, tg, 0, count);
  1364
+            } else {
  1365
+                Array.Copy(data, head, tg, 0, z1);
  1366
+                int z2 = count - z1;
  1367
+                Array.Copy(data, 0, tg, z1, z2);
  1368
+            }
  1369
+        }
  1370
+
  1371
+        public Variable[] CopyAsArray() {
  1372
+            Variable[] ret = new Variable[count];
  1373
+            CopyToArray(ret);
  1374
+            return ret;
  1375
+        }
  1376
+
1348 1377
         private void checkgrow() {
1349 1378
             if (count == data.Length - 1) {
1350 1379
                 Variable[] ndata = new Variable[data.Length * 2];
1351  
-                int z1 = data.Length - head;
1352  
-                if (z1 >= count) {
1353  
-                    Array.Copy(data, head, ndata, 0, count);
1354  
-                } else {
1355  
-                    Array.Copy(data, head, ndata, 0, z1);
1356  
-                    int z2 = count - z1;
1357  
-                    Array.Copy(data, 0, ndata, z1, z2);
1358  
-                }
1359 1380
                 data = ndata;
1360 1381
                 head = 0;
1361 1382
             }
1  src/CLRTypes.pm
@@ -131,6 +131,7 @@ my %typedata = (
131 131
     'Kernel.DefaultNew'    => [m => 'Variable'],
132 132
     'Kernel.NewROScalar'   => [m => 'Variable'],
133 133
     'Kernel.NewRWScalar'   => [m => 'Variable'],
  134
+    'Kernel.SortHelper'    => [m => 'VarDeque'],
134 135
     'Kernel.NewRWListVar'  => [m => 'Variable'],
135 136
     'Console.WriteLine'    => [m => 'Void'],
136 137
     'Console.Error.WriteLine'    => [m => 'Void'],
1  src/CgOp.pm
@@ -193,6 +193,7 @@ use warnings;
193 193
     sub vvarlist_unshiftn { rawcall($_[0], 'UnshiftN', $_[1]) }
194 194
     sub vvarlist_push { rawcall($_[0], 'Push', $_[1]) }
195 195
     sub vvarlist_item { getindex($_[0], $_[1]) }
  196
+    sub vvarlist_sort { rawscall('Kernel.SortHelper', callframe(), $_[0], $_[1]) }
196 197
 
197 198
     sub frame_caller { getfield('caller', $_[0]) }
198 199
     sub frame_file { rawcall($_[0], 'ExecutingFile') }
20  test2.pl
... ...
@@ -1,5 +1,6 @@
1 1
 # vim: ft=perl6
2 2
 use Test;
  3
+use MONKEY_TYPING;
3 4
 
4 5
 sub infix:<x>($str, $ct) {
5 6
     my $i = +$ct;
@@ -23,11 +24,30 @@ sub infix:<gt>($s1, $s2) { ($s1 leg $s2) > 0  }
23 24
 sub infix:<le>($s1, $s2) { ($s1 leg $s2) <= 0 }
24 25
 sub infix:<lt>($s1, $s2) { ($s1 leg $s2) < 0  }
25 26
 
  27
+augment class Any {
  28
+    method sort($cmp = &infix:<leg>) {
  29
+        my $l = self.list.eager;
  30
+        Q:CgOp {
  31
+            (letn n (obj_newblank (obj_llhow (@ {List})))
  32
+              (setslot flat (l n) (bool 1))
  33
+              (setslot items (l n) (vvarlist_sort (@ {$cmp})
  34
+                  (getslot items vvarlist (@ {$l}))))
  35
+              (setslot rest (l n) (vvarlist_new_empty))
  36
+              (newrwlistvar (l n)))
  37
+        }
  38
+    }
  39
+}
  40
+
  41
+sub sort(*@bits) { @bits.sort }
  42
+
26 43
 ok 'cow' le 'sow', 'cow le sow';
27 44
 ok !('sow' le 'cow'), 'sow !le cow';
28 45
 ok 'row' lt 'tow', 'row lt tow';
29 46
 ok 'how' gt 'bow', 'how gt bow';
30 47
 ok 'yow' ge 'yow', 'yow ge yow';
  48
+is join("|", sort <c f d z a>), 'a|c|d|f|z', '&sort works';
  49
+is join("|", <a3 b2 c1 d0>.sort({ substr($^a,1) leg substr($^b,1) })),
  50
+    'd0|c1|b2|a3', '.sort with callback works';
31 51
 
32 52
 #is $?FILE, 'test.pl', '$?FILE works';
33 53
 #is $?ORIG.substr(0,5), '# vim', '$?ORIG works';
4  v6/TODO
... ...
@@ -1,9 +1,9 @@
1 1
 ~
2 2
 <?{ }>
3  
-<alpha>
4 3
 () being Nil
5 4
 circumfix:<[ ]>
6 5
 Cursor.add_categorical
  6
+Cursor.alpha
7 7
 Cursor.canonicalize_name
8 8
 Cursor.check_old_cclass
9 9
 Cursor.cursor_all
@@ -28,7 +28,6 @@ $*FOO as a parameter
28 28
 func(|($key => $value))
29 29
 gt, lt, leg, etc
30 30
 hash literals
31  
-infix:<x>
32 31
 List.at-pos(WhateverCode)
33 32
 Match.CURSOR
34 33
 Match.iterator should return numbered captures
@@ -50,6 +49,7 @@ defined($thing)
50 49
 :exists
51 50
 Hash.keys &keys
52 51
 Hash.LISTSTORE
  52
+infix:<x>
53 53
 invert(%hash)
54 54
 &item
55 55
 &join

0 notes on commit 9f1bf0e

Please sign in to comment.
Something went wrong with that request. Please try again.