Make array length foldable

db4
Slava Pestov 2008-07-16 16:48:09 -05:00
parent 2025e6733d
commit d0ae6bdb8f
7 changed files with 17 additions and 16 deletions

View File

@ -1,11 +1,11 @@
! Copyright (C) 2005, 2007 Slava Pestov. ! Copyright (C) 2005, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel kernel.private math math.private sequences USING: accessors kernel kernel.private math math.private
sequences.private ; sequences sequences.private ;
IN: arrays IN: arrays
M: array clone (clone) ; M: array clone (clone) ;
M: array length array-capacity ; M: array length length>> ;
M: array nth-unsafe >r >fixnum r> array-nth ; M: array nth-unsafe >r >fixnum r> array-nth ;
M: array set-nth-unsafe >r >fixnum r> set-array-nth ; M: array set-nth-unsafe >r >fixnum r> set-array-nth ;
M: array resize resize-array ; M: array resize resize-array ;

View File

@ -37,7 +37,7 @@ nl
array? hashtable? vector? array? hashtable? vector?
tuple? sbuf? node? tombstone? tuple? sbuf? node? tombstone?
array-capacity array-nth set-array-nth array-nth set-array-nth
wrap probe wrap probe

View File

@ -225,7 +225,9 @@ bi
{ "imaginary" { "real" "math" } read-only } { "imaginary" { "real" "math" } read-only }
} define-builtin } define-builtin
"array" "arrays" create { } define-builtin "array" "arrays" create {
{ "length" { "array-capacity" "sequences.private" } read-only }
} define-builtin
"wrapper" "kernel" create { "wrapper" "kernel" create {
{ "wrapped" read-only } { "wrapped" read-only }
@ -261,7 +263,9 @@ bi
{ "sub-primitive" read-only } { "sub-primitive" read-only }
} define-builtin } define-builtin
"byte-array" "byte-arrays" create { } define-builtin "byte-array" "byte-arrays" create {
{ "length" { "array-capacity" "sequences.private" } read-only }
} define-builtin
"callstack" "kernel" create { } define-builtin "callstack" "kernel" create { } define-builtin

View File

@ -1,11 +1,11 @@
! Copyright (C) 2007, 2008 Slava Pestov. ! Copyright (C) 2007, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel kernel.private alien.accessors sequences USING: accessors kernel kernel.private alien.accessors sequences
sequences.private math ; sequences.private math ;
IN: byte-arrays IN: byte-arrays
M: byte-array clone (clone) ; M: byte-array clone (clone) ;
M: byte-array length array-capacity ; M: byte-array length length>> ;
M: byte-array nth-unsafe swap >fixnum alien-unsigned-1 ; M: byte-array nth-unsafe swap >fixnum alien-unsigned-1 ;
M: byte-array set-nth-unsafe swap >fixnum set-alien-unsigned-1 ; M: byte-array set-nth-unsafe swap >fixnum set-alien-unsigned-1 ;
: >byte-array ( seq -- byte-array ) B{ } clone-like ; inline : >byte-array ( seq -- byte-array ) B{ } clone-like ; inline

View File

@ -91,7 +91,7 @@ ERROR: bad-superclass class ;
#! 4 slot == superclasses>> #! 4 slot == superclasses>>
rot dup tuple? [ rot dup tuple? [
layout-of 4 slot layout-of 4 slot
2dup array-capacity fixnum< 2dup 1 slot fixnum<
[ array-nth eq? ] [ 3drop f ] if [ array-nth eq? ] [ 3drop f ] if
] [ 3drop f ] if ; inline ] [ 3drop f ] if ; inline

View File

@ -12,7 +12,7 @@ TUPLE: hashtable
<PRIVATE <PRIVATE
: wrap ( i array -- n ) : wrap ( i array -- n )
array-capacity 1 fixnum-fast fixnum-bitand ; inline length>> 1 fixnum-fast fixnum-bitand ; inline
: hash@ ( key array -- i ) : hash@ ( key array -- i )
>r hashcode >fixnum dup fixnum+fast r> wrap ; inline >r hashcode >fixnum dup fixnum+fast r> wrap ; inline
@ -30,7 +30,7 @@ TUPLE: hashtable
] if ; inline ] if ; inline
: key@ ( key hash -- array n ? ) : key@ ( key hash -- array n ? )
array>> dup array-capacity 0 eq? array>> dup length>> 0 eq?
[ no-key ] [ 2dup hash@ (key@) ] if ; inline [ no-key ] [ 2dup hash@ (key@) ] if ; inline
: <hash-array> ( n -- array ) : <hash-array> ( n -- array )
@ -71,7 +71,7 @@ TUPLE: hashtable
: hash-large? ( hash -- ? ) : hash-large? ( hash -- ? )
[ count>> 3 fixnum*fast 1 fixnum+fast ] [ count>> 3 fixnum*fast 1 fixnum+fast ]
[ array>> array-capacity ] bi fixnum> ; inline [ array>> length>> ] bi fixnum> ; inline
: hash-stale? ( hash -- ? ) : hash-stale? ( hash -- ? )
[ deleted>> 10 fixnum*fast ] [ count>> ] bi fixnum> ; inline [ deleted>> 10 fixnum*fast ] [ count>> ] bi fixnum> ; inline

View File

@ -60,9 +60,6 @@ INSTANCE: immutable-sequence sequence
<PRIVATE <PRIVATE
: array-capacity ( array -- n )
1 slot { array-capacity } declare ; inline
: array-nth ( n array -- elt ) : array-nth ( n array -- elt )
swap 2 fixnum+fast slot ; inline swap 2 fixnum+fast slot ; inline