]> gitweb.factorcode.org Git - factor.git/blobdiff - core/math/math.factor
kernel: move recusrive-hashcode to math and add test
[factor.git] / core / math / math.factor
index b23e129946579b98699b27dd2a8de528529835e7..869d0aa133261075d9f419eb25d813d9f07d3770 100644 (file)
@@ -119,9 +119,9 @@ MATH: /mod ( x y -- z w ) foldable
 MATH: bitand ( x y -- z ) foldable
 MATH: bitor  ( x y -- z ) foldable
 MATH: bitxor ( x y -- z ) foldable
-GENERIC# shift 1 ( x n -- y ) foldable
+GENERIC#: shift 1 ( x n -- y ) foldable
 GENERIC: bitnot ( x -- y ) foldable
-GENERIC# bit? 1 ( x n -- ? ) foldable
+GENERIC#: bit? 1 ( x n -- ? ) foldable
 
 GENERIC: abs ( x -- y ) foldable
 
@@ -131,6 +131,9 @@ GENERIC: (log2) ( x -- n ) foldable
 
 PRIVATE>
 
+: recursive-hashcode ( n obj quot -- code )
+    pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
+
 ERROR: log2-expects-positive x ;
 
 : log2 ( x -- n )
@@ -147,14 +150,16 @@ ERROR: log2-expects-positive x ;
 : even? ( n -- ? ) 1 bitand zero? ; inline
 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
 
-GENERIC: neg? ( x -- -x )
+GENERIC: neg? ( x -- ? )
 
 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
 
-: when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline
+: when-zero ( ... n quot: ( ... -- ... x ) -- ... x ) [ ] if-zero ; inline
+
+: unless-zero ( ... n quot: ( ... n -- ... ) -- ... ) [ ] swap if-zero ; inline
 
-: unless-zero ( ..a n quot: ( ..a n -- ..b ) -- ..b ) [ ] swap if-zero ; inline
+: until-zero ( ... n quot: ( ... x -- ... y ) -- ... ) [ dup zero? ] swap until drop ; inline
 
 UNION: integer fixnum bignum ;
 
@@ -245,50 +250,31 @@ GENERIC: prev-float ( m -- n )
 : align ( m w -- n )
     1 - [ + ] keep bitnot bitand ; inline
 
-<PRIVATE
-
-: iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
-
-: if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
-
-: iterate-step ( i n quot -- i n quot )
-    ! Apply quot to i, keep i and quot, hide n.
-    [ nip call ] 3keep ; inline
-
-: iterate-rot ( ? i n quot -- i n quot ? )
-    [ rot ] dip swap ; inline
-
-: iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
-
-PRIVATE>
-
-: (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
-    [ iterate-step iterate-next (each-integer) ]
-    [ 3drop ] if-iterate? ; inline recursive
-
-: (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
-    [
-        iterate-step iterate-rot
-        [ 2drop ] [ iterate-next (find-integer) ] if
-    ] [ 3drop f ] if-iterate? ; inline recursive
-
-: (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
-    [
-        iterate-step iterate-rot
-        [ iterate-next (all-integers?) ] [ 3drop f ] if
-    ] [ 3drop t ] if-iterate? ; inline recursive
+: each-integer-from ( ... i n quot: ( ... i -- ... ) -- ... )
+    2over < [
+        [ nip call ] 3keep
+        [ 1 + ] 2dip each-integer-from
+    ] [
+        3drop
+    ] if ; inline recursive
 
 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
-    iterate-prep (each-integer) ; inline
+    [ 0 ] 2dip each-integer-from ; inline
 
 : times ( ... n quot: ( ... -- ... ) -- ... )
     [ drop ] prepose each-integer ; inline
 
-: find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
-    iterate-prep (find-integer) ; inline
+: find-integer-from ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
+    2over < [
+        [ nip call ] 3keep roll
+        [ 2drop ]
+        [ [ 1 + ] 2dip find-integer-from ] if
+    ] [
+        3drop f
+    ] if ; inline recursive
 
-: all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
-    iterate-prep (all-integers?) ; inline
+: find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
+    [ 0 ] 2dip find-integer-from ; inline
 
 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
     over 0 < [
@@ -300,3 +286,15 @@ PRIVATE>
             [ 1 - ] dip find-last-integer
         ] if
     ] if ; inline recursive
+
+: all-integers-from? ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
+    2over < [
+        [ nip call ] 3keep roll
+        [ [ 1 + ] 2dip all-integers-from? ]
+        [ 3drop f ] if
+    ] [
+        3drop t
+    ] if ; inline recursive
+
+: all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
+    [ 0 ] 2dip all-integers-from? ; inline