1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: functors sequences sequences.private prettyprint.custom
4 kernel words classes math parser alien.c-types byte-arrays
6 IN: specialized-arrays.functor
8 ERROR: bad-byte-array-length byte-array type ;
10 M: bad-byte-array-length summary
11 drop "Byte array length doesn't divide type width" ;
13 : (c-array) ( n c-type -- array )
14 heap-size * (byte-array) ; inline
16 FUNCTOR: define-array ( T -- )
22 byte-array>A DEFINES byte-array>${A}
25 NTH [ T dup c-getter array-accessor ]
26 SET-NTH [ T dup c-setter array-accessor ]
31 { length array-capacity read-only }
32 { underlying byte-array read-only } ;
34 : <A> ( n -- specialized-array ) dup T <c-array> A boa ; inline
36 : (A) ( n -- specialized-array ) dup T (c-array) A boa ; inline
38 : byte-array>A ( byte-array -- specialized-array )
39 dup length T heap-size /mod 0 = [ drop T bad-byte-array-length ] unless
42 M: A clone [ length>> ] [ underlying>> clone ] bi A boa ;
44 M: A length length>> ;
46 M: A nth-unsafe underlying>> NTH call ;
48 M: A set-nth-unsafe underlying>> SET-NTH call ;
50 : >A ( seq -- specialized-array ) A new clone-like ; inline
52 M: A like drop dup A instance? [ >A execute ] unless ;
54 M: A new-sequence drop (A) execute ;
56 M: A equal? over A instance? [ sequence= ] [ 2drop f ] if ;
60 [ T heap-size * ] [ underlying>> ] bi*
65 M: A byte-length underlying>> length ;
67 M: A pprint-delims drop A{ \ } ;
69 M: A >pprint-sequence ;
71 M: A pprint* pprint-object ;
73 : A{ \ } [ >A execute ] parse-literal ; parsing