1 ! Copyright (C) 2008 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel cocoa cocoa.types alien.c-types locals math
4 sequences vectors fry libc destructors
5 specialized-arrays.direct.alien ;
8 CONSTANT: NS-EACH-BUFFER-SIZE 16
10 : with-enumeration-buffers ( quot -- )
12 "NSFastEnumerationState" malloc-object &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:
21 state NSFastEnumerationState-itemsPtr [ stackbuf ] unless*
22 swap <direct-void*-array> quot each
23 object quot state stackbuf count (NSFastEnumeration-each)
24 ] if ; 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 ;