1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.tuple kernel math math.order sequences
14 : sign/mod ( x y -- z w )
15 [ [ /i ] 2keep pick * - ] keep 0 < [ neg ] when ; inline
19 : <range> ( a b step -- range )
21 [ sign/mod 0 < [ 1 + ] unless 0 max ] keep
24 M: range length length>> ; inline
27 [ step>> * ] keep from>> + ; inline
29 ! We want M\ tuple hashcode, not M\ sequence hashcode here!
30 ! sequences hashcode is O(n) in number of elements
31 M: range hashcode* tuple-hashcode ;
33 INSTANCE: range immutable-sequence
38 [ swap [ first-unsafe ] [ last-unsafe ] bi + * 2 / ] if-zero ;
42 : twiddle ( a b -- a b step ) 2dup > -1 1 ? ; inline
44 : (a.. ( a b step -- a' b' step ) dup [ + ] curry 2dip ; inline
46 : ..b) ( a b step -- a' b' step ) dup [ - ] curry dip ; inline
50 : [a..b] ( a b -- range ) twiddle <range> ; inline
52 : (a..b] ( a b -- range ) twiddle (a.. <range> ; inline
54 : [a..b) ( a b -- range ) twiddle ..b) <range> ; inline
56 : (a..b) ( a b -- range ) twiddle (a.. ..b) <range> ; inline
58 : [0..b] ( b -- range ) 0 swap [a..b] ; inline
60 : [1..b] ( b -- range ) 1 swap [a..b] ; inline
62 : [0..b) ( b -- range ) 0 swap [a..b) ; inline
64 : [1..b) ( b -- range ) 1 swap [a..b) ; inline