1 ! Copyright (C) 2008 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data assocs classes.struct cocoa
4 cocoa.runtime cocoa.types destructors hashtables kernel libc
5 sequences specialized-arrays vectors ;
10 CONSTANT: NS-EACH-BUFFER-SIZE 16
12 : with-enumeration-buffers ( quot -- )
14 NSFastEnumerationState malloc-struct &free
15 NS-EACH-BUFFER-SIZE id malloc-array &free
18 ] with-destructors ; inline
20 :: (NSFastEnumeration-each) ( ... object quot: ( ... elt -- ) state stackbuf count -- ... )
21 object state stackbuf count -> countByEnumeratingWithState:objects:count: :> items-count
23 state itemsPtr>> [ items-count id <c-direct-array> ] [ stackbuf ] if* :> items
24 items-count <iota> [ items nth quot call ] each
25 object quot state stackbuf count (NSFastEnumeration-each)
26 ] unless ; inline recursive
28 : NSFastEnumeration-each ( ... object quot: ( ... elt -- ... ) -- ... )
29 [ (NSFastEnumeration-each) ] with-enumeration-buffers ; inline
31 : NSFastEnumeration-map ( ... object quot: ( ... elt -- ... newelt ) -- ... vector )
32 NS-EACH-BUFFER-SIZE <vector>
33 [ '[ @ _ push ] NSFastEnumeration-each ] keep ; inline
35 : NSFastEnumeration>vector ( object -- vector )
36 [ ] NSFastEnumeration-map ;
38 : NSFastEnumeration>hashtable ( ... object quot: ( ... elt -- ... key value ) -- ... vector )
39 NS-EACH-BUFFER-SIZE <hashtable>
40 [ '[ @ swap _ set-at ] NSFastEnumeration-each ] keep ; inline