1 ! Copyright (C) 2008 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel classes.struct cocoa cocoa.runtime cocoa.types alien.data
4 locals math sequences vectors fry libc destructors specialized-arrays ;
8 CONSTANT: NS-EACH-BUFFER-SIZE 16
10 : with-enumeration-buffers ( quot -- )
12 NSFastEnumerationState malloc-struct &free
13 NS-EACH-BUFFER-SIZE id malloc-array &free
16 ] with-destructors ; inline
18 :: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- )
19 object state stackbuf count -> countByEnumeratingWithState:objects:count: :> items-count
21 state itemsPtr>> [ items-count id <c-direct-array> ] [ stackbuf ] if* :> items
22 items-count iota [ items nth quot call ] each
23 object quot state stackbuf count (NSFastEnumeration-each)
24 ] unless ; inline recursive
26 : NSFastEnumeration-each ( object quot -- )
27 [ (NSFastEnumeration-each) ] with-enumeration-buffers ; inline
29 : NSFastEnumeration-map ( object quot -- vector )
30 NS-EACH-BUFFER-SIZE <vector>
31 [ '[ @ _ push ] NSFastEnumeration-each ] keep ; inline
33 : NSFastEnumeration>vector ( object -- vector )
34 [ ] NSFastEnumeration-map ;