-USING: math.ranges sequences tools.test arrays ;
+USING: math math.ranges sequences sets tools.test arrays ;
IN: math.ranges.tests
[ { } ] [ 1 1 (a,b) >array ] unit-test
[ { 1 } ] [ 1 2 [a,b) >array ] unit-test
[ { 1 2 } ] [ 1 2 [a,b] >array ] unit-test
-[ { } ] [ 2 1 (a,b) >array ] unit-test
+[ { } ] [ 2 1 (a,b) >array ] unit-test
[ { 1 } ] [ 2 1 (a,b] >array ] unit-test
[ { 2 } ] [ 2 1 [a,b) >array ] unit-test
[ { 2 1 } ] [ 2 1 [a,b] >array ] unit-test
[ 0 ] [ -1 5 [0,b] clamp-to-range ] unit-test
[ 5 ] [ 6 5 [0,b] clamp-to-range ] unit-test
[ { 0 1 2 3 4 } ] [ 5 sequence-index-range >array ] unit-test
+
+[ 100 ] [
+ 1 100 [a,b] [ 2^ [1,b] ] map prune length
+] unit-test
\ No newline at end of file
-! Copyright (C) 2008 Slava Pestov.
+! Copyright (C) 2008, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel layouts math math.order namespaces sequences
-sequences.private accessors ;
+sequences.private accessors classes.tuple arrays ;
IN: math.ranges
TUPLE: range
M: range nth-unsafe ( n range -- obj )
[ step>> * ] keep from>> + ;
+! For ranges with many elements, the default element-wise methods
+! sequences define are unsuitable because they're O(n)
+M: range equal? over range? [ tuple= ] [ 2drop f ] if ;
+
+M: range hashcode* tuple-hashcode ;
+
INSTANCE: range immutable-sequence
: twiddle ( a b -- a b step ) 2dup > -1 1 ? ; inline
ERROR: bad-superclass class ;
-<PRIVATE
-
: tuple= ( tuple1 tuple2 -- ? )
- 2dup [ layout-of ] bi@ eq? [
- [ drop tuple-size ]
- [ [ [ drop array-nth ] [ nip array-nth ] 3bi = ] 2curry ]
- 2bi all-integers?
- ] [
- 2drop f
- ] if ; inline
+ 2dup [ tuple? ] both? [
+ 2dup [ layout-of ] bi@ eq? [
+ [ drop tuple-size ]
+ [ [ [ drop array-nth ] [ nip array-nth ] 3bi = ] 2curry ]
+ 2bi all-integers?
+ ] [ 2drop f ] if
+ ] [ 2drop f ] if ; inline
+
+<PRIVATE
: tuple-predicate-quot/1 ( class -- quot )
#! Fast path for tuples with no superclass
M: tuple equal? over tuple? [ tuple= ] [ 2drop f ] if ;
-M: tuple hashcode*
+GENERIC: tuple-hashcode ( n tuple -- x )
+
+M: tuple tuple-hashcode
[
[ class hashcode ] [ tuple-size ] [ ] tri
[ rot ] dip [
] 2curry each
] recursive-hashcode ;
+M: tuple hashcode* tuple-hashcode ;
+
M: tuple-class new
dup "prototype" word-prop
[ (clone) ] [ tuple-layout <tuple> ] ?if ;