]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.rotated: can be a lot faster if we don't circular-wrap.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 12 May 2015 02:41:01 +0000 (19:41 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 12 May 2015 02:41:01 +0000 (19:41 -0700)
extra/sequences/rotated/rotated.factor

index 0255689fc2d7e1e914e07806e804d0f28a8f0c0f..eb38af94697a25305edb50a27c9461af2dbd2495 100644 (file)
@@ -1,20 +1,24 @@
 ! Copyright (C) 2012 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
-USING: accessors circular kernel math sequences ;
+USING: accessors kernel math sequences ;
 IN: sequences.rotated
 
 TUPLE: rotated
-{ circular circular read-only }
+{ seq read-only }
 { n integer read-only } ;
 
-: <rotated> ( seq n -- rotated )
-    [ <circular> ] dip rotated boa ;
+C: <rotated> rotated
 
-M: rotated length circular>> length ;
+M: rotated length seq>> length ;
 
-M: rotated virtual@ [ n>> + ] [ circular>> ] bi ;
+M: rotated virtual@
+    [ n>> + ] [ seq>> ] bi [
+        length over 0 < [ + ] [
+            2dup >= [ - ] [ drop ] if
+        ] if
+    ] keep ;
 
-M: rotated virtual-exemplar circular>> ;
+M: rotated virtual-exemplar seq>> ;
 
 INSTANCE: rotated virtual-sequence