interval-maps: bit more speed using unsafe words.
parent
ffbe20556b
commit
6cba7af1e1
|
@ -1,22 +1,22 @@
|
|||
! Copyright (C) 2008 Daniel Ehrenberg.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel sequences arrays accessors grouping math.order
|
||||
sorting binary-search math assocs locals namespaces make ;
|
||||
USING: accessors arrays assocs binary-search grouping kernel
|
||||
locals make math math.order sequences sequences.private sorting ;
|
||||
IN: interval-maps
|
||||
|
||||
TUPLE: interval-map array ;
|
||||
TUPLE: interval-map { array array read-only } ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
ALIAS: start first
|
||||
ALIAS: end second
|
||||
ALIAS: value third
|
||||
ALIAS: start first-unsafe
|
||||
ALIAS: end second-unsafe
|
||||
ALIAS: value third-unsafe
|
||||
|
||||
: find-interval ( key interval-map -- interval-node )
|
||||
array>> [ start <=> ] with search nip ;
|
||||
array>> [ start <=> ] with search nip ; inline
|
||||
|
||||
: interval-contains? ( key interval-node -- ? )
|
||||
first2 between? ;
|
||||
first2-unsafe between? ; inline
|
||||
|
||||
: all-intervals ( sequence -- intervals )
|
||||
[ [ dup number? [ dup 2array ] when ] dip ] { } assoc-map-as ;
|
||||
|
@ -31,9 +31,15 @@ ALIAS: value third
|
|||
: >intervals ( specification -- intervals )
|
||||
[ suffix ] { } assoc>map concat 3 group ;
|
||||
|
||||
ERROR: not-an-interval-map obj ;
|
||||
|
||||
: check-interval-map ( map -- map )
|
||||
dup interval-map? [ not-an-interval-map ] unless ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: interval-at* ( key map -- value ? )
|
||||
check-interval-map
|
||||
[ drop ] [ find-interval ] 2bi
|
||||
[ nip ] [ interval-contains? ] 2bi
|
||||
[ value t ] [ drop f f ] if ; inline
|
||||
|
@ -43,7 +49,7 @@ PRIVATE>
|
|||
: interval-key? ( key map -- ? ) interval-at* nip ;
|
||||
|
||||
: interval-values ( map -- values )
|
||||
array>> [ value ] map ;
|
||||
check-interval-map array>> [ value ] map ;
|
||||
|
||||
: <interval-map> ( specification -- map )
|
||||
all-intervals [ first second ] sort-with
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
! Copyright (C) 2009 Daniel Ehrenberg.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: accessors alien.c-types arrays assocs binary-search
|
||||
combinators fry grouping hints kernel locals make math
|
||||
math.order sequences sorting specialized-arrays ;
|
||||
combinators fry grouping kernel locals make math math.order
|
||||
sequences sequences.private sorting specialized-arrays ;
|
||||
SPECIALIZED-ARRAY: uint
|
||||
IN: interval-sets
|
||||
! Sets of positive integers
|
||||
|
@ -11,22 +11,25 @@ TUPLE: interval-set { array uint-array read-only } ;
|
|||
|
||||
<PRIVATE
|
||||
|
||||
ALIAS: start first
|
||||
ALIAS: end second
|
||||
ALIAS: start first-unsafe
|
||||
ALIAS: end second-unsafe
|
||||
|
||||
: find-interval ( key interval-set -- slice )
|
||||
array>> 2 <sliced-groups>
|
||||
[ start <=> ] with search nip ; inline
|
||||
|
||||
ERROR: not-an-interval-set obj ;
|
||||
|
||||
: check-interval-set ( map -- map )
|
||||
dup interval-set? [ not-an-interval-set ] unless ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: in? ( key set -- ? )
|
||||
dupd find-interval
|
||||
check-interval-set dupd find-interval
|
||||
[ [ start ] [ end 1 - ] bi between? ]
|
||||
[ drop f ] if* ;
|
||||
|
||||
HINTS: in? { integer interval-set } ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: spec>pairs ( sequence -- intervals )
|
||||
|
|
Loading…
Reference in New Issue