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